📄 jsarena.c
字号:
/* * If p points to an oversized allocation, it owns an entire arena, so we * can simply realloc the arena. */ if (size > pool->arenasize) return JS_ArenaRealloc(pool, p, size, incr); JS_ARENA_ALLOCATE(newp, pool, size + incr); if (newp) memcpy(newp, p, size); return newp;}/* * Free tail arenas linked after head, which may not be the true list head. * Reset pool->current to point to head in case it pointed at a tail arena. */static voidFreeArenaList(JSArenaPool *pool, JSArena *head){ JSArena **ap, *a; ap = &head->next; a = *ap; if (!a) return;#ifdef DEBUG do { JS_ASSERT(a->base <= a->avail && a->avail <= a->limit); a->avail = a->base; JS_CLEAR_UNUSED(a); } while ((a = a->next) != NULL); a = *ap;#endif do { *ap = a->next; JS_CLEAR_ARENA(a); JS_COUNT_ARENA(pool,--); free(a); } while ((a = *ap) != NULL); pool->current = head;}JS_PUBLIC_API(void)JS_ArenaRelease(JSArenaPool *pool, char *mark){ JSArena *a; for (a = &pool->first; a; a = a->next) { JS_ASSERT(a->base <= a->avail && a->avail <= a->limit); if (JS_UPTRDIFF(mark, a->base) <= JS_UPTRDIFF(a->avail, a->base)) { a->avail = JS_ARENA_ALIGN(pool, mark); JS_ASSERT(a->avail <= a->limit); FreeArenaList(pool, a); return; } }}JS_PUBLIC_API(void)JS_ArenaFreeAllocation(JSArenaPool *pool, void *p, size_t size){ JSArena **ap, *a, *b; jsuword q; /* * If the allocation is oversized, it consumes an entire arena, and it has * a header just before the allocation pointing back to its predecessor's * next member. Otherwise, we have to search pool for a. */ if (size > pool->arenasize) { ap = *PTR_TO_HEADER(pool, p); a = *ap; } else { q = (jsuword)p + size; q = JS_ARENA_ALIGN(pool, q); ap = &pool->first.next; while ((a = *ap) != NULL) { JS_ASSERT(a->base <= a->avail && a->avail <= a->limit); if (a->avail == q) { /* * If a is consumed by the allocation at p, we can free it to * the malloc heap. */ if (a->base == (jsuword)p) break; /* * We can't free a, but we can "retract" its avail cursor -- * whether there are others after it in pool. */ a->avail = (jsuword)p; return; } ap = &a->next; } } /* * At this point, a is doomed, so ensure that pool->current doesn't point * at it. We must preserve LIFO order of mark/release cursors, so we use * the oversized-allocation arena's back pointer (or if not oversized, we * use the result of searching the entire pool) to compute the address of * the arena that precedes a. */ if (pool->current == a) pool->current = (JSArena *) ((char *)ap - offsetof(JSArena, next)); /* * This is a non-LIFO deallocation, so take care to fix up a->next's back * pointer in its header, if a->next is oversized. */ *ap = b = a->next; if (b && b->avail - b->base > pool->arenasize) { JS_ASSERT(GET_HEADER(pool, b) == &a->next); SET_HEADER(pool, b, ap); } JS_CLEAR_ARENA(a); JS_COUNT_ARENA(pool,--); free(a);}JS_PUBLIC_API(void)JS_FreeArenaPool(JSArenaPool *pool){ FreeArenaList(pool, &pool->first); COUNT(pool, ndeallocs);}JS_PUBLIC_API(void)JS_FinishArenaPool(JSArenaPool *pool){ FreeArenaList(pool, &pool->first);#ifdef JS_ARENAMETER { JSArenaStats *stats, **statsp; if (pool->stats.name) free(pool->stats.name); for (statsp = &arena_stats_list; (stats = *statsp) != 0; statsp = &stats->next) { if (stats == &pool->stats) { *statsp = stats->next; return; } } }#endif}JS_PUBLIC_API(void)JS_ArenaFinish(){}JS_PUBLIC_API(void)JS_ArenaShutDown(void){}#ifdef JS_ARENAMETERJS_PUBLIC_API(void)JS_ArenaCountAllocation(JSArenaPool *pool, size_t nb){ pool->stats.nallocs++; pool->stats.nbytes += nb; if (nb > pool->stats.maxalloc) pool->stats.maxalloc = nb; pool->stats.variance += nb * nb;}JS_PUBLIC_API(void)JS_ArenaCountInplaceGrowth(JSArenaPool *pool, size_t size, size_t incr){ pool->stats.ninplace++;}JS_PUBLIC_API(void)JS_ArenaCountGrowth(JSArenaPool *pool, size_t size, size_t incr){ pool->stats.ngrows++; pool->stats.nbytes += incr; pool->stats.variance -= size * size; size += incr; if (size > pool->stats.maxalloc) pool->stats.maxalloc = size; pool->stats.variance += size * size;}JS_PUBLIC_API(void)JS_ArenaCountRelease(JSArenaPool *pool, char *mark){ pool->stats.nreleases++;}JS_PUBLIC_API(void)JS_ArenaCountRetract(JSArenaPool *pool, char *mark){ pool->stats.nfastrels++;}#include <math.h>#include <stdio.h>JS_PUBLIC_API(void)JS_DumpArenaStats(FILE *fp){ JSArenaStats *stats; uint32 nallocs, nbytes; double mean, variance, sigma; for (stats = arena_stats_list; stats; stats = stats->next) { nallocs = stats->nallocs; if (nallocs != 0) { nbytes = stats->nbytes; mean = (double)nbytes / nallocs; variance = stats->variance * nallocs - nbytes * nbytes; if (variance < 0 || nallocs == 1) variance = 0; else variance /= nallocs * (nallocs - 1); sigma = sqrt(variance); } else { mean = variance = sigma = 0; } fprintf(fp, "\n%s allocation statistics:\n", stats->name); fprintf(fp, " number of arenas: %u\n", stats->narenas); fprintf(fp, " number of allocations: %u\n", stats->nallocs); fprintf(fp, " number of free arena reclaims: %u\n", stats->nreclaims); fprintf(fp, " number of malloc calls: %u\n", stats->nmallocs); fprintf(fp, " number of deallocations: %u\n", stats->ndeallocs); fprintf(fp, " number of allocation growths: %u\n", stats->ngrows); fprintf(fp, " number of in-place growths: %u\n", stats->ninplace); fprintf(fp, " number of realloc'ing growths: %u\n", stats->nreallocs); fprintf(fp, "number of released allocations: %u\n", stats->nreleases); fprintf(fp, " number of fast releases: %u\n", stats->nfastrels); fprintf(fp, " total bytes allocated: %u\n", stats->nbytes); fprintf(fp, " mean allocation size: %g\n", mean); fprintf(fp, " standard deviation: %g\n", sigma); fprintf(fp, " maximum allocation size: %u\n", stats->maxalloc); }}#endif /* JS_ARENAMETER */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -