blob: 3489cb95638dd26ee087c533d7fac42e84f3a466 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrAllocPool_DEFINED
12#define GrAllocPool_DEFINED
13
14#include "GrNoncopyable.h"
15
16class GrAllocPool : GrNoncopyable {
17public:
18 GrAllocPool(size_t blockSize = 0);
19 ~GrAllocPool();
20
21 /**
22 * Frees all blocks that have been allocated with alloc().
23 */
24 void reset();
25
26 /**
27 * Returns a block of memory bytes size big. This address must not be
28 * passed to realloc/free/delete or any other function that assumes the
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000029 * address was allocated by malloc or new (because it hasn't).
reed@google.comac10a2d2010-12-22 21:39:39 +000030 */
31 void* alloc(size_t bytes);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000032
reed@google.comac10a2d2010-12-22 21:39:39 +000033 /**
34 * Releases the most recently allocated bytes back to allocpool.
35 */
36 void release(size_t bytes);
37
38private:
39 struct Block;
40
41 Block* fBlock;
42 size_t fMinBlockSize;
43
44#if GR_DEBUG
45 int fBlocksAllocated;
46 void validate() const;
47#else
48 void validate() const {}
49#endif
50};
51
52template <typename T> class GrTAllocPool {
53public:
54 GrTAllocPool(int count) : fPool(count * sizeof(T)) {}
55
56 void reset() { fPool.reset(); }
57 T* alloc() { return (T*)fPool.alloc(sizeof(T)); }
58
59private:
60 GrAllocPool fPool;
61};
62
63#endif
64