fix logic
[personal-kdelibs.git] / khtml / rendering / render_arena.cpp
blob13564059fc3b94f99f2e5035138ee088c6ed0e6b
1 /*
2 * Copyright (C) 2002 Apple Computer, Inc.
3 * Copyright (C) 2003 Dirk Mueller (mueller@kde.org)
5 * Portions are Copyright (C) 1998 Netscape Communications Corporation.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Alternatively, the contents of this file may be used under the terms
22 * of either the Mozilla Public License Version 1.1, found at
23 * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
24 * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
25 * (the "GPL"), in which case the provisions of the MPL or the GPL are
26 * applicable instead of those above. If you wish to allow use of your
27 * version of this file only under the terms of one of those two
28 * licenses (the MPL or the GPL) and not to allow others to use your
29 * version of this file under the LGPL, indicate your decision by
30 * deletingthe provisions above and replace them with the notice and
31 * other provisions required by the MPL or the GPL, as the case may be.
32 * If you do not delete the provisions above, a recipient may use your
33 * version of this file under any of the LGPL, the MPL or the GPL.
36 #include "render_arena.h"
38 #include <string.h>
39 #include <assert.h>
41 using namespace khtml;
43 namespace khtml {
45 typedef struct {
46 RenderArena *arena;
47 size_t size;
48 } RenderArenaDebugHeader;
50 #ifdef VALGRIND_SUPPORT
51 Arena* findContainingArena(ArenaPool* pool, void* ptr) {
52 uword ptrBits = reinterpret_cast<uword>(ptr);
53 for (Arena* a = &pool->first; a; a = a->next)
54 if (ptrBits >= a->base && ptrBits < a->limit)
55 return a;
56 return 0; //Should not happen
58 #endif
60 RenderArena::RenderArena(unsigned int arenaSize)
62 // Initialize the arena pool
63 INIT_ARENA_POOL(&m_pool, "RenderArena", arenaSize);
65 // Zero out the recyclers array
66 memset(m_recyclers, 0, sizeof(m_recyclers));
69 RenderArena::~RenderArena()
71 // Free the arena in the pool and finish using it
72 FreeArenaPool(&m_pool);
75 void* RenderArena::allocate(size_t size)
77 #ifndef KHTML_USE_ARENA_ALLOCATOR
78 // Use standard malloc so that memory debugging tools work.
79 void *block = ::malloc(sizeof(RenderArenaDebugHeader) + size);
80 RenderArenaDebugHeader *header = (RenderArenaDebugHeader *)block;
81 header->arena = this;
82 header->size = size;
83 return header + 1;
84 #else
85 void* result = 0;
87 // Ensure we have correct alignment for pointers. Important for Tru64
88 size = KHTML_ROUNDUP(size, sizeof(void*));
90 // Check recyclers first
91 if (size < KHTML_MAX_RECYCLED_SIZE) {
92 const int index = size >> 2;
94 result = m_recyclers[index];
95 if (result) {
96 #ifdef VALGRIND_SUPPORT
97 VALGRIND_MEMPOOL_ALLOC(findContainingArena(&m_pool, result)->base, result, size);
98 #endif
99 // Need to move to the next object
100 void* next = *((void**)result);
101 m_recyclers[index] = next;
105 if (!result) {
106 // Allocate a new chunk from the arena
107 ARENA_ALLOCATE(result, &m_pool, size);
110 return result;
111 #endif
114 void RenderArena::free(size_t size, void* ptr)
116 #ifndef KHTML_USE_ARENA_ALLOCATOR
117 // Use standard free so that memory debugging tools work.
118 assert(this);
119 RenderArenaDebugHeader *header = (RenderArenaDebugHeader *)ptr - 1;
120 //assert(header->size == size);
121 //assert(header->arena == this);
122 ::free(header);
123 #else
125 #ifdef VALGRIND_SUPPORT
126 VALGRIND_MEMPOOL_FREE(findContainingArena(&m_pool, ptr)->base, ptr);
127 #endif
129 // Ensure we have correct alignment for pointers. Important for Tru64
130 size = KHTML_ROUNDUP(size, sizeof(void*));
132 // See if it's a size that we recycle
133 if (size < KHTML_MAX_RECYCLED_SIZE) {
134 const int index = size >> 2;
135 void* currentTop = m_recyclers[index];
136 m_recyclers[index] = ptr;
137 *((void**)ptr) = currentTop;
139 #endif