Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
sktext::gpu::BagOfBytes Class Reference

#include <SubRunAllocator.h>

Public Types

template<int size>
using Storage = std::array< char, PlatformMinimumSizeWithOverhead(size, 1)>
 

Public Member Functions

 BagOfBytes (char *block, size_t blockSize, size_t firstHeapAllocation)
 
 BagOfBytes (size_t firstHeapAllocation=0)
 
 BagOfBytes (const BagOfBytes &)=delete
 
BagOfBytesoperator= (const BagOfBytes &)=delete
 
 BagOfBytes (BagOfBytes &&that)
 
BagOfBytesoperator= (BagOfBytes &&that)
 
 ~BagOfBytes ()
 
template<typename T >
char * allocateBytesFor (int n=1)
 
void * alignedBytes (int unsafeSize, int unsafeAlignment)
 

Static Public Member Functions

static constexpr int PlatformMinimumSizeWithOverhead (int requestedSize, int assumedAlignment)
 
static constexpr int MinimumSizeWithOverhead (int requestedSize, int assumedAlignment, int blockSize, int maxAlignment)
 
template<typename T >
static bool WillCountFit (int n)
 

Detailed Description

Definition at line 34 of file SubRunAllocator.h.

Member Typedef Documentation

◆ Storage

template<int size>
using sktext::gpu::BagOfBytes::Storage = std::array<char, PlatformMinimumSizeWithOverhead(size, 1)>

Definition at line 98 of file SubRunAllocator.h.

Constructor & Destructor Documentation

◆ BagOfBytes() [1/4]

sktext::gpu::BagOfBytes::BagOfBytes ( char *  block,
size_t  blockSize,
size_t  firstHeapAllocation 
)

Definition at line 20 of file SubRunAllocator.cpp.

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}
#define SkASSERT_RELEASE(cond)
Definition SkAssert.h:100

◆ BagOfBytes() [2/4]

sktext::gpu::BagOfBytes::BagOfBytes ( size_t  firstHeapAllocation = 0)
explicit

Definition at line 33 of file SubRunAllocator.cpp.

34 : BagOfBytes(nullptr, 0, firstHeapAllocation) {}

◆ BagOfBytes() [3/4]

sktext::gpu::BagOfBytes::BagOfBytes ( const BagOfBytes )
delete

◆ BagOfBytes() [4/4]

sktext::gpu::BagOfBytes::BagOfBytes ( BagOfBytes &&  that)
inline

Definition at line 40 of file SubRunAllocator.h.

41 : fEndByte{std::exchange(that.fEndByte, nullptr)}
42 , fCapacity{that.fCapacity}
43 , fFibProgression{that.fFibProgression} {}

◆ ~BagOfBytes()

sktext::gpu::BagOfBytes::~BagOfBytes ( )

Definition at line 36 of file SubRunAllocator.cpp.

36 {
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}

Member Function Documentation

◆ alignedBytes()

void * sktext::gpu::BagOfBytes::alignedBytes ( int  unsafeSize,
int  unsafeAlignment 
)

Definition at line 49 of file SubRunAllocator.cpp.

49 {
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}
constexpr bool SkIsPow2(T value)
Definition SkMath.h:51

◆ allocateBytesFor()

template<typename T >
char * sktext::gpu::BagOfBytes::allocateBytesFor ( int  n = 1)
inline

Definition at line 108 of file SubRunAllocator.h.

108 {
109 static_assert(alignof(T) <= kMaxAlignment, "Alignment is too big for arena");
110 static_assert(sizeof(T) < kMaxByteSize, "Size is too big for arena");
111 SkASSERT_RELEASE(WillCountFit<T>(n));
112
113 int size = n ? n * sizeof(T) : 1;
114 return this->allocateBytes(size, alignof(T));
115 }
#define T

◆ MinimumSizeWithOverhead()

static constexpr int sktext::gpu::BagOfBytes::MinimumSizeWithOverhead ( int  requestedSize,
int  assumedAlignment,
int  blockSize,
int  maxAlignment 
)
inlinestaticconstexpr

Definition at line 59 of file SubRunAllocator.h.

60 {
61 SkASSERT_RELEASE(0 <= requestedSize && requestedSize < kMaxByteSize);
62 SkASSERT_RELEASE(SkIsPow2(assumedAlignment) && SkIsPow2(maxAlignment));
63
64 const int minAlignment = std::min(maxAlignment, assumedAlignment);
65 // There are two cases, one easy and one subtle. The easy case is when minAlignment ==
66 // maxAlignment. When that happens, the term maxAlignment - minAlignment is zero, and the
67 // block will be placed at the proper alignment because alignUp is properly
68 // aligned.
69 // The subtle case is where minAlignment < maxAlignment. Because
70 // minAlignment < maxAlignment, alignUp(requestedSize, minAlignment) + blockSize does not
71 // guarantee that block can be placed at a maxAlignment address. Block can be placed at
72 // maxAlignment/minAlignment different address to achieve alignment, so we need
73 // to add memory to allow the block to be placed on a maxAlignment address.
74 // For example, if assumedAlignment = 4 and maxAlignment = 16 then block can be placed at
75 // the following address offsets at the end of minimumSize bytes.
76 // 0 * minAlignment = 0
77 // 1 * minAlignment = 4
78 // 2 * minAlignment = 8
79 // 3 * minAlignment = 12
80 // Following this logic, the equation for the additional bytes is
81 // (maxAlignment/minAlignment - 1) * minAlignment
82 // = maxAlignment - minAlignment.
83 int minimumSize = SkToInt(AlignUp(requestedSize, minAlignment))
84 + blockSize
85 + maxAlignment - minAlignment;
86
87 // If minimumSize is > 32k then round to a 4K boundary unless it is too close to the
88 // maximum int. The > 32K heuristic is from the JEMalloc behavior.
89 constexpr int k32K = (1 << 15);
90 if (minimumSize >= k32K && minimumSize < std::numeric_limits<int>::max() - k4K) {
91 minimumSize = SkToInt(AlignUp(minimumSize, k4K));
92 }
93
94 return minimumSize;
95 }
constexpr int SkToInt(S x)
Definition SkTo.h:29

◆ operator=() [1/2]

BagOfBytes & sktext::gpu::BagOfBytes::operator= ( BagOfBytes &&  that)
inline

Definition at line 44 of file SubRunAllocator.h.

44 {
45 this->~BagOfBytes();
46 new (this) BagOfBytes{std::move(that)};
47 return *this;
48 }

◆ operator=() [2/2]

BagOfBytes & sktext::gpu::BagOfBytes::operator= ( const BagOfBytes )
delete

◆ PlatformMinimumSizeWithOverhead()

static constexpr int sktext::gpu::BagOfBytes::PlatformMinimumSizeWithOverhead ( int  requestedSize,
int  assumedAlignment 
)
inlinestaticconstexpr

Definition at line 54 of file SubRunAllocator.h.

54 {
56 requestedSize, assumedAlignment, sizeof(Block), kMaxAlignment);
57 }
static constexpr int MinimumSizeWithOverhead(int requestedSize, int assumedAlignment, int blockSize, int maxAlignment)

◆ WillCountFit()

template<typename T >
static bool sktext::gpu::BagOfBytes::WillCountFit ( int  n)
inlinestatic

Definition at line 102 of file SubRunAllocator.h.

102 {
103 constexpr int kMaxN = kMaxByteSize / sizeof(T);
104 return 0 <= n && n < kMaxN;
105 }

The documentation for this class was generated from the following files: