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 2012 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 | #ifndef GrMemoryPool_DEFINED |
michael@0 | 9 | #define GrMemoryPool_DEFINED |
michael@0 | 10 | |
michael@0 | 11 | #include "GrTypes.h" |
michael@0 | 12 | |
michael@0 | 13 | /** |
michael@0 | 14 | * Allocates memory in blocks and parcels out space in the blocks for allocation |
michael@0 | 15 | * requests. It is optimized for allocate / release speed over memory |
michael@0 | 16 | * effeciency. The interface is designed to be used to implement operator new |
michael@0 | 17 | * and delete overrides. All allocations are expected to be released before the |
michael@0 | 18 | * pool's destructor is called. Allocations will be 8-byte aligned. |
michael@0 | 19 | */ |
michael@0 | 20 | class GrMemoryPool { |
michael@0 | 21 | public: |
michael@0 | 22 | /** |
michael@0 | 23 | * Prealloc size is the amount of space to make available at pool creation |
michael@0 | 24 | * time and keep around until pool destruction. The min alloc size is the |
michael@0 | 25 | * smallest allowed size of additional allocations. |
michael@0 | 26 | */ |
michael@0 | 27 | GrMemoryPool(size_t preallocSize, size_t minAllocSize); |
michael@0 | 28 | |
michael@0 | 29 | ~GrMemoryPool(); |
michael@0 | 30 | |
michael@0 | 31 | /** |
michael@0 | 32 | * Allocates memory. The memory must be freed with release(). |
michael@0 | 33 | */ |
michael@0 | 34 | void* allocate(size_t size); |
michael@0 | 35 | |
michael@0 | 36 | /** |
michael@0 | 37 | * p must have been returned by allocate() |
michael@0 | 38 | */ |
michael@0 | 39 | void release(void* p); |
michael@0 | 40 | |
michael@0 | 41 | /** |
michael@0 | 42 | * Returns true if there are no unreleased allocations. |
michael@0 | 43 | */ |
michael@0 | 44 | bool isEmpty() const { return fTail == fHead && !fHead->fLiveCount; } |
michael@0 | 45 | |
michael@0 | 46 | private: |
michael@0 | 47 | struct BlockHeader; |
michael@0 | 48 | |
michael@0 | 49 | static BlockHeader* CreateBlock(size_t size); |
michael@0 | 50 | |
michael@0 | 51 | static void DeleteBlock(BlockHeader* block); |
michael@0 | 52 | |
michael@0 | 53 | void validate(); |
michael@0 | 54 | |
michael@0 | 55 | struct BlockHeader { |
michael@0 | 56 | BlockHeader* fNext; ///< doubly-linked list of blocks. |
michael@0 | 57 | BlockHeader* fPrev; |
michael@0 | 58 | int fLiveCount; ///< number of outstanding allocations in the |
michael@0 | 59 | ///< block. |
michael@0 | 60 | intptr_t fCurrPtr; ///< ptr to the start of blocks free space. |
michael@0 | 61 | intptr_t fPrevPtr; ///< ptr to the last allocation made |
michael@0 | 62 | size_t fFreeSize; ///< amount of free space left in the block. |
michael@0 | 63 | }; |
michael@0 | 64 | |
michael@0 | 65 | enum { |
michael@0 | 66 | // We assume this alignment is good enough for everybody. |
michael@0 | 67 | kAlignment = 8, |
michael@0 | 68 | kHeaderSize = GR_CT_ALIGN_UP(sizeof(BlockHeader), kAlignment), |
michael@0 | 69 | kPerAllocPad = GR_CT_ALIGN_UP(sizeof(BlockHeader*), kAlignment), |
michael@0 | 70 | }; |
michael@0 | 71 | size_t fPreallocSize; |
michael@0 | 72 | size_t fMinAllocSize; |
michael@0 | 73 | BlockHeader* fHead; |
michael@0 | 74 | BlockHeader* fTail; |
michael@0 | 75 | #ifdef SK_DEBUG |
michael@0 | 76 | int fAllocationCnt; |
michael@0 | 77 | #endif |
michael@0 | 78 | }; |
michael@0 | 79 | |
michael@0 | 80 | #endif |