OLD | NEW |
---|---|
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 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 if (count > kCount) { | 202 if (count > kCount) { |
203 const uint64_t size64 = sk_64_mul(count, sizeof(T)); | 203 const uint64_t size64 = sk_64_mul(count, sizeof(T)); |
204 const size_t size = static_cast<size_t>(size64); | 204 const size_t size = static_cast<size_t>(size64); |
205 if (size != size64) { | 205 if (size != size64) { |
206 sk_out_of_memory(); | 206 sk_out_of_memory(); |
207 } | 207 } |
208 fArray = (T*) sk_malloc_throw(size); | 208 fArray = (T*) sk_malloc_throw(size); |
209 } else if (count > 0) { | 209 } else if (count > 0) { |
210 fArray = (T*) fStorage; | 210 fArray = (T*) fStorage; |
211 } else { | 211 } else { |
212 SkASSERT(0 == count); | |
bsalomon
2016/06/23 17:53:34
I think it'd be slightly clearer to just put SkASS
csmartdalton
2016/06/23 18:07:26
Done.
| |
212 fArray = NULL; | 213 fArray = NULL; |
213 } | 214 } |
214 | 215 |
215 fCount = count; | 216 fCount = count; |
216 } | 217 } |
217 | 218 |
218 iter = fArray; | 219 iter = fArray; |
219 T* stop = fArray + count; | 220 T* stop = fArray + count; |
220 while (iter < stop) { | 221 while (iter < stop) { |
221 new (iter++) T; | 222 new (iter++) T; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 fPtr = reinterpret_cast<T*>(sk_realloc_throw(fPtr, count * sizeof(T))); |
280 } | 281 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
319 T* fPtr; | 320 T* fPtr; |
320 }; | 321 }; |
321 | 322 |
322 template <size_t kCountRequested, typename T> class SkAutoSTMalloc : SkNoncopyab le { | 323 template <size_t kCountRequested, typename T> class SkAutoSTMalloc : SkNoncopyab le { |
323 public: | 324 public: |
324 SkAutoSTMalloc() : fPtr(fTStorage) {} | 325 SkAutoSTMalloc() : fPtr(fTStorage) {} |
325 | 326 |
326 SkAutoSTMalloc(size_t count) { | 327 SkAutoSTMalloc(size_t count) { |
327 if (count > kCount) { | 328 if (count > kCount) { |
328 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_M ALLOC_TEMP); | 329 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_M ALLOC_TEMP); |
330 } else if (count) { | |
331 fPtr = fTStorage; | |
329 } else { | 332 } else { |
330 fPtr = fTStorage; | 333 fPtr = nullptr; |
331 } | 334 } |
332 } | 335 } |
333 | 336 |
334 ~SkAutoSTMalloc() { | 337 ~SkAutoSTMalloc() { |
335 if (fPtr != fTStorage) { | 338 if (fPtr != fTStorage) { |
336 sk_free(fPtr); | 339 sk_free(fPtr); |
337 } | 340 } |
338 } | 341 } |
339 | 342 |
340 // doesn't preserve contents | 343 // doesn't preserve contents |
341 T* reset(size_t count) { | 344 T* reset(size_t count) { |
342 if (fPtr != fTStorage) { | 345 if (fPtr != fTStorage) { |
343 sk_free(fPtr); | 346 sk_free(fPtr); |
344 } | 347 } |
345 if (count > kCount) { | 348 if (count > kCount) { |
346 fPtr = (T*)sk_malloc_throw(count * sizeof(T)); | 349 fPtr = (T*)sk_malloc_throw(count * sizeof(T)); |
350 } else if (count) { | |
351 fPtr = fTStorage; | |
347 } else { | 352 } else { |
348 fPtr = fTStorage; | 353 fPtr = nullptr; |
349 } | 354 } |
350 return fPtr; | 355 return fPtr; |
351 } | 356 } |
352 | 357 |
353 T* get() const { return fPtr; } | 358 T* get() const { return fPtr; } |
354 | 359 |
355 operator T*() { | 360 operator T*() { |
356 return fPtr; | 361 return fPtr; |
357 } | 362 } |
358 | 363 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
470 * Returns void* because this object does not initialize the | 475 * Returns void* because this object does not initialize the |
471 * memory. Use placement new for types that require a cons. | 476 * memory. Use placement new for types that require a cons. |
472 */ | 477 */ |
473 void* get() { return fStorage.get(); } | 478 void* get() { return fStorage.get(); } |
474 const void* get() const { return fStorage.get(); } | 479 const void* get() const { return fStorage.get(); } |
475 private: | 480 private: |
476 SkAlignedSStorage<sizeof(T)*N> fStorage; | 481 SkAlignedSStorage<sizeof(T)*N> fStorage; |
477 }; | 482 }; |
478 | 483 |
479 #endif | 484 #endif |
OLD | NEW |