blob: 4de641d9fcb31327535762a79b0528803a6d9701 [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
joshualittb7133be2015-04-08 09:08:31 -070046 /**
47 * Returns the total allocated size of the GrMemoryPool
48 */
49 size_t size() const { return fSize; }
50
bsalomon@google.com4da34e32012-06-19 15:40:27 +000051private:
52 struct BlockHeader;
53
commit-bot@chromium.org8743b8f2013-08-01 14:23:33 +000054 static BlockHeader* CreateBlock(size_t size);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000055
commit-bot@chromium.org8743b8f2013-08-01 14:23:33 +000056 static void DeleteBlock(BlockHeader* block);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000057
58 void validate();
59
60 struct BlockHeader {
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000061 BlockHeader* fNext; ///< doubly-linked list of blocks.
bsalomon@google.com4da34e32012-06-19 15:40:27 +000062 BlockHeader* fPrev;
tomhudson@google.comdcba4c22012-07-24 21:36:16 +000063 int fLiveCount; ///< number of outstanding allocations in the
64 ///< block.
65 intptr_t fCurrPtr; ///< ptr to the start of blocks free space.
66 intptr_t fPrevPtr; ///< ptr to the last allocation made
67 size_t fFreeSize; ///< amount of free space left in the block.
joshualittb7133be2015-04-08 09:08:31 -070068 size_t fSize; ///< total allocated size of the block
bsalomon@google.com4da34e32012-06-19 15:40:27 +000069 };
70
71 enum {
72 // We assume this alignment is good enough for everybody.
73 kAlignment = 8,
74 kHeaderSize = GR_CT_ALIGN_UP(sizeof(BlockHeader), kAlignment),
75 kPerAllocPad = GR_CT_ALIGN_UP(sizeof(BlockHeader*), kAlignment),
76 };
joshualittb7133be2015-04-08 09:08:31 -070077 size_t fSize;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000078 size_t fPreallocSize;
79 size_t fMinAllocSize;
80 BlockHeader* fHead;
81 BlockHeader* fTail;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000082#ifdef SK_DEBUG
bsalomon@google.com4da34e32012-06-19 15:40:27 +000083 int fAllocationCnt;
joshualittb7133be2015-04-08 09:08:31 -070084 int fAllocBlockCnt;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000085#endif
86};
87
88#endif