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