/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

  1. #ifndef _STACK_ALLOCATOR_H_
  2. #define _STACK_ALLOCATOR_H_
  3. #include "MemoryAllocator.h"
  4. class StackAllocator : public MemoryAllocator
  5. {
  6. void * memory;
  7. void * top;
  8. void * end;
  9. bool delete_memory;
  10. public:
  11. StackAllocator();
  12. StackAllocator(unsigned int total_size);
  13. StackAllocator(void * _memory_start, unsigned int total_size, bool owns_memory)
  14. :
  15. memory(_memory_start),
  16. top(memory),
  17. end(((char *) top) + total_size),
  18. delete_memory(owns_memory)
  19. {
  20. }
  21. ~StackAllocator();
  22. void * Allocate(unsigned int size);
  23. void Deallocate(void * pointer);
  24. inline unsigned int GetFreeSize() const;
  25. };
  26. inline unsigned int StackAllocator::GetFreeSize() const
  27. {
  28. return (unsigned int) ((char *) end - (char *) top);
  29. }
  30. inline void * operator new(size_t nbytes, StackAllocator & alloc)
  31. {
  32. return alloc.Allocate(nbytes);
  33. }
  34. inline void operator delete(void * pointer, StackAllocator & alloc)
  35. {
  36. alloc.Deallocate(pointer);
  37. }
  38. #endif