1 #ifndef _DEMEMPOOL_HPP
2 #define _DEMEMPOOL_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements C++ Base Library
5 * -----------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Memory pool (deMemPool wrapper).
24 *//*--------------------------------------------------------------------*/
25
26 #include "deDefs.hpp"
27 #include "deMemPool.h"
28
29 #include <new>
30
31 namespace de
32 {
33
34 /*--------------------------------------------------------------------*//*!
35 * \brief Memory pool
36 *//*--------------------------------------------------------------------*/
37 class MemPool
38 {
39 public:
40 MemPool(const deMemPoolUtil *util = DE_NULL, uint32_t flags = 0u);
41 MemPool(MemPool *parent);
42 ~MemPool(void);
43
getRawPool(void)44 deMemPool *getRawPool(void)
45 {
46 return m_pool;
47 }
48
getNumChildren(void) const49 int getNumChildren(void) const
50 {
51 return deMemPool_getNumChildren(m_pool);
52 }
53
getNumAllocatedBytes(bool recurse) const54 uintptr_t getNumAllocatedBytes(bool recurse) const
55 {
56 return deMemPool_getNumAllocatedBytes(m_pool, recurse ? true : false);
57 }
getCapacity(bool recurse) const58 uintptr_t getCapacity(bool recurse) const
59 {
60 return deMemPool_getCapacity(m_pool, recurse ? true : false);
61 }
62
63 void *alloc(uintptr_t numBytes);
64 void *alignedAlloc(uintptr_t numBytes, uint32_t alignBytes);
65
66 private:
67 MemPool(const MemPool &other); // Not allowed!
68 MemPool &operator=(const MemPool &other); // Not allowed!
69
70 deMemPool *m_pool;
71 };
72
73 // MemPool utils.
74
75 char *copyToPool(de::MemPool *pool, const char *string);
76
77 // MemPool inline implementations.
78
MemPool(const deMemPoolUtil * util,uint32_t flags)79 inline MemPool::MemPool(const deMemPoolUtil *util, uint32_t flags)
80 {
81 m_pool = deMemPool_createRoot(util, flags);
82 if (!m_pool)
83 throw std::bad_alloc();
84 }
85
MemPool(MemPool * parent)86 inline MemPool::MemPool(MemPool *parent)
87 {
88 m_pool = deMemPool_create(parent->m_pool);
89 if (!m_pool)
90 throw std::bad_alloc();
91 }
92
~MemPool(void)93 inline MemPool::~MemPool(void)
94 {
95 deMemPool_destroy(m_pool);
96 }
97
alloc(uintptr_t numBytes)98 inline void *MemPool::alloc(uintptr_t numBytes)
99 {
100 // \todo [2013-02-07 pyry] Use uintptr_t in deMemPool.
101 DE_ASSERT((uintptr_t)(int)numBytes == numBytes);
102 void *ptr = deMemPool_alloc(m_pool, (int)numBytes);
103 if (!ptr)
104 throw std::bad_alloc();
105 return ptr;
106 }
107
alignedAlloc(uintptr_t numBytes,uint32_t alignBytes)108 inline void *MemPool::alignedAlloc(uintptr_t numBytes, uint32_t alignBytes)
109 {
110 // \todo [2013-02-07 pyry] Use uintptr_t in deMemPool.
111 DE_ASSERT((uintptr_t)(int)numBytes == numBytes);
112 void *ptr = deMemPool_alignedAlloc(m_pool, (int)numBytes, alignBytes);
113 if (!ptr)
114 throw std::bad_alloc();
115 return ptr;
116 }
117
118 } // namespace de
119
120 #endif // _DEMEMPOOL_HPP
121