epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef GrAllocPool_DEFINED |
| 12 | #define GrAllocPool_DEFINED |
| 13 | |
| 14 | #include "GrNoncopyable.h" |
| 15 | |
| 16 | class GrAllocPool : GrNoncopyable { |
| 17 | public: |
| 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.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 29 | * address was allocated by malloc or new (because it hasn't). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 30 | */ |
| 31 | void* alloc(size_t bytes); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 32 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | /** |
| 34 | * Releases the most recently allocated bytes back to allocpool. |
| 35 | */ |
| 36 | void release(size_t bytes); |
| 37 | |
| 38 | private: |
| 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 | |
| 52 | template <typename T> class GrTAllocPool { |
| 53 | public: |
| 54 | GrTAllocPool(int count) : fPool(count * sizeof(T)) {} |
| 55 | |
| 56 | void reset() { fPool.reset(); } |
| 57 | T* alloc() { return (T*)fPool.alloc(sizeof(T)); } |
| 58 | |
| 59 | private: |
| 60 | GrAllocPool fPool; |
| 61 | }; |
| 62 | |
| 63 | #endif |
| 64 | |