1 2/* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10#ifndef SkChunkAlloc_DEFINED 11#define SkChunkAlloc_DEFINED 12 13#include "SkTypes.h" 14 15class SkChunkAlloc : SkNoncopyable { 16public: 17 SkChunkAlloc(size_t minSize); 18 ~SkChunkAlloc(); 19 20 /** Free up all allocated blocks. This invalidates all returned 21 pointers. 22 */ 23 void reset(); 24 25 /** Reuse all allocated blocks. This invalidates all returned 26 pointers (like reset) but doesn't necessarily free up all 27 of the privately allocated blocks. This is more efficient 28 if you plan to reuse the allocator multiple times. 29 */ 30 void reuse(); 31 32 enum AllocFailType { 33 kReturnNil_AllocFailType, 34 kThrow_AllocFailType 35 }; 36 37 void* alloc(size_t bytes, AllocFailType); 38 void* allocThrow(size_t bytes) { 39 return this->alloc(bytes, kThrow_AllocFailType); 40 } 41 42 /** Call this to unalloc the most-recently allocated ptr by alloc(). On 43 success, the number of bytes freed is returned, or 0 if the block could 44 not be unallocated. This is a hint to the underlying allocator that 45 the previous allocation may be reused, but the implementation is free 46 to ignore this call (and return 0). 47 */ 48 size_t unalloc(void* ptr); 49 50 size_t totalCapacity() const { return fTotalCapacity; } 51 52 /** 53 * Returns true if the specified address is within one of the chunks, and 54 * has at least 1-byte following the address (i.e. if addr points to the 55 * end of a chunk, then contains() will return false). 56 */ 57 bool contains(const void* addr) const; 58 59private: 60 struct Block; 61 Block* fBlock; 62 size_t fMinSize; 63 Block* fPool; 64 size_t fTotalCapacity; 65 66 Block* newBlock(size_t bytes, AllocFailType ftype); 67}; 68 69#endif 70