plarena.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef plarena_h___
  6. #define plarena_h___
  7. /*
  8. * Lifetime-based fast allocation, inspired by much prior art, including
  9. * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes"
  10. * David R. Hanson, Software -- Practice and Experience, Vol. 20(1).
  11. *
  12. * Also supports LIFO allocation (PL_ARENA_MARK/PL_ARENA_RELEASE).
  13. */
  14. #include "prtypes.h"
  15. PR_BEGIN_EXTERN_C
  16. typedef struct PLArena PLArena;
  17. struct PLArena {
  18. PLArena *next; /* next arena for this lifetime */
  19. PRUword base; /* aligned base address, follows this header */
  20. PRUword limit; /* one beyond last byte in arena */
  21. PRUword avail; /* points to next available byte */
  22. };
  23. #ifdef PL_ARENAMETER
  24. typedef struct PLArenaStats PLArenaStats;
  25. struct PLArenaStats {
  26. PLArenaStats *next; /* next in arenaStats list */
  27. char *name; /* name for debugging */
  28. PRUint32 narenas; /* number of arenas in pool */
  29. PRUint32 nallocs; /* number of PL_ARENA_ALLOCATE() calls */
  30. PRUint32 nreclaims; /* number of reclaims from freeArenas */
  31. PRUint32 nmallocs; /* number of malloc() calls */
  32. PRUint32 ndeallocs; /* number of lifetime deallocations */
  33. PRUint32 ngrows; /* number of PL_ARENA_GROW() calls */
  34. PRUint32 ninplace; /* number of in-place growths */
  35. PRUint32 nreleases; /* number of PL_ARENA_RELEASE() calls */
  36. PRUint32 nfastrels; /* number of "fast path" releases */
  37. PRUint32 nbytes; /* total bytes allocated */
  38. PRUint32 maxalloc; /* maximum allocation size in bytes */
  39. PRFloat64 variance; /* size variance accumulator */
  40. };
  41. #endif
  42. typedef struct PLArenaPool PLArenaPool;
  43. struct PLArenaPool {
  44. PLArena first; /* first arena in pool list */
  45. PLArena *current; /* arena from which to allocate space */
  46. PRUint32 arenasize; /* net exact size of a new arena */
  47. PRUword mask; /* alignment mask (power-of-2 - 1) */
  48. #ifdef PL_ARENAMETER
  49. PLArenaStats stats;
  50. #endif
  51. };
  52. /*
  53. * WARNING: The PL_MAKE_MEM_ macros are for internal use by NSPR. Do NOT use
  54. * them in your code.
  55. *
  56. * NOTE: Valgrind support to be added.
  57. *
  58. * The PL_MAKE_MEM_ macros are modeled after the MOZ_MAKE_MEM_ macros in
  59. * Mozilla's mfbt/MemoryChecking.h. Only AddressSanitizer is supported now.
  60. *
  61. * Provides a common interface to the ASan (AddressSanitizer) and Valgrind
  62. * functions used to mark memory in certain ways. In detail, the following
  63. * three macros are provided:
  64. *
  65. * PL_MAKE_MEM_NOACCESS - Mark memory as unsafe to access (e.g. freed)
  66. * PL_MAKE_MEM_UNDEFINED - Mark memory as accessible, with content undefined
  67. * PL_MAKE_MEM_DEFINED - Mark memory as accessible, with content defined
  68. *
  69. * With Valgrind in use, these directly map to the three respective Valgrind
  70. * macros. With ASan in use, the NOACCESS macro maps to poisoning the memory,
  71. * while the UNDEFINED/DEFINED macros unpoison memory.
  72. *
  73. * With no memory checker available, all macros expand to the empty statement.
  74. */
  75. /* WARNING: PL_SANITIZE_ADDRESS is for internal use by this header. Do NOT
  76. * define or test this macro in your code.
  77. */
  78. #if defined(__has_feature)
  79. #if __has_feature(address_sanitizer)
  80. #define PL_SANITIZE_ADDRESS 1
  81. #endif
  82. #elif defined(__SANITIZE_ADDRESS__)
  83. #define PL_SANITIZE_ADDRESS 1
  84. #endif
  85. #if defined(PL_SANITIZE_ADDRESS)
  86. /* These definitions are usually provided through the
  87. * sanitizer/asan_interface.h header installed by ASan.
  88. * See https://github.com/google/sanitizers/wiki/AddressSanitizerManualPoisoning
  89. */
  90. PR_IMPORT(void) __asan_poison_memory_region(void const volatile *addr, size_t size);
  91. PR_IMPORT(void) __asan_unpoison_memory_region(void const volatile *addr, size_t size);
  92. #define PL_MAKE_MEM_NOACCESS(addr, size) \
  93. __asan_poison_memory_region((addr), (size))
  94. #define PL_MAKE_MEM_UNDEFINED(addr, size) \
  95. __asan_unpoison_memory_region((addr), (size))
  96. #define PL_MAKE_MEM_DEFINED(addr, size) \
  97. __asan_unpoison_memory_region((addr), (size))
  98. #else
  99. #define PL_MAKE_MEM_NOACCESS(addr, size)
  100. #define PL_MAKE_MEM_UNDEFINED(addr, size)
  101. #define PL_MAKE_MEM_DEFINED(addr, size)
  102. #endif
  103. /*
  104. * If the including .c file uses only one power-of-2 alignment, it may define
  105. * PL_ARENA_CONST_ALIGN_MASK to the alignment mask and save a few instructions
  106. * per ALLOCATE and GROW.
  107. */
  108. #ifdef PL_ARENA_CONST_ALIGN_MASK
  109. #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + PL_ARENA_CONST_ALIGN_MASK) \
  110. & ~PL_ARENA_CONST_ALIGN_MASK)
  111. #define PL_INIT_ARENA_POOL(pool, name, size) \
  112. PL_InitArenaPool(pool, name, size, PL_ARENA_CONST_ALIGN_MASK + 1)
  113. #else
  114. #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + (pool)->mask) & ~(pool)->mask)
  115. #endif
  116. #define PL_ARENA_ALLOCATE(p, pool, nb) \
  117. PR_BEGIN_MACRO \
  118. PLArena *_a = (pool)->current; \
  119. PRUint32 _nb = PL_ARENA_ALIGN(pool, (PRUint32)nb); \
  120. PRUword _p = _a->avail; \
  121. if (_nb < (PRUint32)nb) { \
  122. _p = 0; \
  123. } else if (_nb > (_a->limit - _a->avail)) { \
  124. _p = (PRUword)PL_ArenaAllocate(pool, _nb); \
  125. } else { \
  126. _a->avail += _nb; \
  127. } \
  128. p = (void *)_p; \
  129. if (p) { \
  130. PL_MAKE_MEM_UNDEFINED(p, (PRUint32)nb); \
  131. PL_ArenaCountAllocation(pool, (PRUint32)nb); \
  132. } \
  133. PR_END_MACRO
  134. #define PL_ARENA_GROW(p, pool, size, incr) \
  135. PR_BEGIN_MACRO \
  136. PLArena *_a = (pool)->current; \
  137. PRUint32 _incr = PL_ARENA_ALIGN(pool, (PRUint32)incr); \
  138. if (_incr < (PRUint32)incr) { \
  139. p = NULL; \
  140. } else if (_a->avail == (PRUword)(p) + PL_ARENA_ALIGN(pool, size) && \
  141. _incr <= (_a->limit - _a->avail)) { \
  142. PL_MAKE_MEM_UNDEFINED((unsigned char *)(p) + size, (PRUint32)incr); \
  143. _a->avail += _incr; \
  144. PL_ArenaCountInplaceGrowth(pool, size, (PRUint32)incr); \
  145. } else { \
  146. p = PL_ArenaGrow(pool, p, size, (PRUint32)incr); \
  147. } \
  148. if (p) {\
  149. PL_ArenaCountGrowth(pool, size, (PRUint32)incr); \
  150. } \
  151. PR_END_MACRO
  152. #define PL_ARENA_MARK(pool) ((void *) (pool)->current->avail)
  153. #define PR_UPTRDIFF(p,q) ((PRUword)(p) - (PRUword)(q))
  154. #define PL_CLEAR_UNUSED_PATTERN(a, pattern) \
  155. PR_BEGIN_MACRO \
  156. PR_ASSERT((a)->avail <= (a)->limit); \
  157. PL_MAKE_MEM_UNDEFINED((void*)(a)->avail, (a)->limit - (a)->avail); \
  158. memset((void*)(a)->avail, (pattern), (a)->limit - (a)->avail); \
  159. PR_END_MACRO
  160. #ifdef DEBUG
  161. #define PL_FREE_PATTERN 0xDA
  162. #define PL_CLEAR_UNUSED(a) PL_CLEAR_UNUSED_PATTERN((a), PL_FREE_PATTERN)
  163. #define PL_CLEAR_ARENA(a) \
  164. PR_BEGIN_MACRO \
  165. PL_MAKE_MEM_UNDEFINED((void*)(a), (a)->limit - (PRUword)(a)); \
  166. memset((void*)(a), PL_FREE_PATTERN, (a)->limit - (PRUword)(a)); \
  167. PR_END_MACRO
  168. #else
  169. #define PL_CLEAR_UNUSED(a)
  170. #define PL_CLEAR_ARENA(a)
  171. #endif
  172. #define PL_ARENA_RELEASE(pool, mark) \
  173. PR_BEGIN_MACRO \
  174. char *_m = (char *)(mark); \
  175. PLArena *_a = (pool)->current; \
  176. if (PR_UPTRDIFF(_m, _a->base) <= PR_UPTRDIFF(_a->avail, _a->base)) { \
  177. _a->avail = (PRUword)PL_ARENA_ALIGN(pool, _m); \
  178. PL_CLEAR_UNUSED(_a); \
  179. PL_MAKE_MEM_NOACCESS((void*)_a->avail, _a->limit - _a->avail); \
  180. PL_ArenaCountRetract(pool, _m); \
  181. } else { \
  182. PL_ArenaRelease(pool, _m); \
  183. } \
  184. PL_ArenaCountRelease(pool, _m); \
  185. PR_END_MACRO
  186. #ifdef PL_ARENAMETER
  187. #define PL_COUNT_ARENA(pool,op) ((pool)->stats.narenas op)
  188. #else
  189. #define PL_COUNT_ARENA(pool,op)
  190. #endif
  191. #define PL_ARENA_DESTROY(pool, a, pnext) \
  192. PR_BEGIN_MACRO \
  193. PL_COUNT_ARENA(pool,--); \
  194. if ((pool)->current == (a)) (pool)->current = &(pool)->first; \
  195. *(pnext) = (a)->next; \
  196. PL_CLEAR_ARENA(a); \
  197. free(a); \
  198. (a) = 0; \
  199. PR_END_MACRO
  200. /*
  201. ** Initialize an arena pool with the given name for debugging and metering,
  202. ** with a minimum gross size per arena of size bytes. The net size per arena
  203. ** is smaller than the gross size by a header of four pointers plus any
  204. ** necessary padding for alignment.
  205. **
  206. ** Note: choose a gross size that's a power of two to avoid the heap allocator
  207. ** rounding the size up.
  208. **/
  209. PR_EXTERN(void) PL_InitArenaPool(
  210. PLArenaPool *pool, const char *name, PRUint32 size, PRUint32 align);
  211. /*
  212. ** Finish using arenas, freeing all memory associated with them.
  213. ** NOTE: this function is now a no-op. If you want to free a single
  214. ** PLArenaPoolUse use PL_FreeArenaPool() or PL_FinishArenaPool().
  215. **/
  216. PR_EXTERN(void) PL_ArenaFinish(void);
  217. /*
  218. ** Free the arenas in pool. The user may continue to allocate from pool
  219. ** after calling this function. There is no need to call PL_InitArenaPool()
  220. ** again unless PL_FinishArenaPool(pool) has been called.
  221. **/
  222. PR_EXTERN(void) PL_FreeArenaPool(PLArenaPool *pool);
  223. /*
  224. ** Free the arenas in pool and finish using it altogether.
  225. **/
  226. PR_EXTERN(void) PL_FinishArenaPool(PLArenaPool *pool);
  227. /*
  228. ** Compact all of the arenas in a pool so that no space is wasted.
  229. ** NOT IMPLEMENTED. Do not use.
  230. **/
  231. PR_EXTERN(void) PL_CompactArenaPool(PLArenaPool *pool);
  232. /*
  233. ** Friend functions used by the PL_ARENA_*() macros.
  234. **
  235. ** WARNING: do not call these functions directly. Always use the
  236. ** PL_ARENA_*() macros.
  237. **/
  238. PR_EXTERN(void *) PL_ArenaAllocate(PLArenaPool *pool, PRUint32 nb);
  239. PR_EXTERN(void *) PL_ArenaGrow(
  240. PLArenaPool *pool, void *p, PRUint32 size, PRUint32 incr);
  241. PR_EXTERN(void) PL_ArenaRelease(PLArenaPool *pool, char *mark);
  242. /*
  243. ** memset contents of all arenas in pool to pattern
  244. */
  245. PR_EXTERN(void) PL_ClearArenaPool(PLArenaPool *pool, PRInt32 pattern);
  246. /*
  247. ** A function like malloc_size() or malloc_usable_size() that measures the
  248. ** size of a heap block.
  249. */
  250. typedef size_t (*PLMallocSizeFn)(const void *ptr);
  251. /*
  252. ** Measure all memory used by a PLArenaPool, excluding the PLArenaPool
  253. ** structure.
  254. */
  255. PR_EXTERN(size_t) PL_SizeOfArenaPoolExcludingPool(
  256. const PLArenaPool *pool, PLMallocSizeFn mallocSizeOf);
  257. #ifdef PL_ARENAMETER
  258. #include <stdio.h>
  259. PR_EXTERN(void) PL_ArenaCountAllocation(PLArenaPool *pool, PRUint32 nb);
  260. PR_EXTERN(void) PL_ArenaCountInplaceGrowth(
  261. PLArenaPool *pool, PRUint32 size, PRUint32 incr);
  262. PR_EXTERN(void) PL_ArenaCountGrowth(
  263. PLArenaPool *pool, PRUint32 size, PRUint32 incr);
  264. PR_EXTERN(void) PL_ArenaCountRelease(PLArenaPool *pool, char *mark);
  265. PR_EXTERN(void) PL_ArenaCountRetract(PLArenaPool *pool, char *mark);
  266. PR_EXTERN(void) PL_DumpArenaStats(FILE *fp);
  267. #else /* !PL_ARENAMETER */
  268. #define PL_ArenaCountAllocation(ap, nb) /* nothing */
  269. #define PL_ArenaCountInplaceGrowth(ap, size, incr) /* nothing */
  270. #define PL_ArenaCountGrowth(ap, size, incr) /* nothing */
  271. #define PL_ArenaCountRelease(ap, mark) /* nothing */
  272. #define PL_ArenaCountRetract(ap, mark) /* nothing */
  273. #endif /* !PL_ARENAMETER */
  274. PR_END_EXTERN_C
  275. #endif /* plarena_h___ */