Code

fix 1243587 and misc fixes
[inkscape.git] / src / dom / js / jsarena.h
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
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/
10  *
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.
15  *
16  * The Original Code is Mozilla Communicator client code, released
17  * March 31, 1998.
18  *
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.
23  *
24  * Contributor(s):
25  *
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.
37  *
38  * ***** END LICENSE BLOCK ***** */
40 #ifndef jsarena_h___
41 #define jsarena_h___
42 /*
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).
46  *
47  * Also supports LIFO allocation (JS_ARENA_MARK/JS_ARENA_RELEASE).
48  */
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 */
63 };
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      nreclaims;      /* number of reclaims from freeArenas */
74     uint32      nmallocs;       /* number of malloc() calls */
75     uint32      ndeallocs;      /* number of lifetime deallocations */
76     uint32      ngrows;         /* number of JS_ARENA_GROW() calls */
77     uint32      ninplace;       /* number of in-place growths */
78     uint32      nreallocs;      /* number of arena grow extending reallocs */
79     uint32      nreleases;      /* number of JS_ARENA_RELEASE() calls */
80     uint32      nfastrels;      /* number of "fast path" releases */
81     size_t      nbytes;         /* total bytes allocated */
82     size_t      maxalloc;       /* maximum allocation size in bytes */
83     double      variance;       /* size variance accumulator */
84 };
85 #endif
87 struct JSArenaPool {
88     JSArena     first;          /* first arena in pool list */
89     JSArena     *current;       /* arena from which to allocate space */
90     size_t      arenasize;      /* net exact size of a new arena */
91     jsuword     mask;           /* alignment mask (power-of-2 - 1) */
92 #ifdef JS_ARENAMETER
93     JSArenaStats stats;
94 #endif
95 };
97 /*
98  * If the including .c file uses only one power-of-2 alignment, it may define
99  * JS_ARENA_CONST_ALIGN_MASK to the alignment mask and save a few instructions
100  * per ALLOCATE and GROW.
101  */
102 #ifdef JS_ARENA_CONST_ALIGN_MASK
103 #define JS_ARENA_ALIGN(pool, n) (((jsuword)(n) + JS_ARENA_CONST_ALIGN_MASK)   \
104                                  & ~(jsuword)JS_ARENA_CONST_ALIGN_MASK)
106 #define JS_INIT_ARENA_POOL(pool, name, size) \
107         JS_InitArenaPool(pool, name, size, JS_ARENA_CONST_ALIGN_MASK + 1)
108 #else
109 #define JS_ARENA_ALIGN(pool, n) (((jsuword)(n) + (pool)->mask) & ~(pool)->mask)
110 #endif
112 #define JS_ARENA_ALLOCATE(p, pool, nb)                                        \
113     JS_ARENA_ALLOCATE_CAST(p, void *, pool, nb)
115 #define JS_ARENA_ALLOCATE_TYPE(p, type, pool)                                 \
116     JS_ARENA_ALLOCATE_COMMON(p, type *, pool, sizeof(type), 0)
118 #define JS_ARENA_ALLOCATE_CAST(p, type, pool, nb)                             \
119     JS_ARENA_ALLOCATE_COMMON(p, type, pool, nb, _nb > _a->limit)
121 /*
122  * NB: In JS_ARENA_ALLOCATE_CAST and JS_ARENA_GROW_CAST, always subtract _nb
123  * from a->limit rather than adding _nb to _p, to avoid overflowing a 32-bit
124  * address space (possible when running a 32-bit program on a 64-bit system
125  * where the kernel maps the heap up against the top of the 32-bit address
126  * space).
127  *
128  * Thanks to Juergen Kreileder <jk@blackdown.de>, who brought this up in
129  * https://bugzilla.mozilla.org/show_bug.cgi?id=279273.
130  */
131 #define JS_ARENA_ALLOCATE_COMMON(p, type, pool, nb, guard)                    \
132     JS_BEGIN_MACRO                                                            \
133         JSArena *_a = (pool)->current;                                        \
134         size_t _nb = JS_ARENA_ALIGN(pool, nb);                                \
135         jsuword _p = _a->avail;                                               \
136         if ((guard) || _p > _a->limit - _nb)                                  \
137             _p = (jsuword)JS_ArenaAllocate(pool, _nb);                        \
138         else                                                                  \
139             _a->avail = _p + _nb;                                             \
140         p = (type) _p;                                                        \
141         JS_ArenaCountAllocation(pool, nb);                                    \
142     JS_END_MACRO
144 #define JS_ARENA_GROW(p, pool, size, incr)                                    \
145     JS_ARENA_GROW_CAST(p, void *, pool, size, incr)
147 #define JS_ARENA_GROW_CAST(p, type, pool, size, incr)                         \
148     JS_BEGIN_MACRO                                                            \
149         JSArena *_a = (pool)->current;                                        \
150         if (_a->avail == (jsuword)(p) + JS_ARENA_ALIGN(pool, size)) {         \
151             size_t _nb = (size) + (incr);                                     \
152             _nb = JS_ARENA_ALIGN(pool, _nb);                                  \
153             if (_a->limit >= _nb && (jsuword)(p) <= _a->limit - _nb) {        \
154                 _a->avail = (jsuword)(p) + _nb;                               \
155                 JS_ArenaCountInplaceGrowth(pool, size, incr);                 \
156             } else if ((jsuword)(p) == _a->base) {                            \
157                 p = (type) JS_ArenaRealloc(pool, p, size, incr);              \
158             } else {                                                          \
159                 p = (type) JS_ArenaGrow(pool, p, size, incr);                 \
160             }                                                                 \
161         } else {                                                              \
162             p = (type) JS_ArenaGrow(pool, p, size, incr);                     \
163         }                                                                     \
164         JS_ArenaCountGrowth(pool, size, incr);                                \
165     JS_END_MACRO
167 #define JS_ARENA_MARK(pool)     ((void *) (pool)->current->avail)
168 #define JS_UPTRDIFF(p,q)        ((jsuword)(p) - (jsuword)(q))
170 #ifdef DEBUG
171 #define JS_FREE_PATTERN         0xDA
172 #define JS_CLEAR_UNUSED(a)      (JS_ASSERT((a)->avail <= (a)->limit),         \
173                                  memset((void*)(a)->avail, JS_FREE_PATTERN,   \
174                                         (a)->limit - (a)->avail))
175 #define JS_CLEAR_ARENA(a)       memset((void*)(a), JS_FREE_PATTERN,           \
176                                        (a)->limit - (jsuword)(a))
177 #else
178 #define JS_CLEAR_UNUSED(a)      /* nothing */
179 #define JS_CLEAR_ARENA(a)       /* nothing */
180 #endif
182 #define JS_ARENA_RELEASE(pool, mark)                                          \
183     JS_BEGIN_MACRO                                                            \
184         char *_m = (char *)(mark);                                            \
185         JSArena *_a = (pool)->current;                                        \
186         if (_a != &(pool)->first &&                                           \
187             JS_UPTRDIFF(_m, _a->base) <= JS_UPTRDIFF(_a->avail, _a->base)) {  \
188             _a->avail = (jsuword)JS_ARENA_ALIGN(pool, _m);                    \
189             JS_ASSERT(_a->avail <= _a->limit);                                \
190             JS_CLEAR_UNUSED(_a);                                              \
191             JS_ArenaCountRetract(pool, _m);                                   \
192         } else {                                                              \
193             JS_ArenaRelease(pool, _m);                                        \
194         }                                                                     \
195         JS_ArenaCountRelease(pool, _m);                                       \
196     JS_END_MACRO
198 #ifdef JS_ARENAMETER
199 #define JS_COUNT_ARENA(pool,op) ((pool)->stats.narenas op)
200 #else
201 #define JS_COUNT_ARENA(pool,op)
202 #endif
204 #define JS_ARENA_DESTROY(pool, a, pnext)                                      \
205     JS_BEGIN_MACRO                                                            \
206         JS_COUNT_ARENA(pool,--);                                              \
207         if ((pool)->current == (a)) (pool)->current = &(pool)->first;         \
208         *(pnext) = (a)->next;                                                 \
209         JS_CLEAR_ARENA(a);                                                    \
210         free(a);                                                              \
211         (a) = NULL;                                                           \
212     JS_END_MACRO
214 /*
215  * Initialize an arena pool with the given name for debugging and metering,
216  * with a minimum size per arena of size bytes.
217  */
218 extern JS_PUBLIC_API(void)
219 JS_InitArenaPool(JSArenaPool *pool, const char *name, size_t size,
220                  size_t align);
222 /*
223  * Free the arenas in pool.  The user may continue to allocate from pool
224  * after calling this function.  There is no need to call JS_InitArenaPool()
225  * again unless JS_FinishArenaPool(pool) has been called.
226  */
227 extern JS_PUBLIC_API(void)
228 JS_FreeArenaPool(JSArenaPool *pool);
230 /*
231  * Free the arenas in pool and finish using it altogether.
232  */
233 extern JS_PUBLIC_API(void)
234 JS_FinishArenaPool(JSArenaPool *pool);
236 /*
237  * Finish using arenas, freeing all memory associated with them except for
238  * any locks needed for thread safety.
239  */
240 extern JS_PUBLIC_API(void)
241 JS_ArenaFinish(void);
243 /*
244  * Free any locks or other memory needed for thread safety, just before
245  * shutting down.  At that point, we must be called by a single thread.
246  *
247  * After shutting down, the next thread to call JS_InitArenaPool must not
248  * race with any other thread.  Once a pool has been initialized, threads
249  * may safely call jsarena.c functions on thread-local pools.  The upshot
250  * is that pools are per-thread, but the underlying global freelist is
251  * thread-safe, provided that both the first pool initialization and the
252  * shut-down call are single-threaded.
253  */
254 extern JS_PUBLIC_API(void)
255 JS_ArenaShutDown(void);
257 /*
258  * Friend functions used by the JS_ARENA_*() macros.
259  */
260 extern JS_PUBLIC_API(void *)
261 JS_ArenaAllocate(JSArenaPool *pool, size_t nb);
263 extern JS_PUBLIC_API(void *)
264 JS_ArenaRealloc(JSArenaPool *pool, void *p, size_t size, size_t incr);
266 extern JS_PUBLIC_API(void *)
267 JS_ArenaGrow(JSArenaPool *pool, void *p, size_t size, size_t incr);
269 extern JS_PUBLIC_API(void)
270 JS_ArenaRelease(JSArenaPool *pool, char *mark);
272 /*
273  * Function to be used directly when an allocation has likely grown to consume
274  * an entire JSArena, in which case the arena is returned to the malloc heap.
275  */
276 extern JS_PUBLIC_API(void)
277 JS_ArenaFreeAllocation(JSArenaPool *pool, void *p, size_t size);
279 #ifdef JS_ARENAMETER
281 #include <stdio.h>
283 extern JS_PUBLIC_API(void)
284 JS_ArenaCountAllocation(JSArenaPool *pool, size_t nb);
286 extern JS_PUBLIC_API(void)
287 JS_ArenaCountInplaceGrowth(JSArenaPool *pool, size_t size, size_t incr);
289 extern JS_PUBLIC_API(void)
290 JS_ArenaCountGrowth(JSArenaPool *pool, size_t size, size_t incr);
292 extern JS_PUBLIC_API(void)
293 JS_ArenaCountRelease(JSArenaPool *pool, char *mark);
295 extern JS_PUBLIC_API(void)
296 JS_ArenaCountRetract(JSArenaPool *pool, char *mark);
298 extern JS_PUBLIC_API(void)
299 JS_DumpArenaStats(FILE *fp);
301 #else  /* !JS_ARENAMETER */
303 #define JS_ArenaCountAllocation(ap, nb)                 /* nothing */
304 #define JS_ArenaCountInplaceGrowth(ap, size, incr)      /* nothing */
305 #define JS_ArenaCountGrowth(ap, size, incr)             /* nothing */
306 #define JS_ArenaCountRelease(ap, mark)                  /* nothing */
307 #define JS_ArenaCountRetract(ap, mark)                  /* nothing */
309 #endif /* !JS_ARENAMETER */
311 JS_END_EXTERN_C
313 #endif /* jsarena_h___ */