|
// 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.
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