PageRenderTime 28ms CodeModel.GetById 25ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Core_MemoryAllocators/StackAllocator.h

http://github.com/Akranar/daguerreo
C Header | 46 lines | 38 code | 8 blank | 0 comment | 0 complexity | d31932c85c0ae26fba2886bb5a487175 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1, LGPL-3.0, GPL-2.0
 1#ifndef _STACK_ALLOCATOR_H_
 2#define _STACK_ALLOCATOR_H_
 3
 4#include "MemoryAllocator.h"
 5
 6class StackAllocator : public MemoryAllocator
 7{
 8	void * memory;
 9	void * top;
10	void * end;
11	bool delete_memory;
12public:
13	StackAllocator();
14	StackAllocator(unsigned int total_size);
15	StackAllocator(void * _memory_start, unsigned int total_size, bool owns_memory)
16	:
17		memory(_memory_start),
18		top(memory),
19		end(((char *) top) + total_size),
20		delete_memory(owns_memory)
21	{
22	}
23	~StackAllocator();
24
25	void * Allocate(unsigned int size);
26	void Deallocate(void * pointer);
27
28	inline unsigned int GetFreeSize() const;
29};
30
31inline unsigned int StackAllocator::GetFreeSize() const
32{
33	return (unsigned int) ((char *) end - (char *) top);
34}
35
36inline void * operator new(size_t nbytes, StackAllocator & alloc)
37{
38	return alloc.Allocate(nbytes);
39}
40
41inline void operator delete(void * pointer, StackAllocator & alloc)
42{
43	alloc.Deallocate(pointer);
44}
45
46#endif