OLD | NEW |
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | 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 | 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 | 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/. */ | 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | 5 |
6 #ifndef plarena_h___ | 6 #ifndef plarena_h___ |
7 #define plarena_h___ | 7 #define plarena_h___ |
8 /* | 8 /* |
9 * Lifetime-based fast allocation, inspired by much prior art, including | 9 * Lifetime-based fast allocation, inspired by much prior art, including |
10 * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes" | 10 * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 PLArena first; /* first arena in pool list */ | 51 PLArena first; /* first arena in pool list */ |
52 PLArena *current; /* arena from which to allocate space */ | 52 PLArena *current; /* arena from which to allocate space */ |
53 PRUint32 arenasize; /* net exact size of a new arena */ | 53 PRUint32 arenasize; /* net exact size of a new arena */ |
54 PRUword mask; /* alignment mask (power-of-2 - 1) */ | 54 PRUword mask; /* alignment mask (power-of-2 - 1) */ |
55 #ifdef PL_ARENAMETER | 55 #ifdef PL_ARENAMETER |
56 PLArenaStats stats; | 56 PLArenaStats stats; |
57 #endif | 57 #endif |
58 }; | 58 }; |
59 | 59 |
60 /* | 60 /* |
| 61 * WARNING: The PL_MAKE_MEM_ macros are for internal use by NSPR. Do NOT use |
| 62 * them in your code. |
| 63 * |
| 64 * NOTE: Valgrind support to be added. |
| 65 * |
| 66 * The PL_MAKE_MEM_ macros are modeled after the MOZ_MAKE_MEM_ macros in |
| 67 * Mozilla's mfbt/MemoryChecking.h. Only AddressSanitizer is supported now. |
| 68 * |
| 69 * Provides a common interface to the ASan (AddressSanitizer) and Valgrind |
| 70 * functions used to mark memory in certain ways. In detail, the following |
| 71 * three macros are provided: |
| 72 * |
| 73 * PL_MAKE_MEM_NOACCESS - Mark memory as unsafe to access (e.g. freed) |
| 74 * PL_MAKE_MEM_UNDEFINED - Mark memory as accessible, with content undefined |
| 75 * PL_MAKE_MEM_DEFINED - Mark memory as accessible, with content defined |
| 76 * |
| 77 * With Valgrind in use, these directly map to the three respective Valgrind |
| 78 * macros. With ASan in use, the NOACCESS macro maps to poisoning the memory, |
| 79 * while the UNDEFINED/DEFINED macros unpoison memory. |
| 80 * |
| 81 * With no memory checker available, all macros expand to the empty statement. |
| 82 */ |
| 83 |
| 84 /* WARNING: PL_SANITIZE_ADDRESS is for internal use by this header. Do NOT |
| 85 * define or test this macro in your code. |
| 86 */ |
| 87 #if defined(__has_feature) |
| 88 #if __has_feature(address_sanitizer) |
| 89 #define PL_SANITIZE_ADDRESS 1 |
| 90 #endif |
| 91 #elif defined(__SANITIZE_ADDRESS__) |
| 92 #define PL_SANITIZE_ADDRESS 1 |
| 93 #endif |
| 94 |
| 95 #if defined(PL_SANITIZE_ADDRESS) |
| 96 |
| 97 /* These definitions are usually provided through the |
| 98 * sanitizer/asan_interface.h header installed by ASan. |
| 99 * See https://code.google.com/p/address-sanitizer/wiki/ManualPoisoning |
| 100 */ |
| 101 |
| 102 void __asan_poison_memory_region(void const volatile *addr, size_t size); |
| 103 void __asan_unpoison_memory_region(void const volatile *addr, size_t size); |
| 104 |
| 105 #define PL_MAKE_MEM_NOACCESS(addr, size) \ |
| 106 __asan_poison_memory_region((addr), (size)) |
| 107 |
| 108 #define PL_MAKE_MEM_UNDEFINED(addr, size) \ |
| 109 __asan_unpoison_memory_region((addr), (size)) |
| 110 |
| 111 #define PL_MAKE_MEM_DEFINED(addr, size) \ |
| 112 __asan_unpoison_memory_region((addr), (size)) |
| 113 |
| 114 #else |
| 115 |
| 116 #define PL_MAKE_MEM_NOACCESS(addr, size) |
| 117 #define PL_MAKE_MEM_UNDEFINED(addr, size) |
| 118 #define PL_MAKE_MEM_DEFINED(addr, size) |
| 119 |
| 120 #endif |
| 121 |
| 122 /* |
61 * If the including .c file uses only one power-of-2 alignment, it may define | 123 * If the including .c file uses only one power-of-2 alignment, it may define |
62 * PL_ARENA_CONST_ALIGN_MASK to the alignment mask and save a few instructions | 124 * PL_ARENA_CONST_ALIGN_MASK to the alignment mask and save a few instructions |
63 * per ALLOCATE and GROW. | 125 * per ALLOCATE and GROW. |
64 */ | 126 */ |
65 #ifdef PL_ARENA_CONST_ALIGN_MASK | 127 #ifdef PL_ARENA_CONST_ALIGN_MASK |
66 #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + PL_ARENA_CONST_ALIGN_MASK) \ | 128 #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + PL_ARENA_CONST_ALIGN_MASK) \ |
67 & ~PL_ARENA_CONST_ALIGN_MASK) | 129 & ~PL_ARENA_CONST_ALIGN_MASK) |
68 | 130 |
69 #define PL_INIT_ARENA_POOL(pool, name, size) \ | 131 #define PL_INIT_ARENA_POOL(pool, name, size) \ |
70 PL_InitArenaPool(pool, name, size, PL_ARENA_CONST_ALIGN_MASK + 1) | 132 PL_InitArenaPool(pool, name, size, PL_ARENA_CONST_ALIGN_MASK + 1) |
71 #else | 133 #else |
72 #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + (pool)->mask) & ~(pool)->mask) | 134 #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + (pool)->mask) & ~(pool)->mask) |
73 #endif | 135 #endif |
74 | 136 |
75 #define PL_ARENA_ALLOCATE(p, pool, nb) \ | 137 #define PL_ARENA_ALLOCATE(p, pool, nb) \ |
76 PR_BEGIN_MACRO \ | 138 PR_BEGIN_MACRO \ |
77 PLArena *_a = (pool)->current; \ | 139 PLArena *_a = (pool)->current; \ |
78 PRUint32 _nb = PL_ARENA_ALIGN(pool, nb); \ | 140 PRUint32 _nb = PL_ARENA_ALIGN(pool, nb); \ |
79 PRUword _p = _a->avail; \ | 141 PRUword _p = _a->avail; \ |
80 PRUword _q = _p + _nb; \ | 142 PRUword _q = _p + _nb; \ |
81 if (_q > _a->limit) \ | 143 if (_q > _a->limit) { \ |
82 _p = (PRUword)PL_ArenaAllocate(pool, _nb); \ | 144 _p = (PRUword)PL_ArenaAllocate(pool, _nb); \ |
83 else \ | 145 } else { \ |
84 _a->avail = _q; \ | 146 _a->avail = _q; \ |
| 147 } \ |
85 p = (void *)_p; \ | 148 p = (void *)_p; \ |
| 149 PL_MAKE_MEM_UNDEFINED(p, nb); \ |
86 PL_ArenaCountAllocation(pool, nb); \ | 150 PL_ArenaCountAllocation(pool, nb); \ |
87 PR_END_MACRO | 151 PR_END_MACRO |
88 | 152 |
89 #define PL_ARENA_GROW(p, pool, size, incr) \ | 153 #define PL_ARENA_GROW(p, pool, size, incr) \ |
90 PR_BEGIN_MACRO \ | 154 PR_BEGIN_MACRO \ |
91 PLArena *_a = (pool)->current; \ | 155 PLArena *_a = (pool)->current; \ |
92 PRUint32 _incr = PL_ARENA_ALIGN(pool, incr); \ | 156 PRUint32 _incr = PL_ARENA_ALIGN(pool, incr); \ |
93 PRUword _p = _a->avail; \ | 157 PRUword _p = _a->avail; \ |
94 PRUword _q = _p + _incr; \ | 158 PRUword _q = _p + _incr; \ |
95 if (_p == (PRUword)(p) + PL_ARENA_ALIGN(pool, size) && \ | 159 if (_p == (PRUword)(p) + PL_ARENA_ALIGN(pool, size) && \ |
96 _q <= _a->limit) { \ | 160 _q <= _a->limit) { \ |
| 161 PL_MAKE_MEM_UNDEFINED((unsigned char *)(p) + size, incr); \ |
97 _a->avail = _q; \ | 162 _a->avail = _q; \ |
98 PL_ArenaCountInplaceGrowth(pool, size, incr); \ | 163 PL_ArenaCountInplaceGrowth(pool, size, incr); \ |
99 } else { \ | 164 } else { \ |
100 p = PL_ArenaGrow(pool, p, size, incr); \ | 165 p = PL_ArenaGrow(pool, p, size, incr); \ |
101 } \ | 166 } \ |
102 PL_ArenaCountGrowth(pool, size, incr); \ | 167 PL_ArenaCountGrowth(pool, size, incr); \ |
103 PR_END_MACRO | 168 PR_END_MACRO |
104 | 169 |
105 #define PL_ARENA_MARK(pool) ((void *) (pool)->current->avail) | 170 #define PL_ARENA_MARK(pool) ((void *) (pool)->current->avail) |
106 #define PR_UPTRDIFF(p,q) ((PRUword)(p) - (PRUword)(q)) | 171 #define PR_UPTRDIFF(p,q) ((PRUword)(p) - (PRUword)(q)) |
107 | 172 |
108 #define PL_CLEAR_UNUSED_PATTERN(a, pattern) \ | 173 #define PL_CLEAR_UNUSED_PATTERN(a, pattern) \ |
109 » (PR_ASSERT((a)->avail <= (a)->limit), \ | 174 PR_BEGIN_MACRO \ |
110 » memset((void*)(a)->avail, (pattern), (a)->limit - (a)->avail)) | 175 PR_ASSERT((a)->avail <= (a)->limit); \ |
| 176 PL_MAKE_MEM_UNDEFINED((void*)(a)->avail, (a)->limit - (a)->avail); \ |
| 177 memset((void*)(a)->avail, (pattern), (a)->limit - (a)->avail); \ |
| 178 PR_END_MACRO |
111 #ifdef DEBUG | 179 #ifdef DEBUG |
112 #define PL_FREE_PATTERN 0xDA | 180 #define PL_FREE_PATTERN 0xDA |
113 #define PL_CLEAR_UNUSED(a) PL_CLEAR_UNUSED_PATTERN((a), PL_FREE_PATTERN) | 181 #define PL_CLEAR_UNUSED(a) PL_CLEAR_UNUSED_PATTERN((a), PL_FREE_PATTERN) |
114 #define PL_CLEAR_ARENA(a) memset((void*)(a), PL_FREE_PATTERN, \ | 182 #define PL_CLEAR_ARENA(a) \ |
115 (a)->limit - (PRUword)(a)) | 183 PR_BEGIN_MACRO \ |
| 184 PL_MAKE_MEM_UNDEFINED((void*)(a), (a)->limit - (PRUword)(a)); \ |
| 185 memset((void*)(a), PL_FREE_PATTERN, (a)->limit - (PRUword)(a)); \ |
| 186 PR_END_MACRO |
116 #else | 187 #else |
117 #define PL_CLEAR_UNUSED(a) | 188 #define PL_CLEAR_UNUSED(a) |
118 #define PL_CLEAR_ARENA(a) | 189 #define PL_CLEAR_ARENA(a) |
119 #endif | 190 #endif |
120 | 191 |
121 #define PL_ARENA_RELEASE(pool, mark) \ | 192 #define PL_ARENA_RELEASE(pool, mark) \ |
122 PR_BEGIN_MACRO \ | 193 PR_BEGIN_MACRO \ |
123 char *_m = (char *)(mark); \ | 194 char *_m = (char *)(mark); \ |
124 PLArena *_a = (pool)->current; \ | 195 PLArena *_a = (pool)->current; \ |
125 if (PR_UPTRDIFF(_m, _a->base) <= PR_UPTRDIFF(_a->avail, _a->base)) { \ | 196 if (PR_UPTRDIFF(_m, _a->base) <= PR_UPTRDIFF(_a->avail, _a->base)) { \ |
126 _a->avail = (PRUword)PL_ARENA_ALIGN(pool, _m); \ | 197 _a->avail = (PRUword)PL_ARENA_ALIGN(pool, _m); \ |
127 PL_CLEAR_UNUSED(_a); \ | 198 PL_CLEAR_UNUSED(_a); \ |
| 199 PL_MAKE_MEM_NOACCESS((void*)_a->avail, _a->limit - _a->avail); \ |
128 PL_ArenaCountRetract(pool, _m); \ | 200 PL_ArenaCountRetract(pool, _m); \ |
129 } else { \ | 201 } else { \ |
130 PL_ArenaRelease(pool, _m); \ | 202 PL_ArenaRelease(pool, _m); \ |
131 } \ | 203 } \ |
132 PL_ArenaCountRelease(pool, _m); \ | 204 PL_ArenaCountRelease(pool, _m); \ |
133 PR_END_MACRO | 205 PR_END_MACRO |
134 | 206 |
135 #ifdef PL_ARENAMETER | 207 #ifdef PL_ARENAMETER |
136 #define PL_COUNT_ARENA(pool,op) ((pool)->stats.narenas op) | 208 #define PL_COUNT_ARENA(pool,op) ((pool)->stats.narenas op) |
137 #else | 209 #else |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 #define PL_ArenaCountInplaceGrowth(ap, size, incr) /* nothing */ | 244 #define PL_ArenaCountInplaceGrowth(ap, size, incr) /* nothing */ |
173 #define PL_ArenaCountGrowth(ap, size, incr) /* nothing */ | 245 #define PL_ArenaCountGrowth(ap, size, incr) /* nothing */ |
174 #define PL_ArenaCountRelease(ap, mark) /* nothing */ | 246 #define PL_ArenaCountRelease(ap, mark) /* nothing */ |
175 #define PL_ArenaCountRetract(ap, mark) /* nothing */ | 247 #define PL_ArenaCountRetract(ap, mark) /* nothing */ |
176 | 248 |
177 #endif /* !PL_ARENAMETER */ | 249 #endif /* !PL_ARENAMETER */ |
178 | 250 |
179 PR_END_EXTERN_C | 251 PR_END_EXTERN_C |
180 | 252 |
181 #endif /* plarena_h___ */ | 253 #endif /* plarena_h___ */ |
OLD | NEW |