OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google, Inc | 2 * Copyright 2014 Google, Inc |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkSmallAllocator_DEFINED | 8 #ifndef SkSmallAllocator_DEFINED |
9 #define SkSmallAllocator_DEFINED | 9 #define SkSmallAllocator_DEFINED |
10 | 10 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 * arguments. | 56 * arguments. |
57 * Note: If kMaxObjects have been created by this SkSmallAllocator, NULL | 57 * Note: If kMaxObjects have been created by this SkSmallAllocator, NULL |
58 * will be returned. | 58 * will be returned. |
59 */ | 59 */ |
60 template<typename T> | 60 template<typename T> |
61 T* createT() { | 61 T* createT() { |
62 void* buf = this->reserveT<T>(); | 62 void* buf = this->reserveT<T>(); |
63 if (NULL == buf) { | 63 if (NULL == buf) { |
64 return NULL; | 64 return NULL; |
65 } | 65 } |
66 SkNEW_PLACEMENT(buf, T); | 66 new (buf) T; |
67 return static_cast<T*>(buf); | 67 return static_cast<T*>(buf); |
68 } | 68 } |
69 | 69 |
70 template<typename T, typename A1> T* createT(const A1& a1) { | 70 template<typename T, typename A1> T* createT(const A1& a1) { |
71 void* buf = this->reserveT<T>(); | 71 void* buf = this->reserveT<T>(); |
72 if (NULL == buf) { | 72 if (NULL == buf) { |
73 return NULL; | 73 return NULL; |
74 } | 74 } |
75 SkNEW_PLACEMENT_ARGS(buf, T, (a1)); | 75 new (buf) T(a1); |
76 return static_cast<T*>(buf); | 76 return static_cast<T*>(buf); |
77 } | 77 } |
78 | 78 |
79 template<typename T, typename A1, typename A2> | 79 template<typename T, typename A1, typename A2> |
80 T* createT(const A1& a1, const A2& a2) { | 80 T* createT(const A1& a1, const A2& a2) { |
81 void* buf = this->reserveT<T>(); | 81 void* buf = this->reserveT<T>(); |
82 if (NULL == buf) { | 82 if (NULL == buf) { |
83 return NULL; | 83 return NULL; |
84 } | 84 } |
85 SkNEW_PLACEMENT_ARGS(buf, T, (a1, a2)); | 85 new (buf) T(a1, a2); |
86 return static_cast<T*>(buf); | 86 return static_cast<T*>(buf); |
87 } | 87 } |
88 | 88 |
89 template<typename T, typename A1, typename A2, typename A3> | 89 template<typename T, typename A1, typename A2, typename A3> |
90 T* createT(const A1& a1, const A2& a2, const A3& a3) { | 90 T* createT(const A1& a1, const A2& a2, const A3& a3) { |
91 void* buf = this->reserveT<T>(); | 91 void* buf = this->reserveT<T>(); |
92 if (NULL == buf) { | 92 if (NULL == buf) { |
93 return NULL; | 93 return NULL; |
94 } | 94 } |
95 SkNEW_PLACEMENT_ARGS(buf, T, (a1, a2, a3)); | 95 new (buf) T(a1, a2, a3); |
96 return static_cast<T*>(buf); | 96 return static_cast<T*>(buf); |
97 } | 97 } |
98 | 98 |
99 template<typename T, typename A1, typename A2, typename A3, typename A4> | 99 template<typename T, typename A1, typename A2, typename A3, typename A4> |
100 T* createT(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { | 100 T* createT(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { |
101 void* buf = this->reserveT<T>(); | 101 void* buf = this->reserveT<T>(); |
102 if (NULL == buf) { | 102 if (NULL == buf) { |
103 return NULL; | 103 return NULL; |
104 } | 104 } |
105 SkNEW_PLACEMENT_ARGS(buf, T, (a1, a2, a3, a4)); | 105 new (buf) T(a1, a2, a3, a4); |
106 return static_cast<T*>(buf); | 106 return static_cast<T*>(buf); |
107 } | 107 } |
108 | 108 |
109 /* | 109 /* |
110 * Reserve a specified amount of space (must be enough space for one T). | 110 * Reserve a specified amount of space (must be enough space for one T). |
111 * The space will be in fStorage if there is room, or on the heap otherwise
. | 111 * The space will be in fStorage if there is room, or on the heap otherwise
. |
112 * Either way, this class will call ~T() in its destructor and free the hea
p | 112 * Either way, this class will call ~T() in its destructor and free the hea
p |
113 * allocation if necessary. | 113 * allocation if necessary. |
114 * Unlike createT(), this method will not call the constructor of T. | 114 * Unlike createT(), this method will not call the constructor of T. |
115 */ | 115 */ |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 167 |
168 // Number of bytes used so far. | 168 // Number of bytes used so far. |
169 size_t fStorageUsed; | 169 size_t fStorageUsed; |
170 // Pad the storage size to be 4-byte aligned. | 170 // Pad the storage size to be 4-byte aligned. |
171 uint32_t fStorage[SkAlign4(kTotalBytes) >> 2]; | 171 uint32_t fStorage[SkAlign4(kTotalBytes) >> 2]; |
172 uint32_t fNumObjects; | 172 uint32_t fNumObjects; |
173 Rec fRecs[kMaxObjects]; | 173 Rec fRecs[kMaxObjects]; |
174 }; | 174 }; |
175 | 175 |
176 #endif // SkSmallAllocator_DEFINED | 176 #endif // SkSmallAllocator_DEFINED |
OLD | NEW |