Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Arena Allocator, DTOR and Embedded Preallocated Buffer

, 25 Nov 2009
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)

Share

About the Author

Mamasha Knows
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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 25 Nov 2009
Article Copyright 2009 by Mamasha Knows
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid