virtmem
virtual memory library for Arduino
alloc.h
Go to the documentation of this file.
1 #ifndef VIRTMEM_ALLOC_H
2 #define VIRTMEM_ALLOC_H
3 
9 #include "base_alloc.h"
10 #include "config/config.h"
11 #include "vptr.h"
12 
13 namespace virtmem {
14 
15 template <typename, typename> class VPtr;
16 
27 template <typename Properties, typename Derived>
28 class VAlloc : public BaseVAlloc
29 {
30  LockPage smallPagesData[Properties::smallPageCount];
31  LockPage mediumPagesData[Properties::mediumPageCount];
32  LockPage bigPagesData[Properties::bigPageCount];
33 #ifdef NVALGRIND
34  uint8_t smallPagePool[Properties::smallPageCount * Properties::smallPageSize] __attribute__ ((aligned (sizeof(TAlign))));
35  uint8_t mediumPagePool[Properties::mediumPageCount * Properties::mediumPageSize] __attribute__ ((aligned (sizeof(TAlign))));
36  uint8_t bigPagePool[Properties::bigPageCount * Properties::bigPageSize] __attribute__ ((aligned (sizeof(TAlign))));
37 #else
38  uint8_t smallPagePool[Properties::smallPageCount * (Properties::smallPageSize + valgrindPad * 2)];
39  uint8_t mediumPagePool[Properties::mediumPageCount * (Properties::mediumPageSize + valgrindPad * 2)];
40  uint8_t bigPagePool[Properties::bigPageCount * (Properties::bigPageSize + valgrindPad * 2)];
41 #endif
42  static VAlloc *instance;
43 
44 protected:
45  VAlloc(void)
46  {
47  ASSERT(!instance);
48  instance = this;
49 #ifdef NVALGRIND
50  initSmallPages(smallPagesData, &smallPagePool[0], Properties::smallPageCount, Properties::smallPageSize);
51  initMediumPages(mediumPagesData, &mediumPagePool[0], Properties::mediumPageCount, Properties::mediumPageSize);
52  initBigPages(bigPagesData, &bigPagePool[0], Properties::bigPageCount, Properties::bigPageSize);
53 #else
54  initSmallPages(smallPagesData, &smallPagePool[pad], Properties::smallPageCount, Properties::smallPageSize);
55  initMediumPages(mediumPagesData, &mediumPagePool[pad], Properties::mediumPageCount, Properties::mediumPageSize);
56  initBigPages(bigPagesData, &bigPagePool[pad], Properties::bigPageCount, Properties::bigPageSize);
57  VALGRIND_MAKE_MEM_NOACCESS(&smallPagePool[0], pad); VALGRIND_MAKE_MEM_NOACCESS(&smallPagePool[Properties::smallPageCount * Properties::smallPageSize + pad], pad);
58  VALGRIND_MAKE_MEM_NOACCESS(&mediumPagePool[0], pad); VALGRIND_MAKE_MEM_NOACCESS(&mediumPagePool[Properties::mediumPageCount * Properties::mediumPageSize + pad], pad);
59  VALGRIND_MAKE_MEM_NOACCESS(&bigPagePool[0], pad); VALGRIND_MAKE_MEM_NOACCESS(&bigPagePool[Properties::bigPageCount * Properties::bigPageSize + pad], pad);
60 #endif
61  }
62  ~VAlloc(void) { instance = 0; }
63 
64 public:
79  static VAlloc *getInstance(void) { return instance; }
80 
81  // C style malloc/free
96  template <typename T> VPtr<T, Derived> alloc(VPtrSize size=sizeof(T))
97  {
99  ret.setRawNum(allocRaw(size));
100  return ret;
101  }
102 
110  template <typename T> void free(VPtr<T, Derived> &p)
111  {
112  freeRaw(p.getRawNum());
113  p.setRawNum(0);
114  }
115 
116  // C++ style new/delete --> call constructors (by placement new) and destructors
129  template <typename T> VPtr<T, Derived> newClass(VPtrSize size=sizeof(T))
130  {
131  virtmem::VPtr<T, Derived> ret = alloc<T>(size);
132  T *ptr = static_cast<T *>(read(ret.getRawNum(), sizeof(T)));
133  new (ptr) T; // UNDONE: can this be ro?
134  return ret;
135  }
136 
146  template <typename T> void deleteClass(VPtr<T, Derived> &p)
147  {
148  p->~T();
149  freeRaw(p.getRawNum());
150  }
151 
152  // C++ style new []/delete [] --> calls constructors and destructors
153  // the size of the array (necessary for destruction) is stored at the beginning of the block.
154  // A pointer offset from this is returned.
165  template <typename T> VPtr<T, Derived> newArray(VPtrSize elements)
166  {
167  VPtrNum p = allocRaw(sizeof(T) * elements + sizeof(VPtrSize));
168  write(p, &elements, sizeof(VPtrSize));
169  p += sizeof(VPtrSize);
170  for (VPtrSize s=0; s<elements; ++s)
171  new (read(p + (s * sizeof(T)), sizeof(T))) T; // UNDONE: can this be ro?
172 
174  ret.setRawNum(p);
175  return ret;
176  }
186  template <typename T> void deleteArray(VPtr<T, Derived> &p)
187  {
188  const VPtrNum soffset = p.getRawNum() - sizeof(VPtrSize); // pointer to size offset
189  VPtrSize *size = static_cast<VPtrSize *>(read(soffset, sizeof(VPtrSize)));
190  for (VPtrSize s=0; s<*size; ++s)
191  {
192  T *ptr = static_cast<T *>(read(p.getRawNum() + (s * sizeof(T)), sizeof(T)));
193  ptr->~T();
194  }
195  freeRaw(soffset); // soffset points at beginning of actual block
196  }
197 
209  template <typename T> struct TVPtr { typedef virtmem::VPtr<T, Derived> type; };
210 
211 #ifdef VIRTMEM_CPP11
212 
221  template <typename T> using VPtr = virtmem::VPtr<T, Derived>;
222 #endif
223 };
224 
225 template <typename Properties, typename Derived>
227 
228 }
229 
230 #endif // VIRTMEM_ALLOC_H
contains all code from virtmem
Definition: base_alloc.cpp:22
Base template class for virtual memory allocators.
Definition: alloc.h:28
PtrNum getRawNum(void) const
Returns a numeric representation of this virtual pointer.
Definition: base_vptr.h:153
void write(VPtrNum p, const void *d, VPtrSize size)
Writes a piece of raw data to (virtual) memory.
Definition: base_alloc.cpp:770
This header file contains several variables that can be used to customize virtmem.
void setRawNum(PtrNum p)
Sets a virtual pointer from a numeric value.
Definition: base_vptr.h:159
VPtrNum allocRaw(VPtrSize size)
Allocates a piece of raw (virtual) memory.
Definition: base_alloc.cpp:560
void freeRaw(VPtrNum ptr)
Frees a memory block for re-usage.
Definition: base_alloc.cpp:647
Base class for virtual memory allocators.
Definition: base_alloc.h:31
VPtr< T, Derived > newClass(VPtrSize size=sizeof(T))
Allocates memory and constructs data type.
Definition: alloc.h:129
uint32_t VPtrNum
Numeric type used to store raw virtual pointer addresses.
Definition: base_alloc.h:21
This header contains the class definition of the VPtr class.
uint32_t VPtrSize
Numeric type used to store the size of a virtual memory block.
Definition: base_alloc.h:22
void free(VPtr< T, Derived > &p)
Frees a block of virtual memory.
Definition: alloc.h:110
Base virtual memory class header.
VPtr< T, Derived > alloc(VPtrSize size=sizeof(T))
Allocates memory from the linked virtual memory allocator.
Definition: alloc.h:96
void deleteArray(VPtr< T, Derived > &p)
Destructs and frees an array of objects.
Definition: alloc.h:186
VPtr< T, Derived > newArray(VPtrSize elements)
Allocates and constructs an array of objects.
Definition: alloc.h:165
void deleteClass(VPtr< T, Derived > &p)
Destructs data type and frees memory.
Definition: alloc.h:146
Generalized shortcut to virtual pointer type linked to this allocator.
Definition: alloc.h:209
Virtual pointer class that provides an interface to access virtual much like 'regular pointers'...
Definition: alloc.h:15
void * read(VPtrNum p, VPtrSize size)
Reads a raw block of (virtual) memory.
Definition: base_alloc.cpp:724
static VAlloc * getInstance(void)
Returns a pointer to the instance of the class.
Definition: alloc.h:79