You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

arena.h 8.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2009-2021, Google LLC
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Google LLC nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef UPB_ARENA_H_
  28. #define UPB_ARENA_H_
  29. #include <assert.h>
  30. #include <stdbool.h>
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include "upb/port_def.inc"
  34. #ifdef __cplusplus
  35. extern "C"
  36. {
  37. #endif
  38. /** upb_alloc *****************************************************************/
  39. /* A upb_alloc is a possibly-stateful allocator object.
  40. *
  41. * It could either be an arena allocator (which doesn't require individual
  42. * free() calls) or a regular malloc() (which does). The client must therefore
  43. * free memory unless it knows that the allocator is an arena allocator. */
  44. struct upb_alloc;
  45. typedef struct upb_alloc upb_alloc;
  46. /* A malloc()/free() function.
  47. * If "size" is 0 then the function acts like free(), otherwise it acts like
  48. * realloc(). Only "oldsize" bytes from a previous allocation are preserved. */
  49. typedef void* upb_alloc_func(upb_alloc* alloc, void* ptr, size_t oldsize, size_t size);
  50. struct upb_alloc
  51. {
  52. upb_alloc_func* func;
  53. };
  54. UPB_INLINE void* upb_malloc(upb_alloc* alloc, size_t size)
  55. {
  56. UPB_ASSERT(alloc);
  57. return alloc->func(alloc, NULL, 0, size);
  58. }
  59. UPB_INLINE void* upb_realloc(upb_alloc* alloc, void* ptr, size_t oldsize, size_t size)
  60. {
  61. UPB_ASSERT(alloc);
  62. return alloc->func(alloc, ptr, oldsize, size);
  63. }
  64. UPB_INLINE void upb_free(upb_alloc* alloc, void* ptr)
  65. {
  66. assert(alloc);
  67. alloc->func(alloc, ptr, 0, 0);
  68. }
  69. /* The global allocator used by upb. Uses the standard malloc()/free(). */
  70. extern upb_alloc upb_alloc_global;
  71. /* Functions that hard-code the global malloc.
  72. *
  73. * We still get benefit because we can put custom logic into our global
  74. * allocator, like injecting out-of-memory faults in debug/testing builds. */
  75. UPB_INLINE void* upb_gmalloc(size_t size)
  76. {
  77. return upb_malloc(&upb_alloc_global, size);
  78. }
  79. UPB_INLINE void* upb_grealloc(void* ptr, size_t oldsize, size_t size)
  80. {
  81. return upb_realloc(&upb_alloc_global, ptr, oldsize, size);
  82. }
  83. UPB_INLINE void upb_gfree(void* ptr)
  84. {
  85. upb_free(&upb_alloc_global, ptr);
  86. }
  87. /* upb_Arena ******************************************************************/
  88. /* upb_Arena is a specific allocator implementation that uses arena allocation.
  89. * The user provides an allocator that will be used to allocate the underlying
  90. * arena blocks. Arenas by nature do not require the individual allocations
  91. * to be freed. However the Arena does allow users to register cleanup
  92. * functions that will run when the arena is destroyed.
  93. *
  94. * A upb_Arena is *not* thread-safe.
  95. *
  96. * You could write a thread-safe arena allocator that satisfies the
  97. * upb_alloc interface, but it would not be as efficient for the
  98. * single-threaded case. */
  99. typedef void upb_CleanupFunc(void* ud);
  100. struct upb_Arena;
  101. typedef struct upb_Arena upb_Arena;
  102. typedef struct
  103. {
  104. /* We implement the allocator interface.
  105. * This must be the first member of upb_Arena!
  106. * TODO(haberman): remove once handlers are gone. */
  107. upb_alloc alloc;
  108. char *ptr, *end;
  109. } _upb_ArenaHead;
  110. /* Creates an arena from the given initial block (if any -- n may be 0).
  111. * Additional blocks will be allocated from |alloc|. If |alloc| is NULL, this
  112. * is a fixed-size arena and cannot grow. */
  113. upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc);
  114. void upb_Arena_Free(upb_Arena* a);
  115. bool upb_Arena_AddCleanup(upb_Arena* a, void* ud, upb_CleanupFunc* func);
  116. bool upb_Arena_Fuse(upb_Arena* a, upb_Arena* b);
  117. void* _upb_Arena_SlowMalloc(upb_Arena* a, size_t size);
  118. UPB_INLINE upb_alloc* upb_Arena_Alloc(upb_Arena* a)
  119. {
  120. return (upb_alloc*)a;
  121. }
  122. UPB_INLINE size_t _upb_ArenaHas(upb_Arena* a)
  123. {
  124. _upb_ArenaHead* h = (_upb_ArenaHead*)a;
  125. return (size_t)(h->end - h->ptr);
  126. }
  127. UPB_INLINE void* _upb_Arena_FastMalloc(upb_Arena* a, size_t size)
  128. {
  129. _upb_ArenaHead* h = (_upb_ArenaHead*)a;
  130. void* ret = h->ptr;
  131. UPB_ASSERT(UPB_ALIGN_MALLOC((uintptr_t)ret) == (uintptr_t)ret);
  132. UPB_ASSERT(UPB_ALIGN_MALLOC(size) == size);
  133. UPB_UNPOISON_MEMORY_REGION(ret, size);
  134. h->ptr += size;
  135. #if UPB_ASAN
  136. {
  137. size_t guard_size = 32;
  138. if (_upb_ArenaHas(a) >= guard_size)
  139. {
  140. h->ptr += guard_size;
  141. }
  142. else
  143. {
  144. h->ptr = h->end;
  145. }
  146. }
  147. #endif
  148. return ret;
  149. }
  150. UPB_INLINE void* upb_Arena_Malloc(upb_Arena* a, size_t size)
  151. {
  152. size = UPB_ALIGN_MALLOC(size);
  153. if (UPB_UNLIKELY(_upb_ArenaHas(a) < size))
  154. {
  155. return _upb_Arena_SlowMalloc(a, size);
  156. }
  157. return _upb_Arena_FastMalloc(a, size);
  158. }
  159. // Shrinks the last alloc from arena.
  160. // REQUIRES: (ptr, oldsize) was the last malloc/realloc from this arena.
  161. // We could also add a upb_Arena_TryShrinkLast() which is simply a no-op if
  162. // this was not the last alloc.
  163. UPB_INLINE void upb_Arena_ShrinkLast(upb_Arena* a, void* ptr, size_t oldsize, size_t size)
  164. {
  165. _upb_ArenaHead* h = (_upb_ArenaHead*)a;
  166. oldsize = UPB_ALIGN_MALLOC(oldsize);
  167. size = UPB_ALIGN_MALLOC(size);
  168. UPB_ASSERT((char*)ptr + oldsize == h->ptr); // Must be the last alloc.
  169. UPB_ASSERT(size <= oldsize);
  170. h->ptr = (char*)ptr + size;
  171. }
  172. UPB_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize, size_t size)
  173. {
  174. _upb_ArenaHead* h = (_upb_ArenaHead*)a;
  175. oldsize = UPB_ALIGN_MALLOC(oldsize);
  176. size = UPB_ALIGN_MALLOC(size);
  177. bool is_most_recent_alloc = (uintptr_t)ptr + oldsize == (uintptr_t)h->ptr;
  178. if (is_most_recent_alloc)
  179. {
  180. ptrdiff_t diff = size - oldsize;
  181. if ((ptrdiff_t)_upb_ArenaHas(a) >= diff)
  182. {
  183. h->ptr += diff;
  184. return ptr;
  185. }
  186. }
  187. else if (size <= oldsize)
  188. {
  189. return ptr;
  190. }
  191. void* ret = upb_Arena_Malloc(a, size);
  192. if (ret && oldsize > 0)
  193. {
  194. memcpy(ret, ptr, UPB_MIN(oldsize, size));
  195. }
  196. return ret;
  197. }
  198. UPB_INLINE upb_Arena* upb_Arena_New(void)
  199. {
  200. return upb_Arena_Init(NULL, 0, &upb_alloc_global);
  201. }
  202. #include "upb/port_undef.inc"
  203. #ifdef __cplusplus
  204. } /* extern "C" */
  205. #endif
  206. #endif /* UPB_ARENA_H_ */