PageRenderTime 28ms CodeModel.GetById 11ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/jsarena.h

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C++ Header | 318 lines | 185 code | 47 blank | 86 comment | 18 complexity | 91c647bcc2878fbbba1fcfda277f2c8e MD5 | raw file
  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 ***** */
 39
 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"
 52
 53JS_BEGIN_EXTERN_C
 54
 55typedef struct JSArena JSArena;
 56typedef struct JSArenaPool JSArenaPool;
 57
 58struct 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};
 64
 65#ifdef JS_ARENAMETER
 66typedef struct JSArenaStats JSArenaStats;
 67
 68struct 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 */
 83};
 84#endif
 85
 86struct 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
 96};
 97
 98#ifdef JS_ARENAMETER
 99#define JS_INIT_NAMED_ARENA_POOL(pool, name, size, align, quotap)             \
100    JS_InitArenaPool(pool, name, size, align, quotap)
101#else
102#define JS_INIT_NAMED_ARENA_POOL(pool, name, size, align, quotap)             \
103    JS_InitArenaPool(pool, size, align, quotap)
104#endif
105
106/*
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.
110 */
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)
114
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, \
117                             quotap)
118
119#else
120#define JS_ARENA_ALIGN(pool, n) (((jsuword)(n) + (pool)->mask) & ~(pool)->mask)
121
122#define JS_INIT_ARENA_POOL(pool, name, size, align, quotap)                   \
123    JS_INIT_NAMED_ARENA_POOL(pool, name, size, align, quotap)
124
125#endif
126
127#define JS_ARENA_ALLOCATE(p, pool, nb)                                        \
128    JS_ARENA_ALLOCATE_CAST(p, void *, pool, nb)
129
130#define JS_ARENA_ALLOCATE_TYPE(p, type, pool)                                 \
131    JS_ARENA_ALLOCATE_COMMON(p, type *, pool, sizeof(type), 0)
132
133#define JS_ARENA_ALLOCATE_CAST(p, type, pool, nb)                             \
134    JS_ARENA_ALLOCATE_COMMON(p, type, pool, nb, _nb > _a->limit)
135
136/*
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
141 * space).
142 *
143 * Thanks to Juergen Kreileder <jk@blackdown.de>, who brought this up in
144 * https://bugzilla.mozilla.org/show_bug.cgi?id=279273.
145 */
146#define JS_ARENA_ALLOCATE_COMMON(p, type, pool, nb, guard)                    \
147    JS_BEGIN_MACRO                                                            \
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);                        \
153        else                                                                  \
154            _a->avail = _p + _nb;                                             \
155        p = (type) _p;                                                        \
156        JS_ArenaCountAllocation(pool, nb);                                    \
157    JS_END_MACRO
158
159#define JS_ARENA_GROW(p, pool, size, incr)                                    \
160    JS_ARENA_GROW_CAST(p, void *, pool, size, incr)
161
162#define JS_ARENA_GROW_CAST(p, type, pool, size, incr)                         \
163    JS_BEGIN_MACRO                                                            \
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);              \
173            } else {                                                          \
174                p = (type) JS_ArenaGrow(pool, p, size, incr);                 \
175            }                                                                 \
176        } else {                                                              \
177            p = (type) JS_ArenaGrow(pool, p, size, incr);                     \
178        }                                                                     \
179        JS_ArenaCountGrowth(pool, size, incr);                                \
180    JS_END_MACRO
181
182#define JS_ARENA_MARK(pool)     ((void *) (pool)->current->avail)
183#define JS_UPTRDIFF(p,q)        ((jsuword)(p) - (jsuword)(q))
184
185/*
186 * Check if the mark is inside arena's allocated area.
187 */
188#define JS_ARENA_MARK_MATCH(a, mark)                                          \
189    (JS_UPTRDIFF(mark, (a)->base) <= JS_UPTRDIFF((a)->avail, (a)->base))
190
191#ifdef DEBUG
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))
198#else
199#define JS_CLEAR_UNUSED(a)      /* nothing */
200#define JS_CLEAR_ARENA(a)       /* nothing */
201#endif
202
203#define JS_ARENA_RELEASE(pool, mark)                                          \
204    JS_BEGIN_MACRO                                                            \
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);                                   \
212        } else {                                                              \
213            JS_ArenaRelease(pool, _m);                                        \
214        }                                                                     \
215        JS_ArenaCountRelease(pool, _m);                                       \
216    JS_END_MACRO
217
218#ifdef JS_ARENAMETER
219#define JS_COUNT_ARENA(pool,op) ((pool)->stats.narenas op)
220#else
221#define JS_COUNT_ARENA(pool,op)
222#endif
223
224#define JS_ARENA_DESTROY(pool, a, pnext)                                      \
225    JS_BEGIN_MACRO                                                            \
226        JS_COUNT_ARENA(pool,--);                                              \
227        if ((pool)->current == (a)) (pool)->current = &(pool)->first;         \
228        *(pnext) = (a)->next;                                                 \
229        JS_CLEAR_ARENA(a);                                                    \
230        free(a);                                                              \
231        (a) = NULL;                                                           \
232    JS_END_MACRO
233
234/*
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.
238 */
239extern JS_PUBLIC_API(void)
240JS_INIT_NAMED_ARENA_POOL(JSArenaPool *pool, const char *name, size_t size,
241                         size_t align, size_t *quotap);
242
243/*
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.
247 */
248extern JS_PUBLIC_API(void)
249JS_FreeArenaPool(JSArenaPool *pool);
250
251/*
252 * Free the arenas in pool and finish using it altogether.
253 */
254extern JS_PUBLIC_API(void)
255JS_FinishArenaPool(JSArenaPool *pool);
256
257/*
258 * Deprecated do-nothing function.
259 */
260extern JS_PUBLIC_API(void)
261JS_ArenaFinish(void);
262
263/*
264 * Deprecated do-nothing function.
265 */
266extern JS_PUBLIC_API(void)
267JS_ArenaShutDown(void);
268
269/*
270 * Friend functions used by the JS_ARENA_*() macros.
271 */
272extern JS_PUBLIC_API(void *)
273JS_ArenaAllocate(JSArenaPool *pool, size_t nb);
274
275extern JS_PUBLIC_API(void *)
276JS_ArenaRealloc(JSArenaPool *pool, void *p, size_t size, size_t incr);
277
278extern JS_PUBLIC_API(void *)
279JS_ArenaGrow(JSArenaPool *pool, void *p, size_t size, size_t incr);
280
281extern JS_PUBLIC_API(void)
282JS_ArenaRelease(JSArenaPool *pool, char *mark);
283
284#ifdef JS_ARENAMETER
285
286#include <stdio.h>
287
288extern JS_PUBLIC_API(void)
289JS_ArenaCountAllocation(JSArenaPool *pool, size_t nb);
290
291extern JS_PUBLIC_API(void)
292JS_ArenaCountInplaceGrowth(JSArenaPool *pool, size_t size, size_t incr);
293
294extern JS_PUBLIC_API(void)
295JS_ArenaCountGrowth(JSArenaPool *pool, size_t size, size_t incr);
296
297extern JS_PUBLIC_API(void)
298JS_ArenaCountRelease(JSArenaPool *pool, char *mark);
299
300extern JS_PUBLIC_API(void)
301JS_ArenaCountRetract(JSArenaPool *pool, char *mark);
302
303extern JS_PUBLIC_API(void)
304JS_DumpArenaStats(FILE *fp);
305
306#else  /* !JS_ARENAMETER */
307
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 */
313
314#endif /* !JS_ARENAMETER */
315
316JS_END_EXTERN_C
317
318#endif /* jsarena_h___ */