File indexing completed on 2024-04-28 15:23:53
0001 /* 0002 * Copyright (C) 2002 Apple Computer, Inc. 0003 * Copyright (C) 2003 Dirk Mueller (mueller@kde.org) 0004 * 0005 * Portions are Copyright (C) 1998 Netscape Communications Corporation. 0006 * 0007 * This library is free software; you can redistribute it and/or 0008 * modify it under the terms of the GNU Lesser General Public 0009 * License as published by the Free Software Foundation; either 0010 * version 2.1 of the License, or (at your option) any later version. 0011 * 0012 * This library is distributed in the hope that it will be useful, 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0015 * Lesser General Public License for more details. 0016 * 0017 * You should have received a copy of the GNU Lesser General Public 0018 * License along with this library; if not, write to the Free Software 0019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 0020 * 0021 * Alternatively, the contents of this file may be used under the terms 0022 * of either the Mozilla Public License Version 1.1, found at 0023 * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public 0024 * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html 0025 * (the "GPL"), in which case the provisions of the MPL or the GPL are 0026 * applicable instead of those above. If you wish to allow use of your 0027 * version of this file only under the terms of one of those two 0028 * licenses (the MPL or the GPL) and not to allow others to use your 0029 * version of this file under the LGPL, indicate your decision by 0030 * deletingthe provisions above and replace them with the notice and 0031 * other provisions required by the MPL or the GPL, as the case may be. 0032 * If you do not delete the provisions above, a recipient may use your 0033 * version of this file under any of the LGPL, the MPL or the GPL. 0034 */ 0035 0036 #include "render_arena.h" 0037 0038 #include <string.h> 0039 #include <assert.h> 0040 0041 using namespace khtml; 0042 0043 namespace khtml 0044 { 0045 0046 typedef struct { 0047 RenderArena *arena; 0048 size_t size; 0049 } RenderArenaDebugHeader; 0050 0051 #ifdef VALGRIND_SUPPORT 0052 Arena *findContainingArena(ArenaPool *pool, void *ptr) 0053 { 0054 uword ptrBits = reinterpret_cast<uword>(ptr); 0055 for (Arena *a = &pool->first; a; a = a->next) 0056 if (ptrBits >= a->base && ptrBits < a->limit) { 0057 return a; 0058 } 0059 return 0; //Should not happen 0060 } 0061 #endif 0062 0063 RenderArena::RenderArena(unsigned int arenaSize) 0064 { 0065 // Initialize the arena pool 0066 INIT_ARENA_POOL(&m_pool, "RenderArena", arenaSize); 0067 0068 // Zero out the recyclers array 0069 memset(m_recyclers, 0, sizeof(m_recyclers)); 0070 } 0071 0072 RenderArena::~RenderArena() 0073 { 0074 // Free the arena in the pool and finish using it 0075 FreeArenaPool(&m_pool); 0076 } 0077 0078 void *RenderArena::allocate(size_t size) 0079 { 0080 #ifndef KHTML_USE_ARENA_ALLOCATOR 0081 // Use standard malloc so that memory debugging tools work. 0082 void *block = ::malloc(sizeof(RenderArenaDebugHeader) + size); 0083 RenderArenaDebugHeader *header = (RenderArenaDebugHeader *)block; 0084 header->arena = this; 0085 header->size = size; 0086 return header + 1; 0087 #else 0088 void *result = nullptr; 0089 0090 // Ensure we have correct alignment for pointers. Important for Tru64 0091 size = KHTML_ROUNDUP(size, sizeof(void *)); 0092 0093 // Check recyclers first 0094 if (size < KHTML_MAX_RECYCLED_SIZE) { 0095 const int index = size >> 2; 0096 0097 result = m_recyclers[index]; 0098 if (result) { 0099 #ifdef VALGRIND_SUPPORT 0100 VALGRIND_MEMPOOL_ALLOC(findContainingArena(&m_pool, result)->base, result, size); 0101 #endif 0102 // Need to move to the next object 0103 void *next = *((void **)result); 0104 m_recyclers[index] = next; 0105 } 0106 } 0107 0108 if (!result) { 0109 // Allocate a new chunk from the arena 0110 ARENA_ALLOCATE(result, &m_pool, size); 0111 } 0112 0113 return result; 0114 #endif 0115 } 0116 0117 void RenderArena::free(size_t size, void *ptr) 0118 { 0119 #ifndef KHTML_USE_ARENA_ALLOCATOR 0120 // Use standard free so that memory debugging tools work. 0121 assert(this); 0122 RenderArenaDebugHeader *header = (RenderArenaDebugHeader *)ptr - 1; 0123 //assert(header->size == size); 0124 //assert(header->arena == this); 0125 ::free(header); 0126 #else 0127 0128 #ifdef VALGRIND_SUPPORT 0129 VALGRIND_MEMPOOL_FREE(findContainingArena(&m_pool, ptr)->base, ptr); 0130 #endif 0131 0132 // Ensure we have correct alignment for pointers. Important for Tru64 0133 size = KHTML_ROUNDUP(size, sizeof(void *)); 0134 0135 // See if it's a size that we recycle 0136 if (size < KHTML_MAX_RECYCLED_SIZE) { 0137 const int index = size >> 2; 0138 void *currentTop = m_recyclers[index]; 0139 m_recyclers[index] = ptr; 0140 *((void **)ptr) = currentTop; 0141 } 0142 #endif 0143 } 0144 0145 }