| 1 |
/* |
| 2 |
* Copyright 2021 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/text/gpu/SubRunAllocator.h" |
| 9 |
|
| 10 |
#include "include/private/base/SkMath.h" |
| 11 |
#include "include/private/base/SkTFitsIn.h" |
| 12 |
|
| 13 |
#include <cstddef> |
| 14 |
#include <memory> |
| 15 |
#include <new> |
| 16 |
|
| 17 |
namespace sktext::gpu { |
| 18 |
|
| 19 |
// -- BagOfBytes --------------------------------------------------------------------------------- |
| 20 |
BagOfBytes::BagOfBytes(char* bytes, size_t size, size_t firstHeapAllocation) |
| 21 |
: fFibProgression(size, firstHeapAllocation) { |
| 22 |
SkASSERT_RELEASE(size < kMaxByteSize); |
| 23 |
SkASSERT_RELEASE(firstHeapAllocation < kMaxByteSize); |
| 24 |
|
| 25 |
std::size_t space = size; |
| 26 |
void* ptr = bytes; |
| 27 |
if (bytes && std::align(kMaxAlignment, sizeof(Block), ptr, space)) { |
| 28 |
this->setupBytesAndCapacity(bytes, size); |
| 29 |
new (fEndByte) Block(nullptr, nullptr); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
BagOfBytes::BagOfBytes(size_t firstHeapAllocation) |
| 34 |
: BagOfBytes(nullptr, 0, firstHeapAllocation) {} |
| 35 |
|
| 36 |
BagOfBytes::~BagOfBytes() { |
| 37 |
Block* cursor = reinterpret_cast<Block*>(fEndByte); |
| 38 |
while (cursor != nullptr) { |
| 39 |
char* toDelete = cursor->fBlockStart; |
| 40 |
cursor = cursor->fPrevious; |
| 41 |
delete [] toDelete; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
BagOfBytes::Block::Block(char* previous, char* startOfBlock) |
| 46 |
: fBlockStart{startOfBlock} |
| 47 |
, fPrevious{reinterpret_cast<Block*>(previous)} {} |
| 48 |
|
| 49 |
void* BagOfBytes::alignedBytes(int size, int alignment) { |
| 50 |
SkASSERT_RELEASE(0 < size && size < kMaxByteSize); |
| 51 |
SkASSERT_RELEASE(0 < alignment && alignment <= kMaxAlignment); |
| 52 |
SkASSERT_RELEASE(SkIsPow2(alignment)); |
| 53 |
|
| 54 |
return this->allocateBytes(size, alignment); |
| 55 |
} |
| 56 |
|
| 57 |
void BagOfBytes::setupBytesAndCapacity(char* bytes, int size) { |
| 58 |
// endByte must be aligned to the maximum alignment to allow tracking alignment using capacity; |
| 59 |
// capacity and endByte are both aligned to max alignment. |
| 60 |
intptr_t endByte = reinterpret_cast<intptr_t>(bytes + size - sizeof(Block)) & -kMaxAlignment; |
| 61 |
fEndByte = reinterpret_cast<char*>(endByte); |
| 62 |
fCapacity = fEndByte - bytes; |
| 63 |
} |
| 64 |
|
| 65 |
void BagOfBytes::needMoreBytes(int requestedSize, int alignment) { |
| 66 |
int nextBlockSize = fFibProgression.nextBlockSize(); |
| 67 |
const int size = PlatformMinimumSizeWithOverhead( |
| 68 |
std::max(requestedSize, nextBlockSize), kAllocationAlignment); |
| 69 |
char* const bytes = new char[size]; |
| 70 |
// fEndByte is changed by setupBytesAndCapacity. Remember it to link back to. |
| 71 |
char* const previousBlock = fEndByte; |
| 72 |
this->setupBytesAndCapacity(bytes, size); |
| 73 |
|
| 74 |
// Make a block to delete these bytes, and points to the previous block. |
| 75 |
new (fEndByte) Block{previousBlock, bytes}; |
| 76 |
|
| 77 |
// Make fCapacity the alignment for the requested object. |
| 78 |
fCapacity = fCapacity & -alignment; |
| 79 |
SkASSERT(fCapacity >= requestedSize); |
| 80 |
} |
| 81 |
|
| 82 |
// -- SubRunAllocator ---------------------------------------------------------------------------- |
| 83 |
SubRunAllocator::SubRunAllocator(char* bytes, int size, int firstHeapAllocation) |
| 84 |
: fAlloc{bytes, SkTo<size_t>(size), SkTo<size_t>(firstHeapAllocation)} { |
| 85 |
SkASSERT_RELEASE(SkTFitsIn<size_t>(size)); |
| 86 |
SkASSERT_RELEASE(SkTFitsIn<size_t>(firstHeapAllocation)); |
| 87 |
} |
| 88 |
|
| 89 |
SubRunAllocator::SubRunAllocator(int firstHeapAllocation) |
| 90 |
: SubRunAllocator(nullptr, 0, firstHeapAllocation) { } |
| 91 |
|
| 92 |
void* SubRunAllocator::alignedBytes(int unsafeSize, int unsafeAlignment) { |
| 93 |
return fAlloc.alignedBytes(unsafeSize, unsafeAlignment); |
| 94 |
} |
| 95 |
|
| 96 |
} // namespace sktext::gpu |