1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/*
* Copyright (C) 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef JSC_Region_h
#define JSC_Region_h
#include "HeapBlock.h"
#include "SuperRegion.h"
#include <wtf/DoublyLinkedList.h>
#include <wtf/MetaAllocatorHandle.h>
#include <wtf/PageAllocationAligned.h>
#define HEAP_MEMORY_ID reinterpret_cast<void*>(static_cast<intptr_t>(-3))
#define ENABLE_SUPER_REGION 0
#ifndef ENABLE_SUPER_REGION
#if USE(JSVALUE64)
#define ENABLE_SUPER_REGION 1
#else
#define ENABLE_SUPER_REGION 0
#endif
#endif
namespace JSC {
class DeadBlock : public HeapBlock<DeadBlock> {
public:
DeadBlock(Region*);
};
inline DeadBlock::DeadBlock(Region* region)
: HeapBlock<DeadBlock>(region)
{
}
class Region : public DoublyLinkedListNode<Region> {
friend CLASS_IF_GCC DoublyLinkedListNode<Region>;
friend class BlockAllocator;
public:
~Region();
static Region* create(SuperRegion*, size_t blockSize);
static Region* createCustomSize(SuperRegion*, size_t blockSize, size_t blockAlignment);
Region* reset(size_t blockSize);
void destroy();
size_t blockSize() const { return m_blockSize; }
bool isFull() const { return m_blocksInUse == m_totalBlocks; }
bool isEmpty() const { return !m_blocksInUse; }
bool isCustomSize() const { return m_isCustomSize; }
DeadBlock* allocate();
void deallocate(void*);
static const size_t s_regionSize = 64 * KB;
static const size_t s_regionMask = ~(s_regionSize - 1);
protected:
Region(size_t blockSize, size_t totalBlocks, bool isExcess);
void initializeBlockList();
bool m_isExcess;
private:
void* base();
size_t size();
size_t m_totalBlocks;
size_t m_blocksInUse;
size_t m_blockSize;
bool m_isCustomSize;
Region* m_prev;
Region* m_next;
DoublyLinkedList<DeadBlock> m_deadBlocks;
};
class NormalRegion : public Region {
friend class Region;
private:
NormalRegion(PassRefPtr<WTF::MetaAllocatorHandle>, size_t blockSize, size_t totalBlocks);
static NormalRegion* tryCreate(SuperRegion*, size_t blockSize);
static NormalRegion* tryCreateCustomSize(SuperRegion*, size_t blockSize, size_t blockAlignment);
void* base() { return m_allocation->start(); }
size_t size() { return m_allocation->sizeInBytes(); }
NormalRegion* reset(size_t blockSize);
RefPtr<WTF::MetaAllocatorHandle> m_allocation;
};
class ExcessRegion : public Region {
friend class Region;
private:
ExcessRegion(PageAllocationAligned&, size_t blockSize, size_t totalBlocks);
~ExcessRegion();
static ExcessRegion* create(size_t blockSize);
static ExcessRegion* createCustomSize(size_t blockSize, size_t blockAlignment);
void* base() { return m_allocation.base(); }
size_t size() { return m_allocation.size(); }
ExcessRegion* reset(size_t blockSize);
PageAllocationAligned m_allocation;
};
inline NormalRegion::NormalRegion(PassRefPtr<WTF::MetaAllocatorHandle> allocation, size_t blockSize, size_t totalBlocks)
: Region(blockSize, totalBlocks, false)
, m_allocation(allocation)
{
initializeBlockList();
}
inline NormalRegion* NormalRegion::tryCreate(SuperRegion* superRegion, size_t blockSize)
{
RefPtr<WTF::MetaAllocatorHandle> allocation = superRegion->allocate(s_regionSize, HEAP_MEMORY_ID);
if (!allocation)
return 0;
return new NormalRegion(allocation, blockSize, s_regionSize / blockSize);
}
inline NormalRegion* NormalRegion::tryCreateCustomSize(SuperRegion* superRegion, size_t blockSize, size_t blockAlignment)
{
ASSERT_UNUSED(blockAlignment, blockAlignment <= s_regionSize);
RefPtr<WTF::MetaAllocatorHandle> allocation = superRegion->allocate(blockSize, HEAP_MEMORY_ID);
if (!allocation)
return 0;
return new NormalRegion(allocation, blockSize, 1);
}
inline NormalRegion* NormalRegion::reset(size_t blockSize)
{
ASSERT(!m_isExcess);
RefPtr<WTF::MetaAllocatorHandle> allocation = m_allocation.release();
return new (NotNull, this) NormalRegion(allocation.release(), blockSize, s_regionSize / blockSize);
}
inline ExcessRegion::ExcessRegion(PageAllocationAligned& allocation, size_t blockSize, size_t totalBlocks)
: Region(blockSize, totalBlocks, true)
, m_allocation(allocation)
{
initializeBlockList();
}
inline ExcessRegion::~ExcessRegion()
{
m_allocation.deallocate();
}
inline ExcessRegion* ExcessRegion::create(size_t blockSize)
{
PageAllocationAligned allocation = PageAllocationAligned::allocate(s_regionSize, s_regionSize, OSAllocator::JSGCHeapPages);
ASSERT(static_cast<bool>(allocation));
return new ExcessRegion(allocation, blockSize, s_regionSize / blockSize);
}
inline ExcessRegion* ExcessRegion::createCustomSize(size_t blockSize, size_t blockAlignment)
{
PageAllocationAligned allocation = PageAllocationAligned::allocate(blockSize, blockAlignment, OSAllocator::JSGCHeapPages);
ASSERT(static_cast<bool>(allocation));
return new ExcessRegion(allocation, blockSize, 1);
}
inline ExcessRegion* ExcessRegion::reset(size_t blockSize)
{
ASSERT(m_isExcess);
PageAllocationAligned allocation = m_allocation;
return new (NotNull, this) ExcessRegion(allocation, blockSize, s_regionSize / blockSize);
}
inline Region::Region(size_t blockSize, size_t totalBlocks, bool isExcess)
: DoublyLinkedListNode<Region>()
, m_isExcess(isExcess)
, m_totalBlocks(totalBlocks)
, m_blocksInUse(0)
, m_blockSize(blockSize)
, m_isCustomSize(false)
, m_prev(0)
, m_next(0)
{
}
inline void Region::initializeBlockList()
{
char* start = static_cast<char*>(base());
char* current = start;
for (size_t i = 0; i < m_totalBlocks; i++) {
ASSERT(current < start + size());
m_deadBlocks.append(new (NotNull, current) DeadBlock(this));
current += m_blockSize;
}
}
inline Region* Region::create(SuperRegion* superRegion, size_t blockSize)
{
#if ENABLE(SUPER_REGION)
ASSERT(blockSize <= s_regionSize);
ASSERT(!(s_regionSize % blockSize));
Region* region = NormalRegion::tryCreate(superRegion, blockSize);
if (LIKELY(!!region))
return region;
#else
UNUSED_PARAM(superRegion);
#endif
return ExcessRegion::create(blockSize);
}
inline Region* Region::createCustomSize(SuperRegion* superRegion, size_t blockSize, size_t blockAlignment)
{
#if ENABLE(SUPER_REGION)
Region* region = NormalRegion::tryCreateCustomSize(superRegion, blockSize, blockAlignment);
if (UNLIKELY(!region))
region = ExcessRegion::createCustomSize(blockSize, blockAlignment);
#else
UNUSED_PARAM(superRegion);
Region* region = ExcessRegion::createCustomSize(blockSize, blockAlignment);
#endif
region->m_isCustomSize = true;
return region;
}
inline Region::~Region()
{
ASSERT(isEmpty());
}
inline void Region::destroy()
{
#if ENABLE(SUPER_REGION)
if (UNLIKELY(m_isExcess))
delete static_cast<ExcessRegion*>(this);
else
delete static_cast<NormalRegion*>(this);
#else
delete static_cast<ExcessRegion*>(this);
#endif
}
inline Region* Region::reset(size_t blockSize)
{
#if ENABLE(SUPER_REGION)
ASSERT(isEmpty());
if (UNLIKELY(m_isExcess))
return static_cast<ExcessRegion*>(this)->reset(blockSize);
return static_cast<NormalRegion*>(this)->reset(blockSize);
#else
return static_cast<ExcessRegion*>(this)->reset(blockSize);
#endif
}
inline DeadBlock* Region::allocate()
{
ASSERT(!isFull());
m_blocksInUse++;
return m_deadBlocks.removeHead();
}
inline void Region::deallocate(void* base)
{
ASSERT(base);
ASSERT(m_blocksInUse);
ASSERT(base >= this->base() && base < static_cast<char*>(this->base()) + size());
DeadBlock* block = new (NotNull, base) DeadBlock(this);
m_deadBlocks.push(block);
m_blocksInUse--;
}
inline void* Region::base()
{
#if ENABLE(SUPER_REGION)
if (UNLIKELY(m_isExcess))
return static_cast<ExcessRegion*>(this)->ExcessRegion::base();
return static_cast<NormalRegion*>(this)->NormalRegion::base();
#else
return static_cast<ExcessRegion*>(this)->ExcessRegion::base();
#endif
}
inline size_t Region::size()
{
#if ENABLE(SUPER_REGION)
if (UNLIKELY(m_isExcess))
return static_cast<ExcessRegion*>(this)->ExcessRegion::size();
return static_cast<NormalRegion*>(this)->NormalRegion::size();
#else
return static_cast<ExcessRegion*>(this)->ExcessRegion::size();
#endif
}
} // namespace JSC
#endif // JSC_Region_h
|