OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkChunkAlloc.h" | 8 #include "SkChunkAlloc.h" |
9 | 9 |
10 // Don't malloc any chunks smaller than this | 10 // Don't malloc any chunks smaller than this |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 SkChunkAlloc::SkChunkAlloc(size_t minSize) { | 46 SkChunkAlloc::SkChunkAlloc(size_t minSize) { |
47 if (minSize < MIN_CHUNKALLOC_BLOCK_SIZE) { | 47 if (minSize < MIN_CHUNKALLOC_BLOCK_SIZE) { |
48 minSize = MIN_CHUNKALLOC_BLOCK_SIZE; | 48 minSize = MIN_CHUNKALLOC_BLOCK_SIZE; |
49 } | 49 } |
50 | 50 |
51 fBlock = NULL; | 51 fBlock = NULL; |
52 fMinSize = minSize; | 52 fMinSize = minSize; |
53 fChunkSize = fMinSize; | 53 fChunkSize = fMinSize; |
54 fTotalCapacity = 0; | 54 fTotalCapacity = 0; |
| 55 fTotalUsed = 0; |
55 fBlockCount = 0; | 56 fBlockCount = 0; |
56 } | 57 } |
57 | 58 |
58 SkChunkAlloc::~SkChunkAlloc() { | 59 SkChunkAlloc::~SkChunkAlloc() { |
59 this->reset(); | 60 this->reset(); |
60 } | 61 } |
61 | 62 |
62 void SkChunkAlloc::reset() { | 63 void SkChunkAlloc::reset() { |
63 Block::FreeChain(fBlock); | 64 Block::FreeChain(fBlock); |
64 fBlock = NULL; | 65 fBlock = NULL; |
65 fChunkSize = fMinSize; // reset to our initial minSize | 66 fChunkSize = fMinSize; // reset to our initial minSize |
66 fTotalCapacity = 0; | 67 fTotalCapacity = 0; |
| 68 fTotalUsed = 0; |
67 fBlockCount = 0; | 69 fBlockCount = 0; |
68 } | 70 } |
69 | 71 |
70 SkChunkAlloc::Block* SkChunkAlloc::newBlock(size_t bytes, AllocFailType ftype) { | 72 SkChunkAlloc::Block* SkChunkAlloc::newBlock(size_t bytes, AllocFailType ftype) { |
71 size_t size = bytes; | 73 size_t size = bytes; |
72 if (size < fChunkSize) { | 74 if (size < fChunkSize) { |
73 size = fChunkSize; | 75 size = fChunkSize; |
74 } | 76 } |
75 | 77 |
76 Block* block = (Block*)sk_malloc_flags(sizeof(Block) + size, | 78 Block* block = (Block*)sk_malloc_flags(sizeof(Block) + size, |
77 ftype == kThrow_AllocFailType ? SK_MALLOC_THROW : 0); | 79 ftype == kThrow_AllocFailType ? SK_MALLOC_THROW : 0); |
78 | 80 |
79 if (block) { | 81 if (block) { |
80 // block->fNext = fBlock; | 82 // block->fNext = fBlock; |
81 block->fFreeSize = size; | 83 block->fFreeSize = size; |
82 block->fFreePtr = block->startOfData(); | 84 block->fFreePtr = block->startOfData(); |
83 | 85 |
84 fTotalCapacity += size; | 86 fTotalCapacity += size; |
85 fBlockCount += 1; | 87 fBlockCount += 1; |
86 | 88 |
87 fChunkSize = increase_next_size(fChunkSize); | 89 fChunkSize = increase_next_size(fChunkSize); |
88 } | 90 } |
89 return block; | 91 return block; |
90 } | 92 } |
91 | 93 |
92 void* SkChunkAlloc::alloc(size_t bytes, AllocFailType ftype) { | 94 void* SkChunkAlloc::alloc(size_t bytes, AllocFailType ftype) { |
| 95 fTotalUsed += bytes; |
| 96 |
93 bytes = SkAlign4(bytes); | 97 bytes = SkAlign4(bytes); |
94 | 98 |
95 Block* block = fBlock; | 99 Block* block = fBlock; |
96 | 100 |
97 if (block == NULL || bytes > block->fFreeSize) { | 101 if (block == NULL || bytes > block->fFreeSize) { |
98 block = this->newBlock(bytes, ftype); | 102 block = this->newBlock(bytes, ftype); |
99 if (NULL == block) { | 103 if (NULL == block) { |
100 return NULL; | 104 return NULL; |
101 } | 105 } |
102 block->fNext = fBlock; | 106 block->fNext = fBlock; |
(...skipping 26 matching lines...) Expand all Loading... |
129 bool SkChunkAlloc::contains(const void* addr) const { | 133 bool SkChunkAlloc::contains(const void* addr) const { |
130 const Block* block = fBlock; | 134 const Block* block = fBlock; |
131 while (block) { | 135 while (block) { |
132 if (block->contains(addr)) { | 136 if (block->contains(addr)) { |
133 return true; | 137 return true; |
134 } | 138 } |
135 block = block->fNext; | 139 block = block->fNext; |
136 } | 140 } |
137 return false; | 141 return false; |
138 } | 142 } |
OLD | NEW |