reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2010 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 8 | #ifndef GrAllocPool_DEFINED |
| 9 | #define GrAllocPool_DEFINED |
| 10 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame^] | 11 | #include "SkTypes.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 12 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame^] | 13 | class GrAllocPool : public SkNoncopyable { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | public: |
| 15 | GrAllocPool(size_t blockSize = 0); |
| 16 | ~GrAllocPool(); |
| 17 | |
| 18 | /** |
| 19 | * Frees all blocks that have been allocated with alloc(). |
| 20 | */ |
| 21 | void reset(); |
| 22 | |
| 23 | /** |
| 24 | * Returns a block of memory bytes size big. This address must not be |
| 25 | * 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] | 26 | * address was allocated by malloc or new (because it hasn't). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 27 | */ |
| 28 | void* alloc(size_t bytes); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 29 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 30 | /** |
| 31 | * Releases the most recently allocated bytes back to allocpool. |
| 32 | */ |
| 33 | void release(size_t bytes); |
| 34 | |
| 35 | private: |
| 36 | struct Block; |
| 37 | |
| 38 | Block* fBlock; |
| 39 | size_t fMinBlockSize; |
| 40 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 41 | #ifdef SK_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 42 | int fBlocksAllocated; |
| 43 | void validate() const; |
| 44 | #else |
| 45 | void validate() const {} |
| 46 | #endif |
| 47 | }; |
| 48 | |
| 49 | template <typename T> class GrTAllocPool { |
| 50 | public: |
| 51 | GrTAllocPool(int count) : fPool(count * sizeof(T)) {} |
| 52 | |
| 53 | void reset() { fPool.reset(); } |
| 54 | T* alloc() { return (T*)fPool.alloc(sizeof(T)); } |
| 55 | |
| 56 | private: |
| 57 | GrAllocPool fPool; |
| 58 | }; |
| 59 | |
| 60 | #endif |