Debian Codegraph @

include/private/base/SkContainers.h

raw
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
7 #include "include/private/base/SkAPI.h"
8 #include "include/private/base/SkAlign.h"
9 #include "include/private/base/SkSpan_impl.h"
10
11 #include <cstddef>
12 #include <cstdint>
13
14 class SK_SPI SkContainerAllocator {
15 public:
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), (size_t) kCapacityMultiple) / sizeof(T);
28 }
29
30 private:
31 friend struct SkContainerAllocatorTestingPeer;
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.
48 SkSpan<std::byte> sk_allocate_canfail(size_t size);
49
50 // Returns the empty span if size is 0. sk_allocate_throw aborts on failure.
51 SkSpan<std::byte> sk_allocate_throw(size_t size);
52
53 SK_SPI void sk_report_container_overflow_and_die();
54 #endif // SkContainers_DEFINED