Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2015 Google Inc. | |
| 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 SkInPlace_DEFINED | |
| 9 #define SkInPlace_DEFINED | |
| 10 | |
| 11 #include "SkTypes.h" | |
| 12 | |
| 13 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
| |
| 14 public: | |
| 15 SkInPlace(void* storage, size_t size) : fStorage(storage), fSize(size) {} | |
| 16 | |
| 17 template <typename T> T* create() { | |
| 18 if (sizeof(T) <= fSize) { | |
| 19 fSize = 0; | |
| 20 return new (fStorage) T; | |
|
scroggo
2015/06/02 21:17:34
SkNEW_PLACEMENT?
| |
| 21 } else { | |
| 22 return SkNEW(T); | |
| 23 } | |
| 24 } | |
| 25 | |
| 26 template <typename T, typename A1> T* create(const A1& a1) { | |
| 27 if (sizeof(T) <= fSize) { | |
| 28 fSize = 0; | |
| 29 return new (fStorage) T(a1); | |
|
scroggo
2015/06/02 21:17:34
SkNEW_PLACEMENT_ARGS?
| |
| 30 } else { | |
| 31 return SkNEW_ARGS(T, (a1)); | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 template <typename T, typename A1, typename A2> T* create(const A1& a1, cons t A2& a2) { | |
| 36 if (sizeof(T) <= fSize) { | |
| 37 fSize = 0; | |
| 38 return new (fStorage) T(a1, a1); | |
| 39 } else { | |
| 40 return SkNEW_ARGS(T, (a1, a2)); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 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
| |
| 45 if (sizeof(T) <= fSize) { | |
| 46 fSize = 0; | |
| 47 return new (fStorage) T(a1, a2, a3); | |
| 48 } else { | |
| 49 return SkNEW_ARGS(T, (a1, a2, a3)); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 template <typename T> void destroy(T* obj) { | |
| 54 if (obj) { | |
| 55 if ((T*)fStorage == obj) { | |
| 56 obj->~T(); | |
| 57 } else { | |
| 58 delete obj; | |
| 59 } | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 void* fStorage; | |
| 65 size_t fSize; | |
| 66 }; | |
| 67 | |
| 68 template <size_t SIZE> class SkSInPlace : public SkInPlace { | |
| 69 public: | |
| 70 SkSInPlace() : SkInPlace(fStorage, SIZE) { | |
| 71 SkASSERT(SIZE <= sizeof(fStorage)); | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 intptr_t fStorage[(SIZE + (sizeof(intptr_t) - 1)) / sizeof(intptr_t)]; | |
| 76 }; | |
| 77 | |
| 78 #endif | |
| OLD | NEW |