pleyo.com

/src/trunk2/BAL/Interfaces/Arena.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998-2000 Netscape Communications Corporation.
00003  * Copyright (C) 2003-6 Apple Computer
00004  *
00005  * Other contributors:
00006  *   Nick Blievers <nickb@adacel.com.au>
00007  *   Jeff Hostetler <jeff@nerdone.com>
00008  *   Tom Rini <trini@kernel.crashing.org>
00009  *   Raffaele Sena <raff@netwinder.org>
00010  *
00011  * This library is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU Lesser General Public
00013  * License as published by the Free Software Foundation; either
00014  * version 2.1 of the License, or (at your option) any later version.
00015  *
00016  * This library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with this library; if not, write to the Free Software
00023  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00024  *
00025  * Alternatively, the contents of this file may be used under the terms
00026  * of either the Mozilla Public License Version 1.1, found at
00027  * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
00028  * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
00029  * (the "GPL"), in which case the provisions of the MPL or the GPL are
00030  * applicable instead of those above.  If you wish to allow use of your
00031  * version of this file only under the terms of one of those two
00032  * licenses (the MPL or the GPL) and not to allow others to use your
00033  * version of this file under the LGPL, indicate your decision by
00034  * deletingthe provisions above and replace them with the notice and
00035  * other provisions required by the MPL or the GPL, as the case may be.
00036  * If you do not delete the provisions above, a recipient may use your
00037  * version of this file under any of the LGPL, the MPL or the GPL.
00038  */
00039 
00040 #ifndef Arena_h
00041 #define Arena_h
00042 
00043 #define ARENA_ALIGN_MASK 3
00044 
00045 namespace WebCore {
00046 
00047 typedef unsigned long uword;
00048 
00049 struct Arena {
00050     Arena* next;        // next arena
00051     uword base;         // aligned base address
00052     uword limit;        // end of arena (1+last byte)
00053     uword avail;        // points to next available byte in arena
00054 };
00055 
00056 struct ArenaPool {
00057     Arena first;        // first arena in pool list.
00058     Arena* current;     // current arena.
00059     unsigned int arenasize;
00060     uword mask;         // Mask (power-of-2 - 1)
00061 };
00062 
00063 void InitArenaPool(ArenaPool *pool, const char *name, 
00064                    unsigned int size, unsigned int align);
00065 void FinishArenaPool(ArenaPool *pool);
00066 void FreeArenaPool(ArenaPool *pool);
00067 void* ArenaAllocate(ArenaPool *pool, unsigned int nb);
00068 
00069 #define ARENA_ALIGN(pool, n) (((uword)(n) + ARENA_ALIGN_MASK) & ~ARENA_ALIGN_MASK)
00070 #define INIT_ARENA_POOL(pool, name, size) \
00071         InitArenaPool(pool, name, size, ARENA_ALIGN_MASK + 1)
00072 
00073 #define ARENA_ALLOCATE(p, pool, nb) \
00074         Arena *_a = (pool)->current; \
00075         unsigned int _nb = ARENA_ALIGN(pool, nb); \
00076         uword _p = _a->avail; \
00077         uword _q = _p + _nb; \
00078         if (_q > _a->limit) \
00079             _p = (uword)ArenaAllocate(pool, _nb); \
00080         else \
00081             _a->avail = _q; \
00082         p = (void *)_p;
00083 
00084 #define ARENA_GROW(p, pool, size, incr) \
00085         Arena *_a = (pool)->current; \
00086         unsigned int _incr = ARENA_ALIGN(pool, incr); \
00087         uword _p = _a->avail; \
00088         uword _q = _p + _incr; \
00089         if (_p == (uword)(p) + ARENA_ALIGN(pool, size) && \
00090             _q <= _a->limit) { \
00091             _a->avail = _q; \
00092         } else { \
00093             p = ArenaGrow(pool, p, size, incr); \
00094         }
00095 
00096 #define ARENA_MARK(pool) ((void *) (pool)->current->avail)
00097 #define UPTRDIFF(p,q) ((uword)(p) - (uword)(q))     
00098 
00099 #ifdef DEBUG
00100 #define FREE_PATTERN 0xDA
00101 #define CLEAR_UNUSED(a) (ASSERT((a)->avail <= (a)->limit), \
00102                            memset((void*)(a)->avail, FREE_PATTERN, \
00103                             (a)->limit - (a)->avail))
00104 #define CLEAR_ARENA(a)  memset((void*)(a), FREE_PATTERN, \
00105                             (a)->limit - (uword)(a))
00106 #else
00107 #define CLEAR_UNUSED(a)
00108 #define CLEAR_ARENA(a)
00109 #endif
00110 
00111 #define ARENA_RELEASE(pool, mark) \
00112          char *_m = (char *)(mark); \
00113          Arena *_a = (pool)->current; \
00114          if (UPTRDIFF(_m, _a->base) <= UPTRDIFF(_a->avail, _a->base)) { \
00115              _a->avail = (uword)ARENA_ALIGN(pool, _m); \
00116              CLEAR_UNUSED(_a); \
00117          } else { \
00118              ArenaRelease(pool, _m); \
00119          }
00120 
00121 #define ARENA_DESTROY(pool, a, pnext) \
00122          if ((pool)->current == (a)) (pool)->current = &(pool)->first; \
00123          *(pnext) = (a)->next; \
00124          CLEAR_ARENA(a); \
00125          fastFree(a); \
00126          (a) = 0;
00127 
00128 }
00129 
00130 #endif

Generated on Wed Nov 21 20:04:16 2007 for Origyn Web Browser by Doxygen 1.5.3

pleyo.com
pleyo.com