Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / js / src / jsarena.h
blob6eeb04a8cfa936e6c4c9fb64fea39ea4adb383af
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
14 * License.
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
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.
24 * Contributor(s):
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 ***** */
40 #ifndef jsarena_h___
41 #define jsarena_h___
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).
49 #include <stdlib.h>
50 #include "jstypes.h"
51 #include "jscompat.h"
53 JS_BEGIN_EXTERN_C
55 typedef struct JSArena JSArena;
56 typedef struct JSArenaPool JSArenaPool;
58 struct JSArena {
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 */
65 #ifdef JS_ARENAMETER
66 typedef struct JSArenaStats JSArenaStats;
68 struct 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 */
84 #endif
86 struct JSArenaPool {
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 */
93 #ifdef JS_ARENAMETER
94 JSArenaStats stats;
95 #endif
98 #define JS_ARENA_ALIGN(pool, n) (((jsuword)(n) + (pool)->mask) & ~(pool)->mask)
100 #define JS_ARENA_ALLOCATE(p, pool, nb) \
101 JS_ARENA_ALLOCATE_CAST(p, void *, pool, nb)
103 #define JS_ARENA_ALLOCATE_TYPE(p, type, pool) \
104 JS_ARENA_ALLOCATE_COMMON(p, type *, pool, sizeof(type), 0)
106 #define JS_ARENA_ALLOCATE_CAST(p, type, pool, nb) \
107 JS_ARENA_ALLOCATE_COMMON(p, type, pool, nb, _nb > _a->limit)
110 * NB: In JS_ARENA_ALLOCATE_CAST and JS_ARENA_GROW_CAST, always subtract _nb
111 * from a->limit rather than adding _nb to _p, to avoid overflowing a 32-bit
112 * address space (possible when running a 32-bit program on a 64-bit system
113 * where the kernel maps the heap up against the top of the 32-bit address
114 * space).
116 * Thanks to Juergen Kreileder <jk@blackdown.de>, who brought this up in
117 * https://bugzilla.mozilla.org/show_bug.cgi?id=279273.
119 #define JS_ARENA_ALLOCATE_COMMON(p, type, pool, nb, guard) \
120 JS_BEGIN_MACRO \
121 JSArena *_a = (pool)->current; \
122 size_t _nb = JS_ARENA_ALIGN(pool, nb); \
123 jsuword _p = _a->avail; \
124 if ((guard) || _p > _a->limit - _nb) \
125 _p = (jsuword)JS_ArenaAllocate(pool, _nb); \
126 else \
127 _a->avail = _p + _nb; \
128 p = (type) _p; \
129 STATIC_ASSUME(!p || ubound((char *)p) >= nb) \
130 JS_ArenaCountAllocation(pool, nb); \
131 JS_END_MACRO
133 #define JS_ARENA_GROW(p, pool, size, incr) \
134 JS_ARENA_GROW_CAST(p, void *, pool, size, incr)
136 #define JS_ARENA_GROW_CAST(p, type, pool, size, incr) \
137 JS_BEGIN_MACRO \
138 JSArena *_a = (pool)->current; \
139 if (_a->avail == (jsuword)(p) + JS_ARENA_ALIGN(pool, size)) { \
140 size_t _nb = (size) + (incr); \
141 _nb = JS_ARENA_ALIGN(pool, _nb); \
142 if (_a->limit >= _nb && (jsuword)(p) <= _a->limit - _nb) { \
143 _a->avail = (jsuword)(p) + _nb; \
144 JS_ArenaCountInplaceGrowth(pool, size, incr); \
145 } else if ((jsuword)(p) == _a->base) { \
146 p = (type) JS_ArenaRealloc(pool, p, size, incr); \
147 } else { \
148 p = (type) JS_ArenaGrow(pool, p, size, incr); \
150 } else { \
151 p = (type) JS_ArenaGrow(pool, p, size, incr); \
153 STATIC_ASSUME(!p || ubound((char *)p) >= size + incr); \
154 JS_ArenaCountGrowth(pool, size, incr); \
155 JS_END_MACRO
157 #define JS_ARENA_MARK(pool) ((void *) (pool)->current->avail)
158 #define JS_UPTRDIFF(p,q) ((jsuword)(p) - (jsuword)(q))
161 * Check if the mark is inside arena's allocated area.
163 #define JS_ARENA_MARK_MATCH(a, mark) \
164 (JS_UPTRDIFF(mark, (a)->base) <= JS_UPTRDIFF((a)->avail, (a)->base))
166 #ifdef DEBUG
167 #define JS_FREE_PATTERN 0xDA
168 #define JS_CLEAR_UNUSED(a) (JS_ASSERT((a)->avail <= (a)->limit), \
169 memset((void*)(a)->avail, JS_FREE_PATTERN, \
170 (a)->limit - (a)->avail))
171 #define JS_CLEAR_ARENA(a) memset((void*)(a), JS_FREE_PATTERN, \
172 (a)->limit - (jsuword)(a))
173 #else
174 #define JS_CLEAR_UNUSED(a) /* nothing */
175 #define JS_CLEAR_ARENA(a) /* nothing */
176 #endif
178 #define JS_ARENA_RELEASE(pool, mark) \
179 JS_BEGIN_MACRO \
180 char *_m = (char *)(mark); \
181 JSArena *_a = (pool)->current; \
182 if (_a != &(pool)->first && JS_ARENA_MARK_MATCH(_a, _m)) { \
183 _a->avail = (jsuword)JS_ARENA_ALIGN(pool, _m); \
184 JS_ASSERT(_a->avail <= _a->limit); \
185 JS_CLEAR_UNUSED(_a); \
186 JS_ArenaCountRetract(pool, _m); \
187 } else { \
188 JS_ArenaRelease(pool, _m); \
190 JS_ArenaCountRelease(pool, _m); \
191 JS_END_MACRO
193 #ifdef JS_ARENAMETER
194 #define JS_COUNT_ARENA(pool,op) ((pool)->stats.narenas op)
195 #else
196 #define JS_COUNT_ARENA(pool,op)
197 #endif
199 #define JS_ARENA_DESTROY(pool, a, pnext) \
200 JS_BEGIN_MACRO \
201 JS_COUNT_ARENA(pool,--); \
202 if ((pool)->current == (a)) (pool)->current = &(pool)->first; \
203 *(pnext) = (a)->next; \
204 JS_CLEAR_ARENA(a); \
205 js_free(a); \
206 (a) = NULL; \
207 JS_END_MACRO
210 * Initialize an arena pool with a minimum size per arena of size bytes.
212 extern JS_PUBLIC_API(void)
213 JS_InitArenaPool(JSArenaPool *pool, const char *name, size_t size,
214 size_t align, size_t *quotap);
217 * Free the arenas in pool. The user may continue to allocate from pool
218 * after calling this function. There is no need to call JS_InitArenaPool()
219 * again unless JS_FinishArenaPool(pool) has been called.
221 extern JS_PUBLIC_API(void)
222 JS_FreeArenaPool(JSArenaPool *pool);
225 * Free the arenas in pool and finish using it altogether.
227 extern JS_PUBLIC_API(void)
228 JS_FinishArenaPool(JSArenaPool *pool);
231 * Deprecated do-nothing function.
233 extern JS_PUBLIC_API(void)
234 JS_ArenaFinish(void);
237 * Deprecated do-nothing function.
239 extern JS_PUBLIC_API(void)
240 JS_ArenaShutDown(void);
243 * Friend functions used by the JS_ARENA_*() macros.
245 extern JS_PUBLIC_API(void *)
246 JS_ArenaAllocate(JSArenaPool *pool, size_t nb);
248 extern JS_PUBLIC_API(void *)
249 JS_ArenaRealloc(JSArenaPool *pool, void *p, size_t size, size_t incr);
251 extern JS_PUBLIC_API(void *)
252 JS_ArenaGrow(JSArenaPool *pool, void *p, size_t size, size_t incr);
254 extern JS_PUBLIC_API(void)
255 JS_ArenaRelease(JSArenaPool *pool, char *mark);
257 #ifdef JS_ARENAMETER
259 #include <stdio.h>
261 extern JS_PUBLIC_API(void)
262 JS_ArenaCountAllocation(JSArenaPool *pool, size_t nb);
264 extern JS_PUBLIC_API(void)
265 JS_ArenaCountInplaceGrowth(JSArenaPool *pool, size_t size, size_t incr);
267 extern JS_PUBLIC_API(void)
268 JS_ArenaCountGrowth(JSArenaPool *pool, size_t size, size_t incr);
270 extern JS_PUBLIC_API(void)
271 JS_ArenaCountRelease(JSArenaPool *pool, char *mark);
273 extern JS_PUBLIC_API(void)
274 JS_ArenaCountRetract(JSArenaPool *pool, char *mark);
276 extern JS_PUBLIC_API(void)
277 JS_DumpArenaStats(FILE *fp);
279 #else /* !JS_ARENAMETER */
281 #define JS_ArenaCountAllocation(ap, nb) /* nothing */
282 #define JS_ArenaCountInplaceGrowth(ap, size, incr) /* nothing */
283 #define JS_ArenaCountGrowth(ap, size, incr) /* nothing */
284 #define JS_ArenaCountRelease(ap, mark) /* nothing */
285 #define JS_ArenaCountRetract(ap, mark) /* nothing */
287 #endif /* !JS_ARENAMETER */
289 JS_END_EXTERN_C
291 #endif /* jsarena_h___ */