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