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/SkSpan_impl.h" 9 10 #include <cstddef> 11 #include <cstdint> 12 13 class SK_SPI SkContainerAllocator { 14 public: SkContainerAllocator(size_t sizeOfT,int maxCapacity)15 SkContainerAllocator(size_t sizeOfT, int maxCapacity) 16 : fSizeOfT{sizeOfT} 17 , fMaxCapacity{maxCapacity} {} 18 19 // allocate will abort on failure. Given a capacity of 0, it will return the empty span. 20 // The bytes allocated are freed using sk_free(). 21 SkSpan<std::byte> allocate(int capacity, double growthFactor = 1.0); 22 23 private: 24 friend struct SkContainerAllocatorTestingPeer; 25 // All capacity counts will be rounded up to kCapacityMultiple. 26 // TODO: this is a constant from the original SkTArray code. This should be checked some how. 27 static constexpr int64_t kCapacityMultiple = 8; 28 29 // Rounds up capacity to next multiple of kCapacityMultiple and pin to fMaxCapacity. 30 size_t roundUpCapacity(int64_t capacity) const; 31 32 // Grows the capacity by growthFactor being sure to stay with in kMinBytes and fMaxCapacity. 33 size_t growthFactorCapacity(int capacity, double growthFactor) const; 34 35 const size_t fSizeOfT; 36 const int64_t fMaxCapacity; 37 }; 38 39 // sk_allocate_canfail returns the empty span on failure. Parameter size must be > 0. 40 SkSpan<std::byte> sk_allocate_canfail(size_t size); 41 42 // Returns the empty span if size is 0. sk_allocate_throw aborts on failure. 43 SkSpan<std::byte> sk_allocate_throw(size_t size); 44 45 SK_SPI void sk_report_container_overflow_and_die(); 46 #endif // SkContainers_DEFINED 47