| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef gc_BufferAllocator_inl_h |
| 8 |
#define gc_BufferAllocator_inl_h |
| 9 |
|
| 10 |
#include "gc/BufferAllocator.h" |
| 11 |
|
| 12 |
#include "mozilla/Atomics.h" |
| 13 |
#include "mozilla/MathAlgorithms.h" |
| 14 |
|
| 15 |
#include "ds/SlimLinkedList.h" |
| 16 |
#include "gc/Cell.h" |
| 17 |
#include "js/HeapAPI.h" |
| 18 |
|
| 19 |
#include "gc/Allocator-inl.h" |
| 20 |
|
| 21 |
namespace js::gc { |
| 22 |
|
| 23 |
// todo: rename |
| 24 |
static constexpr size_t MinAllocSize = MinCellSize; // 16 bytes |
| 25 |
|
| 26 |
static constexpr size_t MaxSmallAllocSize = |
| 27 |
1 << (BufferAllocator::MinMediumAllocShift - 1); |
| 28 |
static constexpr size_t MinMediumAllocSize = |
| 29 |
1 << BufferAllocator::MinMediumAllocShift; |
| 30 |
static constexpr size_t MaxMediumAllocSize = |
| 31 |
1 << BufferAllocator::MaxMediumAllocShift; |
| 32 |
|
| 33 |
/* static */ |
| 34 |
inline bool BufferAllocator::IsSmallAllocSize(size_t bytes) { |
| 35 |
return bytes <= MaxSmallAllocSize; |
| 36 |
} |
| 37 |
|
| 38 |
/* static */ |
| 39 |
inline bool BufferAllocator::IsLargeAllocSize(size_t bytes) { |
| 40 |
return bytes > MaxMediumAllocSize; |
| 41 |
} |
| 42 |
|
| 43 |
/* static */ |
| 44 |
inline size_t BufferAllocator::GetGoodAllocSize(size_t requiredBytes) { |
| 45 |
requiredBytes = std::max(requiredBytes, MinAllocSize); |
| 46 |
|
| 47 |
if (IsLargeAllocSize(requiredBytes)) { |
| 48 |
return RoundUp(requiredBytes, ChunkSize); |
| 49 |
} |
| 50 |
|
| 51 |
// TODO: Support more sizes than powers of 2 |
| 52 |
return mozilla::RoundUpPow2(requiredBytes); |
| 53 |
} |
| 54 |
|
| 55 |
/* static */ |
| 56 |
size_t BufferAllocator::GetGoodPower2AllocSize(size_t requiredBytes) { |
| 57 |
requiredBytes = std::max(requiredBytes, MinAllocSize); |
| 58 |
|
| 59 |
return mozilla::RoundUpPow2(requiredBytes); |
| 60 |
} |
| 61 |
|
| 62 |
/* static */ |
| 63 |
size_t BufferAllocator::GetGoodElementCount(size_t requiredElements, |
| 64 |
size_t elementSize) { |
| 65 |
size_t requiredBytes = requiredElements * elementSize; |
| 66 |
size_t goodSize = GetGoodAllocSize(requiredBytes); |
| 67 |
return goodSize / elementSize; |
| 68 |
} |
| 69 |
|
| 70 |
/* static */ |
| 71 |
size_t BufferAllocator::GetGoodPower2ElementCount(size_t requiredElements, |
| 72 |
size_t elementSize) { |
| 73 |
size_t requiredBytes = requiredElements * elementSize; |
| 74 |
size_t goodSize = GetGoodPower2AllocSize(requiredBytes); |
| 75 |
return goodSize / elementSize; |
| 76 |
} |
| 77 |
|
| 78 |
inline size_t GetGoodAllocSize(size_t requiredBytes) { |
| 79 |
return BufferAllocator::GetGoodAllocSize(requiredBytes); |
| 80 |
} |
| 81 |
|
| 82 |
inline size_t GetGoodElementCount(size_t requiredCount, size_t elementSize) { |
| 83 |
return BufferAllocator::GetGoodElementCount(requiredCount, elementSize); |
| 84 |
} |
| 85 |
|
| 86 |
inline size_t GetGoodPower2AllocSize(size_t requiredBytes) { |
| 87 |
return BufferAllocator::GetGoodPower2AllocSize(requiredBytes); |
| 88 |
} |
| 89 |
|
| 90 |
inline size_t GetGoodPower2ElementCount(size_t requiredCount, |
| 91 |
size_t elementSize) { |
| 92 |
return BufferAllocator::GetGoodPower2ElementCount(requiredCount, elementSize); |
| 93 |
} |
| 94 |
|
| 95 |
inline void* AllocBuffer(JS::Zone* zone, size_t bytes, bool nurseryOwned) { |
| 96 |
if (js::oom::ShouldFailWithOOM()) { |
| 97 |
return nullptr; |
| 98 |
} |
| 99 |
|
| 100 |
return zone->bufferAllocator.alloc(bytes, nurseryOwned); |
| 101 |
} |
| 102 |
|
| 103 |
inline void* AllocBufferInGC(JS::Zone* zone, size_t bytes, bool nurseryOwned) { |
| 104 |
return zone->bufferAllocator.allocInGC(bytes, nurseryOwned); |
| 105 |
} |
| 106 |
|
| 107 |
inline void* ReallocBuffer(JS::Zone* zone, void* alloc, size_t bytes, |
| 108 |
bool nurseryOwned) { |
| 109 |
if (js::oom::ShouldFailWithOOM()) { |
| 110 |
return nullptr; |
| 111 |
} |
| 112 |
|
| 113 |
return zone->bufferAllocator.realloc(alloc, bytes, nurseryOwned); |
| 114 |
} |
| 115 |
|
| 116 |
inline void FreeBuffer(JS::Zone* zone, void* alloc) { |
| 117 |
return zone->bufferAllocator.free(alloc); |
| 118 |
} |
| 119 |
|
| 120 |
inline bool IsBufferAlloc(void* alloc) { |
| 121 |
return BufferAllocator::IsBufferAlloc(alloc); |
| 122 |
} |
| 123 |
|
| 124 |
inline size_t GetAllocSize(JS::Zone* zone, void* alloc) { |
| 125 |
return zone->bufferAllocator.getAllocSize(alloc); |
| 126 |
} |
| 127 |
|
| 128 |
inline bool IsNurseryOwned(JS::Zone* zone, void* alloc) { |
| 129 |
return zone->bufferAllocator.isNurseryOwned(alloc); |
| 130 |
} |
| 131 |
|
| 132 |
inline bool IsBufferAllocMarkedBlack(JS::Zone* zone, void* alloc) { |
| 133 |
return zone->bufferAllocator.isMarkedBlack(alloc); |
| 134 |
} |
| 135 |
|
| 136 |
inline void TraceBufferEdgeInternal(JSTracer* trc, Cell* owner, void** bufferp, |
| 137 |
const char* name) { |
| 138 |
owner->zoneFromAnyThread()->bufferAllocator.traceEdge(trc, owner, bufferp, |
| 139 |
name); |
| 140 |
} |
| 141 |
|
| 142 |
inline void MarkTenuredBuffer(JS::Zone* zone, void* alloc) { |
| 143 |
zone->bufferAllocator.markTenuredAlloc(alloc); |
| 144 |
} |
| 145 |
|
| 146 |
} // namespace js::gc |
| 147 |
|
| 148 |
#endif // gc_BufferAllocator_inl_h |