[FEC] Implemented static-only memory management

This commit is contained in:
jgromes 2023-07-07 20:39:00 +02:00
parent ccbec2c7bd
commit 01917ad0c2
2 changed files with 60 additions and 9 deletions

View file

@ -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<<i)) {
@ -182,7 +200,11 @@ uint32_t RadioLibBCH::encode(uint32_t dataword) {
}
// reset the M(x)+r array elements
int32_t* Mr = new int32_t[this->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);
}

View file

@ -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 BoseChaudhuriHocquenghem (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