VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/lib/ds/plarena.c@ 58639

最後變更 在這個檔案從58639是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 12.6 KB
 
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
53static PLArena *arena_freelist;
54
55#ifdef PL_ARENAMETER
56static 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
65static PRLock *arenaLock;
66static 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*/
82static 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
92static 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
101static void UnlockArena( void )
102{
103 PR_Unlock( arenaLock );
104 return;
105} /* end UnlockArena() */
106
107PR_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 pool->first.base = pool->first.avail = pool->first.limit =
119 (PRUword)PL_ARENA_ALIGN(pool, &pool->first + 1);
120 pool->current = &pool->first;
121 pool->arenasize = size;
122#ifdef PL_ARENAMETER
123 memset(&pool->stats, 0, sizeof pool->stats);
124 pool->stats.name = strdup(name);
125 pool->stats.next = arena_stats_list;
126 arena_stats_list = &pool->stats;
127#endif
128}
129
130
131/*
132** PL_ArenaAllocate() -- allocate space from an arena pool
133**
134** Description: PL_ArenaAllocate() allocates space from an arena
135** pool.
136**
137** First, try to satisfy the request from arenas starting at
138** pool->current.
139**
140** If there is not enough space in the arena pool->current, try
141** to claim an arena, on a first fit basis, from the global
142** freelist (arena_freelist).
143**
144** If no arena in arena_freelist is suitable, then try to
145** allocate a new arena from the heap.
146**
147** Returns: pointer to allocated space or NULL
148**
149** Notes: The original implementation had some difficult to
150** solve bugs; the code was difficult to read. Sometimes it's
151** just easier to rewrite it. I did that. larryh.
152**
153** See also: bugzilla: 45343.
154**
155*/
156
157PR_IMPLEMENT(void *) PL_ArenaAllocate(PLArenaPool *pool, PRUint32 nb)
158{
159 PLArena *a;
160 char *rp; /* returned pointer */
161
162 PR_ASSERT((nb & pool->mask) == 0);
163
164 nb = (PRUword)PL_ARENA_ALIGN(pool, nb); /* force alignment */
165
166 /* attempt to allocate from arenas at pool->current */
167 {
168 a = pool->current;
169 do {
170 if ( a->avail +nb <= a->limit ) {
171 pool->current = a;
172 rp = (char *)a->avail;
173 a->avail += nb;
174 return rp;
175 }
176 } while( NULL != (a = a->next) );
177 }
178
179 /* attempt to allocate from arena_freelist */
180 {
181 PLArena *p; /* previous pointer, for unlinking from freelist */
182
183 /* lock the arena_freelist. Make access to the freelist MT-Safe */
184 if ( PR_FAILURE == LockArena())
185 return(0);
186
187 for ( a = p = arena_freelist; a != NULL ; p = a, a = a->next ) {
188 if ( a->base +nb <= a->limit ) {
189 if ( p == arena_freelist )
190 arena_freelist = a->next;
191 else
192 p->next = a->next;
193 UnlockArena();
194 a->avail = a->base;
195 rp = (char *)a->avail;
196 a->avail += nb;
197 /* the newly allocated arena is linked after pool->current
198 * and becomes pool->current */
199 a->next = pool->current->next;
200 pool->current->next = a;
201 pool->current = a;
202 if ( NULL == pool->first.next )
203 pool->first.next = a;
204 return(rp);
205 }
206 }
207 UnlockArena();
208 }
209
210 /* attempt to allocate from the heap */
211 {
212 PRUint32 sz = PR_MAX(pool->arenasize, nb);
213 sz += sizeof *a + pool->mask; /* header and alignment slop */
214 a = (PLArena*)PR_MALLOC(sz);
215 if ( NULL != a ) {
216 a->limit = (PRUword)a + sz;
217 a->base = a->avail = (PRUword)PL_ARENA_ALIGN(pool, a + 1);
218 rp = (char *)a->avail;
219 a->avail += nb;
220 /* the newly allocated arena is linked after pool->current
221 * and becomes pool->current */
222 a->next = pool->current->next;
223 pool->current->next = a;
224 pool->current = a;
225 if ( NULL == pool->first.next )
226 pool->first.next = a;
227 PL_COUNT_ARENA(pool,++);
228 COUNT(pool, nmallocs);
229 return(rp);
230 }
231 }
232
233 /* we got to here, and there's no memory to allocate */
234 return(NULL);
235} /* --- end PL_ArenaAllocate() --- */
236
237PR_IMPLEMENT(void *) PL_ArenaGrow(
238 PLArenaPool *pool, void *p, PRUint32 size, PRUint32 incr)
239{
240 void *newp;
241
242 PL_ARENA_ALLOCATE(newp, pool, size + incr);
243 if (newp)
244 memcpy(newp, p, size);
245 return newp;
246}
247
248/*
249 * Free tail arenas linked after head, which may not be the true list head.
250 * Reset pool->current to point to head in case it pointed at a tail arena.
251 */
252static void FreeArenaList(PLArenaPool *pool, PLArena *head, PRBool reallyFree)
253{
254 PLArena **ap, *a;
255
256 ap = &head->next;
257 a = *ap;
258 if (!a)
259 return;
260
261#ifdef DEBUG
262 do {
263 PR_ASSERT(a->base <= a->avail && a->avail <= a->limit);
264 a->avail = a->base;
265 PL_CLEAR_UNUSED(a);
266 } while ((a = a->next) != 0);
267 a = *ap;
268#endif
269
270 if (reallyFree) {
271 do {
272 *ap = a->next;
273 PL_CLEAR_ARENA(a);
274 PL_COUNT_ARENA(pool,--);
275 PR_DELETE(a);
276 } while ((a = *ap) != 0);
277 } else {
278 /* Insert the whole arena chain at the front of the freelist. */
279 do {
280 ap = &(*ap)->next;
281 } while (*ap);
282 LockArena();
283 *ap = arena_freelist;
284 arena_freelist = a;
285 head->next = 0;
286 UnlockArena();
287 }
288
289 pool->current = head;
290}
291
292PR_IMPLEMENT(void) PL_ArenaRelease(PLArenaPool *pool, char *mark)
293{
294 PLArena *a;
295
296 for (a = pool->first.next; a; a = a->next) {
297 if (PR_UPTRDIFF(mark, a->base) < PR_UPTRDIFF(a->avail, a->base)) {
298 a->avail = (PRUword)PL_ARENA_ALIGN(pool, mark);
299 FreeArenaList(pool, a, PR_FALSE);
300 return;
301 }
302 }
303}
304
305PR_IMPLEMENT(void) PL_FreeArenaPool(PLArenaPool *pool)
306{
307 FreeArenaList(pool, &pool->first, PR_FALSE);
308 COUNT(pool, ndeallocs);
309}
310
311PR_IMPLEMENT(void) PL_FinishArenaPool(PLArenaPool *pool)
312{
313 FreeArenaList(pool, &pool->first, PR_TRUE);
314#ifdef PL_ARENAMETER
315 {
316 PLArenaStats *stats, **statsp;
317
318 if (pool->stats.name)
319 PR_DELETE(pool->stats.name);
320 for (statsp = &arena_stats_list; (stats = *statsp) != 0;
321 statsp = &stats->next) {
322 if (stats == &pool->stats) {
323 *statsp = stats->next;
324 return;
325 }
326 }
327 }
328#endif
329}
330
331PR_IMPLEMENT(void) PL_CompactArenaPool(PLArenaPool *ap)
332{
333#if XP_MAC
334#pragma unused (ap)
335#if 0
336 PRArena *curr = &(ap->first);
337 while (curr) {
338 reallocSmaller(curr, curr->avail - (uprword_t)curr);
339 curr->limit = curr->avail;
340 curr = curr->next;
341 }
342#endif
343#endif
344}
345
346PR_IMPLEMENT(void) PL_ArenaFinish(void)
347{
348 PLArena *a, *next;
349
350 for (a = arena_freelist; a; a = next) {
351 next = a->next;
352 PR_DELETE(a);
353 }
354 arena_freelist = NULL;
355
356 if (arenaLock) {
357 PR_DestroyLock(arenaLock);
358 arenaLock = NULL;
359 }
360}
361
362#ifdef PL_ARENAMETER
363PR_IMPLEMENT(void) PL_ArenaCountAllocation(PLArenaPool *pool, PRUint32 nb)
364{
365 pool->stats.nallocs++;
366 pool->stats.nbytes += nb;
367 if (nb > pool->stats.maxalloc)
368 pool->stats.maxalloc = nb;
369 pool->stats.variance += nb * nb;
370}
371
372PR_IMPLEMENT(void) PL_ArenaCountInplaceGrowth(
373 PLArenaPool *pool, PRUint32 size, PRUint32 incr)
374{
375 pool->stats.ninplace++;
376}
377
378PR_IMPLEMENT(void) PL_ArenaCountGrowth(
379 PLArenaPool *pool, PRUint32 size, PRUint32 incr)
380{
381 pool->stats.ngrows++;
382 pool->stats.nbytes += incr;
383 pool->stats.variance -= size * size;
384 size += incr;
385 if (size > pool->stats.maxalloc)
386 pool->stats.maxalloc = size;
387 pool->stats.variance += size * size;
388}
389
390PR_IMPLEMENT(void) PL_ArenaCountRelease(PLArenaPool *pool, char *mark)
391{
392 pool->stats.nreleases++;
393}
394
395PR_IMPLEMENT(void) PL_ArenaCountRetract(PLArenaPool *pool, char *mark)
396{
397 pool->stats.nfastrels++;
398}
399
400#include <math.h>
401#include <stdio.h>
402
403PR_IMPLEMENT(void) PL_DumpArenaStats(FILE *fp)
404{
405 PLArenaStats *stats;
406 double mean, variance;
407
408 for (stats = arena_stats_list; stats; stats = stats->next) {
409 if (stats->nallocs != 0) {
410 mean = (double)stats->nbytes / stats->nallocs;
411 variance = fabs(stats->variance / stats->nallocs - mean * mean);
412 } else {
413 mean = variance = 0;
414 }
415
416 fprintf(fp, "\n%s allocation statistics:\n", stats->name);
417 fprintf(fp, " number of arenas: %u\n", stats->narenas);
418 fprintf(fp, " number of allocations: %u\n", stats->nallocs);
419 fprintf(fp, " number of free arena reclaims: %u\n", stats->nreclaims);
420 fprintf(fp, " number of malloc calls: %u\n", stats->nmallocs);
421 fprintf(fp, " number of deallocations: %u\n", stats->ndeallocs);
422 fprintf(fp, " number of allocation growths: %u\n", stats->ngrows);
423 fprintf(fp, " number of in-place growths: %u\n", stats->ninplace);
424 fprintf(fp, "number of released allocations: %u\n", stats->nreleases);
425 fprintf(fp, " number of fast releases: %u\n", stats->nfastrels);
426 fprintf(fp, " total bytes allocated: %u\n", stats->nbytes);
427 fprintf(fp, " mean allocation size: %g\n", mean);
428 fprintf(fp, " standard deviation: %g\n", sqrt(variance));
429 fprintf(fp, " maximum allocation size: %u\n", stats->maxalloc);
430 }
431}
432#endif /* PL_ARENAMETER */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette