1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is Netscape
|
---|
18 | * Communications Corporation. Portions created by Netscape are
|
---|
19 | * Copyright (C) 1998-2000 Netscape Communications Corporation. All
|
---|
20 | * Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK *****
|
---|
37 | */
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * Lifetime-based fast allocation, inspired by much prior art, including
|
---|
41 | * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes"
|
---|
42 | * David R. Hanson, Software -- Practice and Experience, Vol. 20(1).
|
---|
43 | */
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include <string.h>
|
---|
46 | #include "plarena.h"
|
---|
47 | #include "prmem.h"
|
---|
48 | #include "prbit.h"
|
---|
49 | #include "prlog.h"
|
---|
50 | #include "prlock.h"
|
---|
51 | #include "prinit.h"
|
---|
52 |
|
---|
53 | static PLArena *arena_freelist;
|
---|
54 |
|
---|
55 | #ifdef PL_ARENAMETER
|
---|
56 | static PLArenaStats *arena_stats_list;
|
---|
57 |
|
---|
58 | #define COUNT(pool,what) (pool)->stats.what++
|
---|
59 | #else
|
---|
60 | #define COUNT(pool,what) /* nothing */
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | #define PL_ARENA_DEFAULT_ALIGN sizeof(double)
|
---|
64 |
|
---|
65 | static PRLock *arenaLock;
|
---|
66 | static PRCallOnceType once;
|
---|
67 |
|
---|
68 | /*
|
---|
69 | ** InitializeArenas() -- Initialize arena operations.
|
---|
70 | **
|
---|
71 | ** InitializeArenas() is called exactly once and only once from
|
---|
72 | ** LockArena(). This function creates the arena protection
|
---|
73 | ** lock: arenaLock.
|
---|
74 | **
|
---|
75 | ** Note: If the arenaLock cannot be created, InitializeArenas()
|
---|
76 | ** fails quietly, returning only PR_FAILURE. This percolates up
|
---|
77 | ** to the application using the Arena API. He gets no arena
|
---|
78 | ** from PL_ArenaAllocate(). It's up to him to fail gracefully
|
---|
79 | ** or recover.
|
---|
80 | **
|
---|
81 | */
|
---|
82 | static PRStatus InitializeArenas( void )
|
---|
83 | {
|
---|
84 | PR_ASSERT( arenaLock == NULL );
|
---|
85 | arenaLock = PR_NewLock();
|
---|
86 | if ( arenaLock == NULL )
|
---|
87 | return PR_FAILURE;
|
---|
88 | else
|
---|
89 | return PR_SUCCESS;
|
---|
90 | } /* end ArenaInitialize() */
|
---|
91 |
|
---|
92 | static PRStatus LockArena( void )
|
---|
93 | {
|
---|
94 | PRStatus rc = PR_CallOnce( &once, InitializeArenas );
|
---|
95 |
|
---|
96 | if ( PR_FAILURE != rc )
|
---|
97 | PR_Lock( arenaLock );
|
---|
98 | return(rc);
|
---|
99 | } /* end LockArena() */
|
---|
100 |
|
---|
101 | static void UnlockArena( void )
|
---|
102 | {
|
---|
103 | PR_Unlock( arenaLock );
|
---|
104 | return;
|
---|
105 | } /* end UnlockArena() */
|
---|
106 |
|
---|
107 | PR_IMPLEMENT(void) PL_InitArenaPool(
|
---|
108 | PLArenaPool *pool, const char *name, PRUint32 size, PRUint32 align)
|
---|
109 | {
|
---|
110 | #if defined(XP_MAC)
|
---|
111 | #pragma unused (name)
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | if (align == 0)
|
---|
115 | align = PL_ARENA_DEFAULT_ALIGN;
|
---|
116 | pool->mask = PR_BITMASK(PR_CeilingLog2(align));
|
---|
117 | pool->first.next = NULL;
|
---|
118 | /* Set all three addresses in pool->first to the same dummy value.
|
---|
119 | * These addresses are only compared with each other, but never
|
---|
120 | * dereferenced. */
|
---|
121 | pool->first.base = pool->first.avail = pool->first.limit =
|
---|
122 | (PRUword)PL_ARENA_ALIGN(pool, &pool->first + 1);
|
---|
123 | pool->current = &pool->first;
|
---|
124 | pool->arenasize = size;
|
---|
125 | #ifdef PL_ARENAMETER
|
---|
126 | memset(&pool->stats, 0, sizeof pool->stats);
|
---|
127 | pool->stats.name = strdup(name);
|
---|
128 | pool->stats.next = arena_stats_list;
|
---|
129 | arena_stats_list = &pool->stats;
|
---|
130 | #endif
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | /*
|
---|
135 | ** PL_ArenaAllocate() -- allocate space from an arena pool
|
---|
136 | **
|
---|
137 | ** Description: PL_ArenaAllocate() allocates space from an arena
|
---|
138 | ** pool.
|
---|
139 | **
|
---|
140 | ** First, try to satisfy the request from arenas starting at
|
---|
141 | ** pool->current.
|
---|
142 | **
|
---|
143 | ** If there is not enough space in the arena pool->current, try
|
---|
144 | ** to claim an arena, on a first fit basis, from the global
|
---|
145 | ** freelist (arena_freelist).
|
---|
146 | **
|
---|
147 | ** If no arena in arena_freelist is suitable, then try to
|
---|
148 | ** allocate a new arena from the heap.
|
---|
149 | **
|
---|
150 | ** Returns: pointer to allocated space or NULL
|
---|
151 | **
|
---|
152 | ** Notes: The original implementation had some difficult to
|
---|
153 | ** solve bugs; the code was difficult to read. Sometimes it's
|
---|
154 | ** just easier to rewrite it. I did that. larryh.
|
---|
155 | **
|
---|
156 | ** See also: bugzilla: 45343.
|
---|
157 | **
|
---|
158 | */
|
---|
159 |
|
---|
160 | PR_IMPLEMENT(void *) PL_ArenaAllocate(PLArenaPool *pool, PRUint32 nb)
|
---|
161 | {
|
---|
162 | PLArena *a;
|
---|
163 | char *rp; /* returned pointer */
|
---|
164 | PRUint32 nbOld;
|
---|
165 |
|
---|
166 | PR_ASSERT((nb & pool->mask) == 0);
|
---|
167 |
|
---|
168 | nbOld = nb;
|
---|
169 | nb = (PRUword)PL_ARENA_ALIGN(pool, nb); /* force alignment */
|
---|
170 | if (nb < nbOld)
|
---|
171 | return NULL;
|
---|
172 |
|
---|
173 | /* attempt to allocate from arenas at pool->current */
|
---|
174 | {
|
---|
175 | a = pool->current;
|
---|
176 | do {
|
---|
177 | if ( a->avail +nb <= a->limit ) {
|
---|
178 | pool->current = a;
|
---|
179 | rp = (char *)a->avail;
|
---|
180 | a->avail += nb;
|
---|
181 | return rp;
|
---|
182 | }
|
---|
183 | } while( NULL != (a = a->next) );
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* attempt to allocate from arena_freelist */
|
---|
187 | {
|
---|
188 | PLArena *p; /* previous pointer, for unlinking from freelist */
|
---|
189 |
|
---|
190 | /* lock the arena_freelist. Make access to the freelist MT-Safe */
|
---|
191 | if ( PR_FAILURE == LockArena())
|
---|
192 | return(0);
|
---|
193 |
|
---|
194 | for ( a = p = arena_freelist; a != NULL ; p = a, a = a->next ) {
|
---|
195 | if ( a->base +nb <= a->limit ) {
|
---|
196 | if ( p == arena_freelist )
|
---|
197 | arena_freelist = a->next;
|
---|
198 | else
|
---|
199 | p->next = a->next;
|
---|
200 | UnlockArena();
|
---|
201 | a->avail = a->base;
|
---|
202 | rp = (char *)a->avail;
|
---|
203 | a->avail += nb;
|
---|
204 | /* the newly allocated arena is linked after pool->current
|
---|
205 | * and becomes pool->current */
|
---|
206 | a->next = pool->current->next;
|
---|
207 | pool->current->next = a;
|
---|
208 | pool->current = a;
|
---|
209 | if ( NULL == pool->first.next )
|
---|
210 | pool->first.next = a;
|
---|
211 | return(rp);
|
---|
212 | }
|
---|
213 | }
|
---|
214 | UnlockArena();
|
---|
215 | }
|
---|
216 |
|
---|
217 | /* attempt to allocate from the heap */
|
---|
218 | {
|
---|
219 | PRUint32 sz = PR_MAX(pool->arenasize, nb);
|
---|
220 | sz += sizeof *a + pool->mask; /* header and alignment slop */
|
---|
221 | a = (PLArena*)PR_MALLOC(sz);
|
---|
222 | if ( NULL != a ) {
|
---|
223 | a->limit = (PRUword)a + sz;
|
---|
224 | a->base = a->avail = (PRUword)PL_ARENA_ALIGN(pool, a + 1);
|
---|
225 | rp = (char *)a->avail;
|
---|
226 | a->avail += nb;
|
---|
227 | PR_ASSERT(a->avail <= a->limit);
|
---|
228 | /* the newly allocated arena is linked after pool->current
|
---|
229 | * and becomes pool->current */
|
---|
230 | a->next = pool->current->next;
|
---|
231 | pool->current->next = a;
|
---|
232 | pool->current = a;
|
---|
233 | if ( NULL == pool->first.next )
|
---|
234 | pool->first.next = a;
|
---|
235 | PL_COUNT_ARENA(pool,++);
|
---|
236 | COUNT(pool, nmallocs);
|
---|
237 | return(rp);
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | /* we got to here, and there's no memory to allocate */
|
---|
242 | return(NULL);
|
---|
243 | } /* --- end PL_ArenaAllocate() --- */
|
---|
244 |
|
---|
245 | PR_IMPLEMENT(void *) PL_ArenaGrow(
|
---|
246 | PLArenaPool *pool, void *p, PRUint32 size, PRUint32 incr)
|
---|
247 | {
|
---|
248 | void *newp;
|
---|
249 |
|
---|
250 | if (PR_UINT32_MAX - size < incr)
|
---|
251 | return NULL;
|
---|
252 | PL_ARENA_ALLOCATE(newp, pool, size + incr);
|
---|
253 | if (newp)
|
---|
254 | memcpy(newp, p, size);
|
---|
255 | return newp;
|
---|
256 | }
|
---|
257 |
|
---|
258 | /*
|
---|
259 | * Free tail arenas linked after head, which may not be the true list head.
|
---|
260 | * Reset pool->current to point to head in case it pointed at a tail arena.
|
---|
261 | */
|
---|
262 | static void FreeArenaList(PLArenaPool *pool, PLArena *head, PRBool reallyFree)
|
---|
263 | {
|
---|
264 | PLArena **ap, *a;
|
---|
265 |
|
---|
266 | ap = &head->next;
|
---|
267 | a = *ap;
|
---|
268 | if (!a)
|
---|
269 | return;
|
---|
270 |
|
---|
271 | #ifdef DEBUG
|
---|
272 | do {
|
---|
273 | PR_ASSERT(a->base <= a->avail && a->avail <= a->limit);
|
---|
274 | a->avail = a->base;
|
---|
275 | PL_CLEAR_UNUSED(a);
|
---|
276 | } while ((a = a->next) != 0);
|
---|
277 | a = *ap;
|
---|
278 | #endif
|
---|
279 |
|
---|
280 | if (reallyFree) {
|
---|
281 | do {
|
---|
282 | *ap = a->next;
|
---|
283 | PL_CLEAR_ARENA(a);
|
---|
284 | PL_COUNT_ARENA(pool,--);
|
---|
285 | PR_DELETE(a);
|
---|
286 | } while ((a = *ap) != 0);
|
---|
287 | } else {
|
---|
288 | /* Insert the whole arena chain at the front of the freelist. */
|
---|
289 | do {
|
---|
290 | ap = &(*ap)->next;
|
---|
291 | } while (*ap);
|
---|
292 | LockArena();
|
---|
293 | *ap = arena_freelist;
|
---|
294 | arena_freelist = a;
|
---|
295 | head->next = 0;
|
---|
296 | UnlockArena();
|
---|
297 | }
|
---|
298 |
|
---|
299 | pool->current = head;
|
---|
300 | }
|
---|
301 |
|
---|
302 | PR_IMPLEMENT(void) PL_ArenaRelease(PLArenaPool *pool, char *mark)
|
---|
303 | {
|
---|
304 | PLArena *a;
|
---|
305 |
|
---|
306 | for (a = pool->first.next; a; a = a->next) {
|
---|
307 | if (PR_UPTRDIFF(mark, a->base) < PR_UPTRDIFF(a->avail, a->base)) {
|
---|
308 | a->avail = (PRUword)PL_ARENA_ALIGN(pool, mark);
|
---|
309 | FreeArenaList(pool, a, PR_FALSE);
|
---|
310 | return;
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | PR_IMPLEMENT(void) PL_FreeArenaPool(PLArenaPool *pool)
|
---|
316 | {
|
---|
317 | FreeArenaList(pool, &pool->first, PR_FALSE);
|
---|
318 | COUNT(pool, ndeallocs);
|
---|
319 | }
|
---|
320 |
|
---|
321 | PR_IMPLEMENT(void) PL_FinishArenaPool(PLArenaPool *pool)
|
---|
322 | {
|
---|
323 | FreeArenaList(pool, &pool->first, PR_TRUE);
|
---|
324 | #ifdef PL_ARENAMETER
|
---|
325 | {
|
---|
326 | PLArenaStats *stats, **statsp;
|
---|
327 |
|
---|
328 | if (pool->stats.name)
|
---|
329 | PR_DELETE(pool->stats.name);
|
---|
330 | for (statsp = &arena_stats_list; (stats = *statsp) != 0;
|
---|
331 | statsp = &stats->next) {
|
---|
332 | if (stats == &pool->stats) {
|
---|
333 | *statsp = stats->next;
|
---|
334 | return;
|
---|
335 | }
|
---|
336 | }
|
---|
337 | }
|
---|
338 | #endif
|
---|
339 | }
|
---|
340 |
|
---|
341 | PR_IMPLEMENT(void) PL_CompactArenaPool(PLArenaPool *ap)
|
---|
342 | {
|
---|
343 | #if XP_MAC
|
---|
344 | #pragma unused (ap)
|
---|
345 | #if 0
|
---|
346 | PRArena *curr = &(ap->first);
|
---|
347 | while (curr) {
|
---|
348 | reallocSmaller(curr, curr->avail - (uprword_t)curr);
|
---|
349 | curr->limit = curr->avail;
|
---|
350 | curr = curr->next;
|
---|
351 | }
|
---|
352 | #endif
|
---|
353 | #endif
|
---|
354 | }
|
---|
355 |
|
---|
356 | PR_IMPLEMENT(void) PL_ArenaFinish(void)
|
---|
357 | {
|
---|
358 | PLArena *a, *next;
|
---|
359 |
|
---|
360 | for (a = arena_freelist; a; a = next) {
|
---|
361 | next = a->next;
|
---|
362 | PR_DELETE(a);
|
---|
363 | }
|
---|
364 | arena_freelist = NULL;
|
---|
365 |
|
---|
366 | if (arenaLock) {
|
---|
367 | PR_DestroyLock(arenaLock);
|
---|
368 | arenaLock = NULL;
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | #ifdef PL_ARENAMETER
|
---|
373 | PR_IMPLEMENT(void) PL_ArenaCountAllocation(PLArenaPool *pool, PRUint32 nb)
|
---|
374 | {
|
---|
375 | pool->stats.nallocs++;
|
---|
376 | pool->stats.nbytes += nb;
|
---|
377 | if (nb > pool->stats.maxalloc)
|
---|
378 | pool->stats.maxalloc = nb;
|
---|
379 | pool->stats.variance += nb * nb;
|
---|
380 | }
|
---|
381 |
|
---|
382 | PR_IMPLEMENT(void) PL_ArenaCountInplaceGrowth(
|
---|
383 | PLArenaPool *pool, PRUint32 size, PRUint32 incr)
|
---|
384 | {
|
---|
385 | pool->stats.ninplace++;
|
---|
386 | }
|
---|
387 |
|
---|
388 | PR_IMPLEMENT(void) PL_ArenaCountGrowth(
|
---|
389 | PLArenaPool *pool, PRUint32 size, PRUint32 incr)
|
---|
390 | {
|
---|
391 | pool->stats.ngrows++;
|
---|
392 | pool->stats.nbytes += incr;
|
---|
393 | pool->stats.variance -= size * size;
|
---|
394 | size += incr;
|
---|
395 | if (size > pool->stats.maxalloc)
|
---|
396 | pool->stats.maxalloc = size;
|
---|
397 | pool->stats.variance += size * size;
|
---|
398 | }
|
---|
399 |
|
---|
400 | PR_IMPLEMENT(void) PL_ArenaCountRelease(PLArenaPool *pool, char *mark)
|
---|
401 | {
|
---|
402 | pool->stats.nreleases++;
|
---|
403 | }
|
---|
404 |
|
---|
405 | PR_IMPLEMENT(void) PL_ArenaCountRetract(PLArenaPool *pool, char *mark)
|
---|
406 | {
|
---|
407 | pool->stats.nfastrels++;
|
---|
408 | }
|
---|
409 |
|
---|
410 | #include <math.h>
|
---|
411 | #include <stdio.h>
|
---|
412 |
|
---|
413 | PR_IMPLEMENT(void) PL_DumpArenaStats(FILE *fp)
|
---|
414 | {
|
---|
415 | PLArenaStats *stats;
|
---|
416 | double mean, variance;
|
---|
417 |
|
---|
418 | for (stats = arena_stats_list; stats; stats = stats->next) {
|
---|
419 | if (stats->nallocs != 0) {
|
---|
420 | mean = (double)stats->nbytes / stats->nallocs;
|
---|
421 | variance = fabs(stats->variance / stats->nallocs - mean * mean);
|
---|
422 | } else {
|
---|
423 | mean = variance = 0;
|
---|
424 | }
|
---|
425 |
|
---|
426 | fprintf(fp, "\n%s allocation statistics:\n", stats->name);
|
---|
427 | fprintf(fp, " number of arenas: %u\n", stats->narenas);
|
---|
428 | fprintf(fp, " number of allocations: %u\n", stats->nallocs);
|
---|
429 | fprintf(fp, " number of free arena reclaims: %u\n", stats->nreclaims);
|
---|
430 | fprintf(fp, " number of malloc calls: %u\n", stats->nmallocs);
|
---|
431 | fprintf(fp, " number of deallocations: %u\n", stats->ndeallocs);
|
---|
432 | fprintf(fp, " number of allocation growths: %u\n", stats->ngrows);
|
---|
433 | fprintf(fp, " number of in-place growths: %u\n", stats->ninplace);
|
---|
434 | fprintf(fp, "number of released allocations: %u\n", stats->nreleases);
|
---|
435 | fprintf(fp, " number of fast releases: %u\n", stats->nfastrels);
|
---|
436 | fprintf(fp, " total bytes allocated: %u\n", stats->nbytes);
|
---|
437 | fprintf(fp, " mean allocation size: %g\n", mean);
|
---|
438 | fprintf(fp, " standard deviation: %g\n", sqrt(variance));
|
---|
439 | fprintf(fp, " maximum allocation size: %u\n", stats->maxalloc);
|
---|
440 | }
|
---|
441 | }
|
---|
442 | #endif /* PL_ARENAMETER */
|
---|