OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 Google Inc. |
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 "GrAllocPool.h" | 8 #include "GrAllocPool.h" |
9 | 9 |
10 #include "GrTypes.h" | 10 #include "GrTypes.h" |
11 | 11 |
12 #define GrAllocPool_MIN_BLOCK_SIZE ((size_t)128) | 12 #define GrAllocPool_MIN_BLOCK_SIZE ((size_t)128) |
13 | 13 |
14 struct GrAllocPool::Block { | 14 struct GrAllocPool::Block { |
15 Block* fNext; | 15 Block* fNext; |
16 char* fPtr; | 16 char* fPtr; |
17 size_t fBytesFree; | 17 size_t fBytesFree; |
18 size_t fBytesTotal; | 18 size_t fBytesTotal; |
19 | 19 |
20 static Block* Create(size_t size, Block* next) { | 20 static Block* Create(size_t size, Block* next) { |
21 SkASSERT(size >= GrAllocPool_MIN_BLOCK_SIZE); | 21 SkASSERT(size >= GrAllocPool_MIN_BLOCK_SIZE); |
22 | 22 |
23 Block* block = (Block*)GrMalloc(sizeof(Block) + size); | 23 Block* block = (Block*)sk_malloc_throw(sizeof(Block) + size); |
24 block->fNext = next; | 24 block->fNext = next; |
25 block->fPtr = (char*)block + sizeof(Block); | 25 block->fPtr = (char*)block + sizeof(Block); |
26 block->fBytesFree = size; | 26 block->fBytesFree = size; |
27 block->fBytesTotal = size; | 27 block->fBytesTotal = size; |
28 return block; | 28 return block; |
29 } | 29 } |
30 | 30 |
31 bool canAlloc(size_t bytes) const { | 31 bool canAlloc(size_t bytes) const { |
32 return bytes <= fBytesFree; | 32 return bytes <= fBytesFree; |
33 } | 33 } |
(...skipping 28 matching lines...) Expand all Loading... |
62 GrAllocPool::~GrAllocPool() { | 62 GrAllocPool::~GrAllocPool() { |
63 this->reset(); | 63 this->reset(); |
64 } | 64 } |
65 | 65 |
66 void GrAllocPool::reset() { | 66 void GrAllocPool::reset() { |
67 this->validate(); | 67 this->validate(); |
68 | 68 |
69 Block* block = fBlock; | 69 Block* block = fBlock; |
70 while (block) { | 70 while (block) { |
71 Block* next = block->fNext; | 71 Block* next = block->fNext; |
72 GrFree(block); | 72 sk_free(block); |
73 block = next; | 73 block = next; |
74 } | 74 } |
75 fBlock = NULL; | 75 fBlock = NULL; |
76 SkDEBUGCODE(fBlocksAllocated = 0;) | 76 SkDEBUGCODE(fBlocksAllocated = 0;) |
77 } | 77 } |
78 | 78 |
79 void* GrAllocPool::alloc(size_t size) { | 79 void* GrAllocPool::alloc(size_t size) { |
80 this->validate(); | 80 this->validate(); |
81 | 81 |
82 if (!fBlock || !fBlock->canAlloc(size)) { | 82 if (!fBlock || !fBlock->canAlloc(size)) { |
83 size_t blockSize = GrMax(fMinBlockSize, size); | 83 size_t blockSize = GrMax(fMinBlockSize, size); |
84 fBlock = Block::Create(blockSize, fBlock); | 84 fBlock = Block::Create(blockSize, fBlock); |
85 SkDEBUGCODE(fBlocksAllocated += 1;) | 85 SkDEBUGCODE(fBlocksAllocated += 1;) |
86 } | 86 } |
87 return fBlock->alloc(size); | 87 return fBlock->alloc(size); |
88 } | 88 } |
89 | 89 |
90 void GrAllocPool::release(size_t bytes) { | 90 void GrAllocPool::release(size_t bytes) { |
91 this->validate(); | 91 this->validate(); |
92 | 92 |
93 while (bytes && NULL != fBlock) { | 93 while (bytes && NULL != fBlock) { |
94 bytes = fBlock->release(bytes); | 94 bytes = fBlock->release(bytes); |
95 if (fBlock->empty()) { | 95 if (fBlock->empty()) { |
96 Block* next = fBlock->fNext; | 96 Block* next = fBlock->fNext; |
97 GrFree(fBlock); | 97 sk_free(fBlock); |
98 fBlock = next; | 98 fBlock = next; |
99 SkDEBUGCODE(fBlocksAllocated -= 1;) | 99 SkDEBUGCODE(fBlocksAllocated -= 1;) |
100 } | 100 } |
101 } | 101 } |
102 } | 102 } |
103 | 103 |
104 #ifdef SK_DEBUG | 104 #ifdef SK_DEBUG |
105 | 105 |
106 void GrAllocPool::validate() const { | 106 void GrAllocPool::validate() const { |
107 Block* block = fBlock; | 107 Block* block = fBlock; |
108 int count = 0; | 108 int count = 0; |
109 while (block) { | 109 while (block) { |
110 count += 1; | 110 count += 1; |
111 block = block->fNext; | 111 block = block->fNext; |
112 } | 112 } |
113 SkASSERT(fBlocksAllocated == count); | 113 SkASSERT(fBlocksAllocated == count); |
114 } | 114 } |
115 | 115 |
116 #endif | 116 #endif |
OLD | NEW |