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 2006 The Android Open Source Project |
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 "SkChunkAlloc.h" |
michael@0 | 9 | |
michael@0 | 10 | // Don't malloc any chunks smaller than this |
michael@0 | 11 | #define MIN_CHUNKALLOC_BLOCK_SIZE 1024 |
michael@0 | 12 | |
michael@0 | 13 | // Return the new min blocksize given the current value |
michael@0 | 14 | static size_t increase_next_size(size_t size) { |
michael@0 | 15 | return size + (size >> 1); |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 19 | |
michael@0 | 20 | struct SkChunkAlloc::Block { |
michael@0 | 21 | Block* fNext; |
michael@0 | 22 | size_t fFreeSize; |
michael@0 | 23 | char* fFreePtr; |
michael@0 | 24 | // data[] follows |
michael@0 | 25 | |
michael@0 | 26 | char* startOfData() { |
michael@0 | 27 | return reinterpret_cast<char*>(this + 1); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | static void FreeChain(Block* block) { |
michael@0 | 31 | while (block) { |
michael@0 | 32 | Block* next = block->fNext; |
michael@0 | 33 | sk_free(block); |
michael@0 | 34 | block = next; |
michael@0 | 35 | } |
michael@0 | 36 | }; |
michael@0 | 37 | |
michael@0 | 38 | bool contains(const void* addr) const { |
michael@0 | 39 | const char* ptr = reinterpret_cast<const char*>(addr); |
michael@0 | 40 | return ptr >= (const char*)(this + 1) && ptr < fFreePtr; |
michael@0 | 41 | } |
michael@0 | 42 | }; |
michael@0 | 43 | |
michael@0 | 44 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 45 | |
michael@0 | 46 | SkChunkAlloc::SkChunkAlloc(size_t minSize) { |
michael@0 | 47 | if (minSize < MIN_CHUNKALLOC_BLOCK_SIZE) { |
michael@0 | 48 | minSize = MIN_CHUNKALLOC_BLOCK_SIZE; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | fBlock = NULL; |
michael@0 | 52 | fMinSize = minSize; |
michael@0 | 53 | fChunkSize = fMinSize; |
michael@0 | 54 | fTotalCapacity = 0; |
michael@0 | 55 | fTotalUsed = 0; |
michael@0 | 56 | fBlockCount = 0; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | SkChunkAlloc::~SkChunkAlloc() { |
michael@0 | 60 | this->reset(); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | void SkChunkAlloc::reset() { |
michael@0 | 64 | Block::FreeChain(fBlock); |
michael@0 | 65 | fBlock = NULL; |
michael@0 | 66 | fChunkSize = fMinSize; // reset to our initial minSize |
michael@0 | 67 | fTotalCapacity = 0; |
michael@0 | 68 | fTotalUsed = 0; |
michael@0 | 69 | fBlockCount = 0; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | SkChunkAlloc::Block* SkChunkAlloc::newBlock(size_t bytes, AllocFailType ftype) { |
michael@0 | 73 | size_t size = bytes; |
michael@0 | 74 | if (size < fChunkSize) { |
michael@0 | 75 | size = fChunkSize; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | Block* block = (Block*)sk_malloc_flags(sizeof(Block) + size, |
michael@0 | 79 | ftype == kThrow_AllocFailType ? SK_MALLOC_THROW : 0); |
michael@0 | 80 | |
michael@0 | 81 | if (block) { |
michael@0 | 82 | // block->fNext = fBlock; |
michael@0 | 83 | block->fFreeSize = size; |
michael@0 | 84 | block->fFreePtr = block->startOfData(); |
michael@0 | 85 | |
michael@0 | 86 | fTotalCapacity += size; |
michael@0 | 87 | fBlockCount += 1; |
michael@0 | 88 | |
michael@0 | 89 | fChunkSize = increase_next_size(fChunkSize); |
michael@0 | 90 | } |
michael@0 | 91 | return block; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | void* SkChunkAlloc::alloc(size_t bytes, AllocFailType ftype) { |
michael@0 | 95 | fTotalUsed += bytes; |
michael@0 | 96 | |
michael@0 | 97 | bytes = SkAlign4(bytes); |
michael@0 | 98 | |
michael@0 | 99 | Block* block = fBlock; |
michael@0 | 100 | |
michael@0 | 101 | if (block == NULL || bytes > block->fFreeSize) { |
michael@0 | 102 | block = this->newBlock(bytes, ftype); |
michael@0 | 103 | if (NULL == block) { |
michael@0 | 104 | return NULL; |
michael@0 | 105 | } |
michael@0 | 106 | block->fNext = fBlock; |
michael@0 | 107 | fBlock = block; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | SkASSERT(block && bytes <= block->fFreeSize); |
michael@0 | 111 | char* ptr = block->fFreePtr; |
michael@0 | 112 | |
michael@0 | 113 | block->fFreeSize -= bytes; |
michael@0 | 114 | block->fFreePtr = ptr + bytes; |
michael@0 | 115 | return ptr; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | size_t SkChunkAlloc::unalloc(void* ptr) { |
michael@0 | 119 | size_t bytes = 0; |
michael@0 | 120 | Block* block = fBlock; |
michael@0 | 121 | if (block) { |
michael@0 | 122 | char* cPtr = reinterpret_cast<char*>(ptr); |
michael@0 | 123 | char* start = block->startOfData(); |
michael@0 | 124 | if (start <= cPtr && cPtr < block->fFreePtr) { |
michael@0 | 125 | bytes = block->fFreePtr - cPtr; |
michael@0 | 126 | block->fFreeSize += bytes; |
michael@0 | 127 | block->fFreePtr = cPtr; |
michael@0 | 128 | } |
michael@0 | 129 | } |
michael@0 | 130 | return bytes; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | bool SkChunkAlloc::contains(const void* addr) const { |
michael@0 | 134 | const Block* block = fBlock; |
michael@0 | 135 | while (block) { |
michael@0 | 136 | if (block->contains(addr)) { |
michael@0 | 137 | return true; |
michael@0 | 138 | } |
michael@0 | 139 | block = block->fNext; |
michael@0 | 140 | } |
michael@0 | 141 | return false; |
michael@0 | 142 | } |