From 01917ad0c220c9a9e1921102ad1aa3ecc21a10db Mon Sep 17 00:00:00 2001 From: jgromes Date: Fri, 7 Jul 2023 20:39:00 +0200 Subject: [PATCH] [FEC] Implemented static-only memory management --- src/utils/FEC.cpp | 51 +++++++++++++++++++++++++++++++++++++++++------ src/utils/FEC.h | 18 ++++++++++++++--- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/utils/FEC.cpp b/src/utils/FEC.cpp index 107ec1b5..e8cf260e 100644 --- a/src/utils/FEC.cpp +++ b/src/utils/FEC.cpp @@ -14,9 +14,11 @@ void RadioLibBCH::begin(uint8_t n, uint8_t k, uint32_t poly) { this->n = n; this->k = k; this->poly = poly; + #if !defined(RADIOLIB_STATIC_ONLY) this->alphaTo = new int32_t[n + 1]; this->indexOf = new int32_t[n + 1]; this->generator = new int32_t[n - k + 1]; + #endif // find the maximum power of the polynomial for(this->m = 0; this->m < 31; this->m++) { @@ -113,7 +115,11 @@ void RadioLibBCH::begin(uint8_t n, uint8_t k, uint32_t poly) { // Search for roots 1, 2, ..., m-1 in cycle sets int32_t rdncy = 0; - int32_t* min = new int32_t[this->n - this->k + 1]; + #if defined(RADIOLIB_STATIC_ONLY) + int32_t min[RADIOLIB_BCH_MAX_N - RADIOLIB_BCH_MAX_K + 1]; + #else + int32_t* min = new int32_t[this->n - this->k + 1]; + #endif kaux = 0; for(ii = 1; ii <= jj; ii++) { @@ -133,7 +139,11 @@ void RadioLibBCH::begin(uint8_t n, uint8_t k, uint32_t poly) { } int32_t noterms = kaux; - int32_t* zeros = new int32_t[this->n - this->k + 1]; + #if defined(RADIOLIB_STATIC_ONLY) + int32_t zeros[RADIOLIB_BCH_MAX_N - RADIOLIB_BCH_MAX_K + 1]; + #else + int32_t* zeros = new int32_t[this->n - this->k + 1]; + #endif kaux = 1; for(ii = 0; ii < noterms; ii++) { @@ -143,7 +153,9 @@ void RadioLibBCH::begin(uint8_t n, uint8_t k, uint32_t poly) { } } + #if !defined(RADIOLIB_STATIC_ONLY) delete[] min; + #endif // Compute generator polynomial this->generator[0] = this->alphaTo[zeros[1]]; @@ -161,7 +173,9 @@ void RadioLibBCH::begin(uint8_t n, uint8_t k, uint32_t poly) { this->generator[0] = this->alphaTo[(this->indexOf[this->generator[0]] + zeros[ii]) % this->n]; } + #if !defined(RADIOLIB_STATIC_ONLY) delete[] zeros; + #endif } /* @@ -171,7 +185,11 @@ void RadioLibBCH::begin(uint8_t n, uint8_t k, uint32_t poly) { */ uint32_t RadioLibBCH::encode(uint32_t dataword) { // we only use the "k" most significant bits - int32_t* data = new int32_t[this->k]; + #if defined(RADIOLIB_STATIC_ONLY) + int32_t data[RADIOLIB_BCH_MAX_K]; + #else + int32_t* data = new int32_t[this->k]; + #endif int32_t j1 = 0; for(int32_t i = this->n; i > (this->n - this->k); i--) { if(dataword & ((uint32_t)1<n]; + #if defined(RADIOLIB_STATIC_ONLY) + int32_t Mr[RADIOLIB_BCH_MAX_N]; + #else + int32_t* Mr = new int32_t[this->n]; + #endif memset(Mr, 0x00, this->n*sizeof(int32_t)); // copy the contents of data into Mr and add the zeros @@ -205,16 +227,27 @@ uint32_t RadioLibBCH::encode(uint32_t dataword) { } } - int32_t* bb = new int32_t[this->n - this->k + 1]; + #if defined(RADIOLIB_STATIC_ONLY) + int32_t bb[RADIOLIB_BCH_MAX_N - RADIOLIB_BCH_MAX_K + 1]; + #else + int32_t* bb = new int32_t[this->n - this->k + 1]; + #endif j = 0; for(int32_t i = start; i < end; ++i) { bb[j] = Mr[i]; ++j; } + + #if !defined(RADIOLIB_STATIC_ONLY) delete[] Mr; + #endif int32_t iEvenParity = 0; - int32_t* recd = new int32_t[this->n + 1]; + #if defined(RADIOLIB_STATIC_ONLY) + int32_t recd[RADIOLIB_BCH_MAX_N + 1]; + #else + int32_t* recd = new int32_t[this->n + 1]; + #endif for(uint8_t i = 0; i < this->k; i++) { recd[this->n - i] = data[i]; if(data[i] == 1) { @@ -222,7 +255,9 @@ uint32_t RadioLibBCH::encode(uint32_t dataword) { } } + #if !defined(RADIOLIB_STATIC_ONLY) delete[] data; + #endif for(uint8_t i = 0; i < this->n - this->k + 1; i++) { recd[this->n - this->k - i] = bb[i]; @@ -231,7 +266,9 @@ uint32_t RadioLibBCH::encode(uint32_t dataword) { } } + #if !defined(RADIOLIB_STATIC_ONLY) delete[] bb; + #endif if((iEvenParity % 2) == 0) { recd[0] = 0; @@ -246,7 +283,9 @@ uint32_t RadioLibBCH::encode(uint32_t dataword) { } } + #if !defined(RADIOLIB_STATIC_ONLY) delete[] recd; + #endif return(res); } diff --git a/src/utils/FEC.h b/src/utils/FEC.h index acdb4571..76f2bcad 100644 --- a/src/utils/FEC.h +++ b/src/utils/FEC.h @@ -12,6 +12,11 @@ #define RADIOLIB_PAGER_BCH_K (21) #define RADIOLIB_PAGER_BCH_PRIMITIVE_POLY (0x25) +#if defined(RADIOLIB_STATIC_ONLY) +#define RADIOLIB_BCH_MAX_N (63) +#define RADIOLIB_BCH_MAX_K (31) +#endif + /*! \class RadioLibBCH \brief Class to calculate Bose–Chaudhuri–Hocquenghem (BCH) class of forward error correction codes. @@ -46,9 +51,16 @@ class RadioLibBCH { uint8_t k; uint32_t poly; uint8_t m; - int32_t* alphaTo; - int32_t* indexOf; - int32_t* generator; + + #if defined(RADIOLIB_STATIC_ONLY) + int32_t alphaTo[RADIOLIB_BCH_MAX_N + 1]; + int32_t indexOf[RADIOLIB_BCH_MAX_N + 1]; + int32_t generator[RADIOLIB_BCH_MAX_N - RADIOLIB_BCH_MAX_K + 1]; + #else + int32_t* alphaTo; + int32_t* indexOf; + int32_t* generator; + #endif }; // the global singleton