1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Communicator client code, released
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
43 * Lifetime-based fast allocation, inspired by much prior art, including
44 * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes"
45 * David R. Hanson, Software -- Practice and Experience, Vol. 20(1).
47 * Also supports LIFO allocation (JS_ARENA_MARK/JS_ARENA_RELEASE).
55 typedef struct JSArena JSArena
;
56 typedef struct JSArenaPool JSArenaPool
;
59 JSArena
*next
; /* next arena for this lifetime */
60 jsuword base
; /* aligned base address, follows this header */
61 jsuword limit
; /* one beyond last byte in arena */
62 jsuword avail
; /* points to next available byte */
66 typedef struct JSArenaStats JSArenaStats
;
69 JSArenaStats
*next
; /* next in arenaStats list */
70 char *name
; /* name for debugging */
71 uint32 narenas
; /* number of arenas in pool */
72 uint32 nallocs
; /* number of JS_ARENA_ALLOCATE() calls */
73 uint32 nmallocs
; /* number of malloc() calls */
74 uint32 ndeallocs
; /* number of lifetime deallocations */
75 uint32 ngrows
; /* number of JS_ARENA_GROW() calls */
76 uint32 ninplace
; /* number of in-place growths */
77 uint32 nreallocs
; /* number of arena grow extending reallocs */
78 uint32 nreleases
; /* number of JS_ARENA_RELEASE() calls */
79 uint32 nfastrels
; /* number of "fast path" releases */
80 size_t nbytes
; /* total bytes allocated */
81 size_t maxalloc
; /* maximum allocation size in bytes */
82 double variance
; /* size variance accumulator */
87 JSArena first
; /* first arena in pool list */
88 JSArena
*current
; /* arena from which to allocate space */
89 size_t arenasize
; /* net exact size of a new arena */
90 jsuword mask
; /* alignment mask (power-of-2 - 1) */
91 size_t *quotap
; /* pointer to the quota on pool allocation
92 size or null if pool is unlimited */
99 #define JS_INIT_NAMED_ARENA_POOL(pool, name, size, align, quotap) \
100 JS_InitArenaPool(pool, name, size, align, quotap)
102 #define JS_INIT_NAMED_ARENA_POOL(pool, name, size, align, quotap) \
103 JS_InitArenaPool(pool, size, align, quotap)
107 * If the including .c file uses only one power-of-2 alignment, it may define
108 * JS_ARENA_CONST_ALIGN_MASK to the alignment mask and save a few instructions
109 * per ALLOCATE and GROW.
111 #ifdef JS_ARENA_CONST_ALIGN_MASK
112 #define JS_ARENA_ALIGN(pool, n) (((jsuword)(n) + JS_ARENA_CONST_ALIGN_MASK) \
113 & ~(jsuword)JS_ARENA_CONST_ALIGN_MASK)
115 #define JS_INIT_ARENA_POOL(pool, name, size, quotap) \
116 JS_INIT_NAMED_ARENA_POOL(pool, name, size, JS_ARENA_CONST_ALIGN_MASK + 1, \
120 #define JS_ARENA_ALIGN(pool, n) (((jsuword)(n) + (pool)->mask) & ~(pool)->mask)
122 #define JS_INIT_ARENA_POOL(pool, name, size, align, quotap) \
123 JS_INIT_NAMED_ARENA_POOL(pool, name, size, align, quotap)
127 #define JS_ARENA_ALLOCATE(p, pool, nb) \
128 JS_ARENA_ALLOCATE_CAST(p, void *, pool, nb)
130 #define JS_ARENA_ALLOCATE_TYPE(p, type, pool) \
131 JS_ARENA_ALLOCATE_COMMON(p, type *, pool, sizeof(type), 0)
133 #define JS_ARENA_ALLOCATE_CAST(p, type, pool, nb) \
134 JS_ARENA_ALLOCATE_COMMON(p, type, pool, nb, _nb > _a->limit)
137 * NB: In JS_ARENA_ALLOCATE_CAST and JS_ARENA_GROW_CAST, always subtract _nb
138 * from a->limit rather than adding _nb to _p, to avoid overflowing a 32-bit
139 * address space (possible when running a 32-bit program on a 64-bit system
140 * where the kernel maps the heap up against the top of the 32-bit address
143 * Thanks to Juergen Kreileder <jk@blackdown.de>, who brought this up in
144 * https://bugzilla.mozilla.org/show_bug.cgi?id=279273.
146 #define JS_ARENA_ALLOCATE_COMMON(p, type, pool, nb, guard) \
148 JSArena *_a = (pool)->current; \
149 size_t _nb = JS_ARENA_ALIGN(pool, nb); \
150 jsuword _p = _a->avail; \
151 if ((guard) || _p > _a->limit - _nb) \
152 _p = (jsuword)JS_ArenaAllocate(pool, _nb); \
154 _a->avail = _p + _nb; \
156 JS_ArenaCountAllocation(pool, nb); \
159 #define JS_ARENA_GROW(p, pool, size, incr) \
160 JS_ARENA_GROW_CAST(p, void *, pool, size, incr)
162 #define JS_ARENA_GROW_CAST(p, type, pool, size, incr) \
164 JSArena *_a = (pool)->current; \
165 if (_a->avail == (jsuword)(p) + JS_ARENA_ALIGN(pool, size)) { \
166 size_t _nb = (size) + (incr); \
167 _nb = JS_ARENA_ALIGN(pool, _nb); \
168 if (_a->limit >= _nb && (jsuword)(p) <= _a->limit - _nb) { \
169 _a->avail = (jsuword)(p) + _nb; \
170 JS_ArenaCountInplaceGrowth(pool, size, incr); \
171 } else if ((jsuword)(p) == _a->base) { \
172 p = (type) JS_ArenaRealloc(pool, p, size, incr); \
174 p = (type) JS_ArenaGrow(pool, p, size, incr); \
177 p = (type) JS_ArenaGrow(pool, p, size, incr); \
179 JS_ArenaCountGrowth(pool, size, incr); \
182 #define JS_ARENA_MARK(pool) ((void *) (pool)->current->avail)
183 #define JS_UPTRDIFF(p,q) ((jsuword)(p) - (jsuword)(q))
186 * Check if the mark is inside arena's allocated area.
188 #define JS_ARENA_MARK_MATCH(a, mark) \
189 (JS_UPTRDIFF(mark, (a)->base) <= JS_UPTRDIFF((a)->avail, (a)->base))
192 #define JS_FREE_PATTERN 0xDA
193 #define JS_CLEAR_UNUSED(a) (JS_ASSERT((a)->avail <= (a)->limit), \
194 memset((void*)(a)->avail, JS_FREE_PATTERN, \
195 (a)->limit - (a)->avail))
196 #define JS_CLEAR_ARENA(a) memset((void*)(a), JS_FREE_PATTERN, \
197 (a)->limit - (jsuword)(a))
199 #define JS_CLEAR_UNUSED(a) /* nothing */
200 #define JS_CLEAR_ARENA(a) /* nothing */
203 #define JS_ARENA_RELEASE(pool, mark) \
205 char *_m = (char *)(mark); \
206 JSArena *_a = (pool)->current; \
207 if (_a != &(pool)->first && JS_ARENA_MARK_MATCH(_a, _m)) { \
208 _a->avail = (jsuword)JS_ARENA_ALIGN(pool, _m); \
209 JS_ASSERT(_a->avail <= _a->limit); \
210 JS_CLEAR_UNUSED(_a); \
211 JS_ArenaCountRetract(pool, _m); \
213 JS_ArenaRelease(pool, _m); \
215 JS_ArenaCountRelease(pool, _m); \
219 #define JS_COUNT_ARENA(pool,op) ((pool)->stats.narenas op)
221 #define JS_COUNT_ARENA(pool,op)
224 #define JS_ARENA_DESTROY(pool, a, pnext) \
226 JS_COUNT_ARENA(pool,--); \
227 if ((pool)->current == (a)) (pool)->current = &(pool)->first; \
228 *(pnext) = (a)->next; \
235 * Initialize an arena pool with a minimum size per arena of size bytes.
236 * Always call JS_SET_ARENA_METER_NAME before calling this or use
237 * JS_INIT_ARENA_POOL macro to provide a name for for debugging and metering.
239 extern JS_PUBLIC_API(void)
240 JS_INIT_NAMED_ARENA_POOL(JSArenaPool
*pool
, const char *name
, size_t size
,
241 size_t align
, size_t *quotap
);
244 * Free the arenas in pool. The user may continue to allocate from pool
245 * after calling this function. There is no need to call JS_InitArenaPool()
246 * again unless JS_FinishArenaPool(pool) has been called.
248 extern JS_PUBLIC_API(void)
249 JS_FreeArenaPool(JSArenaPool
*pool
);
252 * Free the arenas in pool and finish using it altogether.
254 extern JS_PUBLIC_API(void)
255 JS_FinishArenaPool(JSArenaPool
*pool
);
258 * Deprecated do-nothing function.
260 extern JS_PUBLIC_API(void)
261 JS_ArenaFinish(void);
264 * Deprecated do-nothing function.
266 extern JS_PUBLIC_API(void)
267 JS_ArenaShutDown(void);
270 * Friend functions used by the JS_ARENA_*() macros.
272 extern JS_PUBLIC_API(void *)
273 JS_ArenaAllocate(JSArenaPool
*pool
, size_t nb
);
275 extern JS_PUBLIC_API(void *)
276 JS_ArenaRealloc(JSArenaPool
*pool
, void *p
, size_t size
, size_t incr
);
278 extern JS_PUBLIC_API(void *)
279 JS_ArenaGrow(JSArenaPool
*pool
, void *p
, size_t size
, size_t incr
);
281 extern JS_PUBLIC_API(void)
282 JS_ArenaRelease(JSArenaPool
*pool
, char *mark
);
288 extern JS_PUBLIC_API(void)
289 JS_ArenaCountAllocation(JSArenaPool
*pool
, size_t nb
);
291 extern JS_PUBLIC_API(void)
292 JS_ArenaCountInplaceGrowth(JSArenaPool
*pool
, size_t size
, size_t incr
);
294 extern JS_PUBLIC_API(void)
295 JS_ArenaCountGrowth(JSArenaPool
*pool
, size_t size
, size_t incr
);
297 extern JS_PUBLIC_API(void)
298 JS_ArenaCountRelease(JSArenaPool
*pool
, char *mark
);
300 extern JS_PUBLIC_API(void)
301 JS_ArenaCountRetract(JSArenaPool
*pool
, char *mark
);
303 extern JS_PUBLIC_API(void)
304 JS_DumpArenaStats(FILE *fp
);
306 #else /* !JS_ARENAMETER */
308 #define JS_ArenaCountAllocation(ap, nb) /* nothing */
309 #define JS_ArenaCountInplaceGrowth(ap, size, incr) /* nothing */
310 #define JS_ArenaCountGrowth(ap, size, incr) /* nothing */
311 #define JS_ArenaCountRelease(ap, mark) /* nothing */
312 #define JS_ArenaCountRetract(ap, mark) /* nothing */
314 #endif /* !JS_ARENAMETER */
318 #endif /* jsarena_h___ */