epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkChunkAlloc_DEFINED |
| 11 | #define SkChunkAlloc_DEFINED |
| 12 | |
| 13 | #include "SkTypes.h" |
| 14 | |
| 15 | class SkChunkAlloc : SkNoncopyable { |
| 16 | public: |
| 17 | SkChunkAlloc(size_t minSize); |
| 18 | ~SkChunkAlloc(); |
| 19 | |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 20 | /** |
| 21 | * Free up all allocated blocks. This invalidates all returned |
| 22 | * pointers. |
| 23 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | void reset(); |
robertphillips | a9061de | 2015-02-27 08:31:57 -0800 | [diff] [blame] | 25 | /** |
| 26 | * Reset to 0 used bytes preserving as much memory as possible. |
| 27 | * This invalidates all returned pointers. |
| 28 | */ |
| 29 | void rewind(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | enum AllocFailType { |
| 32 | kReturnNil_AllocFailType, |
| 33 | kThrow_AllocFailType |
| 34 | }; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 35 | |
milko.leporis | 401e77c | 2016-06-05 13:14:21 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Allocates a memory block of size bytes. |
| 38 | * On success: returns a pointer to beginning of memory block that is |
| 39 | * 8 byte aligned. The content of allocated block is not initialized. |
| 40 | * On failure: calls abort() if called with kThrow_AllocFailType, |
| 41 | * otherwise returns NULL pointer. |
| 42 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | void* alloc(size_t bytes, AllocFailType); |
milko.leporis | 401e77c | 2016-06-05 13:14:21 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Shortcut for calling alloc with kThrow_AllocFailType. |
| 47 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | void* allocThrow(size_t bytes) { |
| 49 | return this->alloc(bytes, kThrow_AllocFailType); |
| 50 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 51 | |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 52 | /** Call this to unalloc the most-recently allocated ptr by alloc(). On |
| 53 | success, the number of bytes freed is returned, or 0 if the block could |
| 54 | not be unallocated. This is a hint to the underlying allocator that |
| 55 | the previous allocation may be reused, but the implementation is free |
| 56 | to ignore this call (and return 0). |
| 57 | */ |
| 58 | size_t unalloc(void* ptr); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 59 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | size_t totalCapacity() const { return fTotalCapacity; } |
reed@google.com | 6757a3c | 2013-06-19 19:25:36 +0000 | [diff] [blame] | 61 | size_t totalUsed() const { return fTotalUsed; } |
robertphillips | a9061de | 2015-02-27 08:31:57 -0800 | [diff] [blame] | 62 | SkDEBUGCODE(int blockCount() const { return fBlockCount; }) |
| 63 | SkDEBUGCODE(size_t totalLost() const { return fTotalLost; }) |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Returns true if the specified address is within one of the chunks, and |
| 67 | * has at least 1-byte following the address (i.e. if addr points to the |
| 68 | * end of a chunk, then contains() will return false). |
| 69 | */ |
| 70 | bool contains(const void* addr) const; |
| 71 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | private: |
| 73 | struct Block; |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 74 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | Block* fBlock; |
| 76 | size_t fMinSize; |
reed@google.com | ebd2496 | 2012-05-17 14:28:11 +0000 | [diff] [blame] | 77 | size_t fChunkSize; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | size_t fTotalCapacity; |
reed@google.com | 6757a3c | 2013-06-19 19:25:36 +0000 | [diff] [blame] | 79 | size_t fTotalUsed; // will be <= fTotalCapacity |
robertphillips | a9061de | 2015-02-27 08:31:57 -0800 | [diff] [blame] | 80 | SkDEBUGCODE(int fBlockCount;) |
| 81 | SkDEBUGCODE(size_t fTotalLost;) // will be <= fTotalCapacity |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | |
| 83 | Block* newBlock(size_t bytes, AllocFailType ftype); |
robertphillips | a9061de | 2015-02-27 08:31:57 -0800 | [diff] [blame] | 84 | Block* addBlockIfNecessary(size_t bytes, AllocFailType ftype); |
| 85 | |
| 86 | SkDEBUGCODE(void validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | #endif |