Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: src/core/SkSmallAllocator.h

Issue 1316233002: Style Change: NULL->nullptr (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2015-08-27 (Thursday) 10:25:06 EDT Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkShader.cpp ('k') | src/core/SkSpriteBlitter_ARGB32.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 #include "SkTDArray.h" 11 #include "SkTDArray.h"
12 #include "SkTypes.h" 12 #include "SkTypes.h"
13 13
14 // Used by SkSmallAllocator to call the destructor for objects it has 14 // Used by SkSmallAllocator to call the destructor for objects it has
15 // allocated. 15 // allocated.
16 template<typename T> void destroyT(void* ptr) { 16 template<typename T> void destroyT(void* ptr) {
17 static_cast<T*>(ptr)->~T(); 17 static_cast<T*>(ptr)->~T();
18 } 18 }
19 19
20 /* 20 /*
21 * Template class for allocating small objects without additional heap memory 21 * Template class for allocating small objects without additional heap memory
22 * allocations. kMaxObjects is a hard limit on the number of objects that can 22 * allocations. kMaxObjects is a hard limit on the number of objects that can
23 * be allocated using this class. After that, attempts to create more objects 23 * be allocated using this class. After that, attempts to create more objects
24 * with this class will assert and return NULL. 24 * with this class will assert and return nullptr.
25 * kTotalBytes is the total number of bytes provided for storage for all 25 * kTotalBytes is the total number of bytes provided for storage for all
26 * objects created by this allocator. If an object to be created is larger 26 * objects created by this allocator. If an object to be created is larger
27 * than the storage (minus storage already used), it will be allocated on the 27 * than the storage (minus storage already used), it will be allocated on the
28 * heap. This class's destructor will handle calling the destructor for each 28 * heap. This class's destructor will handle calling the destructor for each
29 * object it allocated and freeing its memory. 29 * object it allocated and freeing its memory.
30 */ 30 */
31 template<uint32_t kMaxObjects, size_t kTotalBytes> 31 template<uint32_t kMaxObjects, size_t kTotalBytes>
32 class SkSmallAllocator : SkNoncopyable { 32 class SkSmallAllocator : SkNoncopyable {
33 public: 33 public:
34 SkSmallAllocator() 34 SkSmallAllocator()
35 : fStorageUsed(0) 35 : fStorageUsed(0)
36 , fNumObjects(0) 36 , fNumObjects(0)
37 {} 37 {}
38 38
39 ~SkSmallAllocator() { 39 ~SkSmallAllocator() {
40 // Destruct in reverse order, in case an earlier object points to a 40 // Destruct in reverse order, in case an earlier object points to a
41 // later object. 41 // later object.
42 while (fNumObjects > 0) { 42 while (fNumObjects > 0) {
43 fNumObjects--; 43 fNumObjects--;
44 Rec* rec = &fRecs[fNumObjects]; 44 Rec* rec = &fRecs[fNumObjects];
45 rec->fKillProc(rec->fObj); 45 rec->fKillProc(rec->fObj);
46 // Safe to do if fObj is in fStorage, since fHeapStorage will 46 // Safe to do if fObj is in fStorage, since fHeapStorage will
47 // point to NULL. 47 // point to nullptr.
48 sk_free(rec->fHeapStorage); 48 sk_free(rec->fHeapStorage);
49 } 49 }
50 } 50 }
51 51
52 /* 52 /*
53 * Create a new object of type T. Its lifetime will be handled by this 53 * Create a new object of type T. Its lifetime will be handled by this
54 * SkSmallAllocator. 54 * SkSmallAllocator.
55 * Each version behaves the same but takes a different number of 55 * Each version behaves the same but takes a different number of
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, nullptr
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 (nullptr == buf) {
64 return NULL; 64 return nullptr;
65 } 65 }
66 new (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 (nullptr == buf) {
73 return NULL; 73 return nullptr;
74 } 74 }
75 new (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 (nullptr == buf) {
83 return NULL; 83 return nullptr;
84 } 84 }
85 new (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 (nullptr == buf) {
93 return NULL; 93 return nullptr;
94 } 94 }
95 new (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 (nullptr == buf) {
103 return NULL; 103 return nullptr;
104 } 104 }
105 new (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 */
116 template<typename T> void* reserveT(size_t storageRequired = sizeof(T)) { 116 template<typename T> void* reserveT(size_t storageRequired = sizeof(T)) {
117 SkASSERT(fNumObjects < kMaxObjects); 117 SkASSERT(fNumObjects < kMaxObjects);
118 SkASSERT(storageRequired >= sizeof(T)); 118 SkASSERT(storageRequired >= sizeof(T));
119 if (kMaxObjects == fNumObjects) { 119 if (kMaxObjects == fNumObjects) {
120 return NULL; 120 return nullptr;
121 } 121 }
122 const size_t storageRemaining = SkAlign4(kTotalBytes) - fStorageUsed; 122 const size_t storageRemaining = SkAlign4(kTotalBytes) - fStorageUsed;
123 storageRequired = SkAlign4(storageRequired); 123 storageRequired = SkAlign4(storageRequired);
124 Rec* rec = &fRecs[fNumObjects]; 124 Rec* rec = &fRecs[fNumObjects];
125 if (storageRequired > storageRemaining) { 125 if (storageRequired > storageRemaining) {
126 // Allocate on the heap. Ideally we want to avoid this situation, 126 // Allocate on the heap. Ideally we want to avoid this situation,
127 // but we're not sure we can catch all callers, so handle it but 127 // but we're not sure we can catch all callers, so handle it but
128 // assert false in debug mode. 128 // assert false in debug mode.
129 SkASSERT(false); 129 SkASSERT(false);
130 rec->fStorageSize = 0; 130 rec->fStorageSize = 0;
131 rec->fHeapStorage = sk_malloc_throw(storageRequired); 131 rec->fHeapStorage = sk_malloc_throw(storageRequired);
132 rec->fObj = static_cast<void*>(rec->fHeapStorage); 132 rec->fObj = static_cast<void*>(rec->fHeapStorage);
133 } else { 133 } else {
134 // There is space in fStorage. 134 // There is space in fStorage.
135 rec->fStorageSize = storageRequired; 135 rec->fStorageSize = storageRequired;
136 rec->fHeapStorage = NULL; 136 rec->fHeapStorage = nullptr;
137 SkASSERT(SkIsAlign4(fStorageUsed)); 137 SkASSERT(SkIsAlign4(fStorageUsed));
138 rec->fObj = static_cast<void*>(fStorage + (fStorageUsed / 4)); 138 rec->fObj = static_cast<void*>(fStorage + (fStorageUsed / 4));
139 fStorageUsed += storageRequired; 139 fStorageUsed += storageRequired;
140 } 140 }
141 rec->fKillProc = destroyT<T>; 141 rec->fKillProc = destroyT<T>;
142 fNumObjects++; 142 fNumObjects++;
143 return rec->fObj; 143 return rec->fObj;
144 } 144 }
145 145
146 /* 146 /*
(...skipping 20 matching lines...) Expand all
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
OLDNEW
« no previous file with comments | « src/core/SkShader.cpp ('k') | src/core/SkSpriteBlitter_ARGB32.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698