Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C++

Arena Allocator, DTOR and Embedded Preallocated Buffer

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
25 Nov 2009CPOL8 min read 37.4K   361   33  
Arena like memory management, embedding allocations inside Arena, DTOR, context thinking
// arena_unitest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "arena_allocator.h"


struct Thing {};


// constructing arena usage
void constructing_arena()
{
	{
		// allocate and construct a Thing
		MyArena<256> arena;
		Thing* thing = new (arena) Thing;
	}

	{
		// allocate dynamic array of integers
		int n = 10;
		MyArena<256> arena;
		int* arr = new (arena) int [n];
	}

	{
		// allocate big dynamic array
		MyArena<256> arena;
		double* arr = new (arena) double [1024*1024];   // Arena propagates this allocation to heap
	}
}


// constructing arena usage
void allocating_objects()
{
	{
		// allocate and construct a Thing
		MyArena<256> arena;
		Thing* thing = new (arena) Thing;
	}

	{
		// allocate dynamic array of integers
		int n = 10;
		MyArena<256> arena;
		int* arr = new (arena) int [n];
	}

	{
		// allocate big dynamic array
		MyArena<256> arena;
		double* arr = new (arena) double [1024*1024];   // Arena propagates this allocation to heap
	}
}


// constructing arena usage
void destroying_objects()
{
	{
		// register Thing's DTOR
		MyArena<256> arena;
		Thing* thing = new (arena) Thing;
		arena.DTOR(thing);            // DTOR will be called when arena goes out of scope
	}

	{
		// register array of DTORs
		int n = 10;
		MyArena<256> arena;
		Thing* things = new (arena) Thing [n];
		arena.DTOR(things, n);        // DTORs will be called when arena goes out of scope
	}
}


// is operator new [] ever called ?
#if 0
inline
	void* operator new [] (size_t size, Arena& arena)
{
	return
		arena.alloc(size);
}
#endif


struct Obj
{
	virtual ~Obj()	{;}
};


int main(int argc, char* argv[])
{
	MyArena<256> arena;
	int* pi = new (arena) int [10];
	Obj* po = new (arena) Obj [10];

	unitest::arena_usecases();

	constructing_arena();
	allocating_objects();
	destroying_objects();

	return 0;
}

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
Chief Technology Officer Cpp2Mtl Integration Solutions
United States United States
My real name is Reuven Bass. My first article here was published under the Mamasha Knows pseudonym. It worked. So, I stay with Mamasha for a while. (If it works - do not touch it)

Programming became my life from thirteen. I love coding. I love beauty. I always try to combine coding and beauty.

RB

Comments and Discussions