SkChunkAlloc.h revision ebd24962dfdb7a62cf97c0e3938851d56cabd10f
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com 28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* 3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project 48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be 6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file. 78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com 108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkChunkAlloc_DEFINED 118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkChunkAlloc_DEFINED 128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTypes.h" 148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkChunkAlloc : SkNoncopyable { 168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkChunkAlloc(size_t minSize); 188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ~SkChunkAlloc(); 198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 20ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com /** 21ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com * Free up all allocated blocks. This invalidates all returned 22ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com * pointers. 23ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com */ 248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void reset(); 258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com enum AllocFailType { 278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com kReturnNil_AllocFailType, 288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com kThrow_AllocFailType 298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com }; 308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* alloc(size_t bytes, AllocFailType); 328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* allocThrow(size_t bytes) { 338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return this->alloc(bytes, kThrow_AllocFailType); 348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 36aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com /** Call this to unalloc the most-recently allocated ptr by alloc(). On 37aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com success, the number of bytes freed is returned, or 0 if the block could 38aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com not be unallocated. This is a hint to the underlying allocator that 39aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com the previous allocation may be reused, but the implementation is free 40aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com to ignore this call (and return 0). 41aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com */ 42aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com size_t unalloc(void* ptr); 43aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com 448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com size_t totalCapacity() const { return fTotalCapacity; } 45ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com int blockCount() const { return fBlockCount; } 46f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com 47f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com /** 48f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * Returns true if the specified address is within one of the chunks, and 49f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * has at least 1-byte following the address (i.e. if addr points to the 50f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * end of a chunk, then contains() will return false). 51f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com */ 52f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com bool contains(const void* addr) const; 53f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com 548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com struct Block; 56ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com 578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Block* fBlock; 588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com size_t fMinSize; 59ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com size_t fChunkSize; 608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com size_t fTotalCapacity; 61ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com int fBlockCount; 628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Block* newBlock(size_t bytes, AllocFailType ftype); 648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 67