Click here to Skip to main content
15,891,951 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.3K   2.5K   61  
A Garbage Collector framework that is based upon Generational Copying
// Generation.h: interface for the Generation class.
//
//////////////////////////////////////////////////////////////////////

#ifndef _HEAPMEMORY_H
#define _HEAPMEMORY_H

#include "StdAfx.h"

class Generation  
{
	friend class GC;

private:
// The generation number
	int _GenerationNumber;
// Pointers to the objects in the generation
	std::vector< void* > _Pointers;
// Points to the top of memory available in the generation
	void* _pTopOfMemory;
// The generation allocates memory from this location	
	void* _pNextObjPtr;
// Returns maximum size available for one generation
	enum { MaxSize = 1000 };
// Table of memory inside generation
	BYTE MemoryTable[MaxSize];
	
public:
// Gets the remaining memory of the Generation
	int GetRemainingMemory() const;
// Returns maximum memory that can be allocated for one generation
	int GetTotalMemory() const { return MaxSize; }
// Allocates memory for an object and returns its void*
	void* Allocate( size_t Size );
// Gets the generation number
	int GetGenerationNumber() const { return _GenerationNumber; }
// Performs bitbybit copying 
	static void* CopyBitByBit( const void* pSourceLocation, size_t Size, Generation* pTargetGeneration );
	
	void operator delete( void* v )
	{
		free( v );		
	}
	
public:
	Generation( int GenNumber = 0 );
	virtual ~Generation();

};

#endif 

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