| 1 |
/* |
| 2 |
* Copyright 2006 The Android Open Source Project |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef SkTemplates_DEFINED |
| 9 |
#define SkTemplates_DEFINED |
| 10 |
|
| 11 |
#include "include/private/base/SkAlign.h" |
| 12 |
#include "include/private/base/SkAssert.h" |
| 13 |
#include "include/private/base/SkDebug.h" |
| 14 |
#include "include/private/base/SkMalloc.h" |
| 15 |
#include "include/private/base/SkTLogic.h" |
| 16 |
#include "include/private/base/SkTo.h" |
| 17 |
|
| 18 |
#include <algorithm> |
| 19 |
#include <array> |
| 20 |
#include <cstddef> |
| 21 |
#include <cstdint> |
| 22 |
#include <cstring> |
| 23 |
#include <memory> |
| 24 |
#include <type_traits> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
|
| 28 |
/** \file SkTemplates.h |
| 29 |
|
| 30 |
This file contains light-weight template classes for type-safe and exception-safe |
| 31 |
resource management. |
| 32 |
*/ |
| 33 |
|
| 34 |
/** |
| 35 |
* Marks a local variable as known to be unused (to avoid warnings). |
| 36 |
* Note that this does *not* prevent the local variable from being optimized away. |
| 37 |
*/ |
| 38 |
template<typename T> inline void sk_ignore_unused_variable(const T&) { } |
| 39 |
|
| 40 |
/** |
| 41 |
* This is a general purpose absolute-value function. |
| 42 |
* See SkAbs32 in (SkSafe32.h) for a 32-bit int specific version that asserts. |
| 43 |
*/ |
| 44 |
template <typename T> static inline T SkTAbs(T value) { |
| 45 |
if (value < 0) { |
| 46 |
value = -value; |
| 47 |
} |
| 48 |
return value; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns a pointer to a D which comes immediately after S[count]. |
| 53 |
*/ |
| 54 |
template <typename D, typename S> inline D* SkTAfter(S* ptr, size_t count = 1) { |
| 55 |
return reinterpret_cast<D*>(ptr + count); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns a pointer to a D which comes byteOffset bytes after S. |
| 60 |
*/ |
| 61 |
template <typename D, typename S> inline D* SkTAddOffset(S* ptr, ptrdiff_t byteOffset) { |
| 62 |
// The intermediate char* has the same cv-ness as D as this produces better error messages. |
| 63 |
// This relies on the fact that reinterpret_cast can add constness, but cannot remove it. |
| 64 |
return reinterpret_cast<D*>(reinterpret_cast<sknonstd::same_cv_t<char, D>*>(ptr) + byteOffset); |
| 65 |
} |
| 66 |
|
| 67 |
template <typename T, T* P> struct SkOverloadedFunctionObject { |
| 68 |
template <typename... Args> |
| 69 |
auto operator()(Args&&... args) const -> decltype(P(std::forward<Args>(args)...)) { |
| 70 |
return P(std::forward<Args>(args)...); |
| 71 |
} |
| 72 |
}; |
| 73 |
|
| 74 |
template <auto F> using SkFunctionObject = |
| 75 |
SkOverloadedFunctionObject<std::remove_pointer_t<decltype(F)>, F>; |
| 76 |
|
| 77 |
/** \class SkAutoTCallVProc |
| 78 |
|
| 79 |
Call a function when this goes out of scope. The template uses two |
| 80 |
parameters, the object, and a function that is to be called in the destructor. |
| 81 |
If release() is called, the object reference is set to null. If the object |
| 82 |
reference is null when the destructor is called, we do not call the |
| 83 |
function. |
| 84 |
*/ |
| 85 |
template <typename T, void (*P)(T*)> class [[nodiscard]] SkAutoTCallVProc |
| 86 |
: public std::unique_ptr<T, SkFunctionObject<P>> { |
| 87 |
using inherited = std::unique_ptr<T, SkFunctionObject<P>>; |
| 88 |
public: |
| 89 |
using inherited::inherited; |
| 90 |
SkAutoTCallVProc(const SkAutoTCallVProc&) = delete; |
| 91 |
SkAutoTCallVProc(SkAutoTCallVProc&& that) : inherited(std::move(that)) {} |
| 92 |
|
| 93 |
operator T*() const { return this->get(); } |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
namespace skia_private { |
| 98 |
/** Allocate an array of T elements on the heap. Once this goes out of scope, the |
| 99 |
* elements will be cleaned up "auto"matically. |
| 100 |
*/ |
| 101 |
template <typename T> class AutoTArray { |
| 102 |
public: |
| 103 |
AutoTArray() {} |
| 104 |
// Allocate size number of T elements |
| 105 |
explicit AutoTArray(size_t size) |
| 106 |
: fData(size > 0 ? new T[check_size_bytes_too_big<T>(size)] : nullptr) |
| 107 |
, fSize(size) {} |
| 108 |
|
| 109 |
// TODO: remove when all uses are gone. |
| 110 |
explicit AutoTArray(int size) : AutoTArray(SkToSizeT(size)) {} |
| 111 |
|
| 112 |
AutoTArray(AutoTArray&& other) |
| 113 |
: fData(std::move(other.fData)) |
| 114 |
, fSize(std::exchange(other.fSize, 0)) {} |
| 115 |
|
| 116 |
AutoTArray& operator=(AutoTArray&& other) { |
| 117 |
if (this != &other) { |
| 118 |
fData = std::move(other.fData); |
| 119 |
fSize = std::exchange(other.fSize, 0); |
| 120 |
} |
| 121 |
return *this; |
| 122 |
} |
| 123 |
|
| 124 |
// Reallocates given a new count. Reallocation occurs even if new count equals old count. |
| 125 |
[[clang::reinitializes]] |
| 126 |
void reset(size_t count = 0) { |
| 127 |
*this = AutoTArray(count); |
| 128 |
} |
| 129 |
|
| 130 |
T* get() const { return fData.get(); } |
| 131 |
|
| 132 |
T& operator[](size_t index) const { |
| 133 |
return fData[sk_collection_check_bounds(index, fSize)]; |
| 134 |
} |
| 135 |
|
| 136 |
const T* data() const { return fData.get(); } |
| 137 |
T* data() { return fData.get(); } |
| 138 |
|
| 139 |
size_t size() const { return fSize; } |
| 140 |
bool empty() const { return fSize == 0; } |
| 141 |
size_t size_bytes() const { return sizeof(T) * fSize; } |
| 142 |
|
| 143 |
T* begin() { |
| 144 |
return fData.get(); |
| 145 |
} |
| 146 |
const T* begin() const { |
| 147 |
return fData.get(); |
| 148 |
} |
| 149 |
|
| 150 |
// It's safe to use fItemArray + fSize because if fItemArray is nullptr then adding 0 is |
| 151 |
// valid and returns nullptr. See [expr.add] in the C++ standard. |
| 152 |
T* end() { |
| 153 |
if (fData == nullptr) { |
| 154 |
SkASSERT(fSize == 0); |
| 155 |
} |
| 156 |
return this->begin() + fSize; |
| 157 |
} |
| 158 |
const T* end() const { |
| 159 |
if (fData == nullptr) { |
| 160 |
SkASSERT(fSize == 0); |
| 161 |
} |
| 162 |
return this->begin() + fSize; |
| 163 |
} |
| 164 |
|
| 165 |
private: |
| 166 |
std::unique_ptr<T[]> fData; |
| 167 |
size_t fSize = 0; |
| 168 |
}; |
| 169 |
|
| 170 |
/** Like AutoTArray with storage for some number of elements "nested within". The requested number |
| 171 |
* of elements to fit in the storage is specified by kCountRequested. kCount is the actual number |
| 172 |
* of elements that will fit in the storage. If the runtime number of elements exceeds the space of |
| 173 |
* the storage, the elements will live on the heap. |
| 174 |
*/ |
| 175 |
template <int kCountRequested, typename T> class AutoSTArray { |
| 176 |
public: |
| 177 |
AutoSTArray(const AutoSTArray&) = delete; |
| 178 |
AutoSTArray& operator=(const AutoSTArray&) = delete; |
| 179 |
|
| 180 |
AutoSTArray(AutoSTArray&& that) { |
| 181 |
if (that.fArray == nullptr) { |
| 182 |
fArray = nullptr; |
| 183 |
fCount = 0; |
| 184 |
} else if (that.fArray == (T*) that.fStorage) { |
| 185 |
fArray = (T*) fStorage; |
| 186 |
fCount = that.fCount; |
| 187 |
std::uninitialized_move(that.fArray, that.fArray + that.fCount, fArray); |
| 188 |
} else { |
| 189 |
fArray = std::exchange(that.fArray, nullptr); |
| 190 |
fCount = std::exchange(that.fCount, 0); |
| 191 |
} |
| 192 |
} |
| 193 |
AutoSTArray& operator=(AutoSTArray&&) = delete; |
| 194 |
|
| 195 |
/** Initialize with no objects */ |
| 196 |
AutoSTArray() { |
| 197 |
fArray = nullptr; |
| 198 |
fCount = 0; |
| 199 |
} |
| 200 |
|
| 201 |
/** Allocate count number of T elements */ |
| 202 |
AutoSTArray(int count) { |
| 203 |
fArray = nullptr; |
| 204 |
fCount = 0; |
| 205 |
this->reset(count); |
| 206 |
} |
| 207 |
|
| 208 |
~AutoSTArray() { |
| 209 |
this->reset(0); |
| 210 |
} |
| 211 |
|
| 212 |
/** Destroys previous objects in the array and default constructs count number of objects */ |
| 213 |
[[clang::reinitializes]] |
| 214 |
void reset(int count) { |
| 215 |
T* start = begin(); |
| 216 |
T* iter = end(); |
| 217 |
while (iter > start) { |
| 218 |
(--iter)->~T(); |
| 219 |
} |
| 220 |
|
| 221 |
SkASSERT(count >= 0); |
| 222 |
if (fCount != count) { |
| 223 |
if (fArray != (T*) fStorage) { |
| 224 |
sk_free(fArray); |
| 225 |
} |
| 226 |
|
| 227 |
if (count > kCount) { |
| 228 |
fArray = (T*) sk_malloc_throw(count, sizeof(T)); |
| 229 |
} else if (count > 0) { |
| 230 |
fArray = (T*) fStorage; |
| 231 |
} else { |
| 232 |
fArray = nullptr; |
| 233 |
} |
| 234 |
|
| 235 |
fCount = count; |
| 236 |
} |
| 237 |
|
| 238 |
iter = begin(); |
| 239 |
T* stop = end(); |
| 240 |
while (iter < stop) { |
| 241 |
new (iter++) T; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
/* Removes elements with index >= count */ |
| 246 |
void trimTo(int count) { |
| 247 |
SkASSERT(count >= 0); |
| 248 |
if (count >= fCount) { |
| 249 |
return; |
| 250 |
} |
| 251 |
T* start = begin() + count; |
| 252 |
T* iter = end(); |
| 253 |
while (iter > start) { |
| 254 |
(--iter)->~T(); |
| 255 |
} |
| 256 |
fCount = count; |
| 257 |
} |
| 258 |
|
| 259 |
/** Return the number of T elements in the array */ |
| 260 |
int count() const { return fCount; } |
| 261 |
|
| 262 |
/** Return the array of T elements. Will be nullptr if count == 0 */ |
| 263 |
T* get() const { return fArray; } |
| 264 |
|
| 265 |
T* begin() { return fArray; } |
| 266 |
|
| 267 |
const T* begin() const { return fArray; } |
| 268 |
|
| 269 |
T* end() { return fArray + fCount; } |
| 270 |
|
| 271 |
const T* end() const { return fArray + fCount; } |
| 272 |
|
| 273 |
/** Return the nth element in the array */ |
| 274 |
T& operator[](int index) const { |
| 275 |
return fArray[sk_collection_check_bounds(index, fCount)]; |
| 276 |
} |
| 277 |
|
| 278 |
/** Aliases matching other types, like std::vector. */ |
| 279 |
const T* data() const { return fArray; } |
| 280 |
T* data() { return fArray; } |
| 281 |
size_t size() const { return fCount; } |
| 282 |
|
| 283 |
private: |
| 284 |
#if defined(SK_BUILD_FOR_GOOGLE3) |
| 285 |
// Stack frame size is limited for SK_BUILD_FOR_GOOGLE3. 4k is less than the actual max, |
| 286 |
// but some functions have multiple large stack allocations. |
| 287 |
static constexpr int kMaxBytes = 4 * 1024; |
| 288 |
static constexpr int kMinCount = kCountRequested * sizeof(T) > kMaxBytes |
| 289 |
? kMaxBytes / sizeof(T) |
| 290 |
: kCountRequested; |
| 291 |
#else |
| 292 |
static constexpr int kMinCount = kCountRequested; |
| 293 |
#endif |
| 294 |
|
| 295 |
// Because we are also storing an int, there is a tiny bit of padding that |
| 296 |
// the C++ compiler adds after fStorage if sizeof(T) <= alignof(T*). |
| 297 |
// Thus, we can expand how many elements are stored on the stack to make use of this |
| 298 |
// (e.g. 1 extra element for 4 byte T if kCountRequested was even). |
| 299 |
static_assert(alignof(int) <= alignof(T*) || alignof(int) <= alignof(T)); |
| 300 |
public: |
| 301 |
static constexpr int kCount = |
| 302 |
SkAlignTo(kMinCount*sizeof(T) + sizeof(int), std::max(alignof(T*), alignof(T))) / sizeof(T); |
| 303 |
|
| 304 |
private: |
| 305 |
T* fArray; |
| 306 |
alignas(T) std::byte fStorage[kCount * sizeof(T)]; |
| 307 |
int fCount; |
| 308 |
}; |
| 309 |
|
| 310 |
/** Manages an array of T elements, freeing the array in the destructor. |
| 311 |
* Does NOT call any constructors/destructors on T (T must be POD). |
| 312 |
*/ |
| 313 |
template <typename T, |
| 314 |
typename = std::enable_if_t<std::is_trivially_default_constructible<T>::value && |
| 315 |
std::is_trivially_destructible<T>::value>> |
| 316 |
class AutoTMalloc { |
| 317 |
public: |
| 318 |
/** Takes ownership of the ptr. The ptr must be a value which can be passed to sk_free. */ |
| 319 |
explicit AutoTMalloc(T* ptr = nullptr) : fPtr(ptr) {} |
| 320 |
|
| 321 |
/** Allocates space for 'count' Ts. */ |
| 322 |
explicit AutoTMalloc(size_t count) |
| 323 |
: fPtr(count ? (T*)sk_malloc_throw(count, sizeof(T)) : nullptr) {} |
| 324 |
|
| 325 |
AutoTMalloc(AutoTMalloc&&) = default; |
| 326 |
AutoTMalloc& operator=(AutoTMalloc&&) = default; |
| 327 |
|
| 328 |
/** Resize the memory area pointed to by the current ptr preserving contents. */ |
| 329 |
void realloc(size_t count) { |
| 330 |
fPtr.reset(count ? (T*)sk_realloc_throw(fPtr.release(), count * sizeof(T)) : nullptr); |
| 331 |
} |
| 332 |
|
| 333 |
/** Resize the memory area pointed to by the current ptr without preserving contents. */ |
| 334 |
[[clang::reinitializes]] |
| 335 |
T* reset(size_t count = 0) { |
| 336 |
fPtr.reset(count ? (T*)sk_malloc_throw(count, sizeof(T)) : nullptr); |
| 337 |
return this->get(); |
| 338 |
} |
| 339 |
|
| 340 |
T* get() const { return fPtr.get(); } |
| 341 |
|
| 342 |
operator T*() { return fPtr.get(); } |
| 343 |
|
| 344 |
operator const T*() const { return fPtr.get(); } |
| 345 |
|
| 346 |
T& operator[](int index) { return fPtr.get()[index]; } |
| 347 |
|
| 348 |
const T& operator[](int index) const { return fPtr.get()[index]; } |
| 349 |
|
| 350 |
/** Aliases matching other types, like std::vector. */ |
| 351 |
const T* data() const { return fPtr.get(); } |
| 352 |
T* data() { return fPtr.get(); } |
| 353 |
|
| 354 |
/** |
| 355 |
* Transfer ownership of the ptr to the caller, setting the internal |
| 356 |
* pointer to NULL. Note that this differs from get(), which also returns |
| 357 |
* the pointer, but it does not transfer ownership. |
| 358 |
*/ |
| 359 |
T* release() { return fPtr.release(); } |
| 360 |
|
| 361 |
private: |
| 362 |
std::unique_ptr<T, SkOverloadedFunctionObject<void(void*), sk_free>> fPtr; |
| 363 |
}; |
| 364 |
|
| 365 |
template <size_t kCountRequested, |
| 366 |
typename T, |
| 367 |
typename = std::enable_if_t<std::is_trivially_default_constructible<T>::value && |
| 368 |
std::is_trivially_destructible<T>::value>> |
| 369 |
class AutoSTMalloc { |
| 370 |
public: |
| 371 |
AutoSTMalloc() : fPtr(fTStorage) {} |
| 372 |
|
| 373 |
AutoSTMalloc(size_t count) { |
| 374 |
if (count > kCount) { |
| 375 |
fPtr = (T*)sk_malloc_throw(count, sizeof(T)); |
| 376 |
} else if (count) { |
| 377 |
fPtr = fTStorage; |
| 378 |
} else { |
| 379 |
fPtr = nullptr; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
AutoSTMalloc(const AutoSTMalloc&) = delete; |
| 384 |
AutoSTMalloc& operator=(const AutoSTMalloc&) = delete; |
| 385 |
|
| 386 |
AutoSTMalloc(AutoSTMalloc&& that) { |
| 387 |
if (that.fPtr == nullptr) { |
| 388 |
fPtr = nullptr; |
| 389 |
} else if (that.fPtr == that.fTStorage) { |
| 390 |
fPtr = fTStorage; |
| 391 |
memcpy(fPtr, that.fPtr, kCount * sizeof(T)); |
| 392 |
} else { |
| 393 |
fPtr = std::exchange(that.fPtr, nullptr); |
| 394 |
} |
| 395 |
} |
| 396 |
AutoSTMalloc& operator=(AutoSTMalloc&&) = delete; |
| 397 |
|
| 398 |
~AutoSTMalloc() { |
| 399 |
if (fPtr != fTStorage) { |
| 400 |
sk_free(fPtr); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
// doesn't preserve contents |
| 405 |
[[clang::reinitializes]] |
| 406 |
T* reset(size_t count) { |
| 407 |
if (fPtr != fTStorage) { |
| 408 |
sk_free(fPtr); |
| 409 |
} |
| 410 |
if (count > kCount) { |
| 411 |
fPtr = (T*)sk_malloc_throw(count, sizeof(T)); |
| 412 |
} else if (count) { |
| 413 |
fPtr = fTStorage; |
| 414 |
} else { |
| 415 |
fPtr = nullptr; |
| 416 |
} |
| 417 |
return fPtr; |
| 418 |
} |
| 419 |
|
| 420 |
T* get() const { return fPtr; } |
| 421 |
|
| 422 |
operator T*() { |
| 423 |
return fPtr; |
| 424 |
} |
| 425 |
|
| 426 |
operator const T*() const { |
| 427 |
return fPtr; |
| 428 |
} |
| 429 |
|
| 430 |
T& operator[](int index) { |
| 431 |
return fPtr[index]; |
| 432 |
} |
| 433 |
|
| 434 |
const T& operator[](int index) const { |
| 435 |
return fPtr[index]; |
| 436 |
} |
| 437 |
|
| 438 |
/** Aliases matching other types, like std::vector. */ |
| 439 |
const T* data() const { return fPtr; } |
| 440 |
T* data() { return fPtr; } |
| 441 |
|
| 442 |
// Reallocs the array, can be used to shrink the allocation. Makes no attempt to be intelligent |
| 443 |
void realloc(size_t count) { |
| 444 |
if (count > kCount) { |
| 445 |
if (fPtr == fTStorage) { |
| 446 |
fPtr = (T*)sk_malloc_throw(count, sizeof(T)); |
| 447 |
memcpy((void*)fPtr, fTStorage, kCount * sizeof(T)); |
| 448 |
} else { |
| 449 |
fPtr = (T*)sk_realloc_throw(fPtr, count, sizeof(T)); |
| 450 |
} |
| 451 |
} else if (count) { |
| 452 |
if (fPtr != fTStorage) { |
| 453 |
fPtr = (T*)sk_realloc_throw(fPtr, count, sizeof(T)); |
| 454 |
} |
| 455 |
} else { |
| 456 |
this->reset(0); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
private: |
| 461 |
// Since we use uint32_t storage, we might be able to get more elements for free. |
| 462 |
static constexpr size_t kCountWithPadding = SkAlign4(kCountRequested*sizeof(T)) / sizeof(T); |
| 463 |
#if defined(SK_BUILD_FOR_GOOGLE3) |
| 464 |
// Stack frame size is limited for SK_BUILD_FOR_GOOGLE3. 4k is less than the actual max, but some functions |
| 465 |
// have multiple large stack allocations. |
| 466 |
static constexpr size_t kMaxBytes = 4 * 1024; |
| 467 |
static constexpr size_t kMinCount = kCountRequested * sizeof(T) > kMaxBytes |
| 468 |
? kMaxBytes / sizeof(T) |
| 469 |
: kCountWithPadding; |
| 470 |
#else |
| 471 |
static constexpr size_t kMinCount = kCountWithPadding; |
| 472 |
#endif |
| 473 |
|
| 474 |
public: |
| 475 |
static constexpr size_t kCount = kMinCount; |
| 476 |
|
| 477 |
private: |
| 478 |
|
| 479 |
T* fPtr; |
| 480 |
union { |
| 481 |
uint32_t fStorage32[SkAlign4(kCount*sizeof(T)) >> 2]; |
| 482 |
T fTStorage[1]; // do NOT want to invoke T::T() |
| 483 |
}; |
| 484 |
}; |
| 485 |
|
| 486 |
using UniqueVoidPtr = std::unique_ptr<void, SkOverloadedFunctionObject<void(void*), sk_free>>; |
| 487 |
|
| 488 |
} // namespace skia_private |
| 489 |
|
| 490 |
template<typename C, std::size_t... Is> |
| 491 |
constexpr auto SkMakeArrayFromIndexSequence(C c, std::index_sequence<Is...> is) |
| 492 |
-> std::array<decltype(c(std::declval<typename decltype(is)::value_type>())), sizeof...(Is)> { |
| 493 |
return {{ c(Is)... }}; |
| 494 |
} |
| 495 |
|
| 496 |
template<size_t N, typename C> constexpr auto SkMakeArray(C c) |
| 497 |
-> std::array<decltype(c(std::declval<typename std::index_sequence<N>::value_type>())), N> { |
| 498 |
return SkMakeArrayFromIndexSequence(c, std::make_index_sequence<N>{}); |
| 499 |
} |
| 500 |
|
| 501 |
#endif |