2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkTemplates_DEFINED
|
|
|
|
#define SkTemplates_DEFINED
|
|
|
|
|
2015-01-07 21:28:08 +00:00
|
|
|
#include "SkMath.h"
|
2015-09-07 19:45:52 +00:00
|
|
|
#include "SkTLogic.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkTypes.h"
|
2014-03-12 21:41:06 +00:00
|
|
|
#include <limits.h>
|
2016-03-16 17:28:35 +00:00
|
|
|
#include <memory>
|
2012-10-31 20:53:49 +00:00
|
|
|
#include <new>
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** \file SkTemplates.h
|
|
|
|
|
|
|
|
This file contains light-weight template classes for type-safe and exception-safe
|
|
|
|
resource management.
|
|
|
|
*/
|
|
|
|
|
2013-02-04 15:58:08 +00:00
|
|
|
/**
|
|
|
|
* Marks a local variable as known to be unused (to avoid warnings).
|
|
|
|
* Note that this does *not* prevent the local variable from being optimized away.
|
|
|
|
*/
|
|
|
|
template<typename T> inline void sk_ignore_unused_variable(const T&) { }
|
|
|
|
|
2013-05-07 15:28:15 +00:00
|
|
|
/**
|
|
|
|
* Returns a pointer to a D which comes immediately after S[count].
|
|
|
|
*/
|
|
|
|
template <typename D, typename S> static D* SkTAfter(S* ptr, size_t count = 1) {
|
|
|
|
return reinterpret_cast<D*>(ptr + count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a pointer to a D which comes byteOffset bytes after S.
|
|
|
|
*/
|
|
|
|
template <typename D, typename S> static D* SkTAddOffset(S* ptr, size_t byteOffset) {
|
2015-08-28 14:09:20 +00:00
|
|
|
// The intermediate char* has the same cv-ness as D as this produces better error messages.
|
2013-05-07 15:28:15 +00:00
|
|
|
// This relies on the fact that reinterpret_cast can add constness, but cannot remove it.
|
2015-08-28 14:09:20 +00:00
|
|
|
return reinterpret_cast<D*>(reinterpret_cast<sknonstd::same_cv_t<char, D>*>(ptr) + byteOffset);
|
2013-05-07 15:28:15 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 19:45:52 +00:00
|
|
|
template <typename R, typename T, R (*P)(T*)> struct SkFunctionWrapper {
|
|
|
|
R operator()(T* t) { return P(t); }
|
|
|
|
};
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** \class SkAutoTCallVProc
|
|
|
|
|
|
|
|
Call a function when this goes out of scope. The template uses two
|
|
|
|
parameters, the object, and a function that is to be called in the destructor.
|
2016-03-16 20:53:35 +00:00
|
|
|
If release() is called, the object reference is set to null. If the object
|
2008-12-17 15:59:43 +00:00
|
|
|
reference is null when the destructor is called, we do not call the
|
|
|
|
function.
|
|
|
|
*/
|
2016-01-25 03:49:24 +00:00
|
|
|
template <typename T, void (*P)(T*)> class SkAutoTCallVProc
|
2016-03-16 17:28:35 +00:00
|
|
|
: public std::unique_ptr<T, SkFunctionWrapper<void, T, P>> {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2016-03-16 17:28:35 +00:00
|
|
|
SkAutoTCallVProc(T* obj): std::unique_ptr<T, SkFunctionWrapper<void, T, P>>(obj) {}
|
2015-09-07 19:45:52 +00:00
|
|
|
|
2016-01-25 03:49:24 +00:00
|
|
|
operator T*() const { return this->get(); }
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** \class SkAutoTCallIProc
|
|
|
|
|
|
|
|
Call a function when this goes out of scope. The template uses two
|
|
|
|
parameters, the object, and a function that is to be called in the destructor.
|
2016-03-16 20:53:35 +00:00
|
|
|
If release() is called, the object reference is set to null. If the object
|
2008-12-17 15:59:43 +00:00
|
|
|
reference is null when the destructor is called, we do not call the
|
|
|
|
function.
|
|
|
|
*/
|
2016-01-25 03:49:24 +00:00
|
|
|
template <typename T, int (*P)(T*)> class SkAutoTCallIProc
|
2016-03-16 17:28:35 +00:00
|
|
|
: public std::unique_ptr<T, SkFunctionWrapper<int, T, P>> {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2016-03-16 17:28:35 +00:00
|
|
|
SkAutoTCallIProc(T* obj): std::unique_ptr<T, SkFunctionWrapper<int, T, P>>(obj) {}
|
2015-02-11 15:18:51 +00:00
|
|
|
|
2016-01-25 03:49:24 +00:00
|
|
|
operator T*() const { return this->get(); }
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2013-08-29 22:14:04 +00:00
|
|
|
/** \class SkAutoTDelete
|
|
|
|
An SkAutoTDelete<T> is like a T*, except that the destructor of SkAutoTDelete<T>
|
|
|
|
automatically deletes the pointer it holds (if any). That is, SkAutoTDelete<T>
|
|
|
|
owns the T object that it points to. Like a T*, an SkAutoTDelete<T> may hold
|
|
|
|
either NULL or a pointer to a T object. Also like T*, SkAutoTDelete<T> is
|
|
|
|
thread-compatible, and once you dereference it, you get the threadsafety
|
|
|
|
guarantees of T.
|
|
|
|
|
|
|
|
The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*)
|
|
|
|
*/
|
2016-03-16 17:28:35 +00:00
|
|
|
template <typename T> class SkAutoTDelete : public std::unique_ptr<T> {
|
2013-04-23 15:37:27 +00:00
|
|
|
public:
|
2016-03-16 17:28:35 +00:00
|
|
|
SkAutoTDelete(T* obj = NULL) : std::unique_ptr<T>(obj) {}
|
2016-01-25 03:18:54 +00:00
|
|
|
|
2016-01-25 03:49:24 +00:00
|
|
|
operator T*() const { return this->get(); }
|
2016-03-16 17:28:35 +00:00
|
|
|
|
2016-03-17 00:33:54 +00:00
|
|
|
#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
|
|
|
|
// Need to update graphics/BitmapRegionDecoder.cpp.
|
|
|
|
T* detach() { return this->release(); }
|
|
|
|
#endif
|
2013-04-23 15:37:27 +00:00
|
|
|
};
|
|
|
|
|
2016-03-16 17:28:35 +00:00
|
|
|
template <typename T> class SkAutoTDeleteArray : public std::unique_ptr<T[]> {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2016-03-16 17:28:35 +00:00
|
|
|
SkAutoTDeleteArray(T array[]) : std::unique_ptr<T[]>(array) {}
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Allocate an array of T elements, and free the array in the destructor
|
|
|
|
*/
|
|
|
|
template <typename T> class SkAutoTArray : SkNoncopyable {
|
|
|
|
public:
|
2012-08-14 15:10:09 +00:00
|
|
|
SkAutoTArray() {
|
|
|
|
fArray = NULL;
|
|
|
|
SkDEBUGCODE(fCount = 0;)
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Allocate count number of T elements
|
|
|
|
*/
|
2012-08-14 15:10:09 +00:00
|
|
|
explicit SkAutoTArray(int count) {
|
|
|
|
SkASSERT(count >= 0);
|
|
|
|
fArray = NULL;
|
|
|
|
if (count) {
|
2015-08-26 20:07:48 +00:00
|
|
|
fArray = new T[count];
|
2012-08-14 15:10:09 +00:00
|
|
|
}
|
|
|
|
SkDEBUGCODE(fCount = count;)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Reallocates given a new count. Reallocation occurs even if new count equals old count.
|
|
|
|
*/
|
|
|
|
void reset(int count) {
|
2015-08-26 20:07:48 +00:00
|
|
|
delete[] fArray;
|
2012-08-14 15:10:09 +00:00
|
|
|
SkASSERT(count >= 0);
|
2008-12-17 15:59:43 +00:00
|
|
|
fArray = NULL;
|
|
|
|
if (count) {
|
2015-08-26 20:07:48 +00:00
|
|
|
fArray = new T[count];
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
SkDEBUGCODE(fCount = count;)
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
~SkAutoTArray() { delete[] fArray; }
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** Return the array of T elements. Will be NULL if count == 0
|
|
|
|
*/
|
|
|
|
T* get() const { return fArray; }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the nth element in the array
|
|
|
|
*/
|
|
|
|
T& operator[](int index) const {
|
2012-08-14 15:10:09 +00:00
|
|
|
SkASSERT((unsigned)index < (unsigned)fCount);
|
2008-12-17 15:59:43 +00:00
|
|
|
return fArray[index];
|
|
|
|
}
|
|
|
|
|
2015-02-12 21:20:08 +00:00
|
|
|
void swap(SkAutoTArray& other) {
|
|
|
|
SkTSwap(fArray, other.fArray);
|
|
|
|
SkDEBUGCODE(SkTSwap(fCount, other.fCount));
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
T* fArray;
|
2012-08-14 15:10:09 +00:00
|
|
|
SkDEBUGCODE(int fCount;)
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2016-02-05 15:02:38 +00:00
|
|
|
/** Wraps SkAutoTArray, with room for kCountRequested elements preallocated.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2016-02-05 15:02:38 +00:00
|
|
|
template <int kCountRequested, typename T> class SkAutoSTArray : SkNoncopyable {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2013-06-13 15:13:46 +00:00
|
|
|
/** Initialize with no objects */
|
|
|
|
SkAutoSTArray() {
|
|
|
|
fArray = NULL;
|
|
|
|
fCount = 0;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Allocate count number of T elements
|
|
|
|
*/
|
2013-10-14 21:53:24 +00:00
|
|
|
SkAutoSTArray(int count) {
|
2013-06-13 15:13:46 +00:00
|
|
|
fArray = NULL;
|
|
|
|
fCount = 0;
|
|
|
|
this->reset(count);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
~SkAutoSTArray() {
|
2013-06-13 15:13:46 +00:00
|
|
|
this->reset(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Destroys previous objects in the array and default constructs count number of objects */
|
2013-10-14 21:53:24 +00:00
|
|
|
void reset(int count) {
|
2013-05-29 20:10:25 +00:00
|
|
|
T* start = fArray;
|
|
|
|
T* iter = start + fCount;
|
|
|
|
while (iter > start) {
|
|
|
|
(--iter)->~T();
|
|
|
|
}
|
2013-06-13 15:13:46 +00:00
|
|
|
|
|
|
|
if (fCount != count) {
|
2016-02-05 15:02:38 +00:00
|
|
|
if (fCount > kCount) {
|
2013-07-12 18:44:23 +00:00
|
|
|
// 'fArray' was allocated last time so free it now
|
|
|
|
SkASSERT((T*) fStorage != fArray);
|
2013-06-13 15:13:46 +00:00
|
|
|
sk_free(fArray);
|
|
|
|
}
|
|
|
|
|
2016-02-05 15:02:38 +00:00
|
|
|
if (count > kCount) {
|
2015-01-07 21:28:08 +00:00
|
|
|
const uint64_t size64 = sk_64_mul(count, sizeof(T));
|
|
|
|
const size_t size = static_cast<size_t>(size64);
|
|
|
|
if (size != size64) {
|
|
|
|
sk_out_of_memory();
|
|
|
|
}
|
|
|
|
fArray = (T*) sk_malloc_throw(size);
|
2013-06-13 15:13:46 +00:00
|
|
|
} else if (count > 0) {
|
|
|
|
fArray = (T*) fStorage;
|
|
|
|
} else {
|
|
|
|
fArray = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fCount = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter = fArray;
|
|
|
|
T* stop = fArray + count;
|
|
|
|
while (iter < stop) {
|
2015-08-26 20:07:48 +00:00
|
|
|
new (iter++) T;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the number of T elements in the array
|
|
|
|
*/
|
2013-10-14 21:53:24 +00:00
|
|
|
int count() const { return fCount; }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the array of T elements. Will be NULL if count == 0
|
|
|
|
*/
|
|
|
|
T* get() const { return fArray; }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the nth element in the array
|
|
|
|
*/
|
|
|
|
T& operator[](int index) const {
|
2013-10-14 21:53:24 +00:00
|
|
|
SkASSERT(index < fCount);
|
2008-12-17 15:59:43 +00:00
|
|
|
return fArray[index];
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2016-02-05 15:02:38 +00:00
|
|
|
#if defined(GOOGLE3)
|
|
|
|
// Stack frame size is limited for GOOGLE3. 4k is less than the actual max, but some functions
|
|
|
|
// have multiple large stack allocations.
|
|
|
|
static const int kMaxBytes = 4 * 1024;
|
|
|
|
static const int kCount = kCountRequested * sizeof(T) > kMaxBytes
|
|
|
|
? kMaxBytes / sizeof(T)
|
|
|
|
: kCountRequested;
|
|
|
|
#else
|
|
|
|
static const int kCount = kCountRequested;
|
|
|
|
#endif
|
|
|
|
|
2013-10-14 21:53:24 +00:00
|
|
|
int fCount;
|
2008-12-17 15:59:43 +00:00
|
|
|
T* fArray;
|
|
|
|
// since we come right after fArray, fStorage should be properly aligned
|
2016-02-05 15:02:38 +00:00
|
|
|
char fStorage[kCount * sizeof(T)];
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2013-05-07 15:28:15 +00:00
|
|
|
/** Manages an array of T elements, freeing the array in the destructor.
|
|
|
|
* Does NOT call any constructors/destructors on T (T must be POD).
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
template <typename T> class SkAutoTMalloc : SkNoncopyable {
|
|
|
|
public:
|
2013-05-07 15:28:15 +00:00
|
|
|
/** Takes ownership of the ptr. The ptr must be a value which can be passed to sk_free. */
|
|
|
|
explicit SkAutoTMalloc(T* ptr = NULL) {
|
|
|
|
fPtr = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Allocates space for 'count' Ts. */
|
|
|
|
explicit SkAutoTMalloc(size_t count) {
|
2014-06-26 15:04:28 +00:00
|
|
|
fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-06-30 21:32:31 +00:00
|
|
|
|
|
|
|
~SkAutoTMalloc() {
|
2008-12-17 15:59:43 +00:00
|
|
|
sk_free(fPtr);
|
|
|
|
}
|
2011-06-30 21:32:31 +00:00
|
|
|
|
2013-05-07 15:28:15 +00:00
|
|
|
/** Resize the memory area pointed to by the current ptr preserving contents. */
|
|
|
|
void realloc(size_t count) {
|
|
|
|
fPtr = reinterpret_cast<T*>(sk_realloc_throw(fPtr, count * sizeof(T)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Resize the memory area pointed to by the current ptr without preserving contents. */
|
2016-03-17 17:51:27 +00:00
|
|
|
T* reset(size_t count = 0) {
|
2011-06-30 21:32:31 +00:00
|
|
|
sk_free(fPtr);
|
2016-03-17 17:51:27 +00:00
|
|
|
fPtr = count ? (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW) : nullptr;
|
2015-12-10 18:44:13 +00:00
|
|
|
return fPtr;
|
2011-06-30 21:32:31 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
T* get() const { return fPtr; }
|
|
|
|
|
2011-06-30 21:32:31 +00:00
|
|
|
operator T*() {
|
|
|
|
return fPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator const T*() const {
|
|
|
|
return fPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator[](int index) {
|
|
|
|
return fPtr[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
const T& operator[](int index) const {
|
|
|
|
return fPtr[index];
|
|
|
|
}
|
|
|
|
|
2013-05-07 15:28:15 +00:00
|
|
|
/**
|
|
|
|
* Transfer ownership of the ptr to the caller, setting the internal
|
|
|
|
* pointer to NULL. Note that this differs from get(), which also returns
|
|
|
|
* the pointer, but it does not transfer ownership.
|
|
|
|
*/
|
2016-03-16 20:53:35 +00:00
|
|
|
T* release() {
|
2013-05-07 15:28:15 +00:00
|
|
|
T* ptr = fPtr;
|
|
|
|
fPtr = NULL;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2013-05-07 15:28:15 +00:00
|
|
|
T* fPtr;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2016-02-05 15:02:38 +00:00
|
|
|
template <size_t kCountRequested, typename T> class SkAutoSTMalloc : SkNoncopyable {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2015-04-08 14:33:33 +00:00
|
|
|
SkAutoSTMalloc() : fPtr(fTStorage) {}
|
2013-05-01 14:21:20 +00:00
|
|
|
|
2011-06-30 21:32:31 +00:00
|
|
|
SkAutoSTMalloc(size_t count) {
|
2016-02-05 15:02:38 +00:00
|
|
|
if (count > kCount) {
|
2013-05-01 14:21:20 +00:00
|
|
|
fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLOC_TEMP);
|
2011-06-30 21:32:31 +00:00
|
|
|
} else {
|
2015-04-08 14:33:33 +00:00
|
|
|
fPtr = fTStorage;
|
2011-06-30 21:32:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~SkAutoSTMalloc() {
|
|
|
|
if (fPtr != fTStorage) {
|
|
|
|
sk_free(fPtr);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-06-30 21:32:31 +00:00
|
|
|
|
|
|
|
// doesn't preserve contents
|
2013-06-10 18:58:11 +00:00
|
|
|
T* reset(size_t count) {
|
2011-06-30 21:32:31 +00:00
|
|
|
if (fPtr != fTStorage) {
|
2008-12-17 15:59:43 +00:00
|
|
|
sk_free(fPtr);
|
2011-06-30 21:32:31 +00:00
|
|
|
}
|
2016-02-05 15:02:38 +00:00
|
|
|
if (count > kCount) {
|
2015-04-08 14:33:33 +00:00
|
|
|
fPtr = (T*)sk_malloc_throw(count * sizeof(T));
|
2011-06-30 21:32:31 +00:00
|
|
|
} else {
|
2015-04-08 14:33:33 +00:00
|
|
|
fPtr = fTStorage;
|
2011-06-30 21:32:31 +00:00
|
|
|
}
|
2013-06-10 18:58:11 +00:00
|
|
|
return fPtr;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-06-30 21:32:31 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
T* get() const { return fPtr; }
|
|
|
|
|
2011-06-30 21:32:31 +00:00
|
|
|
operator T*() {
|
|
|
|
return fPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator const T*() const {
|
|
|
|
return fPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator[](int index) {
|
|
|
|
return fPtr[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
const T& operator[](int index) const {
|
|
|
|
return fPtr[index];
|
|
|
|
}
|
|
|
|
|
2015-04-08 14:33:33 +00:00
|
|
|
// Reallocs the array, can be used to shrink the allocation. Makes no attempt to be intelligent
|
|
|
|
void realloc(size_t count) {
|
2016-02-05 15:02:38 +00:00
|
|
|
if (count > kCount) {
|
2015-04-08 14:33:33 +00:00
|
|
|
if (fPtr == fTStorage) {
|
|
|
|
fPtr = (T*)sk_malloc_throw(count * sizeof(T));
|
2016-02-05 15:02:38 +00:00
|
|
|
memcpy(fPtr, fTStorage, kCount * sizeof(T));
|
2015-04-08 14:33:33 +00:00
|
|
|
} else {
|
|
|
|
fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T));
|
|
|
|
}
|
|
|
|
} else if (fPtr != fTStorage) {
|
|
|
|
fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2016-02-05 15:02:38 +00:00
|
|
|
// Since we use uint32_t storage, we might be able to get more elements for free.
|
|
|
|
static const size_t kCountWithPadding = SkAlign4(kCountRequested*sizeof(T)) / sizeof(T);
|
|
|
|
#if defined(GOOGLE3)
|
|
|
|
// Stack frame size is limited for GOOGLE3. 4k is less than the actual max, but some functions
|
|
|
|
// have multiple large stack allocations.
|
|
|
|
static const size_t kMaxBytes = 4 * 1024;
|
|
|
|
static const size_t kCount = kCountRequested * sizeof(T) > kMaxBytes
|
|
|
|
? kMaxBytes / sizeof(T)
|
|
|
|
: kCountWithPadding;
|
|
|
|
#else
|
|
|
|
static const size_t kCount = kCountWithPadding;
|
|
|
|
#endif
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
T* fPtr;
|
|
|
|
union {
|
2016-02-05 15:02:38 +00:00
|
|
|
uint32_t fStorage32[SkAlign4(kCount*sizeof(T)) >> 2];
|
2008-12-17 15:59:43 +00:00
|
|
|
T fTStorage[1]; // do NOT want to invoke T::T()
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-06-04 13:31:31 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pass the object and the storage that was offered during SkInPlaceNewCheck, and this will
|
|
|
|
* safely destroy (and free if it was dynamically allocated) the object.
|
|
|
|
*/
|
|
|
|
template <typename T> void SkInPlaceDeleteCheck(T* obj, void* storage) {
|
|
|
|
if (storage == obj) {
|
|
|
|
obj->~T();
|
|
|
|
} else {
|
2015-08-26 20:07:48 +00:00
|
|
|
delete obj;
|
2015-06-04 13:31:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates T, using storage if it is large enough, and allocating on the heap (via new) if
|
|
|
|
* storage is not large enough.
|
|
|
|
*
|
|
|
|
* obj = SkInPlaceNewCheck<Type>(storage, size);
|
|
|
|
* ...
|
|
|
|
* SkInPlaceDeleteCheck(obj, storage);
|
|
|
|
*/
|
|
|
|
template <typename T> T* SkInPlaceNewCheck(void* storage, size_t size) {
|
2015-08-26 20:07:48 +00:00
|
|
|
return (sizeof(T) <= size) ? new (storage) T : new T;
|
2015-06-04 13:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename A1, typename A2, typename A3>
|
|
|
|
T* SkInPlaceNewCheck(void* storage, size_t size, const A1& a1, const A2& a2, const A3& a3) {
|
2015-08-26 20:07:48 +00:00
|
|
|
return (sizeof(T) <= size) ? new (storage) T(a1, a2, a3) : new T(a1, a2, a3);
|
2015-06-04 13:31:31 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 18:41:47 +00:00
|
|
|
template <typename T, typename A1, typename A2, typename A3, typename A4>
|
|
|
|
T* SkInPlaceNewCheck(void* storage, size_t size,
|
|
|
|
const A1& a1, const A2& a2, const A3& a3, const A4& a4) {
|
|
|
|
return (sizeof(T) <= size) ? new (storage) T(a1, a2, a3, a4) : new T(a1, a2, a3, a4);
|
|
|
|
}
|
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
/**
|
|
|
|
* Reserves memory that is aligned on double and pointer boundaries.
|
|
|
|
* Hopefully this is sufficient for all practical purposes.
|
|
|
|
*/
|
|
|
|
template <size_t N> class SkAlignedSStorage : SkNoncopyable {
|
|
|
|
public:
|
2015-06-04 13:31:31 +00:00
|
|
|
size_t size() const { return N; }
|
2011-09-14 13:54:05 +00:00
|
|
|
void* get() { return fData; }
|
2014-12-15 22:16:27 +00:00
|
|
|
const void* get() const { return fData; }
|
2015-06-04 13:31:31 +00:00
|
|
|
|
2011-09-14 13:54:05 +00:00
|
|
|
private:
|
|
|
|
union {
|
|
|
|
void* fPtr;
|
|
|
|
double fDouble;
|
|
|
|
char fData[N];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reserves memory that is aligned on double and pointer boundaries.
|
|
|
|
* Hopefully this is sufficient for all practical purposes. Otherwise,
|
|
|
|
* we have to do some arcane trickery to determine alignment of non-POD
|
|
|
|
* types. Lifetime of the memory is the lifetime of the object.
|
|
|
|
*/
|
|
|
|
template <int N, typename T> class SkAlignedSTStorage : SkNoncopyable {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Returns void* because this object does not initialize the
|
|
|
|
* memory. Use placement new for types that require a cons.
|
|
|
|
*/
|
|
|
|
void* get() { return fStorage.get(); }
|
2015-08-11 21:47:42 +00:00
|
|
|
const void* get() const { return fStorage.get(); }
|
2011-09-14 13:54:05 +00:00
|
|
|
private:
|
|
|
|
SkAlignedSStorage<sizeof(T)*N> fStorage;
|
|
|
|
};
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|