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 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #endif
  37. /** upb_alloc *****************************************************************/
  38. /* A upb_alloc is a possibly-stateful allocator object.
  39. *
  40. * It could either be an arena allocator (which doesn't require individual
  41. * free() calls) or a regular malloc() (which does). The client must therefore
  42. * free memory unless it knows that the allocator is an arena allocator. */
  43. struct upb_alloc;
  44. typedef struct upb_alloc upb_alloc;
  45. /* A malloc()/free() function.
  46. * If "size" is 0 then the function acts like free(), otherwise it acts like
  47. * realloc(). Only "oldsize" bytes from a previous allocation are preserved. */
  48. typedef void* upb_alloc_func(upb_alloc* alloc, void* ptr, size_t oldsize,
  49. size_t size);
  50. struct upb_alloc {
  51. upb_alloc_func* func;
  52. };
  53. UPB_INLINE void* upb_malloc(upb_alloc* alloc, size_t size) {
  54. UPB_ASSERT(alloc);
  55. return alloc->func(alloc, NULL, 0, size);
  56. }
  57. UPB_INLINE void* upb_realloc(upb_alloc* alloc, void* ptr, size_t oldsize,
  58. size_t size) {
  59. UPB_ASSERT(alloc);
  60. return alloc->func(alloc, ptr, oldsize, size);
  61. }
  62. UPB_INLINE void upb_free(upb_alloc* alloc, void* ptr) {
  63. assert(alloc);
  64. alloc->func(alloc, ptr, 0, 0);
  65. }
  66. /* The global allocator used by upb. Uses the standard malloc()/free(). */
  67. extern upb_alloc upb_alloc_global;
  68. /* Functions that hard-code the global malloc.
  69. *
  70. * We still get benefit because we can put custom logic into our global
  71. * allocator, like injecting out-of-memory faults in debug/testing builds. */
  72. UPB_INLINE void* upb_gmalloc(size_t size) {
  73. return upb_malloc(&upb_alloc_global, size);
  74. }
  75. UPB_INLINE void* upb_grealloc(void* ptr, size_t oldsize, size_t size) {
  76. return upb_realloc(&upb_alloc_global, ptr, oldsize, size);
  77. }
  78. UPB_INLINE void upb_gfree(void* ptr) { upb_free(&upb_alloc_global, ptr); }
  79. /* upb_Arena ******************************************************************/
  80. /* upb_Arena is a specific allocator implementation that uses arena allocation.
  81. * The user provides an allocator that will be used to allocate the underlying
  82. * arena blocks. Arenas by nature do not require the individual allocations
  83. * to be freed. However the Arena does allow users to register cleanup
  84. * functions that will run when the arena is destroyed.
  85. *
  86. * A upb_Arena is *not* thread-safe.
  87. *
  88. * You could write a thread-safe arena allocator that satisfies the
  89. * upb_alloc interface, but it would not be as efficient for the
  90. * single-threaded case. */
  91. typedef void upb_CleanupFunc(void* ud);
  92. struct upb_Arena;
  93. typedef struct upb_Arena upb_Arena;
  94. typedef struct {
  95. /* We implement the allocator interface.
  96. * This must be the first member of upb_Arena!
  97. * TODO(haberman): remove once handlers are gone. */
  98. upb_alloc alloc;
  99. char *ptr, *end;
  100. } _upb_ArenaHead;
  101. /* Creates an arena from the given initial block (if any -- n may be 0).
  102. * Additional blocks will be allocated from |alloc|. If |alloc| is NULL, this
  103. * is a fixed-size arena and cannot grow. */
  104. upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc);
  105. void upb_Arena_Free(upb_Arena* a);
  106. bool upb_Arena_AddCleanup(upb_Arena* a, void* ud, upb_CleanupFunc* func);
  107. bool upb_Arena_Fuse(upb_Arena* a, upb_Arena* b);
  108. void* _upb_Arena_SlowMalloc(upb_Arena* a, size_t size);
  109. UPB_INLINE upb_alloc* upb_Arena_Alloc(upb_Arena* a) { return (upb_alloc*)a; }
  110. UPB_INLINE size_t _upb_ArenaHas(upb_Arena* a) {
  111. _upb_ArenaHead* h = (_upb_ArenaHead*)a;
  112. return (size_t)(h->end - h->ptr);
  113. }
  114. UPB_INLINE void* _upb_Arena_FastMalloc(upb_Arena* a, size_t size) {
  115. _upb_ArenaHead* h = (_upb_ArenaHead*)a;
  116. void* ret = h->ptr;
  117. UPB_ASSERT(UPB_ALIGN_MALLOC((uintptr_t)ret) == (uintptr_t)ret);
  118. UPB_ASSERT(UPB_ALIGN_MALLOC(size) == size);
  119. UPB_UNPOISON_MEMORY_REGION(ret, size);
  120. h->ptr += size;
  121. #if UPB_ASAN
  122. {
  123. size_t guard_size = 32;
  124. if (_upb_ArenaHas(a) >= guard_size) {
  125. h->ptr += guard_size;
  126. } else {
  127. h->ptr = h->end;
  128. }
  129. }
  130. #endif
  131. return ret;
  132. }
  133. UPB_INLINE void* upb_Arena_Malloc(upb_Arena* a, size_t size) {
  134. size = UPB_ALIGN_MALLOC(size);
  135. if (UPB_UNLIKELY(_upb_ArenaHas(a) < size)) {
  136. return _upb_Arena_SlowMalloc(a, size);
  137. }
  138. return _upb_Arena_FastMalloc(a, size);
  139. }
  140. // Shrinks the last alloc from arena.
  141. // REQUIRES: (ptr, oldsize) was the last malloc/realloc from this arena.
  142. // We could also add a upb_Arena_TryShrinkLast() which is simply a no-op if
  143. // this was not the last alloc.
  144. UPB_INLINE void upb_Arena_ShrinkLast(upb_Arena* a, void* ptr, size_t oldsize,
  145. size_t size) {
  146. _upb_ArenaHead* h = (_upb_ArenaHead*)a;
  147. oldsize = UPB_ALIGN_MALLOC(oldsize);
  148. size = UPB_ALIGN_MALLOC(size);
  149. UPB_ASSERT((char*)ptr + oldsize == h->ptr); // Must be the last alloc.
  150. UPB_ASSERT(size <= oldsize);
  151. h->ptr = (char*)ptr + size;
  152. }
  153. UPB_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize,
  154. size_t size) {
  155. _upb_ArenaHead* h = (_upb_ArenaHead*)a;
  156. oldsize = UPB_ALIGN_MALLOC(oldsize);
  157. size = UPB_ALIGN_MALLOC(size);
  158. bool is_most_recent_alloc = (uintptr_t)ptr + oldsize == (uintptr_t)h->ptr;
  159. if (is_most_recent_alloc) {
  160. ptrdiff_t diff = size - oldsize;
  161. if ((ptrdiff_t)_upb_ArenaHas(a) >= diff) {
  162. h->ptr += diff;
  163. return ptr;
  164. }
  165. } else if (size <= oldsize) {
  166. return ptr;
  167. }
  168. void* ret = upb_Arena_Malloc(a, size);
  169. if (ret && oldsize > 0) {
  170. memcpy(ret, ptr, UPB_MIN(oldsize, size));
  171. }
  172. return ret;
  173. }
  174. UPB_INLINE upb_Arena* upb_Arena_New(void) {
  175. return upb_Arena_Init(NULL, 0, &upb_alloc_global);
  176. }
  177. #include "upb/port_undef.inc"
  178. #ifdef __cplusplus
  179. } /* extern "C" */
  180. #endif
  181. #endif /* UPB_ARENA_H_ */