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