nsprpub/lib/ds/plarena.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial