commit fb46692ca4279b521b72c8bb594322b0f0e10551
parent 23aa791488381a4958afebdefa9cdd367b2f00ea
Author: Johannes Lorenz <[email protected]>
Date: Fri, 7 Aug 2015 18:16:22 +0200
Added assertions for DummyAllocator.
Diffstat:
4 files changed, 40 insertions(+), 13 deletions(-)
diff --git a/TODO.txt b/TODO.txt
@@ -14,7 +14,7 @@ Status:
(1) will be fixed by fundamental
(2) will be fixed with fftw3 (?), until then maybe use separate mutex?
(3) will be fixed by fundamental
-(4)
+(4) fixed + tested
(5) fixed + tested
(6) fixed + tested (only basic testing)
(7) -> will not be fixed (should stay random)
diff --git a/src/Misc/Allocator.cpp b/src/Misc/Allocator.cpp
@@ -7,7 +7,7 @@
#include "Allocator.h"
//Used for dummy allocations
-Allocator DummyAlloc;
+DummyAllocator DummyAlloc;
//recursive type class to avoid void *v = *(void**)v style casting
struct next_t
@@ -57,7 +57,7 @@ Allocator::~Allocator(void)
delete impl;
}
-void *Allocator::alloc_mem(size_t mem_size)
+void *AllocatorClass::alloc_mem(size_t mem_size)
{
impl->totalAlloced += mem_size;
void *mem = tlsf_malloc(impl->tlsf, mem_size);
@@ -66,14 +66,14 @@ void *Allocator::alloc_mem(size_t mem_size)
//printf("Allocator result = %p\n", mem);
return mem;
}
-void Allocator::dealloc_mem(void *memory)
+void AllocatorClass::dealloc_mem(void *memory)
{
//printf("dealloc_mem(%d)\n", tlsf_block_size(memory));
tlsf_free(impl->tlsf, memory);
//free(memory);
}
-bool Allocator::lowMemory(unsigned n, size_t chunk_size)
+bool AllocatorClass::lowMemory(unsigned n, size_t chunk_size) const
{
//This should stay on the stack
void *buf[n];
@@ -90,7 +90,7 @@ bool Allocator::lowMemory(unsigned n, size_t chunk_size)
}
-void Allocator::addMemory(void *v, size_t mem_size)
+void AllocatorClass::addMemory(void *v, size_t mem_size)
{
next_t *n = impl->pools;
while(n->next) n = n->next;
diff --git a/src/Misc/Allocator.h b/src/Misc/Allocator.h
@@ -2,14 +2,17 @@
#include <cstdlib>
#include <utility>
+//! Allocator Base class
+//! subclasses must specify allocation and deallocation
class Allocator
{
public:
Allocator(void);
Allocator(const Allocator&) = delete;
- ~Allocator(void);
- void *alloc_mem(size_t mem_size);
- void dealloc_mem(void *memory);
+ virtual ~Allocator(void);
+
+ virtual void *alloc_mem(size_t mem_size) = 0;
+ virtual void dealloc_mem(void *memory) = 0;
template <typename T, typename... Ts>
T *alloc(Ts&&... ts)
@@ -64,10 +67,10 @@ class Allocator
}
}
- void addMemory(void *, size_t mem_size);
+ virtual void addMemory(void *, size_t mem_size) = 0;
//Return true if the current pool cannot allocate n chunks of chunk_size
- bool lowMemory(unsigned n, size_t chunk_size);
+ virtual bool lowMemory(unsigned n, size_t chunk_size) const = 0;
bool memFree(void *pool) const;
//returns number of pools
@@ -80,7 +83,31 @@ class Allocator
struct AllocatorImpl *impl;
};
-extern Allocator DummyAlloc;
+//! the allocator for normal use
+class AllocatorClass : public Allocator
+{
+ void *alloc_mem(size_t mem_size);
+ void dealloc_mem(void *memory);
+ void addMemory(void *, size_t mem_size);
+ bool lowMemory(unsigned n, size_t chunk_size) const;
+ using Allocator::Allocator;
+};
+
+//! the dummy allocator, which does not allow any allocation
+class DummyAllocator : public Allocator
+{
+ void not_allowed() const {
+ throw "(de)allocation forbidden"; // TODO: std exception
+ }
+public:
+ void *alloc_mem(size_t ) { return not_allowed(), nullptr; }
+ void dealloc_mem(void* ) { not_allowed(); } // TODO: more functions?
+ void addMemory(void *, size_t ) { not_allowed(); }
+ bool lowMemory(unsigned , size_t ) const { return not_allowed(), true; }
+ using Allocator::Allocator;
+};
+
+extern DummyAllocator DummyAlloc;
/**
* General notes on Memory Allocation Within ZynAddSubFX
diff --git a/src/Misc/Master.cpp b/src/Misc/Master.cpp
@@ -296,7 +296,7 @@ Master::Master(const SYNTH_T &synth_, Config* config)
{
bToU = NULL;
uToB = NULL;
- memory = new Allocator();
+ memory = new AllocatorClass();
swaplr = 0;
off = 0;
smps = 0;