Click here to Skip to main content
15,896,359 members
Articles / Programming Languages / C++

Vitrual Memory Wrapper Class

Rate me:
Please Sign up or sign in to vote.
4.29/5 (14 votes)
11 Aug 2006CPOL3 min read 39.6K   1.3K   24  
This article introduces a C++ wrapper class that encapsulates the details of using virtual memory.
// Module: VMemory.h
// Copyright(C) 2004 Arman Sahakyan

#ifndef __MODULE_USER_ARM_S__2004_8_7_11_39__TTWI__VIRTUAL_MEMORY_H_
#define __MODULE_USER_ARM_S__2004_8_7_11_39__TTWI__VIRTUAL_MEMORY_H_

class CVMemory
{
	PVOID m_pvBase;
	DWORD m_dwSize;
public:
	CVMemory() {
		m_pvBase = NULL;
		m_dwSize = 0;
	}
	~CVMemory() {
		Release();
	}

	PVOID Reserve(
		DWORD dwSize, 
		PVOID pvBaseAddr = NULL, 
		DWORD fdwProtect = PAGE_READWRITE);
	
	PVOID Commit(
		DWORD dwSize = -1, 
		PVOID pvBaseAddr = NULL, 
		DWORD fdwProtect = PAGE_READWRITE);

	PVOID ReserveCommit(
		DWORD dwSize, 
		PVOID pvBaseAddr = NULL, 
		DWORD fdwProtect = PAGE_READWRITE); 

	void Decommit(DWORD dwSize = -1, PVOID pvAddress = NULL);
 
	BOOL Release();

	DWORD GetSize() const { return m_dwSize; }
	DWORD GetRealSize() const { 
		return m_dwSize == 0 ? 0 : ((m_dwSize / 4096) + 1) * 4096; 
	}
	operator PVOID () {
		return m_pvBase;
	}
};

inline PVOID CVMemory::Reserve(DWORD dwSize, PVOID pvBaseAddr/* = NULL*/, DWORD fdwProtect /*= PAGE_READWRITE*/)
{
	ASSERT(m_pvBase == NULL);
	DWORD fdwFlags = MEM_RESERVE;
	if (pvBaseAddr) fdwFlags |= MEM_TOP_DOWN;
	m_dwSize = dwSize;
	return m_pvBase = ::VirtualAlloc(pvBaseAddr, dwSize, fdwFlags, fdwProtect);
}

inline BOOL CVMemory::Release()
{
	BOOL fRes = ::VirtualFree(m_pvBase, 0, MEM_RELEASE);
	if (fRes) { m_pvBase = NULL; m_dwSize = 0; }
	return fRes;
}

inline PVOID CVMemory::Commit(DWORD dwSize /*= -1*/, PVOID pvBaseAddr /*= NULL*/, DWORD fdwProtect /*= PAGE_READWRITE*/)
{
	PVOID pBase = m_pvBase;
	if (pvBaseAddr) pBase = pvBaseAddr;
	DWORD dwSz = m_dwSize;
	if (dwSize != -1) dwSz = dwSize;

	return ::VirtualAlloc(pBase, dwSz, MEM_COMMIT, fdwProtect);
}

inline PVOID CVMemory::ReserveCommit(DWORD dwSize, PVOID pvBaseAddr /*= NULL*/, DWORD fdwProtect /*= PAGE_READWRITE*/)
{
	ASSERT(m_pvBase == NULL);
	DWORD fdwFlags = MEM_RESERVE | MEM_COMMIT;
	if (pvBaseAddr) fdwFlags |= MEM_TOP_DOWN;
	m_dwSize = dwSize;
	return m_pvBase = ::VirtualAlloc(pvBaseAddr, dwSize, fdwFlags, fdwProtect);
}

inline void CVMemory::Decommit(DWORD dwSize /* = -1*/, PVOID pvAddress /*= NULL*/)
{
	PVOID pBase = m_pvBase;
	if (pvAddress) pBase = pvAddress;
	DWORD dwSz = m_dwSize;
	if (dwSize != -1) dwSz = dwSize;
	::VirtualFree(pBase, dwSz, MEM_DECOMMIT);
}


#endif // __MODULE_USER_ARM_S__2004_8_7_11_39__TTWI__VIRTUAL_MEMORY_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 13
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions