path: root/CommonLibs
diff options
authorPau Espin Pedrol <pespin@sysmocom.de>2018-12-03 17:46:04 +0100
committerHarald Welte <laforge@gnumonks.org>2018-12-05 19:41:34 +0000
commitf7331764ac22d3c25838be057835b79ef7ceaadd (patch)
treefff479bb9fe2eee69484e455b8a9eaa3b11b32d2 /CommonLibs
parent800c029c709fda37ec1dd37b833ae5decd03ec2c (diff)
SigProcLib: Improve Vector buffer allocation mess
Original issue: In order to use SSE instructions, 16-byte aligned memory chunks are needed, and C++ version < C++11 doesn't provide for a native new/delete store. For that reason, memalign() must be used in the implementation of convolve_h_alloc() for some buffers. On the other side, The C++ code relies on C++ "new T[]" operator to allocate a chunk of memory containing an array of class instances. As classes are complex types, they cannot be allocated through C structures (calling malloc). Experimentally can be seen too that it's unreliable and the process will crash during startup if malloc() is used and then a Complex<> deferred from it. Previous implementation allowed for use of convolve_h_alloc or new[] based on how the (signal)Vector is called, because then the buffer is not going to be managed internally. But that's unreliable since resize() calling resize() on it could use "delete" operator on a malloc'ed buffer, and end up having a new new[] allocated buffer. It was also found that some of the callers were actually leaking memory through ASan (because the buffer is not managed by the Vector instance). IMHO best option would be to rewrite all this code using C structures and malloc/free exclusively, since it would make all this cod eeasier to maintain. But for now, let's extend the Vector class to allow specifying an external alloc/free function and let the Vector instance take care of the ownership of the buffer in all scenarios. Change-Id: Ie484a4762a7f77fe1b105188ea03a6f025730b82
Diffstat (limited to 'CommonLibs')
1 files changed, 31 insertions, 12 deletions
diff --git a/CommonLibs/Vector.h b/CommonLibs/Vector.h
index 9119683..4c96b78 100644
--- a/CommonLibs/Vector.h
+++ b/CommonLibs/Vector.h
@@ -32,11 +32,14 @@
#include <string.h>
#include <iostream>
#include <assert.h>
+#include <stdlib.h>
// We cant use Logger.h in this file...
extern int gVectorDebug;
#define BVDEBUG(msg) if (gVectorDebug) {std::cout << msg;}
+typedef void (*vector_free_func)(void* wData);
+typedef void *(*vector_alloc_func)(size_t newSize);
A simplified Vector template with aliases.
@@ -60,6 +63,8 @@ template <class T> class Vector {
T* mData; ///< allocated data block, if any
T* mStart; ///< start of useful data
T* mEnd; ///< end of useful data + 1
+ vector_alloc_func mAllocFunc; ///< function used to alloc new mData during resize.
+ vector_free_func mFreeFunc; ///< function used to free mData.
@@ -85,9 +90,19 @@ template <class T> class Vector {
/** Change the size of the Vector, discarding content. */
void resize(size_t newSize)
- if (mData!=NULL) delete[] mData;
+ if (mData!=NULL) {
+ if (mFreeFunc)
+ mFreeFunc(mData);
+ else
+ delete[] mData;
+ }
if (newSize==0) mData=NULL;
- else mData = new T[newSize];
+ else {
+ if (mAllocFunc)
+ mData = (T*) mAllocFunc(newSize);
+ else
+ mData = new T[newSize];
+ }
mStart = mData;
mEnd = mStart + newSize;
@@ -116,29 +131,31 @@ template <class T> class Vector {
/** Build an empty Vector of a given size. */
- Vector(size_t wSize=0):mData(NULL) { resize(wSize); }
+ Vector(size_t wSize=0, vector_alloc_func wAllocFunc=NULL, vector_free_func wFreeFunc=NULL)
+ :mData(NULL), mAllocFunc(wAllocFunc), mFreeFunc(wFreeFunc)
+ { resize(wSize); }
/** Build a Vector by moving another. */
Vector(Vector<T>&& other)
- :mData(other.mData),mStart(other.mStart),mEnd(other.mEnd)
+ :mData(other.mData),mStart(other.mStart),mEnd(other.mEnd), mAllocFunc(other.mAllocFunc), mFreeFunc(other.mFreeFunc)
{ other.mData=NULL; }
/** Build a Vector by copying another. */
- Vector(const Vector<T>& other):mData(NULL) { clone(other); }
+ Vector(const Vector<T>& other):mData(NULL), mAllocFunc(other.mAllocFunc), mFreeFunc(other.mFreeFunc) { clone(other); }
/** Build a Vector with explicit values. */
- Vector(T* wData, T* wStart, T* wEnd)
- :mData(wData),mStart(wStart),mEnd(wEnd)
+ Vector(T* wData, T* wStart, T* wEnd, vector_alloc_func wAllocFunc=NULL, vector_free_func wFreeFunc=NULL)
+ :mData(wData),mStart(wStart),mEnd(wEnd), mAllocFunc(wAllocFunc), mFreeFunc(wFreeFunc)
{ }
/** Build a vector from an existing block, NOT to be deleted upon destruction. */
- Vector(T* wStart, size_t span)
- :mData(NULL),mStart(wStart),mEnd(wStart+span)
+ Vector(T* wStart, size_t span, vector_alloc_func wAllocFunc=NULL, vector_free_func wFreeFunc=NULL)
+ :mData(NULL),mStart(wStart),mEnd(wStart+span),mAllocFunc(wAllocFunc), mFreeFunc(wFreeFunc)
{ }
/** Build a Vector by concatenation. */
- Vector(const Vector<T>& other1, const Vector<T>& other2)
- :mData(NULL)
+ Vector(const Vector<T>& other1, const Vector<T>& other2, vector_alloc_func wAllocFunc=NULL, vector_free_func wFreeFunc=NULL)
+ :mData(NULL), mAllocFunc(wAllocFunc), mFreeFunc(wFreeFunc)
memcpy(mStart, other1.mStart, other1.bytes());
@@ -162,6 +179,8 @@ template <class T> class Vector {
+ mAllocFunc=other.mAllocFunc;
+ mFreeFunc=other.mFreeFunc;