blob: 9594c940575c33f044090d49a7632b2f49159a45 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#include "GrAllocPool.h"
9
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000010#include "GrTypes.h"
11
reed@google.comac10a2d2010-12-22 21:39:39 +000012#define GrAllocPool_MIN_BLOCK_SIZE ((size_t)128)
13
14struct GrAllocPool::Block {
15 Block* fNext;
16 char* fPtr;
17 size_t fBytesFree;
18 size_t fBytesTotal;
19
20 static Block* Create(size_t size, Block* next) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000021 SkASSERT(size >= GrAllocPool_MIN_BLOCK_SIZE);
reed@google.comac10a2d2010-12-22 21:39:39 +000022
reed@google.com939ca7c2013-09-26 19:56:51 +000023 Block* block = (Block*)sk_malloc_throw(sizeof(Block) + size);
reed@google.comac10a2d2010-12-22 21:39:39 +000024 block->fNext = next;
25 block->fPtr = (char*)block + sizeof(Block);
26 block->fBytesFree = size;
27 block->fBytesTotal = size;
28 return block;
29 }
30
31 bool canAlloc(size_t bytes) const {
32 return bytes <= fBytesFree;
33 }
34
35 void* alloc(size_t bytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000036 SkASSERT(bytes <= fBytesFree);
reed@google.comac10a2d2010-12-22 21:39:39 +000037 fBytesFree -= bytes;
38 void* ptr = fPtr;
39 fPtr += bytes;
40 return ptr;
41 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000042
reed@google.comac10a2d2010-12-22 21:39:39 +000043 size_t release(size_t bytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000044 SkASSERT(bytes > 0);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000045 size_t free = SkTMin(bytes, fBytesTotal - fBytesFree);
reed@google.comac10a2d2010-12-22 21:39:39 +000046 fBytesFree += free;
47 fPtr -= free;
48 return bytes - free;
49 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000050
reed@google.comac10a2d2010-12-22 21:39:39 +000051 bool empty() const { return fBytesTotal == fBytesFree; }
52};
53
54///////////////////////////////////////////////////////////////////////////////
55
56GrAllocPool::GrAllocPool(size_t blockSize) {
57 fBlock = NULL;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000058 fMinBlockSize = SkTMax(blockSize, GrAllocPool_MIN_BLOCK_SIZE);
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000059 SkDEBUGCODE(fBlocksAllocated = 0;)
reed@google.comac10a2d2010-12-22 21:39:39 +000060}
61
62GrAllocPool::~GrAllocPool() {
63 this->reset();
64}
65
66void GrAllocPool::reset() {
67 this->validate();
68
69 Block* block = fBlock;
70 while (block) {
71 Block* next = block->fNext;
reed@google.com939ca7c2013-09-26 19:56:51 +000072 sk_free(block);
reed@google.comac10a2d2010-12-22 21:39:39 +000073 block = next;
74 }
75 fBlock = NULL;
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000076 SkDEBUGCODE(fBlocksAllocated = 0;)
reed@google.comac10a2d2010-12-22 21:39:39 +000077}
78
79void* GrAllocPool::alloc(size_t size) {
80 this->validate();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000081
reed@google.comac10a2d2010-12-22 21:39:39 +000082 if (!fBlock || !fBlock->canAlloc(size)) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000083 size_t blockSize = SkTMax(fMinBlockSize, size);
reed@google.comac10a2d2010-12-22 21:39:39 +000084 fBlock = Block::Create(blockSize, fBlock);
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000085 SkDEBUGCODE(fBlocksAllocated += 1;)
reed@google.comac10a2d2010-12-22 21:39:39 +000086 }
87 return fBlock->alloc(size);
88}
89
90void GrAllocPool::release(size_t bytes) {
91 this->validate();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000092
bsalomon49f085d2014-09-05 13:34:00 -070093 while (bytes && fBlock) {
reed@google.comac10a2d2010-12-22 21:39:39 +000094 bytes = fBlock->release(bytes);
95 if (fBlock->empty()) {
96 Block* next = fBlock->fNext;
reed@google.com939ca7c2013-09-26 19:56:51 +000097 sk_free(fBlock);
reed@google.comac10a2d2010-12-22 21:39:39 +000098 fBlock = next;
commit-bot@chromium.org1acc3d72013-09-06 23:13:05 +000099 SkDEBUGCODE(fBlocksAllocated -= 1;)
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 }
101 }
102}
103
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000104#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000105
106void GrAllocPool::validate() const {
107 Block* block = fBlock;
108 int count = 0;
109 while (block) {
110 count += 1;
111 block = block->fNext;
112 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000113 SkASSERT(fBlocksAllocated == count);
reed@google.comac10a2d2010-12-22 21:39:39 +0000114}
115
116#endif