1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-09-28 23:10:51 +00:00

Modernize memory management

This commit is contained in:
Carles Fernandez 2019-08-23 21:59:44 +02:00
parent 95f688dcb0
commit fd8787c7a4
No known key found for this signature in database
GPG Key ID: 4C583C52B0C3877D
2 changed files with 22 additions and 39 deletions

View File

@ -31,8 +31,8 @@
#include "viterbi_decoder.h" #include "viterbi_decoder.h"
#include <glog/logging.h> #include <glog/logging.h>
#include <cstring> // for memset #include <algorithm> // for fill_n
#include <ostream> // for operator<<, basic_ostream, char_traits, endl #include <ostream> // for operator<<, basic_ostream, char_traits, endl
// logging // logging
#define EVENT 2 // logs important events which don't occur every block #define EVENT 2 // logs important events which don't occur every block
@ -434,7 +434,6 @@ int Viterbi_Decoder::nsc_enc_bit(int state_out_p[], int input, int state_in,
length: The highest bit position in the symbol length: The highest bit position in the symbol
This function is used by nsc_enc_bit(), rsc_enc_bit(), and rsc_tail() */ This function is used by nsc_enc_bit(), rsc_enc_bit(), and rsc_tail() */
int Viterbi_Decoder::parity_counter(int symbol, int length) int Viterbi_Decoder::parity_counter(int symbol, int length)
{ {
int counter; int counter;
@ -453,14 +452,13 @@ Viterbi_Decoder::Prev::Prev(int states, int t)
{ {
this->t = t; this->t = t;
num_states = states; num_states = states;
state = new int[states]; state.reserve(num_states);
bit = new int[states]; bit.reserve(num_states);
metric = new float[states]; metric.reserve(num_states);
refcount = new int; refcount = 1;
*refcount = 1; std::fill_n(state.begin(), num_states, 0);
memset(state, 0, sizeof(int) * num_states); std::fill_n(bit.begin(), num_states, 0);
memset(bit, 0, sizeof(int) * num_states); std::fill_n(metric.begin(), num_states, 0.0F);
memset(metric, 0, sizeof(float) * num_states);
} }
@ -468,7 +466,7 @@ Viterbi_Decoder::Prev::Prev(int states, int t)
Viterbi_Decoder::Prev::Prev(const Prev& prev) Viterbi_Decoder::Prev::Prev(const Prev& prev)
{ {
refcount = prev.refcount; refcount = prev.refcount;
(*refcount)++; refcount++;
t = prev.t; t = prev.t;
state = prev.state; state = prev.state;
num_states = prev.num_states; num_states = prev.num_states;
@ -477,7 +475,7 @@ Viterbi_Decoder::Prev::Prev(const Prev& prev)
VLOG(LMORE) << "Prev(" VLOG(LMORE) << "Prev("
<< "?" << "?"
<< ", " << t << ")" << ", " << t << ")"
<< " copy, new refcount = " << *refcount; << " copy, new refcount = " << refcount;
} }
@ -491,21 +489,14 @@ Viterbi_Decoder::Prev& Viterbi_Decoder::Prev::operator=(const Prev& other)
} }
// handle old resources // handle old resources
if (*refcount == 1) if (refcount != 1)
{ // if they are not used anymore -> unallocate them
delete[] state;
delete[] bit;
delete[] metric;
delete refcount;
}
else
{ // this object is not anymore using them { // this object is not anymore using them
(*refcount)--; refcount--;
} }
// increase ref counter for this resource set // increase ref counter for this resource set
refcount = other.refcount; refcount = other.refcount;
(*refcount)++; refcount++;
// take over resources // take over resources
t = other.t; t = other.t;
@ -516,28 +507,20 @@ Viterbi_Decoder::Prev& Viterbi_Decoder::Prev::operator=(const Prev& other)
VLOG(LMORE) << "Prev(" VLOG(LMORE) << "Prev("
<< "?" << "?"
<< ", " << t << ")" << ", " << t << ")"
<< " assignment, new refcount = " << *refcount; << " assignment, new refcount = " << refcount;
return *this; return *this;
} }
Viterbi_Decoder::Prev::~Prev() Viterbi_Decoder::Prev::~Prev()
{ {
if (*refcount == 1) if (refcount != 1)
{ {
delete[] state; refcount--;
delete[] bit;
delete[] metric;
delete refcount;
// std::cout << "~Prev(" << "?" << ", " << t << ")" << " destructor with delete" << std::endl;
}
else
{
(*refcount)--;
VLOG(LMORE) << "~Prev(" VLOG(LMORE) << "~Prev("
<< "?" << "?"
<< ", " << t << ")" << ", " << t << ")"
<< " destructor after copy, new refcount = " << *refcount; << " destructor after copy, new refcount = " << refcount;
} }
} }

View File

@ -79,10 +79,10 @@ private:
private: private:
int t; int t;
int* state; std::vector<int> state;
int* bit; std::vector<int> bit;
float* metric; std::vector<float> metric;
int* refcount; int refcount;
}; };
// code properties // code properties