KHtml

render_arena.cpp
1 /*
2  * Copyright (C) 2002 Apple Computer, Inc.
3  * Copyright (C) 2003 Dirk Mueller ([email protected])
4  *
5  * Portions are Copyright (C) 1998 Netscape Communications Corporation.
6  *
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.
11  *
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.
16  *
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
20  *
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.
34  */
35 
36 #include "render_arena.h"
37 
38 #include <string.h>
39 #include <assert.h>
40 
41 using namespace khtml;
42 
43 namespace khtml
44 {
45 
46 typedef struct {
47  RenderArena *arena;
48  size_t size;
49 } RenderArenaDebugHeader;
50 
51 #ifdef VALGRIND_SUPPORT
52 Arena *findContainingArena(ArenaPool *pool, void *ptr)
53 {
54  uword ptrBits = reinterpret_cast<uword>(ptr);
55  for (Arena *a = &pool->first; a; a = a->next)
56  if (ptrBits >= a->base && ptrBits < a->limit) {
57  return a;
58  }
59  return 0; //Should not happen
60 }
61 #endif
62 
63 RenderArena::RenderArena(unsigned int arenaSize)
64 {
65  // Initialize the arena pool
66  INIT_ARENA_POOL(&m_pool, "RenderArena", arenaSize);
67 
68  // Zero out the recyclers array
69  memset(m_recyclers, 0, sizeof(m_recyclers));
70 }
71 
72 RenderArena::~RenderArena()
73 {
74  // Free the arena in the pool and finish using it
75  FreeArenaPool(&m_pool);
76 }
77 
78 void *RenderArena::allocate(size_t size)
79 {
80 #ifndef KHTML_USE_ARENA_ALLOCATOR
81  // Use standard malloc so that memory debugging tools work.
82  void *block = ::malloc(sizeof(RenderArenaDebugHeader) + size);
83  RenderArenaDebugHeader *header = (RenderArenaDebugHeader *)block;
84  header->arena = this;
85  header->size = size;
86  return header + 1;
87 #else
88  void *result = nullptr;
89 
90  // Ensure we have correct alignment for pointers. Important for Tru64
91  size = KHTML_ROUNDUP(size, sizeof(void *));
92 
93  // Check recyclers first
94  if (size < KHTML_MAX_RECYCLED_SIZE) {
95  const int index = size >> 2;
96 
97  result = m_recyclers[index];
98  if (result) {
99 #ifdef VALGRIND_SUPPORT
100  VALGRIND_MEMPOOL_ALLOC(findContainingArena(&m_pool, result)->base, result, size);
101 #endif
102  // Need to move to the next object
103  void *next = *((void **)result);
104  m_recyclers[index] = next;
105  }
106  }
107 
108  if (!result) {
109  // Allocate a new chunk from the arena
110  ARENA_ALLOCATE(result, &m_pool, size);
111  }
112 
113  return result;
114 #endif
115 }
116 
117 void RenderArena::free(size_t size, void *ptr)
118 {
119 #ifndef KHTML_USE_ARENA_ALLOCATOR
120  // Use standard free so that memory debugging tools work.
121  assert(this);
122  RenderArenaDebugHeader *header = (RenderArenaDebugHeader *)ptr - 1;
123  //assert(header->size == size);
124  //assert(header->arena == this);
125  ::free(header);
126 #else
127 
128 #ifdef VALGRIND_SUPPORT
129  VALGRIND_MEMPOOL_FREE(findContainingArena(&m_pool, ptr)->base, ptr);
130 #endif
131 
132  // Ensure we have correct alignment for pointers. Important for Tru64
133  size = KHTML_ROUNDUP(size, sizeof(void *));
134 
135  // See if it's a size that we recycle
136  if (size < KHTML_MAX_RECYCLED_SIZE) {
137  const int index = size >> 2;
138  void *currentTop = m_recyclers[index];
139  m_recyclers[index] = ptr;
140  *((void **)ptr) = currentTop;
141  }
142 #endif
143 }
144 
145 }
This file is part of the HTML rendering engine for KDE.
MESSAGECORE_EXPORT KMime::Content * next(KMime::Content *node, bool allowChildren=true)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 22:48:19 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.