/src/nspr/lib/ds/plarena.c
Line | Count | Source (jump to first uncovered line) |
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 | | /* |
7 | | * Lifetime-based fast allocation, inspired by much prior art, including |
8 | | * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes" |
9 | | * David R. Hanson, Software -- Practice and Experience, Vol. 20(1). |
10 | | */ |
11 | | #include <stdlib.h> |
12 | | #include <string.h> |
13 | | #include "plarena.h" |
14 | | #include "prmem.h" |
15 | | #include "prbit.h" |
16 | | #include "prlog.h" |
17 | | #include "prlock.h" |
18 | | #include "prinit.h" |
19 | | |
20 | | #ifdef PL_ARENAMETER |
21 | | static PLArenaStats* arena_stats_list; |
22 | | |
23 | | # define COUNT(pool, what) (pool)->stats.what++ |
24 | | #else |
25 | | # define COUNT(pool, what) /* nothing */ |
26 | | #endif |
27 | | |
28 | 0 | #define PL_ARENA_DEFAULT_ALIGN sizeof(double) |
29 | | |
30 | | PR_IMPLEMENT(void) |
31 | | PL_InitArenaPool(PLArenaPool* pool, const char* name, PRUint32 size, |
32 | 2.94M | PRUint32 align) { |
33 | | /* |
34 | | * Look-up table of PR_BITMASK(PR_CeilingLog2(align)) values for |
35 | | * align = 1 to 32. |
36 | | */ |
37 | 2.94M | static const PRUint8 pmasks[33] = { |
38 | 2.94M | 0, /* not used */ |
39 | 2.94M | 0, 1, 3, 3, 7, 7, 7, 7, |
40 | 2.94M | 15, 15, 15, 15, 15, 15, 15, 15, /* 1 ... 16 */ |
41 | 2.94M | 31, 31, 31, 31, 31, 31, 31, 31, |
42 | 2.94M | 31, 31, 31, 31, 31, 31, 31, 31 /* 17 ... 32 */ |
43 | 2.94M | }; |
44 | | |
45 | 2.94M | if (align == 0) { |
46 | 0 | align = PL_ARENA_DEFAULT_ALIGN; |
47 | 0 | } |
48 | | |
49 | 2.94M | if (align < sizeof(pmasks) / sizeof(pmasks[0])) { |
50 | 2.94M | pool->mask = pmasks[align]; |
51 | 2.94M | } else { |
52 | 0 | pool->mask = PR_BITMASK(PR_CeilingLog2(align)); |
53 | 0 | } |
54 | | |
55 | 2.94M | pool->first.next = NULL; |
56 | | /* Set all three addresses in pool->first to the same dummy value. |
57 | | * These addresses are only compared with each other, but never |
58 | | * dereferenced. */ |
59 | 2.94M | pool->first.base = pool->first.avail = pool->first.limit = |
60 | 2.94M | (PRUword)PL_ARENA_ALIGN(pool, &pool->first + 1); |
61 | 2.94M | pool->current = &pool->first; |
62 | | /* |
63 | | * Compute the net size so that each arena's gross size is |size|. |
64 | | * sizeof(PLArena) + pool->mask is the header and alignment slop |
65 | | * that PL_ArenaAllocate adds to the net size. |
66 | | */ |
67 | 2.94M | if (size > sizeof(PLArena) + pool->mask) { |
68 | 2.94M | pool->arenasize = size - (sizeof(PLArena) + pool->mask); |
69 | 2.94M | } else { |
70 | 0 | pool->arenasize = size; |
71 | 0 | } |
72 | | #ifdef PL_ARENAMETER |
73 | | memset(&pool->stats, 0, sizeof pool->stats); |
74 | | pool->stats.name = strdup(name); |
75 | | pool->stats.next = arena_stats_list; |
76 | | arena_stats_list = &pool->stats; |
77 | | #endif |
78 | 2.94M | } |
79 | | |
80 | | /* |
81 | | ** PL_ArenaAllocate() -- allocate space from an arena pool |
82 | | ** |
83 | | ** Description: PL_ArenaAllocate() allocates space from an arena |
84 | | ** pool. |
85 | | ** |
86 | | ** First, try to satisfy the request from arenas starting at |
87 | | ** pool->current. Then try to allocate a new arena from the heap. |
88 | | ** |
89 | | ** Returns: pointer to allocated space or NULL |
90 | | ** |
91 | | ** Notes: The original implementation had some difficult to |
92 | | ** solve bugs; the code was difficult to read. Sometimes it's |
93 | | ** just easier to rewrite it. I did that. larryh. |
94 | | ** |
95 | | ** See also: bugzilla: 45343. |
96 | | ** |
97 | | */ |
98 | | |
99 | 4.04M | PR_IMPLEMENT(void*) PL_ArenaAllocate(PLArenaPool* pool, PRUint32 nb) { |
100 | 4.04M | PLArena* a; |
101 | 4.04M | char* rp; /* returned pointer */ |
102 | 4.04M | PRUint32 nbOld; |
103 | | |
104 | 4.04M | PR_ASSERT((nb & pool->mask) == 0); |
105 | | |
106 | 4.04M | nbOld = nb; |
107 | 4.04M | nb = (PRUword)PL_ARENA_ALIGN(pool, nb); /* force alignment */ |
108 | 4.04M | if (nb < nbOld) { |
109 | 0 | return NULL; |
110 | 0 | } |
111 | | |
112 | | /* attempt to allocate from arenas at pool->current */ |
113 | 4.04M | { |
114 | 4.04M | a = pool->current; |
115 | 4.04M | do { |
116 | 4.04M | if (nb <= a->limit - a->avail) { |
117 | 0 | pool->current = a; |
118 | 0 | rp = (char*)a->avail; |
119 | 0 | a->avail += nb; |
120 | 0 | return rp; |
121 | 0 | } |
122 | 4.04M | } while (NULL != (a = a->next)); |
123 | 4.04M | } |
124 | | |
125 | | /* attempt to allocate from the heap */ |
126 | 4.04M | { |
127 | 4.04M | PRUint32 sz = PR_MAX(pool->arenasize, nb); |
128 | 4.04M | if (PR_UINT32_MAX - sz < sizeof *a + pool->mask) { |
129 | 0 | a = NULL; |
130 | 4.04M | } else { |
131 | 4.04M | sz += sizeof *a + pool->mask; /* header and alignment slop */ |
132 | 4.04M | a = (PLArena*)PR_MALLOC(sz); |
133 | 4.04M | } |
134 | 4.04M | if (NULL != a) { |
135 | 4.04M | a->limit = (PRUword)a + sz; |
136 | 4.04M | a->base = a->avail = (PRUword)PL_ARENA_ALIGN(pool, a + 1); |
137 | 4.04M | PL_MAKE_MEM_NOACCESS((void*)a->avail, a->limit - a->avail); |
138 | 4.04M | rp = (char*)a->avail; |
139 | 4.04M | a->avail += nb; |
140 | 4.04M | PR_ASSERT(a->avail <= a->limit); |
141 | | /* the newly allocated arena is linked after pool->current |
142 | | * and becomes pool->current */ |
143 | 4.04M | a->next = pool->current->next; |
144 | 4.04M | pool->current->next = a; |
145 | 4.04M | pool->current = a; |
146 | 4.04M | if (NULL == pool->first.next) { |
147 | 0 | pool->first.next = a; |
148 | 0 | } |
149 | 4.04M | PL_COUNT_ARENA(pool, ++); |
150 | 4.04M | COUNT(pool, nmallocs); |
151 | 4.04M | return (rp); |
152 | 4.04M | } |
153 | 4.04M | } |
154 | | |
155 | | /* we got to here, and there's no memory to allocate */ |
156 | 0 | return (NULL); |
157 | 4.04M | } /* --- end PL_ArenaAllocate() --- */ |
158 | | |
159 | | PR_IMPLEMENT(void*) |
160 | 69.5k | PL_ArenaGrow(PLArenaPool* pool, void* p, PRUint32 size, PRUint32 incr) { |
161 | 69.5k | void* newp; |
162 | | |
163 | 69.5k | if (PR_UINT32_MAX - size < incr) { |
164 | 0 | return NULL; |
165 | 0 | } |
166 | 69.5k | PL_ARENA_ALLOCATE(newp, pool, size + incr); |
167 | 69.5k | if (newp) { |
168 | 69.5k | memcpy(newp, p, size); |
169 | 69.5k | } |
170 | 69.5k | return newp; |
171 | 69.5k | } |
172 | | |
173 | 657k | PR_IMPLEMENT(void) PL_ClearArenaPool(PLArenaPool* pool, PRInt32 pattern) { |
174 | 657k | PLArena* a; |
175 | | |
176 | 2.30M | for (a = pool->first.next; a; a = a->next) { |
177 | 1.64M | PR_ASSERT(a->base <= a->avail && a->avail <= a->limit); |
178 | 1.64M | a->avail = a->base; |
179 | 1.64M | PL_CLEAR_UNUSED_PATTERN(a, pattern); |
180 | 1.64M | PL_MAKE_MEM_NOACCESS((void*)a->avail, a->limit - a->avail); |
181 | 1.64M | } |
182 | 657k | } |
183 | | |
184 | | /* |
185 | | * Free tail arenas linked after head, which may not be the true list head. |
186 | | * Reset pool->current to point to head in case it pointed at a tail arena. |
187 | | */ |
188 | 3.20M | static void FreeArenaList(PLArenaPool* pool, PLArena* head) { |
189 | 3.20M | PLArena* a = head->next; |
190 | 3.20M | if (!a) { |
191 | 1.24M | return; |
192 | 1.24M | } |
193 | | |
194 | 1.96M | head->next = NULL; |
195 | | |
196 | 4.04M | do { |
197 | 4.04M | PLArena* tmp = a; |
198 | 4.04M | a = a->next; |
199 | 4.04M | PL_CLEAR_ARENA(tmp); |
200 | 4.04M | PL_COUNT_ARENA(pool, --); |
201 | 4.04M | PR_DELETE(tmp); |
202 | 4.04M | } while (a); |
203 | | |
204 | 1.96M | pool->current = head; |
205 | 1.96M | } |
206 | | |
207 | 260k | PR_IMPLEMENT(void) PL_ArenaRelease(PLArenaPool* pool, char* mark) { |
208 | 260k | PLArena* a; |
209 | | |
210 | 274M | for (a = &pool->first; a; a = a->next) { |
211 | 274M | if (PR_UPTRDIFF(mark, a->base) <= PR_UPTRDIFF(a->avail, a->base)) { |
212 | 260k | a->avail = (PRUword)PL_ARENA_ALIGN(pool, mark); |
213 | 260k | FreeArenaList(pool, a); |
214 | 260k | return; |
215 | 260k | } |
216 | 274M | } |
217 | 260k | } |
218 | | |
219 | 2.76M | PR_IMPLEMENT(void) PL_FreeArenaPool(PLArenaPool* pool) { |
220 | 2.76M | FreeArenaList(pool, &pool->first); |
221 | 2.76M | COUNT(pool, ndeallocs); |
222 | 2.76M | } |
223 | | |
224 | 181k | PR_IMPLEMENT(void) PL_FinishArenaPool(PLArenaPool* pool) { |
225 | 181k | FreeArenaList(pool, &pool->first); |
226 | | #ifdef PL_ARENAMETER |
227 | | { |
228 | | PLArenaStats *stats, **statsp; |
229 | | |
230 | | if (pool->stats.name) { |
231 | | PR_DELETE(pool->stats.name); |
232 | | } |
233 | | for (statsp = &arena_stats_list; (stats = *statsp) != 0; |
234 | | statsp = &stats->next) { |
235 | | if (stats == &pool->stats) { |
236 | | *statsp = stats->next; |
237 | | return; |
238 | | } |
239 | | } |
240 | | } |
241 | | #endif |
242 | 181k | } |
243 | | |
244 | 0 | PR_IMPLEMENT(void) PL_CompactArenaPool(PLArenaPool* ap) {} |
245 | | |
246 | 0 | PR_IMPLEMENT(void) PL_ArenaFinish(void) {} |
247 | | |
248 | | PR_IMPLEMENT(size_t) |
249 | | PL_SizeOfArenaPoolExcludingPool(const PLArenaPool* pool, |
250 | 0 | PLMallocSizeFn mallocSizeOf) { |
251 | | /* |
252 | | * The first PLArena is within |pool|, so don't measure it. Subsequent |
253 | | * PLArenas are separate and must be measured. |
254 | | */ |
255 | 0 | size_t size = 0; |
256 | 0 | const PLArena* arena = pool->first.next; |
257 | 0 | while (arena) { |
258 | 0 | size += mallocSizeOf(arena); |
259 | 0 | arena = arena->next; |
260 | 0 | } |
261 | 0 | return size; |
262 | 0 | } |
263 | | |
264 | | #ifdef PL_ARENAMETER |
265 | | PR_IMPLEMENT(void) PL_ArenaCountAllocation(PLArenaPool* pool, PRUint32 nb) { |
266 | | pool->stats.nallocs++; |
267 | | pool->stats.nbytes += nb; |
268 | | if (nb > pool->stats.maxalloc) { |
269 | | pool->stats.maxalloc = nb; |
270 | | } |
271 | | pool->stats.variance += nb * nb; |
272 | | } |
273 | | |
274 | | PR_IMPLEMENT(void) |
275 | | PL_ArenaCountInplaceGrowth(PLArenaPool* pool, PRUint32 size, PRUint32 incr) { |
276 | | pool->stats.ninplace++; |
277 | | } |
278 | | |
279 | | PR_IMPLEMENT(void) |
280 | | PL_ArenaCountGrowth(PLArenaPool* pool, PRUint32 size, PRUint32 incr) { |
281 | | pool->stats.ngrows++; |
282 | | pool->stats.nbytes += incr; |
283 | | pool->stats.variance -= size * size; |
284 | | size += incr; |
285 | | if (size > pool->stats.maxalloc) { |
286 | | pool->stats.maxalloc = size; |
287 | | } |
288 | | pool->stats.variance += size * size; |
289 | | } |
290 | | |
291 | | PR_IMPLEMENT(void) PL_ArenaCountRelease(PLArenaPool* pool, char* mark) { |
292 | | pool->stats.nreleases++; |
293 | | } |
294 | | |
295 | | PR_IMPLEMENT(void) PL_ArenaCountRetract(PLArenaPool* pool, char* mark) { |
296 | | pool->stats.nfastrels++; |
297 | | } |
298 | | |
299 | | # include <math.h> |
300 | | # include <stdio.h> |
301 | | |
302 | | PR_IMPLEMENT(void) PL_DumpArenaStats(FILE* fp) { |
303 | | PLArenaStats* stats; |
304 | | double mean, variance; |
305 | | |
306 | | for (stats = arena_stats_list; stats; stats = stats->next) { |
307 | | if (stats->nallocs != 0) { |
308 | | mean = (double)stats->nbytes / stats->nallocs; |
309 | | variance = fabs(stats->variance / stats->nallocs - mean * mean); |
310 | | } else { |
311 | | mean = variance = 0; |
312 | | } |
313 | | |
314 | | fprintf(fp, "\n%s allocation statistics:\n", stats->name); |
315 | | fprintf(fp, " number of arenas: %u\n", stats->narenas); |
316 | | fprintf(fp, " number of allocations: %u\n", stats->nallocs); |
317 | | fprintf(fp, " number of free arena reclaims: %u\n", stats->nreclaims); |
318 | | fprintf(fp, " number of malloc calls: %u\n", stats->nmallocs); |
319 | | fprintf(fp, " number of deallocations: %u\n", stats->ndeallocs); |
320 | | fprintf(fp, " number of allocation growths: %u\n", stats->ngrows); |
321 | | fprintf(fp, " number of in-place growths: %u\n", stats->ninplace); |
322 | | fprintf(fp, "number of released allocations: %u\n", stats->nreleases); |
323 | | fprintf(fp, " number of fast releases: %u\n", stats->nfastrels); |
324 | | fprintf(fp, " total bytes allocated: %u\n", stats->nbytes); |
325 | | fprintf(fp, " mean allocation size: %g\n", mean); |
326 | | fprintf(fp, " standard deviation: %g\n", sqrt(variance)); |
327 | | fprintf(fp, " maximum allocation size: %u\n", stats->maxalloc); |
328 | | } |
329 | | } |
330 | | #endif /* PL_ARENAMETER */ |