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// FIXME: We'd always like to use AllocAlignmentInteger for Arena alignment
45// but there is concern over the memory growth this may cause.
46#ifdef WTF_USE_ARENA_ALLOC_ALIGNMENT_INTEGER
47#define ARENA_ALIGN_MASK (sizeof(WTF::AllocAlignmentInteger) - 1)
48#else
49#define ARENA_ALIGN_MASK 3
50#endif
51
52namespace WebCore {
53
54typedef uintptr_t uword;
55
56struct Arena {
57    Arena* next;        // next arena
58    uword base;         // aligned base address
59    uword limit;        // end of arena (1+last byte)
60    uword avail;        // points to next available byte in arena
61};
62
63struct ArenaPool {
64    Arena first;        // first arena in pool list.
65    Arena* current;     // current arena.
66    unsigned int arenasize;
67    uword mask;         // Mask (power-of-2 - 1)
68};
69
70void InitArenaPool(ArenaPool*, const char* name, unsigned int size, unsigned int align);
71void FinishArenaPool(ArenaPool*);
72void* ArenaAllocate(ArenaPool*, unsigned int numBytes, unsigned int& bytesAllocated);
73
74#define ARENA_ALIGN(n) (((uword)(n) + ARENA_ALIGN_MASK) & ~ARENA_ALIGN_MASK)
75#define INIT_ARENA_POOL(pool, name, size) InitArenaPool(pool, name, size, ARENA_ALIGN_MASK + 1)
76#define ARENA_ALLOCATE(p, pool, nb, bytesAllocated) \
77    Arena* _a = (pool)->current; \
78    unsigned int _nb = ARENA_ALIGN(nb); \
79    uword _p = _a->avail; \
80    uword _q = _p + _nb; \
81    if (_q > _a->limit) \
82        _p = (uword)ArenaAllocate(pool, _nb, *bytesAllocated); \
83    else \
84        _a->avail = _q; \
85    p = (void*)_p;
86
87}
88
89#endif
90