Chromium Code Reviews| Index: src/core/SkInPlace.h |
| diff --git a/src/core/SkInPlace.h b/src/core/SkInPlace.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..02421040f0f770c715bfdc77982a36b79f242928 |
| --- /dev/null |
| +++ b/src/core/SkInPlace.h |
| @@ -0,0 +1,78 @@ |
| +/* |
| + * Copyright 2015 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#ifndef SkInPlace_DEFINED |
| +#define SkInPlace_DEFINED |
| + |
| +#include "SkTypes.h" |
| + |
| +class SkInPlace { |
|
scroggo
2015/06/02 21:17:34
Can we merge this with SkSmallAllocator? It looks
reed2
2015/06/03 02:33:35
SmallAllocator is templated on size, meaning the p
|
| +public: |
| + SkInPlace(void* storage, size_t size) : fStorage(storage), fSize(size) {} |
| + |
| + template <typename T> T* create() { |
| + if (sizeof(T) <= fSize) { |
| + fSize = 0; |
| + return new (fStorage) T; |
|
scroggo
2015/06/02 21:17:34
SkNEW_PLACEMENT?
|
| + } else { |
| + return SkNEW(T); |
| + } |
| + } |
| + |
| + template <typename T, typename A1> T* create(const A1& a1) { |
| + if (sizeof(T) <= fSize) { |
| + fSize = 0; |
| + return new (fStorage) T(a1); |
|
scroggo
2015/06/02 21:17:34
SkNEW_PLACEMENT_ARGS?
|
| + } else { |
| + return SkNEW_ARGS(T, (a1)); |
| + } |
| + } |
| + |
| + template <typename T, typename A1, typename A2> T* create(const A1& a1, const A2& a2) { |
| + if (sizeof(T) <= fSize) { |
| + fSize = 0; |
| + return new (fStorage) T(a1, a1); |
| + } else { |
| + return SkNEW_ARGS(T, (a1, a2)); |
| + } |
| + } |
| + |
| + template <typename T, typename A1, typename A2, typename A3> T* create(const A1& a1, const A2& a2, const A3& a3) { |
|
scroggo
2015/06/02 21:17:34
nit: line length
|
| + if (sizeof(T) <= fSize) { |
| + fSize = 0; |
| + return new (fStorage) T(a1, a2, a3); |
| + } else { |
| + return SkNEW_ARGS(T, (a1, a2, a3)); |
| + } |
| + } |
| + |
| + template <typename T> void destroy(T* obj) { |
| + if (obj) { |
| + if ((T*)fStorage == obj) { |
| + obj->~T(); |
| + } else { |
| + delete obj; |
| + } |
| + } |
| + } |
| + |
| +private: |
| + void* fStorage; |
| + size_t fSize; |
| +}; |
| + |
| +template <size_t SIZE> class SkSInPlace : public SkInPlace { |
| +public: |
| + SkSInPlace() : SkInPlace(fStorage, SIZE) { |
| + SkASSERT(SIZE <= sizeof(fStorage)); |
| + } |
| + |
| +private: |
| + intptr_t fStorage[(SIZE + (sizeof(intptr_t) - 1)) / sizeof(intptr_t)]; |
| +}; |
| + |
| +#endif |