180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/* 380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2006 The Android Open Source Project 480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * 580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be 680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file. 780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef SkChunkAlloc_DEFINED 1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkChunkAlloc_DEFINED 1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTypes.h" 1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkChunkAlloc : SkNoncopyable { 1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic: 1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkChunkAlloc(size_t minSize); 1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru ~SkChunkAlloc(); 1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Free up all allocated blocks. This invalidates all returned 2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * pointers. 2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void reset(); 2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru enum AllocFailType { 2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru kReturnNil_AllocFailType, 2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru kThrow_AllocFailType 2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru }; 3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void* alloc(size_t bytes, AllocFailType); 3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void* allocThrow(size_t bytes) { 3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru return this->alloc(bytes, kThrow_AllocFailType); 3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** Call this to unalloc the most-recently allocated ptr by alloc(). On 3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru success, the number of bytes freed is returned, or 0 if the block could 3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru not be unallocated. This is a hint to the underlying allocator that 3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru the previous allocation may be reused, but the implementation is free 4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru to ignore this call (and return 0). 4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru size_t unalloc(void* ptr); 4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru size_t totalCapacity() const { return fTotalCapacity; } 4558190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger size_t totalUsed() const { return fTotalUsed; } 4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int blockCount() const { return fBlockCount; } 4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Returns true if the specified address is within one of the chunks, and 5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * has at least 1-byte following the address (i.e. if addr points to the 5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * end of a chunk, then contains() will return false). 5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru bool contains(const void* addr) const; 5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate: 5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru struct Block; 5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru Block* fBlock; 5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru size_t fMinSize; 6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru size_t fChunkSize; 6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru size_t fTotalCapacity; 6258190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger size_t fTotalUsed; // will be <= fTotalCapacity 6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int fBlockCount; 6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru Block* newBlock(size_t bytes, AllocFailType ftype); 6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}; 6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif 69