1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/lib/ds/plarena.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,253 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef plarena_h___ 1.10 +#define plarena_h___ 1.11 +/* 1.12 + * Lifetime-based fast allocation, inspired by much prior art, including 1.13 + * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes" 1.14 + * David R. Hanson, Software -- Practice and Experience, Vol. 20(1). 1.15 + * 1.16 + * Also supports LIFO allocation (PL_ARENA_MARK/PL_ARENA_RELEASE). 1.17 + */ 1.18 +#include "prtypes.h" 1.19 +#include "plarenas.h" 1.20 + 1.21 +PR_BEGIN_EXTERN_C 1.22 + 1.23 +typedef struct PLArena PLArena; 1.24 + 1.25 +struct PLArena { 1.26 + PLArena *next; /* next arena for this lifetime */ 1.27 + PRUword base; /* aligned base address, follows this header */ 1.28 + PRUword limit; /* one beyond last byte in arena */ 1.29 + PRUword avail; /* points to next available byte */ 1.30 +}; 1.31 + 1.32 +#ifdef PL_ARENAMETER 1.33 +typedef struct PLArenaStats PLArenaStats; 1.34 + 1.35 +struct PLArenaStats { 1.36 + PLArenaStats *next; /* next in arenaStats list */ 1.37 + char *name; /* name for debugging */ 1.38 + PRUint32 narenas; /* number of arenas in pool */ 1.39 + PRUint32 nallocs; /* number of PL_ARENA_ALLOCATE() calls */ 1.40 + PRUint32 nreclaims; /* number of reclaims from freeArenas */ 1.41 + PRUint32 nmallocs; /* number of malloc() calls */ 1.42 + PRUint32 ndeallocs; /* number of lifetime deallocations */ 1.43 + PRUint32 ngrows; /* number of PL_ARENA_GROW() calls */ 1.44 + PRUint32 ninplace; /* number of in-place growths */ 1.45 + PRUint32 nreleases; /* number of PL_ARENA_RELEASE() calls */ 1.46 + PRUint32 nfastrels; /* number of "fast path" releases */ 1.47 + PRUint32 nbytes; /* total bytes allocated */ 1.48 + PRUint32 maxalloc; /* maximum allocation size in bytes */ 1.49 + PRFloat64 variance; /* size variance accumulator */ 1.50 +}; 1.51 +#endif 1.52 + 1.53 +struct PLArenaPool { 1.54 + PLArena first; /* first arena in pool list */ 1.55 + PLArena *current; /* arena from which to allocate space */ 1.56 + PRUint32 arenasize; /* net exact size of a new arena */ 1.57 + PRUword mask; /* alignment mask (power-of-2 - 1) */ 1.58 +#ifdef PL_ARENAMETER 1.59 + PLArenaStats stats; 1.60 +#endif 1.61 +}; 1.62 + 1.63 +/* 1.64 + * WARNING: The PL_MAKE_MEM_ macros are for internal use by NSPR. Do NOT use 1.65 + * them in your code. 1.66 + * 1.67 + * NOTE: Valgrind support to be added. 1.68 + * 1.69 + * The PL_MAKE_MEM_ macros are modeled after the MOZ_MAKE_MEM_ macros in 1.70 + * Mozilla's mfbt/MemoryChecking.h. Only AddressSanitizer is supported now. 1.71 + * 1.72 + * Provides a common interface to the ASan (AddressSanitizer) and Valgrind 1.73 + * functions used to mark memory in certain ways. In detail, the following 1.74 + * three macros are provided: 1.75 + * 1.76 + * PL_MAKE_MEM_NOACCESS - Mark memory as unsafe to access (e.g. freed) 1.77 + * PL_MAKE_MEM_UNDEFINED - Mark memory as accessible, with content undefined 1.78 + * PL_MAKE_MEM_DEFINED - Mark memory as accessible, with content defined 1.79 + * 1.80 + * With Valgrind in use, these directly map to the three respective Valgrind 1.81 + * macros. With ASan in use, the NOACCESS macro maps to poisoning the memory, 1.82 + * while the UNDEFINED/DEFINED macros unpoison memory. 1.83 + * 1.84 + * With no memory checker available, all macros expand to the empty statement. 1.85 + */ 1.86 + 1.87 +/* WARNING: PL_SANITIZE_ADDRESS is for internal use by this header. Do NOT 1.88 + * define or test this macro in your code. 1.89 + */ 1.90 +#if defined(__has_feature) 1.91 +#if __has_feature(address_sanitizer) 1.92 +#define PL_SANITIZE_ADDRESS 1 1.93 +#endif 1.94 +#elif defined(__SANITIZE_ADDRESS__) 1.95 +#define PL_SANITIZE_ADDRESS 1 1.96 +#endif 1.97 + 1.98 +#if defined(PL_SANITIZE_ADDRESS) 1.99 + 1.100 +/* These definitions are usually provided through the 1.101 + * sanitizer/asan_interface.h header installed by ASan. 1.102 + * See https://code.google.com/p/address-sanitizer/wiki/ManualPoisoning 1.103 + */ 1.104 + 1.105 +void __asan_poison_memory_region(void const volatile *addr, size_t size); 1.106 +void __asan_unpoison_memory_region(void const volatile *addr, size_t size); 1.107 + 1.108 +#define PL_MAKE_MEM_NOACCESS(addr, size) \ 1.109 + __asan_poison_memory_region((addr), (size)) 1.110 + 1.111 +#define PL_MAKE_MEM_UNDEFINED(addr, size) \ 1.112 + __asan_unpoison_memory_region((addr), (size)) 1.113 + 1.114 +#define PL_MAKE_MEM_DEFINED(addr, size) \ 1.115 + __asan_unpoison_memory_region((addr), (size)) 1.116 + 1.117 +#else 1.118 + 1.119 +#define PL_MAKE_MEM_NOACCESS(addr, size) 1.120 +#define PL_MAKE_MEM_UNDEFINED(addr, size) 1.121 +#define PL_MAKE_MEM_DEFINED(addr, size) 1.122 + 1.123 +#endif 1.124 + 1.125 +/* 1.126 + * If the including .c file uses only one power-of-2 alignment, it may define 1.127 + * PL_ARENA_CONST_ALIGN_MASK to the alignment mask and save a few instructions 1.128 + * per ALLOCATE and GROW. 1.129 + */ 1.130 +#ifdef PL_ARENA_CONST_ALIGN_MASK 1.131 +#define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + PL_ARENA_CONST_ALIGN_MASK) \ 1.132 + & ~PL_ARENA_CONST_ALIGN_MASK) 1.133 + 1.134 +#define PL_INIT_ARENA_POOL(pool, name, size) \ 1.135 + PL_InitArenaPool(pool, name, size, PL_ARENA_CONST_ALIGN_MASK + 1) 1.136 +#else 1.137 +#define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + (pool)->mask) & ~(pool)->mask) 1.138 +#endif 1.139 + 1.140 +#define PL_ARENA_ALLOCATE(p, pool, nb) \ 1.141 + PR_BEGIN_MACRO \ 1.142 + PLArena *_a = (pool)->current; \ 1.143 + PRUint32 _nb = PL_ARENA_ALIGN(pool, nb); \ 1.144 + PRUword _p = _a->avail; \ 1.145 + PRUword _q = _p + _nb; \ 1.146 + if (_q > _a->limit) { \ 1.147 + _p = (PRUword)PL_ArenaAllocate(pool, _nb); \ 1.148 + } else { \ 1.149 + _a->avail = _q; \ 1.150 + } \ 1.151 + p = (void *)_p; \ 1.152 + PL_MAKE_MEM_UNDEFINED(p, nb); \ 1.153 + PL_ArenaCountAllocation(pool, nb); \ 1.154 + PR_END_MACRO 1.155 + 1.156 +#define PL_ARENA_GROW(p, pool, size, incr) \ 1.157 + PR_BEGIN_MACRO \ 1.158 + PLArena *_a = (pool)->current; \ 1.159 + PRUint32 _incr = PL_ARENA_ALIGN(pool, incr); \ 1.160 + PRUword _p = _a->avail; \ 1.161 + PRUword _q = _p + _incr; \ 1.162 + if (_p == (PRUword)(p) + PL_ARENA_ALIGN(pool, size) && \ 1.163 + _q <= _a->limit) { \ 1.164 + PL_MAKE_MEM_UNDEFINED((unsigned char *)(p) + size, incr); \ 1.165 + _a->avail = _q; \ 1.166 + PL_ArenaCountInplaceGrowth(pool, size, incr); \ 1.167 + } else { \ 1.168 + p = PL_ArenaGrow(pool, p, size, incr); \ 1.169 + } \ 1.170 + PL_ArenaCountGrowth(pool, size, incr); \ 1.171 + PR_END_MACRO 1.172 + 1.173 +#define PL_ARENA_MARK(pool) ((void *) (pool)->current->avail) 1.174 +#define PR_UPTRDIFF(p,q) ((PRUword)(p) - (PRUword)(q)) 1.175 + 1.176 +#define PL_CLEAR_UNUSED_PATTERN(a, pattern) \ 1.177 + PR_BEGIN_MACRO \ 1.178 + PR_ASSERT((a)->avail <= (a)->limit); \ 1.179 + PL_MAKE_MEM_UNDEFINED((void*)(a)->avail, (a)->limit - (a)->avail); \ 1.180 + memset((void*)(a)->avail, (pattern), (a)->limit - (a)->avail); \ 1.181 + PR_END_MACRO 1.182 +#ifdef DEBUG 1.183 +#define PL_FREE_PATTERN 0xDA 1.184 +#define PL_CLEAR_UNUSED(a) PL_CLEAR_UNUSED_PATTERN((a), PL_FREE_PATTERN) 1.185 +#define PL_CLEAR_ARENA(a) \ 1.186 + PR_BEGIN_MACRO \ 1.187 + PL_MAKE_MEM_UNDEFINED((void*)(a), (a)->limit - (PRUword)(a)); \ 1.188 + memset((void*)(a), PL_FREE_PATTERN, (a)->limit - (PRUword)(a)); \ 1.189 + PR_END_MACRO 1.190 +#else 1.191 +#define PL_CLEAR_UNUSED(a) 1.192 +#define PL_CLEAR_ARENA(a) 1.193 +#endif 1.194 + 1.195 +#define PL_ARENA_RELEASE(pool, mark) \ 1.196 + PR_BEGIN_MACRO \ 1.197 + char *_m = (char *)(mark); \ 1.198 + PLArena *_a = (pool)->current; \ 1.199 + if (PR_UPTRDIFF(_m, _a->base) <= PR_UPTRDIFF(_a->avail, _a->base)) { \ 1.200 + _a->avail = (PRUword)PL_ARENA_ALIGN(pool, _m); \ 1.201 + PL_CLEAR_UNUSED(_a); \ 1.202 + PL_MAKE_MEM_NOACCESS((void*)_a->avail, _a->limit - _a->avail); \ 1.203 + PL_ArenaCountRetract(pool, _m); \ 1.204 + } else { \ 1.205 + PL_ArenaRelease(pool, _m); \ 1.206 + } \ 1.207 + PL_ArenaCountRelease(pool, _m); \ 1.208 + PR_END_MACRO 1.209 + 1.210 +#ifdef PL_ARENAMETER 1.211 +#define PL_COUNT_ARENA(pool,op) ((pool)->stats.narenas op) 1.212 +#else 1.213 +#define PL_COUNT_ARENA(pool,op) 1.214 +#endif 1.215 + 1.216 +#define PL_ARENA_DESTROY(pool, a, pnext) \ 1.217 + PR_BEGIN_MACRO \ 1.218 + PL_COUNT_ARENA(pool,--); \ 1.219 + if ((pool)->current == (a)) (pool)->current = &(pool)->first; \ 1.220 + *(pnext) = (a)->next; \ 1.221 + PL_CLEAR_ARENA(a); \ 1.222 + free(a); \ 1.223 + (a) = 0; \ 1.224 + PR_END_MACRO 1.225 + 1.226 +#ifdef PL_ARENAMETER 1.227 + 1.228 +#include <stdio.h> 1.229 + 1.230 +PR_EXTERN(void) PL_ArenaCountAllocation(PLArenaPool *pool, PRUint32 nb); 1.231 + 1.232 +PR_EXTERN(void) PL_ArenaCountInplaceGrowth( 1.233 + PLArenaPool *pool, PRUint32 size, PRUint32 incr); 1.234 + 1.235 +PR_EXTERN(void) PL_ArenaCountGrowth( 1.236 + PLArenaPool *pool, PRUint32 size, PRUint32 incr); 1.237 + 1.238 +PR_EXTERN(void) PL_ArenaCountRelease(PLArenaPool *pool, char *mark); 1.239 + 1.240 +PR_EXTERN(void) PL_ArenaCountRetract(PLArenaPool *pool, char *mark); 1.241 + 1.242 +PR_EXTERN(void) PL_DumpArenaStats(FILE *fp); 1.243 + 1.244 +#else /* !PL_ARENAMETER */ 1.245 + 1.246 +#define PL_ArenaCountAllocation(ap, nb) /* nothing */ 1.247 +#define PL_ArenaCountInplaceGrowth(ap, size, incr) /* nothing */ 1.248 +#define PL_ArenaCountGrowth(ap, size, incr) /* nothing */ 1.249 +#define PL_ArenaCountRelease(ap, mark) /* nothing */ 1.250 +#define PL_ArenaCountRetract(ap, mark) /* nothing */ 1.251 + 1.252 +#endif /* !PL_ARENAMETER */ 1.253 + 1.254 +PR_END_EXTERN_C 1.255 + 1.256 +#endif /* plarena_h___ */