From 456fc7a886dee738047beb50d4c34d2d2527775d Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Tue, 7 Jun 2022 23:24:58 +0300 Subject: [PATCH] remove-stl : almost complete - only remains --- include/ggwave/ggwave.h | 33 +++++------ src/ggwave.cpp | 121 ++++++++++++++++------------------------ 2 files changed, 65 insertions(+), 89 deletions(-) diff --git a/include/ggwave/ggwave.h b/include/ggwave/ggwave.h index 60d2898..84dbc56 100644 --- a/include/ggwave/ggwave.h +++ b/include/ggwave/ggwave.h @@ -339,26 +339,27 @@ private: public: using value_type = T; - ggvector(); - ggvector(T * data, int size); + ggvector() : m_data(nullptr), m_size(0) {} + ggvector(T * data, int size) : m_data(data), m_size(size) {} + ggvector(const ggvector & other) = default; // delete operator= ggvector & operator=(const ggvector &) = delete; ggvector & operator=(ggvector &&) = delete; - T & operator[](int i); - const T & operator[](int i) const; + T & operator[](int i) { return m_data[i]; } + const T & operator[](int i) const { return m_data[i]; } + + int size() const { return m_size; } + T * data() const { return m_data; } + + T * begin() const { return m_data; } + T * end() const { return m_data + m_size; } void assign(const ggvector & other); void copy(const ggvector & other); - int size() const; - T * data() const; - - T * begin() const; - T * end() const; - void zero(); void zero(int n); }; @@ -373,14 +374,14 @@ private: public: using value_type = T; - ggmatrix(); - ggmatrix(T * data, int size0, int size1); + ggmatrix() : m_data(nullptr), m_size0(0), m_size1(0) {} + ggmatrix(T * data, int size0, int size1) : m_data(data), m_size0(size0), m_size1(size1) {} - ggvector operator[](int i); + ggvector operator[](int i) { + return ggvector(m_data + i*m_size1, m_size1); + } - T & operator()(int i, int j); - - int size() const; + int size() const { return m_size0; } void zero(); }; diff --git a/src/ggwave.cpp b/src/ggwave.cpp index 86b0f8b..02582b3 100644 --- a/src/ggwave.cpp +++ b/src/ggwave.cpp @@ -9,9 +9,8 @@ #include "fft.h" #include "reed-solomon/rs.hpp" -#include -#include -#include +#include +#include //#include #ifndef M_PI @@ -285,22 +284,6 @@ int bytesForSampleFormat(GGWave::SampleFormat sampleFormat) { // ggvector // -template -ggvector::ggvector() : m_data(nullptr), m_size(0) {} - -template -ggvector::ggvector(T * data, int size) : m_data(data), m_size(size) {} - -template -T & ggvector::operator[](int i) { - return m_data[i]; -} - -template -const T & ggvector::operator[](int i) const { - return m_data[i]; -} - template void ggvector::assign(const ggvector & other) { m_data = other.m_data; @@ -316,26 +299,6 @@ void ggvector::copy(const ggvector & other) { memcpy(m_data, other.m_data, GG_MIN(m_size, other.m_size)*sizeof(T)); } -template -int ggvector::size() const { - return m_size; -} - -template -T * ggvector::data() const { - return m_data; -} - -template -T * ggvector::begin() const { - return m_data; -} - -template -T * ggvector::end() const { - return m_data + m_size; -} - template void ggvector::zero() { memset(m_data, 0, m_size*sizeof(T)); @@ -346,37 +309,12 @@ void ggvector::zero(int n) { memset(m_data, 0, n*sizeof(T)); } -template struct ggvector; -template struct ggvector; -template struct ggvector; template struct ggvector; // // ggmatrix // - -template -ggmatrix::ggmatrix() : m_data(nullptr), m_size0(0), m_size1(0) {} - -template -ggmatrix::ggmatrix(T * data, int size0, int size1) : m_data(data), m_size0(size0), m_size1(size1) {} - -template -ggvector ggmatrix::operator[](int i) { - return ggvector(m_data + i*m_size1, m_size1); -} - -template -T & ggmatrix::operator()(int i, int j) { - return m_data[i*m_size1 + j]; -} - -template -int ggmatrix::size() const { - return m_size0; -} - template void ggmatrix::zero() { memset(m_data, 0, m_size0*m_size1*sizeof(T)); @@ -436,30 +374,66 @@ GGWave::RxProtocols & GGWave::Protocols::rx() { return protocols; } +// this probably does not matter, but adding it anyway +#ifdef ARDUINO +const int kAlignment = 1; +#else +const int kAlignment = 8; +#endif + +//template +//void ggalloc(std::vector & v, int n, void * buf, int & bufSize) { +// if (buf == nullptr) { +// bufSize += n*sizeof(T); +// bufSize = ((bufSize + kAlignment - 1)/kAlignment)*kAlignment; +// return; +// } +// +// v.resize(n); +// bufSize += n*sizeof(T); +// bufSize = ((bufSize + kAlignment - 1)/kAlignment)*kAlignment; +//} +// +//template +//void ggalloc(std::vector> & v, int n, int m, void * buf, int & bufSize) { +// if (buf == nullptr) { +// bufSize += n*m*sizeof(T); +// bufSize = ((bufSize + kAlignment - 1)/kAlignment)*kAlignment; +// return; +// } +// +// v.resize(n); +// for (int i = 0; i < n; i++) { +// v[i].resize(m); +// } +// bufSize += n*m*sizeof(T); +// bufSize = ((bufSize + kAlignment - 1)/kAlignment)*kAlignment; +//} + template void ggalloc(ggvector & v, int n, void * buf, int & bufSize) { if (buf == nullptr) { bufSize += n*sizeof(T); - bufSize = ((bufSize + 7)/8)*8; + bufSize = ((bufSize + kAlignment - 1)/kAlignment)*kAlignment; return; } v.assign(ggvector((T *)((char *) buf + bufSize), n)); bufSize += n*sizeof(T); - bufSize = ((bufSize + 7)/8)*8; + bufSize = ((bufSize + kAlignment - 1)/kAlignment)*kAlignment; } template void ggalloc(ggmatrix & v, int n, int m, void * buf, int & bufSize) { if (buf == nullptr) { bufSize += n*m*sizeof(T); - bufSize = ((bufSize + 7)/8)*8; + bufSize = ((bufSize + kAlignment - 1) / kAlignment)*kAlignment; return; } v = ggmatrix((T *)((char *) buf + bufSize), n, m); bufSize += n*m*sizeof(T); - bufSize = ((bufSize + 7)/8)*8; + bufSize = ((bufSize + kAlignment - 1)/kAlignment)*kAlignment; } // @@ -538,7 +512,10 @@ bool GGWave::prepare(const Parameters & parameters) { } const auto heapSize0 = m_heapSize; - m_heap = malloc(m_heapSize); + + // not sure if allocating alligned memory makes any difference + //m_heap = malloc(m_heapSize); + m_heap = aligned_alloc(kAlignment, m_heapSize); m_heapSize = 0; if (this->alloc(m_heap, m_heapSize) == false) { @@ -1720,8 +1697,7 @@ void GGWave::decode_variable() { } if (isReceiving) { - std::time_t timestamp = std::time(nullptr); - ggprintf("%sReceiving sound data ...\n", std::asctime(std::localtime(×tamp))); + ggprintf("Receiving sound data ...\n"); m_rx.receiving = true; m_rx.data.zero(); @@ -1775,9 +1751,8 @@ void GGWave::decode_variable() { } if (isEnded && m_rx.framesToRecord > 1) { - std::time_t timestamp = std::time(nullptr); m_rx.recvDuration_frames -= m_rx.framesLeftToRecord - 1; - ggprintf("%sReceived end marker. Frames left = %d, recorded = %d\n", std::asctime(std::localtime(×tamp)), m_rx.framesLeftToRecord, m_rx.recvDuration_frames); + ggprintf("Received end marker. Frames left = %d, recorded = %d\n", m_rx.framesLeftToRecord, m_rx.recvDuration_frames); m_rx.nMarkersSuccess = 0; m_rx.framesLeftToRecord = 1; }