diff options
Diffstat (limited to 'nsprpub/lib/ds/plarena.c')
-rw-r--r-- | nsprpub/lib/ds/plarena.c | 64 |
1 files changed, 39 insertions, 25 deletions
diff --git a/nsprpub/lib/ds/plarena.c b/nsprpub/lib/ds/plarena.c index a582ac6387..3c6df2b7f7 100644 --- a/nsprpub/lib/ds/plarena.c +++ b/nsprpub/lib/ds/plarena.c @@ -35,34 +35,40 @@ PR_IMPLEMENT(void) PL_InitArenaPool( * align = 1 to 32. */ static const PRUint8 pmasks[33] = { - 0, /* not used */ - 0, 1, 3, 3, 7, 7, 7, 7,15,15,15,15,15,15,15,15, /* 1 ... 16 */ - 31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31}; /* 17 ... 32 */ + 0, /* not used */ + 0, 1, 3, 3, 7, 7, 7, 7,15,15,15,15,15,15,15,15, /* 1 ... 16 */ + 31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31 /* 17 ... 32 */ + }; - if (align == 0) + if (align == 0) { align = PL_ARENA_DEFAULT_ALIGN; + } - if (align < sizeof(pmasks)/sizeof(pmasks[0])) + if (align < sizeof(pmasks)/sizeof(pmasks[0])) { pool->mask = pmasks[align]; - else + } + else { pool->mask = PR_BITMASK(PR_CeilingLog2(align)); + } pool->first.next = NULL; /* Set all three addresses in pool->first to the same dummy value. * These addresses are only compared with each other, but never * dereferenced. */ pool->first.base = pool->first.avail = pool->first.limit = - (PRUword)PL_ARENA_ALIGN(pool, &pool->first + 1); + (PRUword)PL_ARENA_ALIGN(pool, &pool->first + 1); pool->current = &pool->first; /* * Compute the net size so that each arena's gross size is |size|. * sizeof(PLArena) + pool->mask is the header and alignment slop * that PL_ArenaAllocate adds to the net size. */ - if (size > sizeof(PLArena) + pool->mask) + if (size > sizeof(PLArena) + pool->mask) { pool->arenasize = size - (sizeof(PLArena) + pool->mask); - else + } + else { pool->arenasize = size; + } #ifdef PL_ARENAMETER memset(&pool->stats, 0, sizeof pool->stats); pool->stats.name = strdup(name); @@ -74,15 +80,15 @@ PR_IMPLEMENT(void) PL_InitArenaPool( /* ** PL_ArenaAllocate() -- allocate space from an arena pool -** +** ** Description: PL_ArenaAllocate() allocates space from an arena -** pool. +** pool. ** ** First, try to satisfy the request from arenas starting at ** pool->current. Then try to allocate a new arena from the heap. ** ** Returns: pointer to allocated space or NULL -** +** ** Notes: The original implementation had some difficult to ** solve bugs; the code was difficult to read. Sometimes it's ** just easier to rewrite it. I did that. larryh. @@ -93,16 +99,17 @@ PR_IMPLEMENT(void) PL_InitArenaPool( PR_IMPLEMENT(void *) PL_ArenaAllocate(PLArenaPool *pool, PRUint32 nb) { - PLArena *a; + PLArena *a; char *rp; /* returned pointer */ PRUint32 nbOld; PR_ASSERT((nb & pool->mask) == 0); - + nbOld = nb; nb = (PRUword)PL_ARENA_ALIGN(pool, nb); /* force alignment */ - if (nb < nbOld) + if (nb < nbOld) { return NULL; + } /* attempt to allocate from arenas at pool->current */ { @@ -117,8 +124,8 @@ PR_IMPLEMENT(void *) PL_ArenaAllocate(PLArenaPool *pool, PRUint32 nb) } while( NULL != (a = a->next) ); } - /* attempt to allocate from the heap */ - { + /* attempt to allocate from the heap */ + { PRUint32 sz = PR_MAX(pool->arenasize, nb); if (PR_UINT32_MAX - sz < sizeof *a + pool->mask) { a = NULL; @@ -133,13 +140,14 @@ PR_IMPLEMENT(void *) PL_ArenaAllocate(PLArenaPool *pool, PRUint32 nb) rp = (char *)a->avail; a->avail += nb; PR_ASSERT(a->avail <= a->limit); - /* the newly allocated arena is linked after pool->current + /* the newly allocated arena is linked after pool->current * and becomes pool->current */ a->next = pool->current->next; pool->current->next = a; pool->current = a; - if ( NULL == pool->first.next ) + if ( NULL == pool->first.next ) { pool->first.next = a; + } PL_COUNT_ARENA(pool,++); COUNT(pool, nmallocs); return(rp); @@ -155,11 +163,13 @@ PR_IMPLEMENT(void *) PL_ArenaGrow( { void *newp; - if (PR_UINT32_MAX - size < incr) + if (PR_UINT32_MAX - size < incr) { return NULL; + } PL_ARENA_ALLOCATE(newp, pool, size + incr); - if (newp) + if (newp) { memcpy(newp, p, size); + } return newp; } @@ -182,8 +192,9 @@ PR_IMPLEMENT(void) PL_ClearArenaPool(PLArenaPool *pool, PRInt32 pattern) static void FreeArenaList(PLArenaPool *pool, PLArena *head) { PLArena *a = head->next; - if (!a) + if (!a) { return; + } head->next = NULL; @@ -224,8 +235,9 @@ PR_IMPLEMENT(void) PL_FinishArenaPool(PLArenaPool *pool) { PLArenaStats *stats, **statsp; - if (pool->stats.name) + if (pool->stats.name) { PR_DELETE(pool->stats.name); + } for (statsp = &arena_stats_list; (stats = *statsp) != 0; statsp = &stats->next) { if (stats == &pool->stats) { @@ -266,8 +278,9 @@ PR_IMPLEMENT(void) PL_ArenaCountAllocation(PLArenaPool *pool, PRUint32 nb) { pool->stats.nallocs++; pool->stats.nbytes += nb; - if (nb > pool->stats.maxalloc) + if (nb > pool->stats.maxalloc) { pool->stats.maxalloc = nb; + } pool->stats.variance += nb * nb; } @@ -284,8 +297,9 @@ PR_IMPLEMENT(void) PL_ArenaCountGrowth( pool->stats.nbytes += incr; pool->stats.variance -= size * size; size += incr; - if (size > pool->stats.maxalloc) + if (size > pool->stats.maxalloc) { pool->stats.maxalloc = size; + } pool->stats.variance += size * size; } |