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// Apple and Android use posix_memalign (Android's aligned_alloc requires API 28+)
41#if defined(__APPLE__) || defined(__ANDROID__)
42inline void* aligned_alloc(size_t alignment, size_t size)
43{
44 void* t = 0;
45 posix_memalign(&t, alignment, size);
46 if (t == 0) {
47 info("bad alloc of size: ", size);
48 std::abort();
49 }
50 TRACY_ALLOC(t, size);
51 return t;
52}
53
54inline void aligned_free(void* mem)
55{
57 free(mem);
58}
59#endif
60
61// Linux (but not Android) and WASM use the standard aligned_alloc
62#if (defined(__linux__) && !defined(__ANDROID__)) || defined(__wasm__)
63inline void* protected_aligned_alloc(size_t alignment, size_t size)
64{
65 size += (size % alignment);
66 void* t = nullptr;
67 // pad size to alignment
68 if (size % alignment != 0) {
69 size += alignment - (size % alignment);
70 }
71 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
72 t = aligned_alloc(alignment, size);
73 if (t == nullptr) {
74 info("bad alloc of size: ", size);
75 std::abort();
76 }
77 TRACY_ALLOC(t, size);
78 return t;
79}
80
81#define aligned_alloc protected_aligned_alloc
82
83inline void aligned_free(void* mem)
84{
86 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory, cppcoreguidelines-no-malloc)
87 free(mem);
88}
89#endif
90
91#ifdef _WIN32
92inline void* aligned_alloc(size_t alignment, size_t size)
93{
94 void* t = _aligned_malloc(size, alignment);
95 TRACY_ALLOC(t, size);
96 return t;
97}
98
99inline void aligned_free(void* mem)
100{
102 _aligned_free(mem);
103}
104#endif
105
106// inline void print_malloc_info()
107// {
108// struct mallinfo minfo = mallinfo();
109
110// info("Total non-mmapped bytes (arena): ", minfo.arena);
111// info("Number of free chunks (ordblks): ", minfo.ordblks);
112// info("Number of fastbin blocks (smblks): ", minfo.smblks);
113// info("Number of mmapped regions (hblks): ", minfo.hblks);
114// info("Space allocated in mmapped regions (hblkhd): ", minfo.hblkhd);
115// info("Maximum total allocated space (usmblks): ", minfo.usmblks);
116// info("Space available in freed fastbin blocks (fsmblks): ", minfo.fsmblks);
117// info("Total allocated space (uordblks): ", minfo.uordblks);
118// info("Total free space (fordblks): ", minfo.fordblks);
119// info("Top-most, releasable space (keepcost): ", minfo.keepcost);
120// }
121
122inline void* tracy_malloc(size_t size)
123{
124 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory, cppcoreguidelines-no-malloc)
125 void* t = malloc(size);
126 TRACY_ALLOC(t, size);
127 return t;
128}
129
130inline void tracy_free(void* mem)
131{
133 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory, cppcoreguidelines-no-malloc)
134 free(mem);
135}
#define info(...)
Definition log.hpp:93
MemoryStore mem
void tracy_free(void *mem)
Definition mem.hpp:130
#define TRACY_ALLOC(t, size)
Definition mem.hpp:14
void * tracy_malloc(size_t size)
Definition mem.hpp:122
#define TRACY_FREE(t)
Definition mem.hpp:15
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13