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

A Generational Garbage Collector in C++

Rate me:
Please Sign up or sign in to vote.
2.57/5 (15 votes)
17 Dec 20015 min read 124.4K   2.5K   61  
A Garbage Collector framework that is based upon Generational Copying
void * operator new( unsigned int cb );
void operator delete( void* v );


#define __PLACEMENT_NEW_INLINE
#include "stdafx.h"

#include "GC.h"

#define GC_POINTER( _Pointer, TypePointer, TypeAllocator ) Pointer<TypePointer> _Pointer = new(_Pointer) TypeAllocator;

void GarbageCollectorFunction();
void TestVirtualFunctions();

int g_Count = 0;

class A
{
	int Data[222];

public:

	A()
	{
		cout << "A::A()" << endl;	
	}

	virtual void VirtualFunction() 
	{ 
		cout << "A::VirtualFunction" << endl;	
	}
	
	virtual void NonVirtualFunction() 
	{ 
		cout << "A::NonVirtualFunction" << endl;	
	}
	
	virtual ~A()
	{		
		cout << "~A::A" << endl;
	}
};

class B : public A 
{
public:

	B() // : _Pointer( new(_Pointer) int )
	{
		cout << "B::B()" << endl;	
	}
	
	virtual void VirtualFunction() 
	{ 
		cout << "B::VirtualFunction" << endl;	
	}
	
	virtual void NonVirtualFunction() 
	{ 
		cout << "B::NonVirtualFunction" << endl;	
	}
	
	virtual ~B() 
	{
		cout << "~B::B" << endl;
	}
};

void main()
{
	TestVirtualFunctions();

	GarbageCollectorFunction();

	cout << endl << "Reference count of all objects on the heap: ";
	cout << g_Count << endl;
	
	getchar();
}

void TestVirtualFunctions()
{
	Pointer<A> pSimple = new(pSimple) A; // Base class pointer pointing to Base class object
	
	Pointer<A> pDerived = new(pDerived) B; // Base class pointer pointing to Derived class object
	
	pSimple->VirtualFunction();

	pDerived->VirtualFunction();

}

void GarbageCollectorFunction()
{	
	int Choice = 0;
	int IntObjects = 0;
		
	cout << endl << "1. Allocate memory without garbage collector";
	cout << endl << "2. Allocate memory with garbage collector";
	cout << endl << "Enter Option: ";
	
	cin >> Choice;

	cout << endl << "Enter Number of Objects to Create: ";
	cin >> IntObjects;

	if( Choice == 1 )
	{
		for( int i = 0; i < IntObjects; i++ )
		{
			int* pInt = new int;

			*pInt = 233; // Do Something with the pointer
		} // The memory leaks !!!
		
	}
	else if( Choice == 2 )
	{
		for( int i = 0; i < IntObjects; i++ )
		{
			Pointer<int> pInt = new(pInt) int;
			
		// Can also have the above declaration as:
		// GC_POINTER( pInt, int, int );
			
			*pInt = 233; // Do Something with the pointer
		} // The memory leaks are reclaimed
	}
}

//*----------------------------------------------------------------------------
// Garbage Collection Test Functions
//*----------------------------------------------------------------------------

// The counter is incremented
void* operator new( unsigned int cb )
{
	g_Count++;
	return malloc( cb );
}

// The counter is decremented
void operator delete( void* v )
{
	g_Count--;
	free( v );
}



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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
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