JASSv2
allocator.h
Go to the documentation of this file.
1 /*
2  ALLOCATOR.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 <stdint.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 
19 #include <atomic>
20 
21 namespace JASS
22  {
23  /*
24  CLASS ALLOCATOR
25  ---------------
26  */
33  class allocator
34  {
35  protected:
36  #ifdef __aarch64__
37  static constexpr size_t alignment_boundary = sizeof(void *);
38  #else
39  static constexpr size_t alignment_boundary = 1;
40  #endif
41  protected:
42  std::atomic<size_t> used;
43  std::atomic<size_t> allocated;
44 
45  public:
46  /*
47  ALLOCATOR::ALLOCATOR()
48  ----------------------
49  */
54  used(0),
55  allocated(0)
56  {
57  }
58 
59  /*
60  ALLOCATOR::~ALLOCATOR()
61  -----------------------
62  */
66  virtual ~allocator()
67  {
68  /*
69  Nothing
70  */
71  }
72 
73  /*
74  ALLOCATOR::OPERATOR==()
75  -----------------------
76  */
82  virtual bool operator==(const allocator &with) = 0;
83 
84  /*
85  ALLOCATOR::OPERATOR!=()
86  -----------------------
87  */
93  virtual bool operator!=(const allocator &with) = 0;
94 
95  /*
96  ALLOCATOR::MALLOC()
97  -------------------
98  */
105  virtual void *malloc(size_t bytes, size_t alignment = alignment_boundary) = 0;
106 
107  /*
108  ALLOCATOR::CAPACITY()
109  ---------------------
110  */
115  size_t capacity(void) const
116  {
117  return allocated;
118  }
119 
120  /*
121  ALLOCATOR::SIZE()
122  -----------------
123  */
128  size_t size(void) const
129  {
130  return used;
131  }
132 
133  /*
134  ALLOCATOR::REALIGN()
135  --------------------
136  */
148  static size_t realign(const void *address, size_t boundary)
149  {
150  return realign((uintptr_t)address, boundary);
151  }
152 
153  /*
154  ALLOCATOR::REALIGN()
155  --------------------
156  */
168  static size_t realign(uintptr_t current_pointer, size_t boundary)
169  {
170  /*
171  Compute the amount of padding that is needed to pad to a boundary of size alignment_boundary
172  */
173  size_t padding = (current_pointer % boundary == 0) ? 0 : boundary - current_pointer % boundary;
174 
175  /*
176  Return the number of bytes that must be addedd to address to make it aligned
177  */
178  return padding;
179  }
180 
181  /*
182  ALLOCATOR::REWIND()
183  -------------------
184  */
189  virtual void rewind(void) = 0;
190  };
191 }
virtual ~allocator()
Destructor.
Definition: allocator.h:66
std::atomic< size_t > allocated
The number of bytes this object has allocated.
Definition: allocator.h:43
virtual bool operator!=(const allocator &with)=0
Compare for inequlity two objects of this class type.
allocator()
Constructor.
Definition: allocator.h:53
Virtual base class for C style allocators.
Definition: allocator.h:33
virtual void rewind(void)=0
Throw away (without calling delete) all objects allocated in the memory space of this allocator Delet...
virtual void * malloc(size_t bytes, size_t alignment=alignment_boundary)=0
Allocate a small chunk of memory from the internal pool and return a pointer to the caller...
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)=0
Compare for equality two objects of this class type.
size_t size(void) const
Return the number of bytes of memory this object has handed back to callers.
Definition: allocator.h:128
static size_t realign(const void *address, size_t boundary)
Compute the number of extra bytes of memory necessary for an allocation to start on an aligned bounda...
Definition: allocator.h:148
static size_t realign(uintptr_t current_pointer, size_t boundary)
Compute the number of extra bytes of memory necessary for an allocation to start on an aligned bounda...
Definition: allocator.h:168
Definition: compress_integer_elias_delta_simd.c:23
static constexpr size_t alignment_boundary
Elsewhere don&#39;t bother with alignment (align on byte boundaries)
Definition: allocator.h:39
size_t capacity(void) const
Return the amount of memory that this object has allocated to it.
Definition: allocator.h:115