1 | /*===- CtxInstrProfiling.h- Contextual instrumentation-based PGO ---------===*\ |
2 | |* |
3 | |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | |* See https://llvm.org/LICENSE.txt for license information. |
5 | |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | |* |
7 | \*===----------------------------------------------------------------------===*/ |
8 | |
9 | #ifndef CTX_PROFILE_CTXINSTRPROFILING_H_ |
10 | #define CTX_PROFILE_CTXINSTRPROFILING_H_ |
11 | |
12 | #include <sanitizer/common_interface_defs.h> |
13 | |
14 | namespace __ctx_profile { |
15 | |
16 | /// Arena (bump allocator) forming a linked list. Intentionally not thread safe. |
17 | /// Allocation and de-allocation happen using sanitizer APIs. We make that |
18 | /// explicit. |
19 | class Arena final { |
20 | public: |
21 | // When allocating a new Arena, optionally specify an existing one to append |
22 | // to, assumed to be the last in the Arena list. We only need to support |
23 | // appending to the arena list. |
24 | static Arena *allocateNewArena(size_t Size, Arena *Prev = nullptr); |
25 | static void freeArenaList(Arena *&A); |
26 | |
27 | uint64_t size() const { return Size; } |
28 | |
29 | // Allocate S bytes or return nullptr if we don't have that many available. |
30 | char *tryBumpAllocate(size_t S) { |
31 | if (Pos + S > Size) |
32 | return nullptr; |
33 | Pos += S; |
34 | return start() + (Pos - S); |
35 | } |
36 | |
37 | Arena *next() const { return Next; } |
38 | |
39 | // the beginning of allocatable memory. |
40 | const char *start() const { return const_cast<Arena *>(this)->start(); } |
41 | const char *pos() const { return start() + Pos; } |
42 | |
43 | private: |
44 | explicit Arena(uint32_t Size) : Size(Size) {} |
45 | ~Arena() = delete; |
46 | |
47 | char *start() { return reinterpret_cast<char *>(&this[1]); } |
48 | |
49 | Arena *Next = nullptr; |
50 | uint64_t Pos = 0; |
51 | const uint64_t Size; |
52 | }; |
53 | |
54 | } // namespace __ctx_profile |
55 | #endif // CTX_PROFILE_CTXINSTRPROFILING_H_ |
56 | |