OLD | NEW |
| (Empty) |
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 | |
6 #ifndef plarena_h___ | |
7 #define plarena_h___ | |
8 /* | |
9 * Lifetime-based fast allocation, inspired by much prior art, including | |
10 * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes" | |
11 * David R. Hanson, Software -- Practice and Experience, Vol. 20(1). | |
12 * | |
13 * Also supports LIFO allocation (PL_ARENA_MARK/PL_ARENA_RELEASE). | |
14 */ | |
15 #include "prtypes.h" | |
16 #include "plarenas.h" | |
17 | |
18 PR_BEGIN_EXTERN_C | |
19 | |
20 typedef struct PLArena PLArena; | |
21 | |
22 struct PLArena { | |
23 PLArena *next; /* next arena for this lifetime */ | |
24 PRUword base; /* aligned base address, follows this header */ | |
25 PRUword limit; /* one beyond last byte in arena */ | |
26 PRUword avail; /* points to next available byte */ | |
27 }; | |
28 | |
29 #ifdef PL_ARENAMETER | |
30 typedef struct PLArenaStats PLArenaStats; | |
31 | |
32 struct PLArenaStats { | |
33 PLArenaStats *next; /* next in arenaStats list */ | |
34 char *name; /* name for debugging */ | |
35 PRUint32 narenas; /* number of arenas in pool */ | |
36 PRUint32 nallocs; /* number of PL_ARENA_ALLOCATE() calls */ | |
37 PRUint32 nreclaims; /* number of reclaims from freeArenas */ | |
38 PRUint32 nmallocs; /* number of malloc() calls */ | |
39 PRUint32 ndeallocs; /* number of lifetime deallocations */ | |
40 PRUint32 ngrows; /* number of PL_ARENA_GROW() calls */ | |
41 PRUint32 ninplace; /* number of in-place growths */ | |
42 PRUint32 nreleases; /* number of PL_ARENA_RELEASE() calls */ | |
43 PRUint32 nfastrels; /* number of "fast path" releases */ | |
44 PRUint32 nbytes; /* total bytes allocated */ | |
45 PRUint32 maxalloc; /* maximum allocation size in bytes */ | |
46 PRFloat64 variance; /* size variance accumulator */ | |
47 }; | |
48 #endif | |
49 | |
50 struct PLArenaPool { | |
51 PLArena first; /* first arena in pool list */ | |
52 PLArena *current; /* arena from which to allocate space */ | |
53 PRUint32 arenasize; /* net exact size of a new arena */ | |
54 PRUword mask; /* alignment mask (power-of-2 - 1) */ | |
55 #ifdef PL_ARENAMETER | |
56 PLArenaStats stats; | |
57 #endif | |
58 }; | |
59 | |
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://github.com/google/sanitizers/wiki/AddressSanitizerManualPoisoning | |
100 */ | |
101 | |
102 PR_IMPORT(void) __asan_poison_memory_region(void const volatile *addr, size_t si
ze); | |
103 PR_IMPORT(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 /* | |
123 * If the including .c file uses only one power-of-2 alignment, it may define | |
124 * PL_ARENA_CONST_ALIGN_MASK to the alignment mask and save a few instructions | |
125 * per ALLOCATE and GROW. | |
126 */ | |
127 #ifdef PL_ARENA_CONST_ALIGN_MASK | |
128 #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + PL_ARENA_CONST_ALIGN_MASK) \ | |
129 & ~PL_ARENA_CONST_ALIGN_MASK) | |
130 | |
131 #define PL_INIT_ARENA_POOL(pool, name, size) \ | |
132 PL_InitArenaPool(pool, name, size, PL_ARENA_CONST_ALIGN_MASK + 1) | |
133 #else | |
134 #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + (pool)->mask) & ~(pool)->mask) | |
135 #endif | |
136 | |
137 #define PL_ARENA_ALLOCATE(p, pool, nb) \ | |
138 PR_BEGIN_MACRO \ | |
139 PLArena *_a = (pool)->current; \ | |
140 PRUint32 _nb = PL_ARENA_ALIGN(pool, (PRUint32)nb); \ | |
141 PRUword _p = _a->avail; \ | |
142 if (_nb < (PRUint32)nb) { \ | |
143 _p = 0; \ | |
144 } else if (_nb > (_a->limit - _a->avail)) { \ | |
145 _p = (PRUword)PL_ArenaAllocate(pool, _nb); \ | |
146 } else { \ | |
147 _a->avail += _nb; \ | |
148 } \ | |
149 p = (void *)_p; \ | |
150 if (p) { \ | |
151 PL_MAKE_MEM_UNDEFINED(p, (PRUint32)nb); \ | |
152 PL_ArenaCountAllocation(pool, (PRUint32)nb); \ | |
153 } \ | |
154 PR_END_MACRO | |
155 | |
156 #define PL_ARENA_GROW(p, pool, size, incr) \ | |
157 PR_BEGIN_MACRO \ | |
158 PLArena *_a = (pool)->current; \ | |
159 PRUint32 _incr = PL_ARENA_ALIGN(pool, (PRUint32)incr); \ | |
160 if (_incr < (PRUint32)incr) { \ | |
161 p = NULL; \ | |
162 } else if (_a->avail == (PRUword)(p) + PL_ARENA_ALIGN(pool, size) && \ | |
163 _incr <= (_a->limit - _a->avail)) { \ | |
164 PL_MAKE_MEM_UNDEFINED((unsigned char *)(p) + size, (PRUint32)incr);
\ | |
165 _a->avail += _incr; \ | |
166 PL_ArenaCountInplaceGrowth(pool, size, (PRUint32)incr); \ | |
167 } else { \ | |
168 p = PL_ArenaGrow(pool, p, size, (PRUint32)incr); \ | |
169 } \ | |
170 if (p) {\ | |
171 PL_ArenaCountGrowth(pool, size, (PRUint32)incr); \ | |
172 } \ | |
173 PR_END_MACRO | |
174 | |
175 #define PL_ARENA_MARK(pool) ((void *) (pool)->current->avail) | |
176 #define PR_UPTRDIFF(p,q) ((PRUword)(p) - (PRUword)(q)) | |
177 | |
178 #define PL_CLEAR_UNUSED_PATTERN(a, pattern) \ | |
179 PR_BEGIN_MACRO \ | |
180 PR_ASSERT((a)->avail <= (a)->limit); \ | |
181 PL_MAKE_MEM_UNDEFINED((void*)(a)->avail, (a)->limit - (a)->avail); \ | |
182 memset((void*)(a)->avail, (pattern), (a)->limit - (a)->avail); \ | |
183 PR_END_MACRO | |
184 #ifdef DEBUG | |
185 #define PL_FREE_PATTERN 0xDA | |
186 #define PL_CLEAR_UNUSED(a) PL_CLEAR_UNUSED_PATTERN((a), PL_FREE_PATTERN) | |
187 #define PL_CLEAR_ARENA(a) \ | |
188 PR_BEGIN_MACRO \ | |
189 PL_MAKE_MEM_UNDEFINED((void*)(a), (a)->limit - (PRUword)(a)); \ | |
190 memset((void*)(a), PL_FREE_PATTERN, (a)->limit - (PRUword)(a)); \ | |
191 PR_END_MACRO | |
192 #else | |
193 #define PL_CLEAR_UNUSED(a) | |
194 #define PL_CLEAR_ARENA(a) | |
195 #endif | |
196 | |
197 #define PL_ARENA_RELEASE(pool, mark) \ | |
198 PR_BEGIN_MACRO \ | |
199 char *_m = (char *)(mark); \ | |
200 PLArena *_a = (pool)->current; \ | |
201 if (PR_UPTRDIFF(_m, _a->base) <= PR_UPTRDIFF(_a->avail, _a->base)) { \ | |
202 _a->avail = (PRUword)PL_ARENA_ALIGN(pool, _m); \ | |
203 PL_CLEAR_UNUSED(_a); \ | |
204 PL_MAKE_MEM_NOACCESS((void*)_a->avail, _a->limit - _a->avail); \ | |
205 PL_ArenaCountRetract(pool, _m); \ | |
206 } else { \ | |
207 PL_ArenaRelease(pool, _m); \ | |
208 } \ | |
209 PL_ArenaCountRelease(pool, _m); \ | |
210 PR_END_MACRO | |
211 | |
212 #ifdef PL_ARENAMETER | |
213 #define PL_COUNT_ARENA(pool,op) ((pool)->stats.narenas op) | |
214 #else | |
215 #define PL_COUNT_ARENA(pool,op) | |
216 #endif | |
217 | |
218 #define PL_ARENA_DESTROY(pool, a, pnext) \ | |
219 PR_BEGIN_MACRO \ | |
220 PL_COUNT_ARENA(pool,--); \ | |
221 if ((pool)->current == (a)) (pool)->current = &(pool)->first; \ | |
222 *(pnext) = (a)->next; \ | |
223 PL_CLEAR_ARENA(a); \ | |
224 free(a); \ | |
225 (a) = 0; \ | |
226 PR_END_MACRO | |
227 | |
228 #ifdef PL_ARENAMETER | |
229 | |
230 #include <stdio.h> | |
231 | |
232 PR_EXTERN(void) PL_ArenaCountAllocation(PLArenaPool *pool, PRUint32 nb); | |
233 | |
234 PR_EXTERN(void) PL_ArenaCountInplaceGrowth( | |
235 PLArenaPool *pool, PRUint32 size, PRUint32 incr); | |
236 | |
237 PR_EXTERN(void) PL_ArenaCountGrowth( | |
238 PLArenaPool *pool, PRUint32 size, PRUint32 incr); | |
239 | |
240 PR_EXTERN(void) PL_ArenaCountRelease(PLArenaPool *pool, char *mark); | |
241 | |
242 PR_EXTERN(void) PL_ArenaCountRetract(PLArenaPool *pool, char *mark); | |
243 | |
244 PR_EXTERN(void) PL_DumpArenaStats(FILE *fp); | |
245 | |
246 #else /* !PL_ARENAMETER */ | |
247 | |
248 #define PL_ArenaCountAllocation(ap, nb) /* nothing */ | |
249 #define PL_ArenaCountInplaceGrowth(ap, size, incr) /* nothing */ | |
250 #define PL_ArenaCountGrowth(ap, size, incr) /* nothing */ | |
251 #define PL_ArenaCountRelease(ap, mark) /* nothing */ | |
252 #define PL_ArenaCountRetract(ap, mark) /* nothing */ | |
253 | |
254 #endif /* !PL_ARENAMETER */ | |
255 | |
256 PR_END_EXTERN_C | |
257 | |
258 #endif /* plarena_h___ */ | |
OLD | NEW |