osmo-trx/Transceiver52M/signalVector.h

56 lines
1.5 KiB
C
Raw Normal View History

#ifndef _SIGNALVECTOR_H_
#define _SIGNALVECTOR_H_
#include <Vector.h>
#include <Complex.h>
/** Vector symmetry */
enum Symmetry {
NONE = 0,
ABSSYM = 1
};
class signalVector: public Vector<complex> {
public:
/** Default constructor */
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
2018-12-03 16:46:04 +00:00
signalVector(size_t size = 0, vector_alloc_func wAllocFunc = NULL, vector_free_func wFreeFunc = NULL);
/** Construct with head room */
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
2018-12-03 16:46:04 +00:00
signalVector(size_t size, size_t start, vector_alloc_func wAllocFunc = NULL, vector_free_func wFreeFunc = NULL);
/** Construct from existing buffer data (buffer not managed) */
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
2018-12-03 16:46:04 +00:00
signalVector(complex *data, size_t start, size_t span, vector_alloc_func wAllocFunc = NULL, vector_free_func wFreeFunc = NULL);
/** Construct by from existing vector */
signalVector(const signalVector &vector);
/** Construct by from existing vector and append head-tail room */
signalVector(const signalVector &vector, size_t start, size_t tail = 0);
/** Override base assignment operator to include start offsets */
void operator=(const signalVector& vector);
/** Return an alias to a segment of this signalVector. */
signalVector segment(size_t start, size_t span);
/** Return head room */
size_t getStart() const;
size_t updateHistory();
Symmetry getSymmetry() const;
void setSymmetry(Symmetry symmetry);
bool isReal() const;
void isReal(bool real);
bool isAligned() const;
void setAligned(bool aligned);
private:
bool real;
bool aligned;
Symmetry symmetry;
};
#endif /* _SIGNALVECTOR_H_ */