Home | History | Annotate | Download | only in platform
      1 /*
      2  * Copyright (C) 1998-2000 Netscape Communications Corporation.
      3  * Copyright (C) 2003-6 Apple Computer
      4  *
      5  * Other contributors:
      6  *   Nick Blievers <nickb (at) adacel.com.au>
      7  *   Jeff Hostetler <jeff (at) nerdone.com>
      8  *   Tom Rini <trini (at) kernel.crashing.org>
      9  *   Raffaele Sena <raff (at) netwinder.org>
     10  *
     11  * This library is free software; you can redistribute it and/or
     12  * modify it under the terms of the GNU Lesser General Public
     13  * License as published by the Free Software Foundation; either
     14  * version 2.1 of the License, or (at your option) any later version.
     15  *
     16  * This library is distributed in the hope that it will be useful,
     17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     19  * Lesser General Public License for more details.
     20  *
     21  * You should have received a copy of the GNU Lesser General Public
     22  * License along with this library; if not, write to the Free Software
     23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     24  *
     25  * Alternatively, the contents of this file may be used under the terms
     26  * of either the Mozilla Public License Version 1.1, found at
     27  * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
     28  * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
     29  * (the "GPL"), in which case the provisions of the MPL or the GPL are
     30  * applicable instead of those above.  If you wish to allow use of your
     31  * version of this file only under the terms of one of those two
     32  * licenses (the MPL or the GPL) and not to allow others to use your
     33  * version of this file under the LGPL, indicate your decision by
     34  * deletingthe provisions above and replace them with the notice and
     35  * other provisions required by the MPL or the GPL, as the case may be.
     36  * If you do not delete the provisions above, a recipient may use your
     37  * version of this file under any of the LGPL, the MPL or the GPL.
     38  */
     39 
     40 #ifndef Arena_h
     41 #define Arena_h
     42 
     43 #define ARENA_ALIGN_MASK 3
     44 
     45 namespace WebCore {
     46 
     47 typedef unsigned long uword;
     48 
     49 struct Arena {
     50     Arena* next;        // next arena
     51     uword base;         // aligned base address
     52     uword limit;        // end of arena (1+last byte)
     53     uword avail;        // points to next available byte in arena
     54 };
     55 
     56 struct ArenaPool {
     57     Arena first;        // first arena in pool list.
     58     Arena* current;     // current arena.
     59     unsigned int arenasize;
     60     uword mask;         // Mask (power-of-2 - 1)
     61 };
     62 
     63 void InitArenaPool(ArenaPool *pool, const char *name,
     64                    unsigned int size, unsigned int align);
     65 void FinishArenaPool(ArenaPool *pool);
     66 void FreeArenaPool(ArenaPool *pool);
     67 void* ArenaAllocate(ArenaPool *pool, unsigned int nb);
     68 
     69 #define ARENA_ALIGN(pool, n) (((uword)(n) + ARENA_ALIGN_MASK) & ~ARENA_ALIGN_MASK)
     70 #define INIT_ARENA_POOL(pool, name, size) \
     71         InitArenaPool(pool, name, size, ARENA_ALIGN_MASK + 1)
     72 
     73 #define ARENA_ALLOCATE(p, pool, nb) \
     74         Arena *_a = (pool)->current; \
     75         unsigned int _nb = ARENA_ALIGN(pool, nb); \
     76         uword _p = _a->avail; \
     77         uword _q = _p + _nb; \
     78         if (_q > _a->limit) \
     79             _p = (uword)ArenaAllocate(pool, _nb); \
     80         else \
     81             _a->avail = _q; \
     82         p = (void *)_p;
     83 
     84 #define ARENA_GROW(p, pool, size, incr) \
     85         Arena *_a = (pool)->current; \
     86         unsigned int _incr = ARENA_ALIGN(pool, incr); \
     87         uword _p = _a->avail; \
     88         uword _q = _p + _incr; \
     89         if (_p == (uword)(p) + ARENA_ALIGN(pool, size) && \
     90             _q <= _a->limit) { \
     91             _a->avail = _q; \
     92         } else { \
     93             p = ArenaGrow(pool, p, size, incr); \
     94         }
     95 
     96 #define ARENA_MARK(pool) ((void *) (pool)->current->avail)
     97 #define UPTRDIFF(p,q) ((uword)(p) - (uword)(q))
     98 
     99 #ifdef DEBUG
    100 #define FREE_PATTERN 0xDA
    101 #define CLEAR_UNUSED(a) ASSERT((a)->avail <= (a)->limit); \
    102                         memset((void*)(a)->avail, FREE_PATTERN, \
    103                             (a)->limit - (a)->avail)
    104 #define CLEAR_ARENA(a)  memset((void*)(a), FREE_PATTERN, \
    105                             (a)->limit - (uword)(a))
    106 #else
    107 #define CLEAR_UNUSED(a)
    108 #define CLEAR_ARENA(a)
    109 #endif
    110 
    111 #define ARENA_RELEASE(pool, mark) \
    112          char *_m = (char *)(mark); \
    113          Arena *_a = (pool)->current; \
    114          if (UPTRDIFF(_m, _a->base) <= UPTRDIFF(_a->avail, _a->base)) { \
    115              _a->avail = (uword)ARENA_ALIGN(pool, _m); \
    116              CLEAR_UNUSED(_a); \
    117          } else { \
    118              ArenaRelease(pool, _m); \
    119          }
    120 
    121 #define ARENA_DESTROY(pool, a, pnext) \
    122          if ((pool)->current == (a)) (pool)->current = &(pool)->first; \
    123          *(pnext) = (a)->next; \
    124          CLEAR_ARENA(a); \
    125          fastFree(a); \
    126          (a) = 0;
    127 
    128 #ifdef ANDROID_INSTRUMENT
    129 size_t ReportPoolSize(const ArenaPool* pool);
    130 #endif
    131 
    132 }
    133 
    134 #endif
    135