Back to index

lightning-sunbird  0.9+nobinonly
plarena.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  * 
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  * 
00017  * The Initial Developer of the Original Code is Netscape
00018  * Communications Corporation.  Portions created by Netscape are 
00019  * Copyright (C) 1998-2000 Netscape Communications Corporation.  All
00020  * Rights Reserved.
00021  * 
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK *****
00037  */
00038 
00039 #ifndef plarena_h___
00040 #define plarena_h___
00041 /*
00042  * Lifetime-based fast allocation, inspired by much prior art, including
00043  * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes"
00044  * David R. Hanson, Software -- Practice and Experience, Vol. 20(1).
00045  *
00046  * Also supports LIFO allocation (PL_ARENA_MARK/PL_ARENA_RELEASE).
00047  */
00048 #include "prtypes.h"
00049 #include "plarenas.h"
00050 
00051 PR_BEGIN_EXTERN_C
00052 
00053 typedef struct PLArena          PLArena;
00054 
00055 struct PLArena {
00056     PLArena     *next;          /* next arena for this lifetime */
00057     PRUword     base;           /* aligned base address, follows this header */
00058     PRUword     limit;          /* one beyond last byte in arena */
00059     PRUword     avail;          /* points to next available byte */
00060 };
00061 
00062 #ifdef PL_ARENAMETER
00063 typedef struct PLArenaStats PLArenaStats;
00064 
00065 struct PLArenaStats {
00066     PLArenaStats  *next;        /* next in arenaStats list */
00067     char          *name;        /* name for debugging */
00068     PRUint32      narenas;      /* number of arenas in pool */
00069     PRUint32      nallocs;      /* number of PL_ARENA_ALLOCATE() calls */
00070     PRUint32      nreclaims;    /* number of reclaims from freeArenas */
00071     PRUint32      nmallocs;     /* number of malloc() calls */
00072     PRUint32      ndeallocs;    /* number of lifetime deallocations */
00073     PRUint32      ngrows;       /* number of PL_ARENA_GROW() calls */
00074     PRUint32      ninplace;     /* number of in-place growths */
00075     PRUint32      nreleases;    /* number of PL_ARENA_RELEASE() calls */
00076     PRUint32      nfastrels;    /* number of "fast path" releases */
00077     PRUint32      nbytes;       /* total bytes allocated */
00078     PRUint32      maxalloc;     /* maximum allocation size in bytes */
00079     PRFloat64     variance;     /* size variance accumulator */
00080 };
00081 #endif
00082 
00083 struct PLArenaPool {
00084     PLArena     first;          /* first arena in pool list */
00085     PLArena     *current;       /* arena from which to allocate space */
00086     PRUint32    arenasize;      /* net exact size of a new arena */
00087     PRUword     mask;           /* alignment mask (power-of-2 - 1) */
00088 #ifdef PL_ARENAMETER
00089     PLArenaStats stats;
00090 #endif
00091 };
00092 
00093 /*
00094  * If the including .c file uses only one power-of-2 alignment, it may define
00095  * PL_ARENA_CONST_ALIGN_MASK to the alignment mask and save a few instructions
00096  * per ALLOCATE and GROW.
00097  */
00098 #ifdef PL_ARENA_CONST_ALIGN_MASK
00099 #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + PL_ARENA_CONST_ALIGN_MASK) \
00100                                 & ~PL_ARENA_CONST_ALIGN_MASK)
00101 
00102 #define PL_INIT_ARENA_POOL(pool, name, size) \
00103         PL_InitArenaPool(pool, name, size, PL_ARENA_CONST_ALIGN_MASK + 1)
00104 #else
00105 #define PL_ARENA_ALIGN(pool, n) (((PRUword)(n) + (pool)->mask) & ~(pool)->mask)
00106 #endif
00107 
00108 #define PL_ARENA_ALLOCATE(p, pool, nb) \
00109     PR_BEGIN_MACRO \
00110         PLArena *_a = (pool)->current; \
00111         PRUint32 _nb = PL_ARENA_ALIGN(pool, nb); \
00112         PRUword _p = _a->avail; \
00113         PRUword _q = _p + _nb; \
00114         if (_q > _a->limit) \
00115             _p = (PRUword)PL_ArenaAllocate(pool, _nb); \
00116         else \
00117             _a->avail = _q; \
00118         p = (void *)_p; \
00119         PL_ArenaCountAllocation(pool, nb); \
00120     PR_END_MACRO
00121 
00122 #define PL_ARENA_GROW(p, pool, size, incr) \
00123     PR_BEGIN_MACRO \
00124         PLArena *_a = (pool)->current; \
00125         PRUint32 _incr = PL_ARENA_ALIGN(pool, incr); \
00126         PRUword _p = _a->avail; \
00127         PRUword _q = _p + _incr; \
00128         if (_p == (PRUword)(p) + PL_ARENA_ALIGN(pool, size) && \
00129             _q <= _a->limit) { \
00130             _a->avail = _q; \
00131             PL_ArenaCountInplaceGrowth(pool, size, incr); \
00132         } else { \
00133             p = PL_ArenaGrow(pool, p, size, incr); \
00134         } \
00135         PL_ArenaCountGrowth(pool, size, incr); \
00136     PR_END_MACRO
00137 
00138 #define PL_ARENA_MARK(pool) ((void *) (pool)->current->avail)
00139 #define PR_UPTRDIFF(p,q) ((PRUword)(p) - (PRUword)(q))
00140 
00141 #ifdef DEBUG
00142 #define PL_FREE_PATTERN 0xDA
00143 #define PL_CLEAR_UNUSED(a) (PR_ASSERT((a)->avail <= (a)->limit), \
00144                            memset((void*)(a)->avail, PL_FREE_PATTERN, \
00145                            (a)->limit - (a)->avail))
00146 #define PL_CLEAR_ARENA(a)  memset((void*)(a), PL_FREE_PATTERN, \
00147                            (a)->limit - (PRUword)(a))
00148 #else
00149 #define PL_CLEAR_UNUSED(a)
00150 #define PL_CLEAR_ARENA(a)
00151 #endif
00152 
00153 #define PL_ARENA_RELEASE(pool, mark) \
00154     PR_BEGIN_MACRO \
00155         char *_m = (char *)(mark); \
00156         PLArena *_a = (pool)->current; \
00157         if (PR_UPTRDIFF(_m, _a->base) <= PR_UPTRDIFF(_a->avail, _a->base)) { \
00158             _a->avail = (PRUword)PL_ARENA_ALIGN(pool, _m); \
00159             PL_CLEAR_UNUSED(_a); \
00160             PL_ArenaCountRetract(pool, _m); \
00161         } else { \
00162             PL_ArenaRelease(pool, _m); \
00163         } \
00164         PL_ArenaCountRelease(pool, _m); \
00165     PR_END_MACRO
00166 
00167 #ifdef PL_ARENAMETER
00168 #define PL_COUNT_ARENA(pool,op) ((pool)->stats.narenas op)
00169 #else
00170 #define PL_COUNT_ARENA(pool,op)
00171 #endif
00172 
00173 #define PL_ARENA_DESTROY(pool, a, pnext) \
00174     PR_BEGIN_MACRO \
00175         PL_COUNT_ARENA(pool,--); \
00176         if ((pool)->current == (a)) (pool)->current = &(pool)->first; \
00177         *(pnext) = (a)->next; \
00178         PL_CLEAR_ARENA(a); \
00179         free(a); \
00180         (a) = 0; \
00181     PR_END_MACRO
00182 
00183 #ifdef PL_ARENAMETER
00184 
00185 #include <stdio.h>
00186 
00187 PR_EXTERN(void) PL_ArenaCountAllocation(PLArenaPool *pool, PRUint32 nb);
00188 
00189 PR_EXTERN(void) PL_ArenaCountInplaceGrowth(
00190     PLArenaPool *pool, PRUint32 size, PRUint32 incr);
00191 
00192 PR_EXTERN(void) PL_ArenaCountGrowth(
00193     PLArenaPool *pool, PRUint32 size, PRUint32 incr);
00194 
00195 PR_EXTERN(void) PL_ArenaCountRelease(PLArenaPool *pool, char *mark);
00196 
00197 PR_EXTERN(void) PL_ArenaCountRetract(PLArenaPool *pool, char *mark);
00198 
00199 PR_EXTERN(void) PL_DumpArenaStats(FILE *fp);
00200 
00201 #else  /* !PL_ARENAMETER */
00202 
00203 #define PL_ArenaCountAllocation(ap, nb)                 /* nothing */
00204 #define PL_ArenaCountInplaceGrowth(ap, size, incr)      /* nothing */
00205 #define PL_ArenaCountGrowth(ap, size, incr)             /* nothing */
00206 #define PL_ArenaCountRelease(ap, mark)                  /* nothing */
00207 #define PL_ArenaCountRetract(ap, mark)                  /* nothing */
00208 
00209 #endif /* !PL_ARENAMETER */
00210 
00211 PR_END_EXTERN_C
00212 
00213 #endif /* plarena_h___ */