JASSv2
allocator_memory.h
Go to the documentation of this file.
1 /*
2  ALLOCATOR_MEMORY.H
3  ------------------
4  Copyright (c) 2016 Andrew Trotman
5  Released under the 2-clause BSD license (See:https://en.wikipedia.org/wiki/BSD_licenses)
6 */
13 #pragma once
14 
15 #include "allocator.h"
16 
17 namespace JASS
18  {
25  class allocator_memory : public allocator
26  {
27  protected:
28  std::atomic<uint8_t *> buffer;
29 
30  public:
31  /*
32  ALLOCATOR_MEMORY::ALLOCATOR_MEMORY()
33  ------------------------------------
34  */
38  allocator_memory(void *buffer, size_t length) :
39  buffer((uint8_t *)buffer)
40  {
41  allocated = length;
42  /*
43  Nothing
44  */
45  }
46 
47  /*
48  ALLOCATOR_MEMORY::~ALLOCATOR_MEMORY()
49  -------------------------------------
50  */
55  {
56  /*
57  Nothing
58  */
59  }
60 
61  /*
62  ALLOCATOR_MEMORY::OPERATOR==()
63  ------------------------------
64  */
70  virtual bool operator==(const allocator &with)
71  {
72  try
73  {
74  /*
75  Downcast the parameter to an allocator_memory and see if it matches this.
76  */
77  auto other = dynamic_cast<const allocator_memory *>(&with);
78  return (used == other->size()) && (allocated == other->capacity()) && (buffer.load() == other->buffer.load());
79  }
80  catch (...)
81  {
82  return false;
83  }
84  }
85 
86  /*
87  ALLOCATOR_MEMORY::OPERATOR!=()
88  ------------------------------
89  */
95  virtual bool operator!=(const allocator &with)
96  {
97  try
98  {
99  /*
100  Downcast the parameter to an allocator_memory and see if it matches this.
101  */
102  auto other = dynamic_cast<const allocator_memory *>(&with);
103  return (used != other->size()) || (allocated != other->capacity()) || (buffer.load() != other->buffer.load());
104  }
105  catch (...)
106  {
107  return false;
108  }
109  }
110 
111  /*
112  ALLOCATOR_MEMORY::MALLOC()
113  --------------------------
114  */
121  virtual void *malloc(size_t bytes, size_t alignment = alignment_boundary);
122 
123  /*
124  ALLOCATOR_MEMORY::REWIND()
125  --------------------------
126  */
131  virtual void rewind(void);
132 
133  /*
134  ALLOCATOR_POOL::UNITTEST()
135  --------------------------
136  */
140  static void unittest(void);
141  };
142  }
virtual void * malloc(size_t bytes, size_t alignment=alignment_boundary)
Allocate a small chunk of memory from the internal pool and return a pointer to the caller...
Definition: allocator_memory.cpp:18
std::atomic< size_t > allocated
The number of bytes this object has allocated.
Definition: allocator.h:43
static void unittest(void)
Unit test this class.
Definition: allocator_memory.cpp:77
virtual void rewind(void)
Throw away (without calling delete) all objects allocated in the memory space of this allocator Delet...
Definition: allocator_memory.cpp:68
Virtual base class for C style allocators.
Definition: allocator.h:33
virtual bool operator!=(const allocator &with)
Compare for inequlity two objects of this class type.
Definition: allocator_memory.h:95
std::atomic< size_t > used
The number of bytes this object has passed back to the caller.
Definition: allocator.h:42
virtual bool operator==(const allocator &with)
Compare for equality two objects of this class type.
Definition: allocator_memory.h:70
Base class for different allocators.
virtual ~allocator_memory()
Destructor.
Definition: allocator_memory.h:54
allocator_memory(void *buffer, size_t length)
Constructor.
Definition: allocator_memory.h:38
std::atomic< uint8_t * > buffer
The buffer we&#39;re allocating from.
Definition: allocator_memory.h:28
Definition: allocator.h:21
static constexpr size_t alignment_boundary
Elsewhere don&#39;t bother with alignment (align on byte boundaries)
Definition: allocator.h:39
Memory allocator out of a single non-growable fixed-sized buffer.
Definition: allocator_memory.h:25