Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
mem.hpp
Go to the documentation of this file.
1#pragma once
2#include "log.hpp"
3#include "memory.h"
4#include "tracy/Tracy.hpp"
5#include "wasm_export.hpp"
6#include <cstdlib>
7#include <memory>
8
9// This can be altered to capture stack traces, though more expensive
10// so wrap TracyAlloc or TracyAllocS. We disable these if gates are being tracked
11// Gates are hackishly tracked as if they were memory, for the sweet sweet memory
12// stack tree that doesn't seem to be available for other metric types.
13#ifndef TRACY_HACK_GATES_AS_MEMORY
14#define TRACY_ALLOC(t, size) TracyAllocS(t, size, /*stack depth*/ 10)
15#define TRACY_FREE(t) TracyFreeS(t, /*stack depth*/ 10)
16#define TRACY_GATE_ALLOC(t)
17#define TRACY_GATE_FREE(t)
18#else
19#include <mutex>
20#include <set>
21#define TRACY_ALLOC(t, size)
22#define TRACY_FREE(t)
23
24namespace bb {
25// These are hacks to make sure tracy plays along
26// If we free an ID not allocated, or allocate an index twice without a free it will complain
27// so we hack thread-safety and an incrementing global ID.
28static std::mutex GLOBAL_GATE_MUTEX;
29static size_t GLOBAL_GATE = 0;
30static std::set<size_t> FREED_GATES; // hack to prevent instrumentation failures
31} // namespace bb
32#define TRACY_GATE_ALLOC(index) TracyAllocS(reinterpret_cast<void*>(index), 1, /*stack depth*/ 50)
33#define TRACY_GATE_FREE(index) TracyFreeS(reinterpret_cast<void*>(index), /*stack depth*/ 50)
34#endif
35// #define TRACY_ALLOC(t, size) TracyAlloc(t, size)
36// #define TRACY_FREE(t) TracyFree(t)
37
38#define pad(size, alignment) (size - (size % alignment) + ((size % alignment) == 0 ? 0 : alignment))
39
40#ifdef __APPLE__
41inline void* aligned_alloc(size_t alignment, size_t size)
42{
43 void* t = 0;
44 posix_memalign(&t, alignment, size);
45 if (t == 0) {
46 info("bad alloc of size: ", size);
47 std::abort();
48 }
49 TRACY_ALLOC(t, size);
50 return t;
51}
52
53inline void aligned_free(void* mem)
54{
56 free(mem);
57}
58#endif
59
60#if defined(__linux__) || defined(__wasm__)
61inline void* protected_aligned_alloc(size_t alignment, size_t size)
62{
63 size += (size % alignment);
64 void* t = nullptr;
65 // pad size to alignment
66 if (size % alignment != 0) {
67 size += alignment - (size % alignment);
68 }
69 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
70 t = aligned_alloc(alignment, size);
71 if (t == nullptr) {
72 info("bad alloc of size: ", size);
73 std::abort();
74 }
75 TRACY_ALLOC(t, size);
76 return t;
77}
78
79#define aligned_alloc protected_aligned_alloc
80
81inline void aligned_free(void* mem)
82{
84 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory, cppcoreguidelines-no-malloc)
85 free(mem);
86}
87#endif
88
89#ifdef _WIN32
90inline void* aligned_alloc(size_t alignment, size_t size)
91{
92 void* t = _aligned_malloc(size, alignment);
93 TRACY_ALLOC(t, size);
94 return t;
95}
96
97inline void aligned_free(void* mem)
98{
100 _aligned_free(mem);
101}
102#endif
103
104// inline void print_malloc_info()
105// {
106// struct mallinfo minfo = mallinfo();
107
108// info("Total non-mmapped bytes (arena): ", minfo.arena);
109// info("Number of free chunks (ordblks): ", minfo.ordblks);
110// info("Number of fastbin blocks (smblks): ", minfo.smblks);
111// info("Number of mmapped regions (hblks): ", minfo.hblks);
112// info("Space allocated in mmapped regions (hblkhd): ", minfo.hblkhd);
113// info("Maximum total allocated space (usmblks): ", minfo.usmblks);
114// info("Space available in freed fastbin blocks (fsmblks): ", minfo.fsmblks);
115// info("Total allocated space (uordblks): ", minfo.uordblks);
116// info("Total free space (fordblks): ", minfo.fordblks);
117// info("Top-most, releasable space (keepcost): ", minfo.keepcost);
118// }
119
120inline void* tracy_malloc(size_t size)
121{
122 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory, cppcoreguidelines-no-malloc)
123 void* t = malloc(size);
124 TRACY_ALLOC(t, size);
125 return t;
126}
127
128inline void tracy_free(void* mem)
129{
131 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory, cppcoreguidelines-no-malloc)
132 free(mem);
133}
void info(Args... args)
Definition log.hpp:70
MemoryStore mem
void tracy_free(void *mem)
Definition mem.hpp:128
#define TRACY_ALLOC(t, size)
Definition mem.hpp:14
void * tracy_malloc(size_t size)
Definition mem.hpp:120
#define TRACY_FREE(t)
Definition mem.hpp:15
Entry point for Barretenberg command-line interface.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13