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

Side by Side Diff: include/private/SkTemplates.h

Issue 2084213003: Make container classes in SkTemplates.h more consistent (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: break dependency Created 4 years, 6 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 | « no previous file | tests/TemplatesTest.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 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #ifndef SkTemplates_DEFINED 10 #ifndef SkTemplates_DEFINED
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 } 185 }
186 186
187 /** Destroys previous objects in the array and default constructs count numb er of objects */ 187 /** Destroys previous objects in the array and default constructs count numb er of objects */
188 void reset(int count) { 188 void reset(int count) {
189 T* start = fArray; 189 T* start = fArray;
190 T* iter = start + fCount; 190 T* iter = start + fCount;
191 while (iter > start) { 191 while (iter > start) {
192 (--iter)->~T(); 192 (--iter)->~T();
193 } 193 }
194 194
195 SkASSERT(count >= 0);
195 if (fCount != count) { 196 if (fCount != count) {
196 if (fCount > kCount) { 197 if (fCount > kCount) {
197 // 'fArray' was allocated last time so free it now 198 // 'fArray' was allocated last time so free it now
198 SkASSERT((T*) fStorage != fArray); 199 SkASSERT((T*) fStorage != fArray);
199 sk_free(fArray); 200 sk_free(fArray);
200 } 201 }
201 202
202 if (count > kCount) { 203 if (count > kCount) {
203 const uint64_t size64 = sk_64_mul(count, sizeof(T)); 204 const uint64_t size64 = sk_64_mul(count, sizeof(T));
204 const size_t size = static_cast<size_t>(size64); 205 const size_t size = static_cast<size_t>(size64);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 */ 261 */
261 template <typename T> class SkAutoTMalloc : SkNoncopyable { 262 template <typename T> class SkAutoTMalloc : SkNoncopyable {
262 public: 263 public:
263 /** Takes ownership of the ptr. The ptr must be a value which can be passed to sk_free. */ 264 /** Takes ownership of the ptr. The ptr must be a value which can be passed to sk_free. */
264 explicit SkAutoTMalloc(T* ptr = NULL) { 265 explicit SkAutoTMalloc(T* ptr = NULL) {
265 fPtr = ptr; 266 fPtr = ptr;
266 } 267 }
267 268
268 /** Allocates space for 'count' Ts. */ 269 /** Allocates space for 'count' Ts. */
269 explicit SkAutoTMalloc(size_t count) { 270 explicit SkAutoTMalloc(size_t count) {
270 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW); 271 fPtr = count ? (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW) : nullptr;
271 } 272 }
272 273
273 ~SkAutoTMalloc() { 274 ~SkAutoTMalloc() {
274 sk_free(fPtr); 275 sk_free(fPtr);
275 } 276 }
276 277
277 /** Resize the memory area pointed to by the current ptr preserving contents . */ 278 /** Resize the memory area pointed to by the current ptr preserving contents . */
278 void realloc(size_t count) { 279 void realloc(size_t count) {
279 fPtr = reinterpret_cast<T*>(sk_realloc_throw(fPtr, count * sizeof(T))); 280 if (count) {
281 fPtr = reinterpret_cast<T*>(sk_realloc_throw(fPtr, count * sizeof(T) ));
282 } else {
283 this->reset(0);
284 }
280 } 285 }
281 286
282 /** Resize the memory area pointed to by the current ptr without preserving contents. */ 287 /** Resize the memory area pointed to by the current ptr without preserving contents. */
283 T* reset(size_t count = 0) { 288 T* reset(size_t count = 0) {
284 sk_free(fPtr); 289 sk_free(fPtr);
285 fPtr = count ? (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW) : nullptr; 290 fPtr = count ? (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW) : nullptr;
286 return fPtr; 291 return fPtr;
287 } 292 }
288 293
289 T* get() const { return fPtr; } 294 T* get() const { return fPtr; }
(...skipping 29 matching lines...) Expand all
319 T* fPtr; 324 T* fPtr;
320 }; 325 };
321 326
322 template <size_t kCountRequested, typename T> class SkAutoSTMalloc : SkNoncopyab le { 327 template <size_t kCountRequested, typename T> class SkAutoSTMalloc : SkNoncopyab le {
323 public: 328 public:
324 SkAutoSTMalloc() : fPtr(fTStorage) {} 329 SkAutoSTMalloc() : fPtr(fTStorage) {}
325 330
326 SkAutoSTMalloc(size_t count) { 331 SkAutoSTMalloc(size_t count) {
327 if (count > kCount) { 332 if (count > kCount) {
328 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_M ALLOC_TEMP); 333 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_M ALLOC_TEMP);
334 } else if (count) {
335 fPtr = fTStorage;
329 } else { 336 } else {
330 fPtr = fTStorage; 337 fPtr = nullptr;
331 } 338 }
332 } 339 }
333 340
334 ~SkAutoSTMalloc() { 341 ~SkAutoSTMalloc() {
335 if (fPtr != fTStorage) { 342 if (fPtr != fTStorage) {
336 sk_free(fPtr); 343 sk_free(fPtr);
337 } 344 }
338 } 345 }
339 346
340 // doesn't preserve contents 347 // doesn't preserve contents
341 T* reset(size_t count) { 348 T* reset(size_t count) {
342 if (fPtr != fTStorage) { 349 if (fPtr != fTStorage) {
343 sk_free(fPtr); 350 sk_free(fPtr);
344 } 351 }
345 if (count > kCount) { 352 if (count > kCount) {
346 fPtr = (T*)sk_malloc_throw(count * sizeof(T)); 353 fPtr = (T*)sk_malloc_throw(count * sizeof(T));
354 } else if (count) {
355 fPtr = fTStorage;
347 } else { 356 } else {
348 fPtr = fTStorage; 357 fPtr = nullptr;
349 } 358 }
350 return fPtr; 359 return fPtr;
351 } 360 }
352 361
353 T* get() const { return fPtr; } 362 T* get() const { return fPtr; }
354 363
355 operator T*() { 364 operator T*() {
356 return fPtr; 365 return fPtr;
357 } 366 }
358 367
(...skipping 11 matching lines...) Expand all
370 379
371 // Reallocs the array, can be used to shrink the allocation. Makes no attem pt to be intelligent 380 // Reallocs the array, can be used to shrink the allocation. Makes no attem pt to be intelligent
372 void realloc(size_t count) { 381 void realloc(size_t count) {
373 if (count > kCount) { 382 if (count > kCount) {
374 if (fPtr == fTStorage) { 383 if (fPtr == fTStorage) {
375 fPtr = (T*)sk_malloc_throw(count * sizeof(T)); 384 fPtr = (T*)sk_malloc_throw(count * sizeof(T));
376 memcpy(fPtr, fTStorage, kCount * sizeof(T)); 385 memcpy(fPtr, fTStorage, kCount * sizeof(T));
377 } else { 386 } else {
378 fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T)); 387 fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T));
379 } 388 }
380 } else if (fPtr != fTStorage) { 389 } else if (count) {
381 fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T)); 390 if (fPtr != fTStorage) {
391 fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T));
392 }
393 } else {
394 this->reset(0);
382 } 395 }
383 } 396 }
384 397
385 private: 398 private:
386 // Since we use uint32_t storage, we might be able to get more elements for free. 399 // Since we use uint32_t storage, we might be able to get more elements for free.
387 static const size_t kCountWithPadding = SkAlign4(kCountRequested*sizeof(T)) / sizeof(T); 400 static const size_t kCountWithPadding = SkAlign4(kCountRequested*sizeof(T)) / sizeof(T);
388 #if defined(GOOGLE3) 401 #if defined(GOOGLE3)
389 // Stack frame size is limited for GOOGLE3. 4k is less than the actual max, but some functions 402 // Stack frame size is limited for GOOGLE3. 4k is less than the actual max, but some functions
390 // have multiple large stack allocations. 403 // have multiple large stack allocations.
391 static const size_t kMaxBytes = 4 * 1024; 404 static const size_t kMaxBytes = 4 * 1024;
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 * Returns void* because this object does not initialize the 483 * Returns void* because this object does not initialize the
471 * memory. Use placement new for types that require a cons. 484 * memory. Use placement new for types that require a cons.
472 */ 485 */
473 void* get() { return fStorage.get(); } 486 void* get() { return fStorage.get(); }
474 const void* get() const { return fStorage.get(); } 487 const void* get() const { return fStorage.get(); }
475 private: 488 private:
476 SkAlignedSStorage<sizeof(T)*N> fStorage; 489 SkAlignedSStorage<sizeof(T)*N> fStorage;
477 }; 490 };
478 491
479 #endif 492 #endif
OLDNEW
« no previous file with comments | « no previous file | tests/TemplatesTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698