michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef plarena_h___ michael@0: #define plarena_h___ michael@0: /* michael@0: * Lifetime-based fast allocation, inspired by much prior art, including michael@0: * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes" michael@0: * David R. Hanson, Software -- Practice and Experience, Vol. 20(1). michael@0: * michael@0: * Also supports LIFO allocation (PL_ARENA_MARK/PL_ARENA_RELEASE). michael@0: */ michael@0: #include "prtypes.h" michael@0: #include "plarenas.h" michael@0: michael@0: PR_BEGIN_EXTERN_C michael@0: michael@0: typedef struct PLArena PLArena; michael@0: michael@0: struct PLArena { michael@0: PLArena *next; /* next arena for this lifetime */ michael@0: PRUword base; /* aligned base address, follows this header */ michael@0: PRUword limit; /* one beyond last byte in arena */ michael@0: PRUword avail; /* points to next available byte */ michael@0: }; michael@0: michael@0: #ifdef PL_ARENAMETER michael@0: typedef struct PLArenaStats PLArenaStats; michael@0: michael@0: struct PLArenaStats { michael@0: PLArenaStats *next; /* next in arenaStats list */ michael@0: char *name; /* name for debugging */ michael@0: PRUint32 narenas; /* number of arenas in pool */ michael@0: PRUint32 nallocs; /* number of PL_ARENA_ALLOCATE() calls */ michael@0: PRUint32 nreclaims; /* number of reclaims from freeArenas */ michael@0: PRUint32 nmallocs; /* number of malloc() calls */ michael@0: PRUint32 ndeallocs; /* number of lifetime deallocations */ michael@0: PRUint32 ngrows; /* number of PL_ARENA_GROW() calls */ michael@0: PRUint32 ninplace; /* number of in-place growths */ michael@0: PRUint32 nreleases; /* number of PL_ARENA_RELEASE() calls */ michael@0: PRUint32 nfastrels; /* number of "fast path" releases */ michael@0: PRUint32 nbytes; /* total bytes allocated */ michael@0: PRUint32 maxalloc; /* maximum allocation size in bytes */ michael@0: PRFloat64 variance; /* size variance accumulator */ michael@0: }; michael@0: #endif michael@0: michael@0: struct PLArenaPool { michael@0: PLArena first; /* first arena in pool list */ michael@0: PLArena *current; /* arena from which to allocate space */ michael@0: PRUint32 arenasize; /* net exact size of a new arena */ michael@0: PRUword mask; /* alignment mask (power-of-2 - 1) */ michael@0: #ifdef PL_ARENAMETER michael@0: PLArenaStats stats; michael@0: #endif michael@0: }; michael@0: michael@0: /* michael@0: * WARNING: The PL_MAKE_MEM_ macros are for internal use by NSPR. Do NOT use michael@0: * them in your code. michael@0: * michael@0: * NOTE: Valgrind support to be added. michael@0: * michael@0: * The PL_MAKE_MEM_ macros are modeled after the MOZ_MAKE_MEM_ macros in michael@0: * Mozilla's mfbt/MemoryChecking.h. Only AddressSanitizer is supported now. michael@0: * michael@0: * Provides a common interface to the ASan (AddressSanitizer) and Valgrind michael@0: * functions used to mark memory in certain ways. In detail, the following michael@0: * three macros are provided: michael@0: * michael@0: * PL_MAKE_MEM_NOACCESS - Mark memory as unsafe to access (e.g. freed) michael@0: * PL_MAKE_MEM_UNDEFINED - Mark memory as accessible, with content undefined michael@0: * PL_MAKE_MEM_DEFINED - Mark memory as accessible, with content defined michael@0: * michael@0: * With Valgrind in use, these directly map to the three respective Valgrind michael@0: * macros. With ASan in use, the NOACCESS macro maps to poisoning the memory, michael@0: * while the UNDEFINED/DEFINED macros unpoison memory. michael@0: * michael@0: * With no memory checker available, all macros expand to the empty statement. michael@0: */ michael@0: michael@0: /* WARNING: PL_SANITIZE_ADDRESS is for internal use by this header. Do NOT michael@0: * define or test this macro in your code. michael@0: */ michael@0: #if defined(__has_feature) michael@0: #if __has_feature(address_sanitizer) michael@0: #define PL_SANITIZE_ADDRESS 1 michael@0: #endif michael@0: #elif defined(__SANITIZE_ADDRESS__) michael@0: #define PL_SANITIZE_ADDRESS 1 michael@0: #endif michael@0: michael@0: #if defined(PL_SANITIZE_ADDRESS) michael@0: michael@0: /* These definitions are usually provided through the michael@0: * sanitizer/asan_interface.h header installed by ASan. michael@0: * See https://code.google.com/p/address-sanitizer/wiki/ManualPoisoning michael@0: */ michael@0: michael@0: void __asan_poison_memory_region(void const volatile *addr, size_t size); michael@0: void __asan_unpoison_memory_region(void const volatile *addr, size_t size); michael@0: michael@0: #define PL_MAKE_MEM_NOACCESS(addr, size) \ michael@0: __asan_poison_memory_region((addr), (size)) michael@0: michael@0: #define PL_MAKE_MEM_UNDEFINED(addr, size) \ michael@0: __asan_unpoison_memory_region((addr), (size)) michael@0: michael@0: #define PL_MAKE_MEM_DEFINED(addr, size) \ michael@0: __asan_unpoison_memory_region((addr), (size)) michael@0: michael@0: #else michael@0: michael@0: #define PL_MAKE_MEM_NOACCESS(addr, size) michael@0: #define PL_MAKE_MEM_UNDEFINED(addr, size) michael@0: #define PL_MAKE_MEM_DEFINED(addr, size) michael@0: michael@0: #endif michael@0: michael@0: /* michael@0: * If the including .c file uses only one power-of-2 alignment, it may define michael@0: * PL_ARENA_CONST_ALIGN_MASK to the alignment mask and save a few instructions michael@0: * per ALLOCATE and GROW. michael@0: */ michael@0: #ifdef PL_ARENA_CONST_ALIGN_MASK michael@0: #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + PL_ARENA_CONST_ALIGN_MASK) \ michael@0: & ~PL_ARENA_CONST_ALIGN_MASK) michael@0: michael@0: #define PL_INIT_ARENA_POOL(pool, name, size) \ michael@0: PL_InitArenaPool(pool, name, size, PL_ARENA_CONST_ALIGN_MASK + 1) michael@0: #else michael@0: #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + (pool)->mask) & ~(pool)->mask) michael@0: #endif michael@0: michael@0: #define PL_ARENA_ALLOCATE(p, pool, nb) \ michael@0: PR_BEGIN_MACRO \ michael@0: PLArena *_a = (pool)->current; \ michael@0: PRUint32 _nb = PL_ARENA_ALIGN(pool, nb); \ michael@0: PRUword _p = _a->avail; \ michael@0: PRUword _q = _p + _nb; \ michael@0: if (_q > _a->limit) { \ michael@0: _p = (PRUword)PL_ArenaAllocate(pool, _nb); \ michael@0: } else { \ michael@0: _a->avail = _q; \ michael@0: } \ michael@0: p = (void *)_p; \ michael@0: PL_MAKE_MEM_UNDEFINED(p, nb); \ michael@0: PL_ArenaCountAllocation(pool, nb); \ michael@0: PR_END_MACRO michael@0: michael@0: #define PL_ARENA_GROW(p, pool, size, incr) \ michael@0: PR_BEGIN_MACRO \ michael@0: PLArena *_a = (pool)->current; \ michael@0: PRUint32 _incr = PL_ARENA_ALIGN(pool, incr); \ michael@0: PRUword _p = _a->avail; \ michael@0: PRUword _q = _p + _incr; \ michael@0: if (_p == (PRUword)(p) + PL_ARENA_ALIGN(pool, size) && \ michael@0: _q <= _a->limit) { \ michael@0: PL_MAKE_MEM_UNDEFINED((unsigned char *)(p) + size, incr); \ michael@0: _a->avail = _q; \ michael@0: PL_ArenaCountInplaceGrowth(pool, size, incr); \ michael@0: } else { \ michael@0: p = PL_ArenaGrow(pool, p, size, incr); \ michael@0: } \ michael@0: PL_ArenaCountGrowth(pool, size, incr); \ michael@0: PR_END_MACRO michael@0: michael@0: #define PL_ARENA_MARK(pool) ((void *) (pool)->current->avail) michael@0: #define PR_UPTRDIFF(p,q) ((PRUword)(p) - (PRUword)(q)) michael@0: michael@0: #define PL_CLEAR_UNUSED_PATTERN(a, pattern) \ michael@0: PR_BEGIN_MACRO \ michael@0: PR_ASSERT((a)->avail <= (a)->limit); \ michael@0: PL_MAKE_MEM_UNDEFINED((void*)(a)->avail, (a)->limit - (a)->avail); \ michael@0: memset((void*)(a)->avail, (pattern), (a)->limit - (a)->avail); \ michael@0: PR_END_MACRO michael@0: #ifdef DEBUG michael@0: #define PL_FREE_PATTERN 0xDA michael@0: #define PL_CLEAR_UNUSED(a) PL_CLEAR_UNUSED_PATTERN((a), PL_FREE_PATTERN) michael@0: #define PL_CLEAR_ARENA(a) \ michael@0: PR_BEGIN_MACRO \ michael@0: PL_MAKE_MEM_UNDEFINED((void*)(a), (a)->limit - (PRUword)(a)); \ michael@0: memset((void*)(a), PL_FREE_PATTERN, (a)->limit - (PRUword)(a)); \ michael@0: PR_END_MACRO michael@0: #else michael@0: #define PL_CLEAR_UNUSED(a) michael@0: #define PL_CLEAR_ARENA(a) michael@0: #endif michael@0: michael@0: #define PL_ARENA_RELEASE(pool, mark) \ michael@0: PR_BEGIN_MACRO \ michael@0: char *_m = (char *)(mark); \ michael@0: PLArena *_a = (pool)->current; \ michael@0: if (PR_UPTRDIFF(_m, _a->base) <= PR_UPTRDIFF(_a->avail, _a->base)) { \ michael@0: _a->avail = (PRUword)PL_ARENA_ALIGN(pool, _m); \ michael@0: PL_CLEAR_UNUSED(_a); \ michael@0: PL_MAKE_MEM_NOACCESS((void*)_a->avail, _a->limit - _a->avail); \ michael@0: PL_ArenaCountRetract(pool, _m); \ michael@0: } else { \ michael@0: PL_ArenaRelease(pool, _m); \ michael@0: } \ michael@0: PL_ArenaCountRelease(pool, _m); \ michael@0: PR_END_MACRO michael@0: michael@0: #ifdef PL_ARENAMETER michael@0: #define PL_COUNT_ARENA(pool,op) ((pool)->stats.narenas op) michael@0: #else michael@0: #define PL_COUNT_ARENA(pool,op) michael@0: #endif michael@0: michael@0: #define PL_ARENA_DESTROY(pool, a, pnext) \ michael@0: PR_BEGIN_MACRO \ michael@0: PL_COUNT_ARENA(pool,--); \ michael@0: if ((pool)->current == (a)) (pool)->current = &(pool)->first; \ michael@0: *(pnext) = (a)->next; \ michael@0: PL_CLEAR_ARENA(a); \ michael@0: free(a); \ michael@0: (a) = 0; \ michael@0: PR_END_MACRO michael@0: michael@0: #ifdef PL_ARENAMETER michael@0: michael@0: #include michael@0: michael@0: PR_EXTERN(void) PL_ArenaCountAllocation(PLArenaPool *pool, PRUint32 nb); michael@0: michael@0: PR_EXTERN(void) PL_ArenaCountInplaceGrowth( michael@0: PLArenaPool *pool, PRUint32 size, PRUint32 incr); michael@0: michael@0: PR_EXTERN(void) PL_ArenaCountGrowth( michael@0: PLArenaPool *pool, PRUint32 size, PRUint32 incr); michael@0: michael@0: PR_EXTERN(void) PL_ArenaCountRelease(PLArenaPool *pool, char *mark); michael@0: michael@0: PR_EXTERN(void) PL_ArenaCountRetract(PLArenaPool *pool, char *mark); michael@0: michael@0: PR_EXTERN(void) PL_DumpArenaStats(FILE *fp); michael@0: michael@0: #else /* !PL_ARENAMETER */ michael@0: michael@0: #define PL_ArenaCountAllocation(ap, nb) /* nothing */ michael@0: #define PL_ArenaCountInplaceGrowth(ap, size, incr) /* nothing */ michael@0: #define PL_ArenaCountGrowth(ap, size, incr) /* nothing */ michael@0: #define PL_ArenaCountRelease(ap, mark) /* nothing */ michael@0: #define PL_ArenaCountRetract(ap, mark) /* nothing */ michael@0: michael@0: #endif /* !PL_ARENAMETER */ michael@0: michael@0: PR_END_EXTERN_C michael@0: michael@0: #endif /* plarena_h___ */