2 * Copyright (C) 1998-2000 Netscape Communications Corporation.
3 * Copyright (C) 2003-6 Apple Computer
6 * Nick Blievers <nickb@adacel.com.au>
7 * Jeff Hostetler <jeff@nerdone.com>
8 * Tom Rini <trini@kernel.crashing.org>
9 * Raffaele Sena <raff@netwinder.org>
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.
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.
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
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.
41 * Lifetime-based fast allocation, inspired by much prior art, including
42 * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes"
43 * David R. Hanson, Software -- Practice and Experience, Vol. 20(1).
52 #include <wtf/Assertions.h>
53 #include <wtf/FastMalloc.h>
59 //#define DEBUG_ARENA_MALLOC
60 #ifdef DEBUG_ARENA_MALLOC
64 #define FREELIST_MAX 30
65 static Arena *arena_freelist;
66 static int freelist_count = 0;
68 #define ARENA_DEFAULT_ALIGN sizeof(double)
69 #define BIT(n) ((unsigned int)1 << (n))
70 #define BITMASK(n) (BIT(n) - 1)
71 #define CEILING_LOG2(_log2,_n) \
72 unsigned int j_ = (unsigned int)(_n); \
74 if ((j_) & ((j_)-1)) \
77 (_log2) += 16, (j_) >>= 16; \
79 (_log2) += 8, (j_) >>= 8; \
81 (_log2) += 4, (j_) >>= 4; \
83 (_log2) += 2, (j_) >>= 2; \
87 static int CeilingLog2(unsigned int i) {
93 void InitArenaPool(ArenaPool* pool, const char*, unsigned size, unsigned align)
96 align = ARENA_DEFAULT_ALIGN;
97 pool->mask = BITMASK(CeilingLog2(align));
98 pool->first.next = NULL;
99 pool->first.base = pool->first.avail = pool->first.limit =
100 (uword)ARENA_ALIGN(&pool->first + 1);
101 pool->current = &pool->first;
102 pool->arenasize = size;
107 ** ArenaAllocate() -- allocate space from an arena pool
109 ** Description: ArenaAllocate() allocates space from an arena
112 ** First try to satisfy the request from arenas starting at
115 ** If there is not enough space in the arena pool->current, try
116 ** to claim an arena, on a first fit basis, from the global
117 ** freelist (arena_freelist).
119 ** If no arena in arena_freelist is suitable, then try to
120 ** allocate a new arena from the heap.
122 ** Returns: pointer to allocated space or NULL
125 void* ArenaAllocate(ArenaPool *pool, unsigned int nb)
128 char *rp; /* returned pointer */
130 ASSERT((nb & pool->mask) == 0);
132 nb = (uword)ARENA_ALIGN(nb); /* force alignment */
134 /* attempt to allocate from arenas at pool->current */
138 if ( a->avail +nb <= a->limit ) {
140 rp = (char *)a->avail;
144 } while( NULL != (a = a->next) );
147 /* attempt to allocate from arena_freelist */
149 Arena *p = NULL; /* previous pointer, for unlinking from freelist */
151 for ( a = arena_freelist; a != NULL ; p = a, a = a->next ) {
152 if ( a->base +nb <= a->limit ) {
154 arena_freelist = a->next;
158 rp = (char *)a->avail;
160 /* the newly allocated arena is linked after pool->current
161 * and becomes pool->current */
162 a->next = pool->current->next;
163 pool->current->next = a;
165 if ( 0 == pool->first.next )
166 pool->first.next = a;
173 /* attempt to allocate from the heap */
175 unsigned int sz = max(pool->arenasize, nb);
176 sz += sizeof *a + pool->mask; /* header and alignment slop */
177 #ifdef DEBUG_ARENA_MALLOC
179 printf("Malloc: %d\n", i);
181 a = (Arena*)fastMalloc(sz);
182 // fastMalloc will abort() if it fails, so we are guaranteed that a is not 0.
183 a->limit = (uword)a + sz;
184 a->base = a->avail = (uword)ARENA_ALIGN(a + 1);
185 rp = (char *)a->avail;
187 /* the newly allocated arena is linked after pool->current
188 * and becomes pool->current */
189 a->next = pool->current->next;
190 pool->current->next = a;
192 if ( !pool->first.next )
193 pool->first.next = a;
196 } /* --- end ArenaAllocate() --- */
199 * Free tail arenas linked after head, which may not be the true list head.
200 * Reset pool->current to point to head in case it pointed at a tail arena.
202 static void FreeArenaList(ArenaPool *pool, Arena *head, bool reallyFree)
213 ASSERT(a->base <= a->avail && a->avail <= a->limit);
216 } while ((a = a->next) != 0);
220 if (freelist_count >= FREELIST_MAX)
227 #ifdef DEBUG_ARENA_MALLOC
230 printf("Free: %d\n", i);
234 } while ((a = *ap) != 0);
236 /* Insert the whole arena chain at the front of the freelist. */
241 *ap = arena_freelist;
245 pool->current = head;
248 void FreeArenaPool(ArenaPool *pool)
250 FreeArenaList(pool, &pool->first, false);
253 void FinishArenaPool(ArenaPool *pool)
255 FreeArenaList(pool, &pool->first, true);