• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, deUint32 flags = 0u);
41 					MemPool					(MemPool* parent);
42 					~MemPool				(void);
43 
getRawPool(void)44 	deMemPool*		getRawPool				(void)					{ return m_pool;																}
45 
getNumChildren(void) const46 	int				getNumChildren			(void) const			{ return deMemPool_getNumChildren(m_pool);										}
47 
getNumAllocatedBytes(bool recurse) const48 	deUintptr		getNumAllocatedBytes	(bool recurse) const	{ return deMemPool_getNumAllocatedBytes(m_pool, recurse ? DE_TRUE : DE_FALSE);	}
getCapacity(bool recurse) const49 	deUintptr		getCapacity				(bool recurse) const	{ return deMemPool_getCapacity(m_pool, recurse ? DE_TRUE : DE_FALSE);			}
50 
51 	void*			alloc					(deUintptr numBytes);
52 	void*			alignedAlloc			(deUintptr numBytes, deUint32 alignBytes);
53 
54 private:
55 					MemPool					(const MemPool& other); // Not allowed!
56 	MemPool&		operator=				(const MemPool& other); // Not allowed!
57 
58 	deMemPool*		m_pool;
59 };
60 
61 // MemPool utils.
62 
63 char* copyToPool (de::MemPool* pool, const char* string);
64 
65 // MemPool inline implementations.
66 
MemPool(const deMemPoolUtil * util,deUint32 flags)67 inline MemPool::MemPool (const deMemPoolUtil* util, deUint32 flags)
68 {
69 	m_pool = deMemPool_createRoot(util, flags);
70 	if (!m_pool)
71 		throw std::bad_alloc();
72 }
73 
MemPool(MemPool * parent)74 inline MemPool::MemPool (MemPool* parent)
75 {
76 	m_pool = deMemPool_create(parent->m_pool);
77 	if (!m_pool)
78 		throw std::bad_alloc();
79 }
80 
~MemPool(void)81 inline MemPool::~MemPool (void)
82 {
83 	deMemPool_destroy(m_pool);
84 }
85 
alloc(deUintptr numBytes)86 inline void* MemPool::alloc (deUintptr numBytes)
87 {
88 	// \todo [2013-02-07 pyry] Use deUintptr in deMemPool.
89 	DE_ASSERT((deUintptr)(int)numBytes == numBytes);
90 	void* ptr = deMemPool_alloc(m_pool, (int)numBytes);
91 	if (!ptr)
92 		throw std::bad_alloc();
93 	return ptr;
94 }
95 
alignedAlloc(deUintptr numBytes,deUint32 alignBytes)96 inline void* MemPool::alignedAlloc (deUintptr numBytes, deUint32 alignBytes)
97 {
98 	// \todo [2013-02-07 pyry] Use deUintptr in deMemPool.
99 	DE_ASSERT((deUintptr)(int)numBytes == numBytes);
100 	void* ptr = deMemPool_alignedAlloc(m_pool, (int)numBytes, alignBytes);
101 	if (!ptr)
102 		throw std::bad_alloc();
103 	return ptr;
104 }
105 
106 } // de
107 
108 #endif // _DEMEMPOOL_HPP
109