allocator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/common/assert.h>
  6. #include <aws/common/common.h>
  7. #include <aws/common/logging.h>
  8. #include <aws/common/math.h>
  9. #include <stdarg.h>
  10. #include <stdlib.h>
  11. #ifdef _WIN32
  12. # include <Windows.h>
  13. #endif
  14. #ifdef __MACH__
  15. # include <CoreFoundation/CoreFoundation.h>
  16. #endif
  17. /* turn off unused named parameter warning on msvc.*/
  18. #ifdef _MSC_VER
  19. # pragma warning(push)
  20. # pragma warning(disable : 4100)
  21. #endif
  22. #ifndef PAGE_SIZE
  23. # define PAGE_SIZE (4 * 1024)
  24. #endif
  25. bool aws_allocator_is_valid(const struct aws_allocator *alloc) {
  26. /* An allocator must define mem_acquire and mem_release. All other fields are optional */
  27. return alloc && AWS_OBJECT_PTR_IS_READABLE(alloc) && alloc->mem_acquire && alloc->mem_release;
  28. }
  29. static void *s_default_malloc(struct aws_allocator *allocator, size_t size) {
  30. (void)allocator;
  31. /* larger allocations should be aligned so that AVX and friends can avoid
  32. * the extra preamble during unaligned versions of memcpy/memset on big buffers
  33. * This will also accelerate hardware CRC and SHA on ARM chips
  34. *
  35. * 64 byte alignment for > page allocations on 64 bit systems
  36. * 32 byte alignment for > page allocations on 32 bit systems
  37. * 16 byte alignment for <= page allocations on 64 bit systems
  38. * 8 byte alignment for <= page allocations on 32 bit systems
  39. *
  40. * We use PAGE_SIZE as the boundary because we are not aware of any allocations of
  41. * this size or greater that are not data buffers
  42. */
  43. const size_t alignment = sizeof(void *) * (size > PAGE_SIZE ? 8 : 2);
  44. #if !defined(_WIN32)
  45. void *result = NULL;
  46. int err = posix_memalign(&result, alignment, size);
  47. (void)err;
  48. AWS_PANIC_OOM(result, "posix_memalign failed to allocate memory");
  49. return result;
  50. #else
  51. void *mem = _aligned_malloc(size, alignment);
  52. AWS_FATAL_POSTCONDITION(mem && "_aligned_malloc failed to allocate memory");
  53. return mem;
  54. #endif
  55. }
  56. static void s_default_free(struct aws_allocator *allocator, void *ptr) {
  57. (void)allocator;
  58. #if !defined(_WIN32)
  59. free(ptr);
  60. #else
  61. _aligned_free(ptr);
  62. #endif
  63. }
  64. static void *s_default_realloc(struct aws_allocator *allocator, void *ptr, size_t oldsize, size_t newsize) {
  65. (void)allocator;
  66. (void)oldsize;
  67. AWS_FATAL_PRECONDITION(newsize);
  68. #if !defined(_WIN32)
  69. if (newsize <= oldsize) {
  70. return ptr;
  71. }
  72. /* newsize is > oldsize, need more memory */
  73. void *new_mem = s_default_malloc(allocator, newsize);
  74. AWS_PANIC_OOM(new_mem, "Unhandled OOM encountered in s_default_malloc");
  75. if (ptr) {
  76. memcpy(new_mem, ptr, oldsize);
  77. s_default_free(allocator, ptr);
  78. }
  79. return new_mem;
  80. #else
  81. const size_t alignment = sizeof(void *) * (newsize > PAGE_SIZE ? 8 : 2);
  82. void *new_mem = _aligned_realloc(ptr, newsize, alignment);
  83. AWS_PANIC_OOM(new_mem, "Unhandled OOM encountered in _aligned_realloc");
  84. return new_mem;
  85. #endif
  86. }
  87. static void *s_default_calloc(struct aws_allocator *allocator, size_t num, size_t size) {
  88. void *mem = s_default_malloc(allocator, num * size);
  89. AWS_PANIC_OOM(mem, "Unhandled OOM encountered in s_default_malloc");
  90. memset(mem, 0, num * size);
  91. return mem;
  92. }
  93. static struct aws_allocator default_allocator = {
  94. .mem_acquire = s_default_malloc,
  95. .mem_release = s_default_free,
  96. .mem_realloc = s_default_realloc,
  97. .mem_calloc = s_default_calloc,
  98. };
  99. struct aws_allocator *aws_default_allocator(void) {
  100. return &default_allocator;
  101. }
  102. void *aws_mem_acquire(struct aws_allocator *allocator, size_t size) {
  103. AWS_FATAL_PRECONDITION(allocator != NULL);
  104. AWS_FATAL_PRECONDITION(allocator->mem_acquire != NULL);
  105. /* Protect against https://wiki.sei.cmu.edu/confluence/display/c/MEM04-C.+Beware+of+zero-length+allocations */
  106. AWS_FATAL_PRECONDITION(size != 0);
  107. void *mem = allocator->mem_acquire(allocator, size);
  108. AWS_PANIC_OOM(mem, "Unhandled OOM encountered in aws_mem_acquire with allocator");
  109. return mem;
  110. }
  111. void *aws_mem_calloc(struct aws_allocator *allocator, size_t num, size_t size) {
  112. AWS_FATAL_PRECONDITION(allocator != NULL);
  113. AWS_FATAL_PRECONDITION(allocator->mem_calloc || allocator->mem_acquire);
  114. /* Protect against https://wiki.sei.cmu.edu/confluence/display/c/MEM04-C.+Beware+of+zero-length+allocations */
  115. AWS_FATAL_PRECONDITION(num != 0 && size != 0);
  116. /* Defensive check: never use calloc with size * num that would overflow
  117. * https://wiki.sei.cmu.edu/confluence/display/c/MEM07-C.+Ensure+that+the+arguments+to+calloc%28%29%2C+when+multiplied%2C+do+not+wrap
  118. */
  119. size_t required_bytes = 0;
  120. AWS_FATAL_POSTCONDITION(!aws_mul_size_checked(num, size, &required_bytes), "calloc computed size > SIZE_MAX");
  121. /* If there is a defined calloc, use it */
  122. if (allocator->mem_calloc) {
  123. void *mem = allocator->mem_calloc(allocator, num, size);
  124. AWS_PANIC_OOM(mem, "Unhandled OOM encountered in aws_mem_acquire with allocator");
  125. return mem;
  126. }
  127. /* Otherwise, emulate calloc */
  128. void *mem = allocator->mem_acquire(allocator, required_bytes);
  129. AWS_PANIC_OOM(mem, "Unhandled OOM encountered in aws_mem_acquire with allocator");
  130. memset(mem, 0, required_bytes);
  131. return mem;
  132. }
  133. #define AWS_ALIGN_ROUND_UP(value, alignment) (((value) + ((alignment)-1)) & ~((alignment)-1))
  134. void *aws_mem_acquire_many(struct aws_allocator *allocator, size_t count, ...) {
  135. enum { S_ALIGNMENT = sizeof(intmax_t) };
  136. va_list args_size;
  137. va_start(args_size, count);
  138. va_list args_allocs;
  139. va_copy(args_allocs, args_size);
  140. size_t total_size = 0;
  141. for (size_t i = 0; i < count; ++i) {
  142. /* Ignore the pointer argument for now */
  143. va_arg(args_size, void **);
  144. size_t alloc_size = va_arg(args_size, size_t);
  145. total_size += AWS_ALIGN_ROUND_UP(alloc_size, S_ALIGNMENT);
  146. }
  147. va_end(args_size);
  148. void *allocation = NULL;
  149. if (total_size > 0) {
  150. allocation = aws_mem_acquire(allocator, total_size);
  151. AWS_PANIC_OOM(allocation, "Unhandled OOM encountered in aws_mem_acquire with allocator");
  152. uint8_t *current_ptr = allocation;
  153. for (size_t i = 0; i < count; ++i) {
  154. void **out_ptr = va_arg(args_allocs, void **);
  155. size_t alloc_size = va_arg(args_allocs, size_t);
  156. alloc_size = AWS_ALIGN_ROUND_UP(alloc_size, S_ALIGNMENT);
  157. *out_ptr = current_ptr;
  158. current_ptr += alloc_size;
  159. }
  160. }
  161. va_end(args_allocs);
  162. return allocation;
  163. }
  164. #undef AWS_ALIGN_ROUND_UP
  165. void aws_mem_release(struct aws_allocator *allocator, void *ptr) {
  166. AWS_FATAL_PRECONDITION(allocator != NULL);
  167. AWS_FATAL_PRECONDITION(allocator->mem_release != NULL);
  168. if (ptr != NULL) {
  169. allocator->mem_release(allocator, ptr);
  170. }
  171. }
  172. int aws_mem_realloc(struct aws_allocator *allocator, void **ptr, size_t oldsize, size_t newsize) {
  173. AWS_FATAL_PRECONDITION(allocator != NULL);
  174. AWS_FATAL_PRECONDITION(allocator->mem_realloc || allocator->mem_acquire);
  175. AWS_FATAL_PRECONDITION(allocator->mem_release);
  176. /* Protect against https://wiki.sei.cmu.edu/confluence/display/c/MEM04-C.+Beware+of+zero-length+allocations */
  177. if (newsize == 0) {
  178. aws_mem_release(allocator, *ptr);
  179. *ptr = NULL;
  180. return AWS_OP_SUCCESS;
  181. }
  182. if (allocator->mem_realloc) {
  183. void *newptr = allocator->mem_realloc(allocator, *ptr, oldsize, newsize);
  184. AWS_PANIC_OOM(newptr, "Unhandled OOM encountered in aws_mem_acquire with allocator");
  185. *ptr = newptr;
  186. return AWS_OP_SUCCESS;
  187. }
  188. /* Since the allocator doesn't support realloc, we'll need to emulate it (inefficiently). */
  189. if (oldsize >= newsize) {
  190. return AWS_OP_SUCCESS;
  191. }
  192. void *newptr = allocator->mem_acquire(allocator, newsize);
  193. AWS_PANIC_OOM(newptr, "Unhandled OOM encountered in aws_mem_acquire with allocator");
  194. memcpy(newptr, *ptr, oldsize);
  195. memset((uint8_t *)newptr + oldsize, 0, newsize - oldsize);
  196. aws_mem_release(allocator, *ptr);
  197. *ptr = newptr;
  198. return AWS_OP_SUCCESS;
  199. }
  200. /* Wraps a CFAllocator around aws_allocator. For Mac only. */
  201. #ifdef __MACH__
  202. static CFStringRef s_cf_allocator_description = CFSTR("CFAllocator wrapping aws_allocator.");
  203. /* note we don't have a standard specification stating sizeof(size_t) == sizeof(void *) so we have some extra casts */
  204. static void *s_cf_allocator_allocate(CFIndex alloc_size, CFOptionFlags hint, void *info) {
  205. (void)hint;
  206. struct aws_allocator *allocator = info;
  207. void *mem = aws_mem_acquire(allocator, (size_t)alloc_size + sizeof(size_t));
  208. size_t allocation_size = (size_t)alloc_size + sizeof(size_t);
  209. memcpy(mem, &allocation_size, sizeof(size_t));
  210. return (void *)((uint8_t *)mem + sizeof(size_t));
  211. }
  212. static void s_cf_allocator_deallocate(void *ptr, void *info) {
  213. struct aws_allocator *allocator = info;
  214. void *original_allocation = (uint8_t *)ptr - sizeof(size_t);
  215. aws_mem_release(allocator, original_allocation);
  216. }
  217. static void *s_cf_allocator_reallocate(void *ptr, CFIndex new_size, CFOptionFlags hint, void *info) {
  218. (void)hint;
  219. struct aws_allocator *allocator = info;
  220. AWS_ASSERT(allocator->mem_realloc);
  221. void *original_allocation = (uint8_t *)ptr - sizeof(size_t);
  222. size_t original_size = 0;
  223. memcpy(&original_size, original_allocation, sizeof(size_t));
  224. aws_mem_realloc(allocator, &original_allocation, original_size, (size_t)new_size);
  225. size_t new_allocation_size = (size_t)new_size;
  226. memcpy(original_allocation, &new_allocation_size, sizeof(size_t));
  227. return (void *)((uint8_t *)original_allocation + sizeof(size_t));
  228. }
  229. static CFStringRef s_cf_allocator_copy_description(const void *info) {
  230. (void)info;
  231. return s_cf_allocator_description;
  232. }
  233. static CFIndex s_cf_allocator_preferred_size(CFIndex size, CFOptionFlags hint, void *info) {
  234. (void)hint;
  235. (void)info;
  236. return size + sizeof(size_t);
  237. }
  238. CFAllocatorRef aws_wrapped_cf_allocator_new(struct aws_allocator *allocator) {
  239. CFAllocatorRef cf_allocator = NULL;
  240. CFAllocatorReallocateCallBack reallocate_callback = NULL;
  241. if (allocator->mem_realloc) {
  242. reallocate_callback = s_cf_allocator_reallocate;
  243. }
  244. CFAllocatorContext context = {
  245. .allocate = s_cf_allocator_allocate,
  246. .copyDescription = s_cf_allocator_copy_description,
  247. .deallocate = s_cf_allocator_deallocate,
  248. .reallocate = reallocate_callback,
  249. .info = allocator,
  250. .preferredSize = s_cf_allocator_preferred_size,
  251. .release = NULL,
  252. .retain = NULL,
  253. .version = 0,
  254. };
  255. cf_allocator = CFAllocatorCreate(NULL, &context);
  256. AWS_FATAL_ASSERT(cf_allocator && "creation of cf allocator failed!");
  257. return cf_allocator;
  258. }
  259. void aws_wrapped_cf_allocator_destroy(CFAllocatorRef allocator) {
  260. CFRelease(allocator);
  261. }
  262. #endif /*__MACH__ */