blob: dc81ee2d47b9d792a3a9cfa06e28dfa1cde0ec0e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrAllocPool.h"
12
13#define GrAllocPool_MIN_BLOCK_SIZE ((size_t)128)
14
15struct GrAllocPool::Block {
16 Block* fNext;
17 char* fPtr;
18 size_t fBytesFree;
19 size_t fBytesTotal;
20
21 static Block* Create(size_t size, Block* next) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000022 SkASSERT(size >= GrAllocPool_MIN_BLOCK_SIZE);
reed@google.comac10a2d2010-12-22 21:39:39 +000023
24 Block* block = (Block*)GrMalloc(sizeof(Block) + size);
25 block->fNext = next;
26 block->fPtr = (char*)block + sizeof(Block);
27 block->fBytesFree = size;
28 block->fBytesTotal = size;
29 return block;
30 }
31
32 bool canAlloc(size_t bytes) const {
33 return bytes <= fBytesFree;
34 }
35
36 void* alloc(size_t bytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000037 SkASSERT(bytes <= fBytesFree);
reed@google.comac10a2d2010-12-22 21:39:39 +000038 fBytesFree -= bytes;
39 void* ptr = fPtr;
40 fPtr += bytes;
41 return ptr;
42 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043
reed@google.comac10a2d2010-12-22 21:39:39 +000044 size_t release(size_t bytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000045 SkASSERT(bytes > 0);
reed@google.comac10a2d2010-12-22 21:39:39 +000046 size_t free = GrMin(bytes, fBytesTotal - fBytesFree);
47 fBytesFree += free;
48 fPtr -= free;
49 return bytes - free;
50 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051
reed@google.comac10a2d2010-12-22 21:39:39 +000052 bool empty() const { return fBytesTotal == fBytesFree; }
53};
54
55///////////////////////////////////////////////////////////////////////////////
56
57GrAllocPool::GrAllocPool(size_t blockSize) {
58 fBlock = NULL;
59 fMinBlockSize = GrMax(blockSize, GrAllocPool_MIN_BLOCK_SIZE);
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000060 SkDEBUGCODE(fBlocksAllocated = 0;)
reed@google.comac10a2d2010-12-22 21:39:39 +000061}
62
63GrAllocPool::~GrAllocPool() {
64 this->reset();
65}
66
67void GrAllocPool::reset() {
68 this->validate();
69
70 Block* block = fBlock;
71 while (block) {
72 Block* next = block->fNext;
73 GrFree(block);
74 block = next;
75 }
76 fBlock = NULL;
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000077 SkDEBUGCODE(fBlocksAllocated = 0;)
reed@google.comac10a2d2010-12-22 21:39:39 +000078}
79
80void* GrAllocPool::alloc(size_t size) {
81 this->validate();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000082
reed@google.comac10a2d2010-12-22 21:39:39 +000083 if (!fBlock || !fBlock->canAlloc(size)) {
84 size_t blockSize = GrMax(fMinBlockSize, size);
85 fBlock = Block::Create(blockSize, fBlock);
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000086 SkDEBUGCODE(fBlocksAllocated += 1;)
reed@google.comac10a2d2010-12-22 21:39:39 +000087 }
88 return fBlock->alloc(size);
89}
90
91void GrAllocPool::release(size_t bytes) {
92 this->validate();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000093
reed@google.comac10a2d2010-12-22 21:39:39 +000094 while (bytes && NULL != fBlock) {
95 bytes = fBlock->release(bytes);
96 if (fBlock->empty()) {
97 Block* next = fBlock->fNext;
98 GrFree(fBlock);
99 fBlock = next;
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +0000100 SkDEBUGCODE(fBlocksAllocated -= 1;)
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 }
102 }
103}
104
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000105#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000106
107void GrAllocPool::validate() const {
108 Block* block = fBlock;
109 int count = 0;
110 while (block) {
111 count += 1;
112 block = block->fNext;
113 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000114 SkASSERT(fBlocksAllocated == count);
reed@google.comac10a2d2010-12-22 21:39:39 +0000115}
116
117#endif