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