| 1 | #ifndef AWS_COMMON_ALLOCATOR_H |
| 2 | #define AWS_COMMON_ALLOCATOR_H |
| 3 | /** |
| 4 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | * SPDX-License-Identifier: Apache-2.0. |
| 6 | */ |
| 7 | |
| 8 | #include <aws/common/macros.h> |
| 9 | #include <aws/common/stdbool.h> |
| 10 | #include <aws/common/stdint.h> |
| 11 | |
| 12 | AWS_PUSH_SANE_WARNING_LEVEL |
| 13 | AWS_EXTERN_C_BEGIN |
| 14 | |
| 15 | /* |
| 16 | * Quick guide to allocators: |
| 17 | * CRT offers several flavours of allocators: |
| 18 | * - default: basic allocator that invokes system one directly. |
| 19 | * - aligned: basic allocator that aligns small allocations on 8 byte |
| 20 | * boundary and big buffers on 32/64 byte (system dependent) boundary. |
| 21 | * Aligned mem can improve perf on some operations, like memcpy or hashes. |
| 22 | * Depending on a system, can result in higher peak memory count in heavy |
| 23 | * acquire/free scenarios (ex. s3), due to memory fragmentation related to how |
| 24 | * aligned allocators work (over allocate, find aligned offset, release extra memory) |
| 25 | * - wrapped_cf: wraps MacOS's Security Framework allocator. |
| 26 | * - mem_tracer: wraps any allocator and provides tracing functionality to allocations |
| 27 | * - small_block_allocator: pools smaller allocations into preallocated buckets. |
| 28 | * Not actively maintained. Avoid if possible. |
| 29 | */ |
| 30 | |
| 31 | /* Allocator structure. An instance of this will be passed around for anything needing memory allocation */ |
| 32 | struct aws_allocator { |
| 33 | void *(*mem_acquire)(struct aws_allocator *allocator, size_t size); |
| 34 | void (*mem_release)(struct aws_allocator *allocator, void *ptr); |
| 35 | /* Optional method; if not supported, this pointer must be NULL */ |
| 36 | void *(*mem_realloc)(struct aws_allocator *allocator, void *oldptr, size_t oldsize, size_t newsize); |
| 37 | /* Optional method; if not supported, this pointer must be NULL */ |
| 38 | void *(*mem_calloc)(struct aws_allocator *allocator, size_t num, size_t size); |
| 39 | void *impl; |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Inexpensive (constant time) check of data-structure invariants. |
| 44 | */ |
| 45 | AWS_COMMON_API |
| 46 | bool aws_allocator_is_valid(const struct aws_allocator *alloc); |
| 47 | |
| 48 | AWS_COMMON_API |
| 49 | struct aws_allocator *aws_default_allocator(void); |
| 50 | |
| 51 | /* |
| 52 | * Allocator that align small allocations on 8 byte boundary and big allocations |
| 53 | * on 32/64 byte boundary. |
| 54 | */ |
| 55 | AWS_COMMON_API |
| 56 | struct aws_allocator *aws_aligned_allocator(void); |
| 57 | |
| 58 | #ifdef __MACH__ |
| 59 | /* Avoid pulling in CoreFoundation headers in a header file. */ |
| 60 | struct __CFAllocator; /* NOLINT(bugprone-reserved-identifier) */ |
| 61 | typedef const struct __CFAllocator *CFAllocatorRef; |
| 62 | |
| 63 | /** |
| 64 | * Wraps a CFAllocator around aws_allocator. For Mac only. Use this anytime you need a CFAllocatorRef for interacting |
| 65 | * with Apple Frameworks. Unfortunately, it allocates memory so we can't make it static file scope, be sure to call |
| 66 | * aws_wrapped_cf_allocator_destroy when finished. |
| 67 | */ |
| 68 | AWS_COMMON_API |
| 69 | CFAllocatorRef aws_wrapped_cf_allocator_new(struct aws_allocator *allocator); |
| 70 | |
| 71 | /** |
| 72 | * Cleans up any resources alloced in aws_wrapped_cf_allocator_new. |
| 73 | */ |
| 74 | AWS_COMMON_API |
| 75 | void aws_wrapped_cf_allocator_destroy(CFAllocatorRef allocator); |
| 76 | #endif |
| 77 | |
| 78 | /** |
| 79 | * Returns at least `size` of memory ready for usage. In versions v0.6.8 and prior, this function was allowed to return |
| 80 | * NULL. In later versions, if allocator->mem_acquire() returns NULL, this function will assert and exit. To handle |
| 81 | * conditions where OOM is not a fatal error, allocator->mem_acquire() is responsible for finding/reclaiming/running a |
| 82 | * GC etc...before returning. |
| 83 | */ |
| 84 | AWS_COMMON_API |
| 85 | void *aws_mem_acquire(struct aws_allocator *allocator, size_t size); |
| 86 | |
| 87 | /** |
| 88 | * Allocates a block of memory for an array of num elements, each of them size bytes long, and initializes all its bits |
| 89 | * to zero. In versions v0.6.8 and prior, this function was allowed to return NULL. |
| 90 | * In later versions, if allocator->mem_calloc() returns NULL, this function will assert and exit. To handle |
| 91 | * conditions where OOM is not a fatal error, allocator->mem_calloc() is responsible for finding/reclaiming/running a |
| 92 | * GC etc...before returning. |
| 93 | */ |
| 94 | AWS_COMMON_API |
| 95 | void *aws_mem_calloc(struct aws_allocator *allocator, size_t num, size_t size); |
| 96 | |
| 97 | /** |
| 98 | * Allocates many chunks of bytes into a single block. Expects to be called with alternating void ** (dest), size_t |
| 99 | * (size). The first void ** will be set to the root of the allocation. Alignment is assumed to be sizeof(intmax_t). |
| 100 | * |
| 101 | * This is useful for allocating structs using the pimpl pattern, as you may allocate the public object and impl object |
| 102 | * in the same contiguous block of memory. |
| 103 | * |
| 104 | * Returns a pointer to the allocation. |
| 105 | * |
| 106 | * In versions v0.6.8 and prior, this function was allowed to return |
| 107 | * NULL. In later versions, if allocator->mem_acquire() returns NULL, this function will assert and exit. To handle |
| 108 | * conditions where OOM is not a fatal error, allocator->mem_acquire() is responsible for finding/reclaiming/running a |
| 109 | * GC etc...before returning. |
| 110 | */ |
| 111 | AWS_COMMON_API |
| 112 | void *aws_mem_acquire_many(struct aws_allocator *allocator, size_t count, ...); |
| 113 | |
| 114 | /** |
| 115 | * Releases ptr back to whatever allocated it. |
| 116 | * Nothing happens if ptr is NULL. |
| 117 | */ |
| 118 | AWS_COMMON_API |
| 119 | void aws_mem_release(struct aws_allocator *allocator, void *ptr); |
| 120 | |
| 121 | /** |
| 122 | * Attempts to adjust the size of the pointed-to memory buffer from oldsize to |
| 123 | * newsize. The pointer (*ptr) may be changed if the memory needs to be |
| 124 | * reallocated. |
| 125 | * |
| 126 | * In versions v0.6.8 and prior, this function was allowed to return |
| 127 | * NULL. In later versions, if allocator->mem_realloc() returns NULL, this function will assert and exit. To handle |
| 128 | * conditions where OOM is not a fatal error, allocator->mem_realloc() is responsible for finding/reclaiming/running a |
| 129 | * GC etc...before returning. |
| 130 | */ |
| 131 | AWS_COMMON_API |
| 132 | int aws_mem_realloc(struct aws_allocator *allocator, void **ptr, size_t oldsize, size_t newsize); |
| 133 | /* |
| 134 | * Maintainer note: The above function doesn't return the pointer (as with |
| 135 | * standard C realloc) as this pattern becomes error-prone when OOMs occur. |
| 136 | * In particular, we want to avoid losing the old pointer when an OOM condition |
| 137 | * occurs, so we prefer to take the old pointer as an in/out reference argument |
| 138 | * that we can leave unchanged on failure. |
| 139 | */ |
| 140 | |
| 141 | enum aws_mem_trace_level { |
| 142 | AWS_MEMTRACE_NONE = 0, /* no tracing */ |
| 143 | AWS_MEMTRACE_BYTES = 1, /* just track allocation sizes and total allocated */ |
| 144 | AWS_MEMTRACE_STACKS = 2, /* capture callstacks for each allocation */ |
| 145 | }; |
| 146 | |
| 147 | /* |
| 148 | * Wraps an allocator and tracks all external allocations. If aws_mem_trace_dump() is called |
| 149 | * and there are still allocations active, they will be reported to the aws_logger at TRACE level. |
| 150 | * allocator - The allocator to wrap |
| 151 | * deprecated - Deprecated arg, ignored. |
| 152 | * level - The level to track allocations at |
| 153 | * frames_per_stack is how many frames to store per callstack if AWS_MEMTRACE_STACKS is in use, |
| 154 | * otherwise it is ignored. 8 tends to be a pretty good number balancing storage space vs useful stacks. |
| 155 | * Returns the tracer allocator, which should be used for all allocations that should be tracked. |
| 156 | */ |
| 157 | AWS_COMMON_API |
| 158 | struct aws_allocator *aws_mem_tracer_new( |
| 159 | struct aws_allocator *allocator, |
| 160 | struct aws_allocator *deprecated, |
| 161 | enum aws_mem_trace_level level, |
| 162 | size_t frames_per_stack); |
| 163 | |
| 164 | /* |
| 165 | * Unwraps the traced allocator and cleans up the tracer. |
| 166 | * Returns the original allocator |
| 167 | */ |
| 168 | AWS_COMMON_API |
| 169 | struct aws_allocator *aws_mem_tracer_destroy(struct aws_allocator *trace_allocator); |
| 170 | |
| 171 | /* |
| 172 | * If there are outstanding allocations, dumps them to log, along with any information gathered |
| 173 | * based on the trace level set when aws_mem_trace() was called. |
| 174 | * Should be passed the tracer allocator returned from aws_mem_trace(). |
| 175 | */ |
| 176 | AWS_COMMON_API |
| 177 | void aws_mem_tracer_dump(struct aws_allocator *trace_allocator); |
| 178 | |
| 179 | /* |
| 180 | * Returns the current number of bytes in outstanding allocations |
| 181 | */ |
| 182 | AWS_COMMON_API |
| 183 | size_t aws_mem_tracer_bytes(struct aws_allocator *trace_allocator); |
| 184 | |
| 185 | /* |
| 186 | * Returns the current number of outstanding allocations |
| 187 | */ |
| 188 | AWS_COMMON_API |
| 189 | size_t aws_mem_tracer_count(struct aws_allocator *trace_allocator); |
| 190 | |
| 191 | /* |
| 192 | * Creates a new Small Block Allocator which fronts the supplied parent allocator. The SBA will intercept |
| 193 | * and handle small allocs, and will forward anything larger to the parent allocator. |
| 194 | * If multi_threaded is true, the internal allocator will protect its internal data structures with a mutex |
| 195 | */ |
| 196 | AWS_COMMON_API |
| 197 | struct aws_allocator *aws_small_block_allocator_new(struct aws_allocator *allocator, bool multi_threaded); |
| 198 | |
| 199 | /* |
| 200 | * Destroys a Small Block Allocator instance and frees its memory to the parent allocator. The parent |
| 201 | * allocator will otherwise be unaffected. |
| 202 | */ |
| 203 | AWS_COMMON_API |
| 204 | void aws_small_block_allocator_destroy(struct aws_allocator *sba_allocator); |
| 205 | |
| 206 | /* |
| 207 | * Returns the number of bytes currently active in the SBA |
| 208 | */ |
| 209 | AWS_COMMON_API |
| 210 | size_t aws_small_block_allocator_bytes_active(struct aws_allocator *sba_allocator); |
| 211 | |
| 212 | /* |
| 213 | * Returns the number of bytes reserved in pages/bins inside the SBA, e.g. the |
| 214 | * current system memory used by the SBA |
| 215 | */ |
| 216 | AWS_COMMON_API |
| 217 | size_t aws_small_block_allocator_bytes_reserved(struct aws_allocator *sba_allocator); |
| 218 | |
| 219 | /* |
| 220 | * Returns the page size that the SBA is using |
| 221 | */ |
| 222 | AWS_COMMON_API |
| 223 | size_t aws_small_block_allocator_page_size(struct aws_allocator *sba_allocator); |
| 224 | |
| 225 | /* |
| 226 | * Returns the amount of memory in each page available to user allocations |
| 227 | */ |
| 228 | AWS_COMMON_API |
| 229 | size_t aws_small_block_allocator_page_size_available(struct aws_allocator *sba_allocator); |
| 230 | |
| 231 | /* |
| 232 | * Create an aligned allocator with an explicit alignment. Always align the allocated buffer with the passed-in |
| 233 | * alignment value. |
| 234 | */ |
| 235 | AWS_COMMON_API |
| 236 | struct aws_allocator *aws_explicit_aligned_allocator_new(size_t alignment); |
| 237 | |
| 238 | /* |
| 239 | * Destroys a customized aligned allocator instance and frees its memory. |
| 240 | */ |
| 241 | AWS_COMMON_API |
| 242 | void aws_explicit_aligned_allocator_destroy(struct aws_allocator *aligned_alloc); |
| 243 | |
| 244 | AWS_EXTERN_C_END |
| 245 | AWS_POP_SANE_WARNING_LEVEL |
| 246 | |
| 247 | #endif /* AWS_COMMON_ALLOCATOR_H */ |