File indexing completed on 2024-04-14 11:05:36

0001 /*
0002  * Copyright (C) 1998-2000 Netscape Communications Corporation.
0003  *
0004  * Other contributors:
0005  *   Nick Blievers <nickb@adacel.com.au>
0006  *   Jeff Hostetler <jeff@nerdone.com>
0007  *   Tom Rini <trini@kernel.crashing.org>
0008  *   Raffaele Sena <raff@netwinder.org>
0009  *
0010  * This library is free software; you can redistribute it and/or
0011  * modify it under the terms of the GNU Lesser General Public
0012  * License as published by the Free Software Foundation; either
0013  * version 2.1 of the License, or (at your option) any later version.
0014  *
0015  * This library is distributed in the hope that it will be useful,
0016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0018  * Lesser General Public License for more details.
0019  *
0020  * You should have received a copy of the GNU Lesser General Public
0021  * License along with this library; if not, write to the Free Software
0022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0023  *
0024  * Alternatively, the contents of this file may be used under the terms
0025  * of either the Mozilla Public License Version 1.1, found at
0026  * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
0027  * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
0028  * (the "GPL"), in which case the provisions of the MPL or the GPL are
0029  * applicable instead of those above.  If you wish to allow use of your
0030  * version of this file only under the terms of one of those two
0031  * licenses (the MPL or the GPL) and not to allow others to use your
0032  * version of this file under the LGPL, indicate your decision by
0033  * deletingthe provisions above and replace them with the notice and
0034  * other provisions required by the MPL or the GPL, as the case may be.
0035  * If you do not delete the provisions above, a recipient may use your
0036  * version of this file under any of the LGPL, the MPL or the GPL.
0037  */
0038 
0039 #ifndef ARENA_H
0040 #define ARENA_H
0041 
0042 #include <config-khtml.h>
0043 #include <QtGlobal>
0044 
0045 // VG annotations for arenas disabled since still buggy
0046 #if 0 && HAVE_VALGRIND_MEMCHECK_H && !defined(NDEBUG)
0047 
0048 #include <valgrind/memcheck.h>
0049 #define VALGRIND_SUPPORT
0050 
0051 #else
0052 
0053 #define VALGRIND_CREATE_MEMPOOL(base, redZone, zeroed)
0054 #define VALGRIND_DESTROY_MEMPOOL(base)
0055 #define VALGRIND_MEMPOOL_ALLOC(base, addr, size)
0056 #define VALGRIND_MEMPOOL_FREE(base, addr)
0057 
0058 #endif
0059 
0060 #define ARENA_ALIGN_MASK 3
0061 
0062 typedef quintptr uword;
0063 
0064 namespace khtml
0065 {
0066 
0067 struct Arena {
0068     Arena *next;    // next arena
0069     uword base;     // aligned base address
0070     uword limit;    // end of arena (1+last byte)
0071     uword avail;    // points to next available byte in arena
0072 };
0073 
0074 struct ArenaPool {
0075     Arena first;    // first arena in pool list.
0076     Arena *current;     // current arena.
0077     unsigned int arenasize;
0078     unsigned int largealloc; // threshold for fractional allocation strategy
0079     unsigned int cumul; // total bytes in pool.
0080     uword mask;     // Mask (power-of-2 - 1)
0081 };
0082 
0083 void InitArenaPool(ArenaPool *pool, const char *name,
0084                    unsigned int size, unsigned int align);
0085 void FinishArenaPool(ArenaPool *pool);
0086 void FreeArenaPool(ArenaPool *pool);
0087 void *ArenaAllocate(ArenaPool *pool, unsigned int nb);
0088 void ArenaFinish(void);
0089 
0090 #define ARENA_ALIGN(pool, n) (((uword)(n) + ARENA_ALIGN_MASK) & ~ARENA_ALIGN_MASK)
0091 #define INIT_ARENA_POOL(pool, name, size) \
0092     InitArenaPool(pool, name, size, ARENA_ALIGN_MASK + 1)
0093 
0094 #define ARENA_ALLOCATE(p, pool, nb) \
0095     Arena *_a = (pool)->current; \
0096     unsigned int _nb = ARENA_ALIGN(pool, nb); \
0097     uword _p = _a->avail; \
0098     uword _q = _p + _nb; \
0099     if (_q > _a->limit) \
0100         _p = (uword)ArenaAllocate(pool, _nb); \
0101     else { \
0102         VALGRIND_MEMPOOL_ALLOC(_a->base, _p, _nb); \
0103         _a->avail = _q; \
0104     } \
0105     p = (void *)_p;
0106 
0107 #define ARENA_MARK(pool) ((void *) (pool)->current->avail)
0108 #define UPTRDIFF(p,q) ((uword)(p) - (uword)(q))
0109 
0110 #ifdef DEBUG
0111 #define FREE_PATTERN 0xDA
0112 #define CLEAR_UNUSED(a) (assert((a)->avail <= (a)->limit), \
0113                          memset((void*)(a)->avail, FREE_PATTERN, \
0114                                 (a)->limit - (a)->avail))
0115 #define CLEAR_ARENA(a)  memset((void*)(a), FREE_PATTERN, \
0116                                (a)->limit - (uword)(a))
0117 #else
0118 #define CLEAR_UNUSED(a)
0119 #define CLEAR_ARENA(a)
0120 #endif
0121 
0122 } // namespace
0123 
0124 #endif