michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef SkChunkAlloc_DEFINED michael@0: #define SkChunkAlloc_DEFINED michael@0: michael@0: #include "SkTypes.h" michael@0: michael@0: class SkChunkAlloc : SkNoncopyable { michael@0: public: michael@0: SkChunkAlloc(size_t minSize); michael@0: ~SkChunkAlloc(); michael@0: michael@0: /** michael@0: * Free up all allocated blocks. This invalidates all returned michael@0: * pointers. michael@0: */ michael@0: void reset(); michael@0: michael@0: enum AllocFailType { michael@0: kReturnNil_AllocFailType, michael@0: kThrow_AllocFailType michael@0: }; michael@0: michael@0: void* alloc(size_t bytes, AllocFailType); michael@0: void* allocThrow(size_t bytes) { michael@0: return this->alloc(bytes, kThrow_AllocFailType); michael@0: } michael@0: michael@0: /** Call this to unalloc the most-recently allocated ptr by alloc(). On michael@0: success, the number of bytes freed is returned, or 0 if the block could michael@0: not be unallocated. This is a hint to the underlying allocator that michael@0: the previous allocation may be reused, but the implementation is free michael@0: to ignore this call (and return 0). michael@0: */ michael@0: size_t unalloc(void* ptr); michael@0: michael@0: size_t totalCapacity() const { return fTotalCapacity; } michael@0: size_t totalUsed() const { return fTotalUsed; } michael@0: int blockCount() const { return fBlockCount; } michael@0: michael@0: /** michael@0: * Returns true if the specified address is within one of the chunks, and michael@0: * has at least 1-byte following the address (i.e. if addr points to the michael@0: * end of a chunk, then contains() will return false). michael@0: */ michael@0: bool contains(const void* addr) const; michael@0: michael@0: private: michael@0: struct Block; michael@0: michael@0: Block* fBlock; michael@0: size_t fMinSize; michael@0: size_t fChunkSize; michael@0: size_t fTotalCapacity; michael@0: size_t fTotalUsed; // will be <= fTotalCapacity michael@0: int fBlockCount; michael@0: michael@0: Block* newBlock(size_t bytes, AllocFailType ftype); michael@0: }; michael@0: michael@0: #endif