13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef _DEMEMPOOL_HPP
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define _DEMEMPOOL_HPP
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements C++ Base Library
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * -----------------------------
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Memory pool (deMemPool wrapper).
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deDefs.hpp"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deMemPool.h"
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <new>
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace de
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*!
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Memory pool
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass MemPool
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					MemPool					(const deMemPoolUtil* util = DE_NULL, deUint32 flags = 0u);
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					MemPool					(MemPool* parent);
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					~MemPool				(void);
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deMemPool*		getRawPool				(void)					{ return m_pool;																}
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int				getNumChildren			(void) const			{ return deMemPool_getNumChildren(m_pool);										}
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deUintptr		getNumAllocatedBytes	(bool recurse) const	{ return deMemPool_getNumAllocatedBytes(m_pool, recurse ? DE_TRUE : DE_FALSE);	}
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deUintptr		getCapacity				(bool recurse) const	{ return deMemPool_getCapacity(m_pool, recurse ? DE_TRUE : DE_FALSE);			}
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void*			alloc					(deUintptr numBytes);
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void*			alignedAlloc			(deUintptr numBytes, deUint32 alignBytes);
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					MemPool					(const MemPool& other); // Not allowed!
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	MemPool&		operator=				(const MemPool& other); // Not allowed!
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deMemPool*		m_pool;
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// MemPool utils.
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyrychar* copyToPool (de::MemPool* pool, const char* string);
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// MemPool inline implementations.
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline MemPool::MemPool (const deMemPoolUtil* util, deUint32 flags)
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_pool = deMemPool_createRoot(util, flags);
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!m_pool)
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw std::bad_alloc();
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline MemPool::MemPool (MemPool* parent)
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_pool = deMemPool_create(parent->m_pool);
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!m_pool)
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw std::bad_alloc();
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline MemPool::~MemPool (void)
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deMemPool_destroy(m_pool);
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline void* MemPool::alloc (deUintptr numBytes)
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// \todo [2013-02-07 pyry] Use deUintptr in deMemPool.
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_ASSERT((deUintptr)(int)numBytes == numBytes);
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void* ptr = deMemPool_alloc(m_pool, (int)numBytes);
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!ptr)
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw std::bad_alloc();
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return ptr;
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline void* MemPool::alignedAlloc (deUintptr numBytes, deUint32 alignBytes)
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// \todo [2013-02-07 pyry] Use deUintptr in deMemPool.
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_ASSERT((deUintptr)(int)numBytes == numBytes);
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void* ptr = deMemPool_alignedAlloc(m_pool, (int)numBytes, alignBytes);
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!ptr)
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw std::bad_alloc();
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return ptr;
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // de
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif // _DEMEMPOOL_HPP
109