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) */
97
* If the including .c file uses only one power-of-2 alignment, it may define
98
* JS_ARENA_CONST_ALIGN_MASK to the alignment mask and save a few instructions
99
* per ALLOCATE and GROW.
101
#ifdef JS_ARENA_CONST_ALIGN_MASK
102
#define JS_ARENA_ALIGN(pool, n) (((jsuword)(n) + JS_ARENA_CONST_ALIGN_MASK) \
103
& ~(jsuword)JS_ARENA_CONST_ALIGN_MASK)
105
#define JS_INIT_ARENA_POOL(pool, name, size) \
106
JS_InitArenaPool(pool, name, size, JS_ARENA_CONST_ALIGN_MASK + 1)
108
#define JS_ARENA_ALIGN(pool, n) (((jsuword)(n) + (pool)->mask) & ~(pool)->mask)
111
#define JS_ARENA_ALLOCATE(p, pool, nb) \
112
JS_ARENA_ALLOCATE_CAST(p, void *, pool, nb)
114
#define JS_ARENA_ALLOCATE_TYPE(p, type, pool) \
115
JS_ARENA_ALLOCATE_COMMON(p, type *, pool, sizeof(type), 0)
117
#define JS_ARENA_ALLOCATE_CAST(p, type, pool, nb) \
118
JS_ARENA_ALLOCATE_COMMON(p, type, pool, nb, _nb > _a->limit)
121
* NB: In JS_ARENA_ALLOCATE_CAST and JS_ARENA_GROW_CAST, always subtract _nb
122
* from a->limit rather than adding _nb to _p, to avoid overflowing a 32-bit
123
* address space (possible when running a 32-bit program on a 64-bit system
124
* where the kernel maps the heap up against the top of the 32-bit address
127
* Thanks to Juergen Kreileder <jk@blackdown.de>, who brought this up in
128
* https://bugzilla.mozilla.org/show_bug.cgi?id=279273.
130
#define JS_ARENA_ALLOCATE_COMMON(p, type, pool, nb, guard) \
132
JSArena *_a = (pool)->current; \
133
size_t _nb = JS_ARENA_ALIGN(pool, nb); \
134
jsuword _p = _a->avail; \
135
if ((guard) || _p > _a->limit - _nb) \
136
_p = (jsuword)JS_ArenaAllocate(pool, _nb); \
138
_a->avail = _p + _nb; \
140
JS_ArenaCountAllocation(pool, nb); \
143
#define JS_ARENA_GROW(p, pool, size, incr) \
144
JS_ARENA_GROW_CAST(p, void *, pool, size, incr)
146
#define JS_ARENA_GROW_CAST(p, type, pool, size, incr) \
148
JSArena *_a = (pool)->current; \
149
if (_a->avail == (jsuword)(p) + JS_ARENA_ALIGN(pool, size)) { \
150
size_t _nb = (size) + (incr); \
151
_nb = JS_ARENA_ALIGN(pool, _nb); \
152
if (_a->limit >= _nb && (jsuword)(p) <= _a->limit - _nb) { \
153
_a->avail = (jsuword)(p) + _nb; \
154
JS_ArenaCountInplaceGrowth(pool, size, incr); \
155
} else if ((jsuword)(p) == _a->base) { \
156
p = (type) JS_ArenaRealloc(pool, p, size, incr); \
158
p = (type) JS_ArenaGrow(pool, p, size, incr); \
161
p = (type) JS_ArenaGrow(pool, p, size, incr); \
163
JS_ArenaCountGrowth(pool, size, incr); \
166
#define JS_ARENA_MARK(pool) ((void *) (pool)->current->avail)
167
#define JS_UPTRDIFF(p,q) ((jsuword)(p) - (jsuword)(q))
170
#define JS_FREE_PATTERN 0xDA
171
#define JS_CLEAR_UNUSED(a) (JS_ASSERT((a)->avail <= (a)->limit), \
172
memset((void*)(a)->avail, JS_FREE_PATTERN, \
173
(a)->limit - (a)->avail))
174
#define JS_CLEAR_ARENA(a) memset((void*)(a), JS_FREE_PATTERN, \
175
(a)->limit - (jsuword)(a))
177
#define JS_CLEAR_UNUSED(a) /* nothing */
178
#define JS_CLEAR_ARENA(a) /* nothing */
181
#define JS_ARENA_RELEASE(pool, mark) \
183
char *_m = (char *)(mark); \
184
JSArena *_a = (pool)->current; \
185
if (_a != &(pool)->first && \
186
JS_UPTRDIFF(_m, _a->base) <= JS_UPTRDIFF(_a->avail, _a->base)) { \
187
_a->avail = (jsuword)JS_ARENA_ALIGN(pool, _m); \
188
JS_ASSERT(_a->avail <= _a->limit); \
189
JS_CLEAR_UNUSED(_a); \
190
JS_ArenaCountRetract(pool, _m); \
192
JS_ArenaRelease(pool, _m); \
194
JS_ArenaCountRelease(pool, _m); \
198
#define JS_COUNT_ARENA(pool,op) ((pool)->stats.narenas op)
200
#define JS_COUNT_ARENA(pool,op)
203
#define JS_ARENA_DESTROY(pool, a, pnext) \
205
JS_COUNT_ARENA(pool,--); \
206
if ((pool)->current == (a)) (pool)->current = &(pool)->first; \
207
*(pnext) = (a)->next; \
214
* Initialize an arena pool with the given name for debugging and metering,
215
* with a minimum size per arena of size bytes.
217
extern JS_PUBLIC_API(void)
218
JS_InitArenaPool(JSArenaPool *pool, const char *name, size_t size,
222
* Free the arenas in pool. The user may continue to allocate from pool
223
* after calling this function. There is no need to call JS_InitArenaPool()
224
* again unless JS_FinishArenaPool(pool) has been called.
226
extern JS_PUBLIC_API(void)
227
JS_FreeArenaPool(JSArenaPool *pool);
230
* Free the arenas in pool and finish using it altogether.
232
extern JS_PUBLIC_API(void)
233
JS_FinishArenaPool(JSArenaPool *pool);
236
* Deprecated do-nothing function.
238
extern JS_PUBLIC_API(void)
239
JS_ArenaFinish(void);
242
* Deprecated do-nothing function.
244
extern JS_PUBLIC_API(void)
245
JS_ArenaShutDown(void);
248
* Friend functions used by the JS_ARENA_*() macros.
250
extern JS_PUBLIC_API(void *)
251
JS_ArenaAllocate(JSArenaPool *pool, size_t nb);
253
extern JS_PUBLIC_API(void *)
254
JS_ArenaRealloc(JSArenaPool *pool, void *p, size_t size, size_t incr);
256
extern JS_PUBLIC_API(void *)
257
JS_ArenaGrow(JSArenaPool *pool, void *p, size_t size, size_t incr);
259
extern JS_PUBLIC_API(void)
260
JS_ArenaRelease(JSArenaPool *pool, char *mark);
263
* Function to be used directly when an allocation has likely grown to consume
264
* an entire JSArena, in which case the arena is returned to the malloc heap.
266
extern JS_PUBLIC_API(void)
267
JS_ArenaFreeAllocation(JSArenaPool *pool, void *p, size_t size);
273
extern JS_PUBLIC_API(void)
274
JS_ArenaCountAllocation(JSArenaPool *pool, size_t nb);
276
extern JS_PUBLIC_API(void)
277
JS_ArenaCountInplaceGrowth(JSArenaPool *pool, size_t size, size_t incr);
279
extern JS_PUBLIC_API(void)
280
JS_ArenaCountGrowth(JSArenaPool *pool, size_t size, size_t incr);
282
extern JS_PUBLIC_API(void)
283
JS_ArenaCountRelease(JSArenaPool *pool, char *mark);
285
extern JS_PUBLIC_API(void)
286
JS_ArenaCountRetract(JSArenaPool *pool, char *mark);
288
extern JS_PUBLIC_API(void)
289
JS_DumpArenaStats(FILE *fp);
291
#else /* !JS_ARENAMETER */
293
#define JS_ArenaCountAllocation(ap, nb) /* nothing */
294
#define JS_ArenaCountInplaceGrowth(ap, size, incr) /* nothing */
295
#define JS_ArenaCountGrowth(ap, size, incr) /* nothing */
296
#define JS_ArenaCountRelease(ap, mark) /* nothing */
297
#define JS_ArenaCountRetract(ap, mark) /* nothing */
299
#endif /* !JS_ARENAMETER */
303
#endif /* jsarena_h___ */