Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2010 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "GrAllocPool.h" |
michael@0 | 9 | |
michael@0 | 10 | #include "GrTypes.h" |
michael@0 | 11 | |
michael@0 | 12 | #define GrAllocPool_MIN_BLOCK_SIZE ((size_t)128) |
michael@0 | 13 | |
michael@0 | 14 | struct GrAllocPool::Block { |
michael@0 | 15 | Block* fNext; |
michael@0 | 16 | char* fPtr; |
michael@0 | 17 | size_t fBytesFree; |
michael@0 | 18 | size_t fBytesTotal; |
michael@0 | 19 | |
michael@0 | 20 | static Block* Create(size_t size, Block* next) { |
michael@0 | 21 | SkASSERT(size >= GrAllocPool_MIN_BLOCK_SIZE); |
michael@0 | 22 | |
michael@0 | 23 | Block* block = (Block*)sk_malloc_throw(sizeof(Block) + size); |
michael@0 | 24 | block->fNext = next; |
michael@0 | 25 | block->fPtr = (char*)block + sizeof(Block); |
michael@0 | 26 | block->fBytesFree = size; |
michael@0 | 27 | block->fBytesTotal = size; |
michael@0 | 28 | return block; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | bool canAlloc(size_t bytes) const { |
michael@0 | 32 | return bytes <= fBytesFree; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | void* alloc(size_t bytes) { |
michael@0 | 36 | SkASSERT(bytes <= fBytesFree); |
michael@0 | 37 | fBytesFree -= bytes; |
michael@0 | 38 | void* ptr = fPtr; |
michael@0 | 39 | fPtr += bytes; |
michael@0 | 40 | return ptr; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | size_t release(size_t bytes) { |
michael@0 | 44 | SkASSERT(bytes > 0); |
michael@0 | 45 | size_t free = GrMin(bytes, fBytesTotal - fBytesFree); |
michael@0 | 46 | fBytesFree += free; |
michael@0 | 47 | fPtr -= free; |
michael@0 | 48 | return bytes - free; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | bool empty() const { return fBytesTotal == fBytesFree; } |
michael@0 | 52 | }; |
michael@0 | 53 | |
michael@0 | 54 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 55 | |
michael@0 | 56 | GrAllocPool::GrAllocPool(size_t blockSize) { |
michael@0 | 57 | fBlock = NULL; |
michael@0 | 58 | fMinBlockSize = GrMax(blockSize, GrAllocPool_MIN_BLOCK_SIZE); |
michael@0 | 59 | SkDEBUGCODE(fBlocksAllocated = 0;) |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | GrAllocPool::~GrAllocPool() { |
michael@0 | 63 | this->reset(); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | void GrAllocPool::reset() { |
michael@0 | 67 | this->validate(); |
michael@0 | 68 | |
michael@0 | 69 | Block* block = fBlock; |
michael@0 | 70 | while (block) { |
michael@0 | 71 | Block* next = block->fNext; |
michael@0 | 72 | sk_free(block); |
michael@0 | 73 | block = next; |
michael@0 | 74 | } |
michael@0 | 75 | fBlock = NULL; |
michael@0 | 76 | SkDEBUGCODE(fBlocksAllocated = 0;) |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | void* GrAllocPool::alloc(size_t size) { |
michael@0 | 80 | this->validate(); |
michael@0 | 81 | |
michael@0 | 82 | if (!fBlock || !fBlock->canAlloc(size)) { |
michael@0 | 83 | size_t blockSize = GrMax(fMinBlockSize, size); |
michael@0 | 84 | fBlock = Block::Create(blockSize, fBlock); |
michael@0 | 85 | SkDEBUGCODE(fBlocksAllocated += 1;) |
michael@0 | 86 | } |
michael@0 | 87 | return fBlock->alloc(size); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | void GrAllocPool::release(size_t bytes) { |
michael@0 | 91 | this->validate(); |
michael@0 | 92 | |
michael@0 | 93 | while (bytes && NULL != fBlock) { |
michael@0 | 94 | bytes = fBlock->release(bytes); |
michael@0 | 95 | if (fBlock->empty()) { |
michael@0 | 96 | Block* next = fBlock->fNext; |
michael@0 | 97 | sk_free(fBlock); |
michael@0 | 98 | fBlock = next; |
michael@0 | 99 | SkDEBUGCODE(fBlocksAllocated -= 1;) |
michael@0 | 100 | } |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | #ifdef SK_DEBUG |
michael@0 | 105 | |
michael@0 | 106 | void GrAllocPool::validate() const { |
michael@0 | 107 | Block* block = fBlock; |
michael@0 | 108 | int count = 0; |
michael@0 | 109 | while (block) { |
michael@0 | 110 | count += 1; |
michael@0 | 111 | block = block->fNext; |
michael@0 | 112 | } |
michael@0 | 113 | SkASSERT(fBlocksAllocated == count); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | #endif |