Click here to Skip to main content
15,893,368 members
Articles / Desktop Programming / Win32

Half Life Game Level Viewer

Rate me:
Please Sign up or sign in to vote.
4.61/5 (23 votes)
7 Feb 2009CPOL26 min read 80K   2.3K   60  
DirectX based application to open and view Half Life 1 game files
//
//	PoolAllocator.h
//
//	Simple pool allocation template class derived from Allocator template class.  This class will provide a pointer to 
//	a single block of type T from the pool.  If the requested number of blocks is greater than one then the 
//	block array will be allocated using 'new' instead of from the pool.
//
//	Copyright 2005 Paul Higinbotham
//

#ifndef __POOLALLOCATOR_H_
#define __POOLALLOCATOR_H_


#include "allocator.h"
#include "..\sorting\HeapSort.h"
#include <new>							// This may cause the std C++ "throwing" new operator to be used
#include <cassert>


namespace ZGraphics
{


#define DEFAULT_POOL_SIZE	50


template <typename T>
class PoolAllocator : public Allocator<T>
{
public:
	PoolAllocator();
	PoolAllocator(size_t tSize);
	virtual ~PoolAllocator();

	virtual T * Allocate(size_t tNumBlocks);
	virtual void Deallocate(T * pBlock, size_t tNumBlocks);

	// Used to create new pool allocator for a different type
	template <typename U>
	struct rebind { typedef PoolAllocator<U> other; };

	// Accessors
	size_t GetPoolSize() const { return m_tSize; };
	size_t GetFreeBlocks() const { return m_tSize - m_tCurrent; }
	size_t GetPoolOverflow() const { return m_tPoolOverflow; }
	size_t GetNewAllocations() const { return m_tNewAllocate; }
	bool GetDestroyAllocatedBlocks() const { return m_bDestroyAllocatedBlocks; }
	void SetDestroyAllocatedBlocks(bool bDoit) { m_bDestroyAllocatedBlocks = bDoit; }

protected:
	void _createPool(size_t tSize);
	inline bool _isFromThisPool(T * pBlock);

private:
	inline void _initMembers();
	void _destroyAllocatedBlocks();
	bool _isPtrInList(T** ppBlocks, size_t tSize, T* pPtr);

private:
	unsigned char *	m_pPool;			// Pointer to allocated memory for blocks of T
	T *				m_pPoolLast;
	T **			m_ppBlocks;			// Pointer to T blocks in allocated memory pool
	size_t			m_tSize;			// Number of T blocks in m_pPool allocation
	size_t			m_tCurrent;			// Current free block pointer
	bool			m_bDestroyAllocatedBlocks;

	size_t			m_tPoolOverflow;	// Count of times pool was full during an allocation request
	size_t			m_tNewAllocate;		// Count of times new was used to allocate instead of pool
};


// Include inline implementation file
#include "PoolAllocator.inl"


} /* namespace ZGraphics */


#endif	/* __POOLALLOCATOR_H_ */

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I am a senior software developer currently doing contract work for Microsoft. My educational background is in electrical engineering and I hold a masters degree from the University of Washington. I have experience in hardware and systems design but have done primarily software development for the last two decades. I have worked for various small companies as well as start-up companies, and have worked as a full time employee SDE at Microsoft Corporation.

Comments and Discussions