1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/gpu/GrMemoryPool.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,80 @@ 1.4 +/* 1.5 + * Copyright 2012 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef GrMemoryPool_DEFINED 1.12 +#define GrMemoryPool_DEFINED 1.13 + 1.14 +#include "GrTypes.h" 1.15 + 1.16 +/** 1.17 + * Allocates memory in blocks and parcels out space in the blocks for allocation 1.18 + * requests. It is optimized for allocate / release speed over memory 1.19 + * effeciency. The interface is designed to be used to implement operator new 1.20 + * and delete overrides. All allocations are expected to be released before the 1.21 + * pool's destructor is called. Allocations will be 8-byte aligned. 1.22 + */ 1.23 +class GrMemoryPool { 1.24 +public: 1.25 + /** 1.26 + * Prealloc size is the amount of space to make available at pool creation 1.27 + * time and keep around until pool destruction. The min alloc size is the 1.28 + * smallest allowed size of additional allocations. 1.29 + */ 1.30 + GrMemoryPool(size_t preallocSize, size_t minAllocSize); 1.31 + 1.32 + ~GrMemoryPool(); 1.33 + 1.34 + /** 1.35 + * Allocates memory. The memory must be freed with release(). 1.36 + */ 1.37 + void* allocate(size_t size); 1.38 + 1.39 + /** 1.40 + * p must have been returned by allocate() 1.41 + */ 1.42 + void release(void* p); 1.43 + 1.44 + /** 1.45 + * Returns true if there are no unreleased allocations. 1.46 + */ 1.47 + bool isEmpty() const { return fTail == fHead && !fHead->fLiveCount; } 1.48 + 1.49 +private: 1.50 + struct BlockHeader; 1.51 + 1.52 + static BlockHeader* CreateBlock(size_t size); 1.53 + 1.54 + static void DeleteBlock(BlockHeader* block); 1.55 + 1.56 + void validate(); 1.57 + 1.58 + struct BlockHeader { 1.59 + BlockHeader* fNext; ///< doubly-linked list of blocks. 1.60 + BlockHeader* fPrev; 1.61 + int fLiveCount; ///< number of outstanding allocations in the 1.62 + ///< block. 1.63 + intptr_t fCurrPtr; ///< ptr to the start of blocks free space. 1.64 + intptr_t fPrevPtr; ///< ptr to the last allocation made 1.65 + size_t fFreeSize; ///< amount of free space left in the block. 1.66 + }; 1.67 + 1.68 + enum { 1.69 + // We assume this alignment is good enough for everybody. 1.70 + kAlignment = 8, 1.71 + kHeaderSize = GR_CT_ALIGN_UP(sizeof(BlockHeader), kAlignment), 1.72 + kPerAllocPad = GR_CT_ALIGN_UP(sizeof(BlockHeader*), kAlignment), 1.73 + }; 1.74 + size_t fPreallocSize; 1.75 + size_t fMinAllocSize; 1.76 + BlockHeader* fHead; 1.77 + BlockHeader* fTail; 1.78 +#ifdef SK_DEBUG 1.79 + int fAllocationCnt; 1.80 +#endif 1.81 +}; 1.82 + 1.83 +#endif