Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkContainers.h
Go to the documentation of this file.
1// Copyright 2022 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
4#ifndef SkContainers_DEFINED
5#define SkContainers_DEFINED
6
10
11#include <cstddef>
12#include <cstdint>
13
15public:
16 SkContainerAllocator(size_t sizeOfT, int maxCapacity)
17 : fSizeOfT{sizeOfT}
18 , fMaxCapacity{maxCapacity} {}
19
20 // allocate will abort on failure. Given a capacity of 0, it will return the empty span.
21 // The bytes allocated are freed using sk_free().
22 SkSpan<std::byte> allocate(int capacity, double growthFactor = 1.0);
23
24 // Rounds a requested capacity up towards `kCapacityMultiple` in a constexpr-friendly fashion.
25 template <typename T>
26 static constexpr size_t RoundUp(size_t capacity) {
27 return SkAlignTo(capacity * sizeof(T), kCapacityMultiple) / sizeof(T);
28 }
29
30private:
32
33 // All capacity counts will be rounded up to kCapacityMultiple. This matches ASAN's shadow
34 // granularity, as well as our typical struct alignment on a 64-bit machine.
35 static constexpr int64_t kCapacityMultiple = 8;
36
37 // Rounds up capacity to next multiple of kCapacityMultiple and pin to fMaxCapacity.
38 size_t roundUpCapacity(int64_t capacity) const;
39
40 // Grows the capacity by growthFactor being sure to stay with in kMinBytes and fMaxCapacity.
41 size_t growthFactorCapacity(int capacity, double growthFactor) const;
42
43 const size_t fSizeOfT;
44 const int64_t fMaxCapacity;
45};
46
47// sk_allocate_canfail returns the empty span on failure. Parameter size must be > 0.
49
50// Returns the empty span if size is 0. sk_allocate_throw aborts on failure.
52
54#endif // SkContainers_DEFINED
#define SK_SPI
Definition SkAPI.h:41
static constexpr size_t SkAlignTo(size_t x, size_t alignment)
Definition SkAlign.h:33
SK_SPI void sk_report_container_overflow_and_die()
SkSpan< std::byte > sk_allocate_throw(size_t size)
SkSpan< std::byte > sk_allocate_canfail(size_t size)
SkContainerAllocator(size_t sizeOfT, int maxCapacity)
static constexpr size_t RoundUp(size_t capacity)
#define T