| 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 SkChunkAlloc_DEFINED | 10 #ifndef SkChunkAlloc_DEFINED |
| 11 #define SkChunkAlloc_DEFINED | 11 #define SkChunkAlloc_DEFINED |
| 12 | 12 |
| 13 #include "SkTypes.h" | 13 #include "SkTypes.h" |
| 14 | 14 |
| 15 class SkChunkAlloc : SkNoncopyable { | 15 class SkChunkAlloc : SkNoncopyable { |
| 16 public: | 16 public: |
| 17 SkChunkAlloc(size_t minSize); | 17 SkChunkAlloc(size_t minSize); |
| 18 ~SkChunkAlloc(); | 18 ~SkChunkAlloc(); |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * Free up all allocated blocks. This invalidates all returned | 21 * Free up all allocated blocks. This invalidates all returned |
| 22 * pointers. | 22 * pointers. |
| 23 */ | 23 */ |
| 24 void reset(); | 24 void reset(); |
| 25 /** |
| 26 * Free up all the blocks except the largest. This invalidates all |
| 27 * returned pointers. |
| 28 */ |
| 29 void rewind(); |
| 25 | 30 |
| 26 enum AllocFailType { | 31 enum AllocFailType { |
| 27 kReturnNil_AllocFailType, | 32 kReturnNil_AllocFailType, |
| 28 kThrow_AllocFailType | 33 kThrow_AllocFailType |
| 29 }; | 34 }; |
| 30 | 35 |
| 36 /** |
| 37 * Attempt to preallocate (but not mark as used) a block of memory |
| 38 */ |
| 39 void preAlloc(size_t bytes); |
| 40 |
| 31 void* alloc(size_t bytes, AllocFailType); | 41 void* alloc(size_t bytes, AllocFailType); |
| 32 void* allocThrow(size_t bytes) { | 42 void* allocThrow(size_t bytes) { |
| 33 return this->alloc(bytes, kThrow_AllocFailType); | 43 return this->alloc(bytes, kThrow_AllocFailType); |
| 34 } | 44 } |
| 35 | 45 |
| 36 /** Call this to unalloc the most-recently allocated ptr by alloc(). On | 46 /** Call this to unalloc the most-recently allocated ptr by alloc(). On |
| 37 success, the number of bytes freed is returned, or 0 if the block could | 47 success, the number of bytes freed is returned, or 0 if the block could |
| 38 not be unallocated. This is a hint to the underlying allocator that | 48 not be unallocated. This is a hint to the underlying allocator that |
| 39 the previous allocation may be reused, but the implementation is free | 49 the previous allocation may be reused, but the implementation is free |
| 40 to ignore this call (and return 0). | 50 to ignore this call (and return 0). |
| (...skipping 15 matching lines...) Expand all Loading... |
| 56 struct Block; | 66 struct Block; |
| 57 | 67 |
| 58 Block* fBlock; | 68 Block* fBlock; |
| 59 size_t fMinSize; | 69 size_t fMinSize; |
| 60 size_t fChunkSize; | 70 size_t fChunkSize; |
| 61 size_t fTotalCapacity; | 71 size_t fTotalCapacity; |
| 62 size_t fTotalUsed; // will be <= fTotalCapacity | 72 size_t fTotalUsed; // will be <= fTotalCapacity |
| 63 int fBlockCount; | 73 int fBlockCount; |
| 64 | 74 |
| 65 Block* newBlock(size_t bytes, AllocFailType ftype); | 75 Block* newBlock(size_t bytes, AllocFailType ftype); |
| 76 Block* addBlockIfNecessary(size_t bytes, AllocFailType ftype); |
| 77 |
| 66 }; | 78 }; |
| 67 | 79 |
| 68 #endif | 80 #endif |
| OLD | NEW |