Click here to Skip to main content
15,891,253 members
Articles / Mobile Apps

VMemPool - Virtual Memory Pool Management class

Rate me:
Please Sign up or sign in to vote.
4.80/5 (18 votes)
8 Aug 2002CPOL2 min read 154.7K   3.4K   70  
If you deal with same size objects in server coding, VMemPool shows a good way for beginners.
// BitSet.cpp: implementation of the CBitSet class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BitSet.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBitSet::CBitSet()
{
	m_pBitSet = NULL;
	m_dwUsedBytes = 0;
	m_bAutoDelete = FALSE;
}

CBitSet::~CBitSet()
{
	if( m_bAutoDelete )
	{
		if( m_pBitSet )
			delete [] m_pBitSet;
	}
}

//===========================================================
// Name   : Create
// Desc   : Initilize Bitset
// Param  : dwBits - bit count to use
//          bZeroInit - init to zero all bit
//          b32BitAlign - 4bytes alignment 
//          pszSource - provided memory block for using bltset
//          dwSourceLen - pszSource's length
// Return : if success , then return true
//----------------------------------------------------------
// Coder    Date              Desc
// bro      2002-03-08
//===========================================================
BOOL CBitSet::Create( DWORD dwBits , BOOL bZeroInit, BOOL b32BitAlign /*= TRUE*/, char* pszSource /*= NULL*/, DWORD dwSourceLen /*= 0 */)
{
	// Calculate bytes to use for bitset block
	m_dwUsedBytes = sCalcUsedBytes(dwBits, b32BitAlign);
	m_pBitSet = pszSource;
	
	// check block size
	if( pszSource && m_dwUsedBytes > dwSourceLen )
		return FALSE;

	// if source memory block isn't provided, then i'll use new memory block
	if( !pszSource ) 
	{
		m_pBitSet = new char[ m_dwUsedBytes ];
		// failed to create
		if(! m_pBitSet )
			return FALSE;
		m_bAutoDelete = TRUE;
	}

	if( bZeroInit )
		memset( m_pBitSet, 0, m_dwUsedBytes );

	return TRUE;
}

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
Korea (Republic of) Korea (Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions