andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: * Lifetime-based fast allocation, inspired by much prior art, including andre@0: * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes" andre@0: * David R. Hanson, Software -- Practice and Experience, Vol. 20(1). andre@0: */ andre@0: #include andre@0: #include andre@0: #include "plarena.h" andre@0: #include "prmem.h" andre@0: #include "prbit.h" andre@0: #include "prlog.h" andre@0: #include "prlock.h" andre@0: #include "prinit.h" andre@0: andre@0: static PLArena *arena_freelist; andre@0: andre@0: #ifdef PL_ARENAMETER andre@0: static PLArenaStats *arena_stats_list; andre@0: andre@0: #define COUNT(pool,what) (pool)->stats.what++ andre@0: #else andre@0: #define COUNT(pool,what) /* nothing */ andre@0: #endif andre@0: andre@0: #define PL_ARENA_DEFAULT_ALIGN sizeof(double) andre@0: andre@0: static PRLock *arenaLock; andre@0: static PRCallOnceType once; andre@0: static const PRCallOnceType pristineCallOnce; andre@0: andre@0: /* andre@0: ** InitializeArenas() -- Initialize arena operations. andre@0: ** andre@0: ** InitializeArenas() is called exactly once and only once from andre@0: ** LockArena(). This function creates the arena protection andre@0: ** lock: arenaLock. andre@0: ** andre@0: ** Note: If the arenaLock cannot be created, InitializeArenas() andre@0: ** fails quietly, returning only PR_FAILURE. This percolates up andre@0: ** to the application using the Arena API. He gets no arena andre@0: ** from PL_ArenaAllocate(). It's up to him to fail gracefully andre@0: ** or recover. andre@0: ** andre@0: */ andre@0: static PRStatus InitializeArenas( void ) andre@0: { andre@0: PR_ASSERT( arenaLock == NULL ); andre@0: arenaLock = PR_NewLock(); andre@0: if ( arenaLock == NULL ) andre@0: return PR_FAILURE; andre@0: else andre@0: return PR_SUCCESS; andre@0: } /* end ArenaInitialize() */ andre@0: andre@0: static PRStatus LockArena( void ) andre@0: { andre@0: PRStatus rc = PR_CallOnce( &once, InitializeArenas ); andre@0: andre@0: if ( PR_FAILURE != rc ) andre@0: PR_Lock( arenaLock ); andre@0: return(rc); andre@0: } /* end LockArena() */ andre@0: andre@0: static void UnlockArena( void ) andre@0: { andre@0: PR_Unlock( arenaLock ); andre@0: return; andre@0: } /* end UnlockArena() */ andre@0: andre@0: PR_IMPLEMENT(void) PL_InitArenaPool( andre@0: PLArenaPool *pool, const char *name, PRUint32 size, PRUint32 align) andre@0: { andre@0: /* andre@0: * Look-up table of PR_BITMASK(PR_CeilingLog2(align)) values for andre@0: * align = 1 to 32. andre@0: */ andre@0: static const PRUint8 pmasks[33] = { andre@0: 0, /* not used */ andre@0: 0, 1, 3, 3, 7, 7, 7, 7,15,15,15,15,15,15,15,15, /* 1 ... 16 */ andre@0: 31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31}; /* 17 ... 32 */ andre@0: andre@0: if (align == 0) andre@0: align = PL_ARENA_DEFAULT_ALIGN; andre@0: andre@0: if (align < sizeof(pmasks)/sizeof(pmasks[0])) andre@0: pool->mask = pmasks[align]; andre@0: else andre@0: pool->mask = PR_BITMASK(PR_CeilingLog2(align)); andre@0: andre@0: pool->first.next = NULL; andre@0: pool->first.base = pool->first.avail = pool->first.limit = andre@0: (PRUword)PL_ARENA_ALIGN(pool, &pool->first + 1); andre@0: pool->current = &pool->first; andre@0: /* andre@0: * Compute the net size so that each arena's gross size is |size|. andre@0: * sizeof(PLArena) + pool->mask is the header and alignment slop andre@0: * that PL_ArenaAllocate adds to the net size. andre@0: */ andre@0: if (size > sizeof(PLArena) + pool->mask) andre@0: pool->arenasize = size - (sizeof(PLArena) + pool->mask); andre@0: else andre@0: pool->arenasize = size; andre@0: #ifdef PL_ARENAMETER andre@0: memset(&pool->stats, 0, sizeof pool->stats); andre@0: pool->stats.name = strdup(name); andre@0: pool->stats.next = arena_stats_list; andre@0: arena_stats_list = &pool->stats; andre@0: #endif andre@0: } andre@0: andre@0: andre@0: /* andre@0: ** PL_ArenaAllocate() -- allocate space from an arena pool andre@0: ** andre@0: ** Description: PL_ArenaAllocate() allocates space from an arena andre@0: ** pool. andre@0: ** andre@0: ** First, try to satisfy the request from arenas starting at andre@0: ** pool->current. andre@0: ** andre@0: ** If there is not enough space in the arena pool->current, try andre@0: ** to claim an arena, on a first fit basis, from the global andre@0: ** freelist (arena_freelist). andre@0: ** andre@0: ** If no arena in arena_freelist is suitable, then try to andre@0: ** allocate a new arena from the heap. andre@0: ** andre@0: ** Returns: pointer to allocated space or NULL andre@0: ** andre@0: ** Notes: The original implementation had some difficult to andre@0: ** solve bugs; the code was difficult to read. Sometimes it's andre@0: ** just easier to rewrite it. I did that. larryh. andre@0: ** andre@0: ** See also: bugzilla: 45343. andre@0: ** andre@0: */ andre@0: andre@0: PR_IMPLEMENT(void *) PL_ArenaAllocate(PLArenaPool *pool, PRUint32 nb) andre@0: { andre@0: PLArena *a; andre@0: char *rp; /* returned pointer */ andre@0: andre@0: PR_ASSERT((nb & pool->mask) == 0); andre@0: andre@0: nb = (PRUword)PL_ARENA_ALIGN(pool, nb); /* force alignment */ andre@0: andre@0: /* attempt to allocate from arenas at pool->current */ andre@0: { andre@0: a = pool->current; andre@0: do { andre@0: if ( nb <= a->limit - a->avail ) { andre@0: pool->current = a; andre@0: rp = (char *)a->avail; andre@0: a->avail += nb; andre@0: return rp; andre@0: } andre@0: } while( NULL != (a = a->next) ); andre@0: } andre@0: andre@0: /* attempt to allocate from arena_freelist */ andre@0: { andre@0: PLArena *p; /* previous pointer, for unlinking from freelist */ andre@0: andre@0: /* lock the arena_freelist. Make access to the freelist MT-Safe */ andre@0: if ( PR_FAILURE == LockArena()) andre@0: return(0); andre@0: andre@0: for ( a = arena_freelist, p = NULL; a != NULL ; p = a, a = a->next ) { andre@0: if ( nb <= a->limit - a->base ) { andre@0: if ( p == NULL ) andre@0: arena_freelist = a->next; andre@0: else andre@0: p->next = a->next; andre@0: UnlockArena(); andre@0: a->avail = a->base; andre@0: rp = (char *)a->avail; andre@0: a->avail += nb; andre@0: /* the newly allocated arena is linked after pool->current andre@0: * and becomes pool->current */ andre@0: a->next = pool->current->next; andre@0: pool->current->next = a; andre@0: pool->current = a; andre@0: if ( NULL == pool->first.next ) andre@0: pool->first.next = a; andre@0: return(rp); andre@0: } andre@0: } andre@0: UnlockArena(); andre@0: } andre@0: andre@0: /* attempt to allocate from the heap */ andre@0: { andre@0: PRUint32 sz = PR_MAX(pool->arenasize, nb); andre@0: if (PR_UINT32_MAX - sz < sizeof *a + pool->mask) { andre@0: a = NULL; andre@0: } else { andre@0: sz += sizeof *a + pool->mask; /* header and alignment slop */ andre@0: a = (PLArena*)PR_MALLOC(sz); andre@0: } andre@0: if ( NULL != a ) { andre@0: a->limit = (PRUword)a + sz; andre@0: a->base = a->avail = (PRUword)PL_ARENA_ALIGN(pool, a + 1); andre@0: PL_MAKE_MEM_NOACCESS((void*)a->avail, a->limit - a->avail); andre@0: rp = (char *)a->avail; andre@0: a->avail += nb; andre@0: /* the newly allocated arena is linked after pool->current andre@0: * and becomes pool->current */ andre@0: a->next = pool->current->next; andre@0: pool->current->next = a; andre@0: pool->current = a; andre@0: if ( NULL == pool->first.next ) andre@0: pool->first.next = a; andre@0: PL_COUNT_ARENA(pool,++); andre@0: COUNT(pool, nmallocs); andre@0: return(rp); andre@0: } andre@0: } andre@0: andre@0: /* we got to here, and there's no memory to allocate */ andre@0: return(NULL); andre@0: } /* --- end PL_ArenaAllocate() --- */ andre@0: andre@0: PR_IMPLEMENT(void *) PL_ArenaGrow( andre@0: PLArenaPool *pool, void *p, PRUint32 size, PRUint32 incr) andre@0: { andre@0: void *newp; andre@0: andre@0: PL_ARENA_ALLOCATE(newp, pool, size + incr); andre@0: if (newp) andre@0: memcpy(newp, p, size); andre@0: return newp; andre@0: } andre@0: andre@0: static void ClearArenaList(PLArena *a, PRInt32 pattern) andre@0: { andre@0: andre@0: for (; a; a = a->next) { andre@0: PR_ASSERT(a->base <= a->avail && a->avail <= a->limit); andre@0: a->avail = a->base; andre@0: PL_CLEAR_UNUSED_PATTERN(a, pattern); andre@0: PL_MAKE_MEM_NOACCESS((void*)a->avail, a->limit - a->avail); andre@0: } andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PL_ClearArenaPool(PLArenaPool *pool, PRInt32 pattern) andre@0: { andre@0: ClearArenaList(pool->first.next, pattern); andre@0: } andre@0: andre@0: /* andre@0: * Free tail arenas linked after head, which may not be the true list head. andre@0: * Reset pool->current to point to head in case it pointed at a tail arena. andre@0: */ andre@0: static void FreeArenaList(PLArenaPool *pool, PLArena *head, PRBool reallyFree) andre@0: { andre@0: PLArena **ap, *a; andre@0: andre@0: ap = &head->next; andre@0: a = *ap; andre@0: if (!a) andre@0: return; andre@0: andre@0: #ifdef DEBUG andre@0: ClearArenaList(a, PL_FREE_PATTERN); andre@0: #endif andre@0: andre@0: if (reallyFree) { andre@0: do { andre@0: *ap = a->next; andre@0: PL_CLEAR_ARENA(a); andre@0: PL_COUNT_ARENA(pool,--); andre@0: PR_DELETE(a); andre@0: } while ((a = *ap) != 0); andre@0: } else { andre@0: /* Insert the whole arena chain at the front of the freelist. */ andre@0: do { andre@0: PL_MAKE_MEM_NOACCESS((void*)(*ap)->base, andre@0: (*ap)->limit - (*ap)->base); andre@0: ap = &(*ap)->next; andre@0: } while (*ap); andre@0: LockArena(); andre@0: *ap = arena_freelist; andre@0: arena_freelist = a; andre@0: head->next = 0; andre@0: UnlockArena(); andre@0: } andre@0: andre@0: pool->current = head; andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PL_ArenaRelease(PLArenaPool *pool, char *mark) andre@0: { andre@0: PLArena *a; andre@0: andre@0: for (a = &pool->first; a; a = a->next) { andre@0: if (PR_UPTRDIFF(mark, a->base) <= PR_UPTRDIFF(a->avail, a->base)) { andre@0: a->avail = (PRUword)PL_ARENA_ALIGN(pool, mark); andre@0: FreeArenaList(pool, a, PR_FALSE); andre@0: return; andre@0: } andre@0: } andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PL_FreeArenaPool(PLArenaPool *pool) andre@0: { andre@0: FreeArenaList(pool, &pool->first, PR_FALSE); andre@0: COUNT(pool, ndeallocs); andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PL_FinishArenaPool(PLArenaPool *pool) andre@0: { andre@0: FreeArenaList(pool, &pool->first, PR_TRUE); andre@0: #ifdef PL_ARENAMETER andre@0: { andre@0: PLArenaStats *stats, **statsp; andre@0: andre@0: if (pool->stats.name) andre@0: PR_DELETE(pool->stats.name); andre@0: for (statsp = &arena_stats_list; (stats = *statsp) != 0; andre@0: statsp = &stats->next) { andre@0: if (stats == &pool->stats) { andre@0: *statsp = stats->next; andre@0: return; andre@0: } andre@0: } andre@0: } andre@0: #endif andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PL_CompactArenaPool(PLArenaPool *ap) andre@0: { andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PL_ArenaFinish(void) andre@0: { andre@0: PLArena *a, *next; andre@0: andre@0: for (a = arena_freelist; a; a = next) { andre@0: next = a->next; andre@0: PR_DELETE(a); andre@0: } andre@0: arena_freelist = NULL; andre@0: andre@0: if (arenaLock) { andre@0: PR_DestroyLock(arenaLock); andre@0: arenaLock = NULL; andre@0: } andre@0: once = pristineCallOnce; andre@0: } andre@0: andre@0: PR_IMPLEMENT(size_t) PL_SizeOfArenaPoolExcludingPool( andre@0: const PLArenaPool *pool, PLMallocSizeFn mallocSizeOf) andre@0: { andre@0: /* andre@0: * The first PLArena is within |pool|, so don't measure it. Subsequent andre@0: * PLArenas are separate and must be measured. andre@0: */ andre@0: size_t size = 0; andre@0: const PLArena *arena = pool->first.next; andre@0: while (arena) { andre@0: size += mallocSizeOf(arena); andre@0: arena = arena->next; andre@0: } andre@0: return size; andre@0: } andre@0: andre@0: #ifdef PL_ARENAMETER andre@0: PR_IMPLEMENT(void) PL_ArenaCountAllocation(PLArenaPool *pool, PRUint32 nb) andre@0: { andre@0: pool->stats.nallocs++; andre@0: pool->stats.nbytes += nb; andre@0: if (nb > pool->stats.maxalloc) andre@0: pool->stats.maxalloc = nb; andre@0: pool->stats.variance += nb * nb; andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PL_ArenaCountInplaceGrowth( andre@0: PLArenaPool *pool, PRUint32 size, PRUint32 incr) andre@0: { andre@0: pool->stats.ninplace++; andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PL_ArenaCountGrowth( andre@0: PLArenaPool *pool, PRUint32 size, PRUint32 incr) andre@0: { andre@0: pool->stats.ngrows++; andre@0: pool->stats.nbytes += incr; andre@0: pool->stats.variance -= size * size; andre@0: size += incr; andre@0: if (size > pool->stats.maxalloc) andre@0: pool->stats.maxalloc = size; andre@0: pool->stats.variance += size * size; andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PL_ArenaCountRelease(PLArenaPool *pool, char *mark) andre@0: { andre@0: pool->stats.nreleases++; andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PL_ArenaCountRetract(PLArenaPool *pool, char *mark) andre@0: { andre@0: pool->stats.nfastrels++; andre@0: } andre@0: andre@0: #include andre@0: #include andre@0: andre@0: PR_IMPLEMENT(void) PL_DumpArenaStats(FILE *fp) andre@0: { andre@0: PLArenaStats *stats; andre@0: double mean, variance; andre@0: andre@0: for (stats = arena_stats_list; stats; stats = stats->next) { andre@0: if (stats->nallocs != 0) { andre@0: mean = (double)stats->nbytes / stats->nallocs; andre@0: variance = fabs(stats->variance / stats->nallocs - mean * mean); andre@0: } else { andre@0: mean = variance = 0; andre@0: } andre@0: andre@0: fprintf(fp, "\n%s allocation statistics:\n", stats->name); andre@0: fprintf(fp, " number of arenas: %u\n", stats->narenas); andre@0: fprintf(fp, " number of allocations: %u\n", stats->nallocs); andre@0: fprintf(fp, " number of free arena reclaims: %u\n", stats->nreclaims); andre@0: fprintf(fp, " number of malloc calls: %u\n", stats->nmallocs); andre@0: fprintf(fp, " number of deallocations: %u\n", stats->ndeallocs); andre@0: fprintf(fp, " number of allocation growths: %u\n", stats->ngrows); andre@0: fprintf(fp, " number of in-place growths: %u\n", stats->ninplace); andre@0: fprintf(fp, "number of released allocations: %u\n", stats->nreleases); andre@0: fprintf(fp, " number of fast releases: %u\n", stats->nfastrels); andre@0: fprintf(fp, " total bytes allocated: %u\n", stats->nbytes); andre@0: fprintf(fp, " mean allocation size: %g\n", mean); andre@0: fprintf(fp, " standard deviation: %g\n", sqrt(variance)); andre@0: fprintf(fp, " maximum allocation size: %u\n", stats->maxalloc); andre@0: } andre@0: } andre@0: #endif /* PL_ARENAMETER */