Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkContainers.cpp
Go to the documentation of this file.
1// Copyright 2019 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
5
10
11#include <algorithm>
12#include <cstddef>
13
14namespace {
15// Return at least as many bytes to keep malloc aligned.
16constexpr size_t kMinBytes = alignof(max_align_t);
17
18SkSpan<std::byte> complete_size(void* ptr, size_t size) {
19 if (ptr == nullptr) {
20 return {};
21 }
22
23 return {static_cast<std::byte*>(ptr), sk_malloc_size(ptr, size)};
24}
25} // namespace
26
27SkSpan<std::byte> SkContainerAllocator::allocate(int capacity, double growthFactor) {
28 SkASSERT(capacity >= 0);
29 SkASSERT(growthFactor >= 1.0);
30 SkASSERT_RELEASE(capacity <= fMaxCapacity);
31
32 if (growthFactor > 1.0 && capacity > 0) {
33 capacity = this->growthFactorCapacity(capacity, growthFactor);
34 }
35
36 return sk_allocate_throw(capacity * fSizeOfT);
37}
38
39size_t SkContainerAllocator::roundUpCapacity(int64_t capacity) const {
40 SkASSERT(capacity >= 0);
41
42 // If round will not go above fMaxCapacity return rounded capacity.
43 if (capacity < fMaxCapacity - kCapacityMultiple) {
44 return SkAlignTo(capacity, kCapacityMultiple);
45 }
46
47 return SkToSizeT(fMaxCapacity);
48}
49
50size_t SkContainerAllocator::growthFactorCapacity(int capacity, double growthFactor) const {
51 SkASSERT(capacity >= 0);
52 SkASSERT(growthFactor >= 1.0);
53 // Multiply by the growthFactor. Remember this must be done in 64-bit ints and not
54 // size_t because size_t changes.
55 const int64_t capacityGrowth = static_cast<int64_t>(capacity * growthFactor);
56
57 // Notice that for small values of capacity, rounding up will provide most of the growth.
58 return this->roundUpCapacity(capacityGrowth);
59}
60
61
63 // Make sure to ask for at least the minimum number of bytes.
64 const size_t adjustedSize = std::max(size, kMinBytes);
65 void* ptr = sk_malloc_canfail(adjustedSize);
66 return complete_size(ptr, adjustedSize);
67}
68
70 if (size == 0) {
71 return {};
72 }
73 // Make sure to ask for at least the minimum number of bytes.
74 const size_t adjustedSize = std::max(size, kMinBytes);
75 void* ptr = sk_malloc_throw(adjustedSize);
76 return complete_size(ptr, adjustedSize);
77}
78
80 SK_ABORT("Requested capacity is too large.");
81}
static constexpr size_t SkAlignTo(size_t x, size_t alignment)
Definition SkAlign.h:33
#define SK_ABORT(message,...)
Definition SkAssert.h:70
#define SkASSERT_RELEASE(cond)
Definition SkAssert.h:100
#define SkASSERT(cond)
Definition SkAssert.h:116
SkSpan< std::byte > sk_allocate_throw(size_t size)
void sk_report_container_overflow_and_die()
SkSpan< std::byte > sk_allocate_canfail(size_t size)
SkSpan< std::byte > sk_allocate_throw(size_t size)
static void * sk_malloc_canfail(size_t size)
Definition SkMalloc.h:93
SK_API size_t sk_malloc_size(void *addr, size_t size)
static void * sk_malloc_throw(size_t size)
Definition SkMalloc.h:67
constexpr size_t SkToSizeT(S x)
Definition SkTo.h:31
SkSpan< std::byte > allocate(int capacity, double growthFactor=1.0)