1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef SkChunkAlloc_DEFINED 18 #define SkChunkAlloc_DEFINED 19 20 #include "SkTypes.h" 21 22 class SkChunkAlloc : SkNoncopyable { 23 public: 24 SkChunkAlloc(size_t minSize); 25 ~SkChunkAlloc(); 26 27 /** Free up all allocated blocks. This invalidates all returned 28 pointers. 29 */ 30 void reset(); 31 32 /** Reuse all allocated blocks. This invalidates all returned 33 pointers (like reset) but doesn't necessarily free up all 34 of the privately allocated blocks. This is more efficient 35 if you plan to reuse the allocator multiple times. 36 */ 37 void reuse(); 38 39 enum AllocFailType { 40 kReturnNil_AllocFailType, 41 kThrow_AllocFailType 42 }; 43 44 void* alloc(size_t bytes, AllocFailType); allocThrow(size_t bytes)45 void* allocThrow(size_t bytes) { 46 return this->alloc(bytes, kThrow_AllocFailType); 47 } 48 totalCapacity()49 size_t totalCapacity() const { return fTotalCapacity; } 50 51 private: 52 struct Block; 53 Block* fBlock; 54 size_t fMinSize; 55 Block* fPool; 56 size_t fTotalCapacity; 57 58 Block* newBlock(size_t bytes, AllocFailType ftype); 59 }; 60 61 #endif 62