From Mature Ibis, 5 Years ago, written in Plain Text.
Embed
  1. #include <cstdlib>
  2.  
  3. #include "makeallocator.h"
  4.  
  5. void Allocator::makeAllocator(size_t maxSize)
  6. {
  7.         this -> start = (char*)malloc(maxSize);
  8.         this -> max = maxSize;
  9.         this -> curNum = 0;
  10.         this -> current = start;
  11. }
  12.  
  13. char* Allocator::alloc(size_t size)
  14. {
  15.         if(curNum + size > max)
  16.         {
  17.                 return nullptr;
  18.         }
  19.         else
  20.         {
  21.                 this -> current += size;
  22.                 this -> curNum += size;
  23.                 return (this -> current - size);
  24.         }
  25. }
  26.  
  27. void Allocator::reset()
  28. {
  29.         this -> current = start;
  30.         this -> curNum = 0;
  31. }
  32.  
  33. char* Allocator:: get_start()
  34. {
  35.         return this -> start;
  36. }