From acfb479770c7f6f43576ecf363df5c3d24467189 Mon Sep 17 00:00:00 2001 From: Tom Early Date: Wed, 29 Jul 2020 12:06:43 -0700 Subject: [PATCH] everything's a smart pointer --- src/cysfconvolution.cpp | 29 +++------------- src/cysfconvolution.h | 8 ++--- src/cysffich.cpp | 19 ++++------- src/cysffich.h | 3 +- src/cysfpayload.cpp | 75 +++++++++++++++-------------------------- src/cysfpayload.h | 13 ++++--- 6 files changed, 48 insertions(+), 99 deletions(-) diff --git a/src/cysfconvolution.cpp b/src/cysfconvolution.cpp index ff41276..5d14764 100644 --- a/src/cysfconvolution.cpp +++ b/src/cysfconvolution.cpp @@ -1,5 +1,6 @@ /* * Copyright (C) 2009-2016 by Jonathan Naylor G4KLX + * Copyright (C) 2030 Thomas A. Early N7TAE * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,34 +36,12 @@ const unsigned int NUM_OF_STATES = 16U; const uint32_t M = 2U; const unsigned int K = 5U; -CYSFConvolution::CYSFConvolution() : - m_metrics1(nullptr), - m_metrics2(nullptr), - m_oldMetrics(nullptr), - m_newMetrics(nullptr), - m_decisions(nullptr), - m_dp(nullptr) -{ - m_metrics1 = new uint16_t[16U]; - m_metrics2 = new uint16_t[16U]; - m_decisions = new uint64_t[180U]; -} - -CYSFConvolution::~CYSFConvolution() -{ - delete[] m_metrics1; - delete[] m_metrics2; - delete[] m_decisions; -} +CYSFConvolution::CYSFConvolution() : m_oldMetrics(m_metrics1), m_newMetrics(m_metrics2), m_dp(m_decisions) {} void CYSFConvolution::start() { - ::memset(m_metrics1, 0x00U, NUM_OF_STATES * sizeof(uint16_t)); - ::memset(m_metrics2, 0x00U, NUM_OF_STATES * sizeof(uint16_t)); - - m_oldMetrics = m_metrics1; - m_newMetrics = m_metrics2; - m_dp = m_decisions; + for (int i=0; i<16; i++) + m_metrics1[i] = m_metrics2[i] = 0U; } void CYSFConvolution::decode(uint8_t s0, uint8_t s1) diff --git a/src/cysfconvolution.h b/src/cysfconvolution.h index 082eeda..db596ad 100644 --- a/src/cysfconvolution.h +++ b/src/cysfconvolution.h @@ -27,7 +27,6 @@ class CYSFConvolution { public: CYSFConvolution(); - ~CYSFConvolution(); void start(); void decode(uint8_t s0, uint8_t s1); @@ -36,13 +35,12 @@ public: void encode(const unsigned char* in, unsigned char* out, unsigned int nBits) const; private: - uint16_t* m_metrics1; - uint16_t* m_metrics2; + uint16_t m_metrics1[16]; + uint16_t m_metrics2[16]; uint16_t* m_oldMetrics; uint16_t* m_newMetrics; - uint64_t* m_decisions; + uint64_t m_decisions[180]; uint64_t* m_dp; }; #endif - diff --git a/src/cysffich.cpp b/src/cysffich.cpp index 68378dd..626003f 100644 --- a/src/cysffich.cpp +++ b/src/cysffich.cpp @@ -35,11 +35,11 @@ const unsigned char BIT_MASK_TABLE[] = {0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U const unsigned int INTERLEAVE_TABLE[] = { - 0U, 40U, 80U, 120U, 160U, - 2U, 42U, 82U, 122U, 162U, - 4U, 44U, 84U, 124U, 164U, - 6U, 46U, 86U, 126U, 166U, - 8U, 48U, 88U, 128U, 168U, + 0U, 40U, 80U, 120U, 160U, + 2U, 42U, 82U, 122U, 162U, + 4U, 44U, 84U, 124U, 164U, + 6U, 46U, 86U, 126U, 166U, + 8U, 48U, 88U, 128U, 168U, 10U, 50U, 90U, 130U, 170U, 12U, 52U, 92U, 132U, 172U, 14U, 54U, 94U, 134U, 174U, @@ -57,18 +57,11 @@ const unsigned int INTERLEAVE_TABLE[] = 38U, 78U, 118U, 158U, 198U }; -CYSFFICH::CYSFFICH() : - m_fich(nullptr) +CYSFFICH::CYSFFICH() { - m_fich = new unsigned char[6U]; ::memset(m_fich, 0U, 6U); } -CYSFFICH::~CYSFFICH() -{ - delete[] m_fich; -} - bool CYSFFICH::decode(const unsigned char* bytes) { assert(bytes != nullptr); diff --git a/src/cysffich.h b/src/cysffich.h index 41f8b44..52d2d01 100644 --- a/src/cysffich.h +++ b/src/cysffich.h @@ -24,7 +24,6 @@ class CYSFFICH { public: CYSFFICH(); - ~CYSFFICH(); bool decode(const unsigned char* bytes); @@ -60,7 +59,7 @@ public: void load(const unsigned char* fich); private: - unsigned char* m_fich; + unsigned char m_fich[6]; }; #endif diff --git a/src/cysfpayload.cpp b/src/cysfpayload.cpp index 28e818a..208ec36 100644 --- a/src/cysfpayload.cpp +++ b/src/cysfpayload.cpp @@ -26,11 +26,11 @@ const unsigned int INTERLEAVE_TABLE_9_20[] = { - 0U, 40U, 80U, 120U, 160U, 200U, 240U, 280U, 320U, - 2U, 42U, 82U, 122U, 162U, 202U, 242U, 282U, 322U, - 4U, 44U, 84U, 124U, 164U, 204U, 244U, 284U, 324U, - 6U, 46U, 86U, 126U, 166U, 206U, 246U, 286U, 326U, - 8U, 48U, 88U, 128U, 168U, 208U, 248U, 288U, 328U, + 0U, 40U, 80U, 120U, 160U, 200U, 240U, 280U, 320U, + 2U, 42U, 82U, 122U, 162U, 202U, 242U, 282U, 322U, + 4U, 44U, 84U, 124U, 164U, 204U, 244U, 284U, 324U, + 6U, 46U, 86U, 126U, 166U, 206U, 246U, 286U, 326U, + 8U, 48U, 88U, 128U, 168U, 208U, 248U, 288U, 328U, 10U, 50U, 90U, 130U, 170U, 210U, 250U, 290U, 330U, 12U, 52U, 92U, 132U, 172U, 212U, 252U, 292U, 332U, 14U, 54U, 94U, 134U, 174U, 214U, 254U, 294U, 334U, @@ -50,11 +50,11 @@ const unsigned int INTERLEAVE_TABLE_9_20[] = const unsigned int INTERLEAVE_TABLE_5_20[] = { - 0U, 40U, 80U, 120U, 160U, - 2U, 42U, 82U, 122U, 162U, - 4U, 44U, 84U, 124U, 164U, - 6U, 46U, 86U, 126U, 166U, - 8U, 48U, 88U, 128U, 168U, + 0U, 40U, 80U, 120U, 160U, + 2U, 42U, 82U, 122U, 162U, + 4U, 44U, 84U, 124U, 164U, + 6U, 46U, 86U, 126U, 166U, + 8U, 48U, 88U, 128U, 168U, 10U, 50U, 90U, 130U, 170U, 12U, 52U, 92U, 132U, 172U, 14U, 54U, 94U, 134U, 174U, @@ -73,31 +73,15 @@ const unsigned int INTERLEAVE_TABLE_5_20[] = }; // This one differs from the others in that it interleaves bits and not dibits -const unsigned char WHITENING_DATA[] = {0x93U, 0xD7U, 0x51U, 0x21U, 0x9CU, 0x2FU, 0x6CU, 0xD0U, 0xEFU, 0x0FU, - 0xF8U, 0x3DU, 0xF1U, 0x73U, 0x20U, 0x94U, 0xEDU, 0x1EU, 0x7CU, 0xD8U - }; +const unsigned char WHITENING_DATA[] = { + 0x93U, 0xD7U, 0x51U, 0x21U, 0x9CU, 0x2FU, 0x6CU, 0xD0U, 0xEFU, 0x0FU, 0xF8U, 0x3DU, 0xF1U, 0x73U, 0x20U, 0x94U, 0xEDU, 0x1EU, 0x7CU, 0xD8U +}; -const unsigned char BIT_MASK_TABLE[] = {0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U}; +const unsigned char BIT_MASK_TABLE[] = { 0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U }; #define WRITE_BIT1(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE[(i)&7]) : (p[(i)>>3] & ~BIT_MASK_TABLE[(i)&7]) #define READ_BIT1(p,i) (p[(i)>>3] & BIT_MASK_TABLE[(i)&7]) -CYSFPayload::CYSFPayload() : - m_uplink(nullptr), - m_downlink(nullptr), - m_source(nullptr), - m_dest(nullptr) -{ -} - -CYSFPayload::~CYSFPayload() -{ - delete[] m_uplink; - delete[] m_downlink; - delete[] m_source; - delete[] m_dest; -} - bool CYSFPayload::processHeaderData(unsigned char* data) { assert(data != nullptr); @@ -140,14 +124,14 @@ bool CYSFPayload::processHeaderData(unsigned char* data) if (m_dest == nullptr) { - m_dest = new unsigned char[YSF_CALLSIGN_LENGTH]; - ::memcpy(m_dest, output + 0U, YSF_CALLSIGN_LENGTH); + m_dest = std::unique_ptr(new unsigned char[YSF_CALLSIGN_LENGTH]); + ::memcpy(m_dest.get(), output, YSF_CALLSIGN_LENGTH); } if (m_source == nullptr) { - m_source = new unsigned char[YSF_CALLSIGN_LENGTH]; - ::memcpy(m_source, output + YSF_CALLSIGN_LENGTH, YSF_CALLSIGN_LENGTH); + m_source = std::unique_ptr(new unsigned char[YSF_CALLSIGN_LENGTH]); + ::memcpy(m_source.get(), output + YSF_CALLSIGN_LENGTH, YSF_CALLSIGN_LENGTH); } for (unsigned int i = 0U; i < 20U; i++) @@ -217,11 +201,11 @@ bool CYSFPayload::processHeaderData(unsigned char* data) for (unsigned int i = 0U; i < 20U; i++) output[i] ^= WHITENING_DATA[i]; - if (m_downlink != nullptr) - ::memcpy(output + 0U, m_downlink, YSF_CALLSIGN_LENGTH); + if (m_downlink) + ::memcpy(output + 0U, m_downlink.get(), YSF_CALLSIGN_LENGTH); - if (m_uplink != nullptr) - ::memcpy(output + YSF_CALLSIGN_LENGTH, m_uplink, YSF_CALLSIGN_LENGTH); + if (m_uplink) + ::memcpy(output + YSF_CALLSIGN_LENGTH, m_uplink.get(), YSF_CALLSIGN_LENGTH); for (unsigned int i = 0U; i < 20U; i++) output[i] ^= WHITENING_DATA[i]; @@ -618,7 +602,7 @@ void CYSFPayload::writeDataFRModeData2(const unsigned char* dt, unsigned char* d void CYSFPayload::setUplink(const std::string& callsign) { - m_uplink = new unsigned char[YSF_CALLSIGN_LENGTH]; + m_uplink = std::unique_ptr(new unsigned char[YSF_CALLSIGN_LENGTH]); std::string uplink = callsign; uplink.resize(YSF_CALLSIGN_LENGTH, ' '); @@ -629,7 +613,7 @@ void CYSFPayload::setUplink(const std::string& callsign) void CYSFPayload::setDownlink(const std::string& callsign) { - m_downlink = new unsigned char[YSF_CALLSIGN_LENGTH]; + m_downlink = std::unique_ptr(new unsigned char[YSF_CALLSIGN_LENGTH]); std::string downlink = callsign; downlink.resize(YSF_CALLSIGN_LENGTH, ' '); @@ -643,7 +627,7 @@ std::string CYSFPayload::getSource() std::string tmp; if (m_dest) - tmp.assign((const char *)m_source, YSF_CALLSIGN_LENGTH); + tmp.assign((const char *)m_source.get(), YSF_CALLSIGN_LENGTH); else tmp = ""; @@ -655,7 +639,7 @@ std::string CYSFPayload::getDest() std::string tmp; if (m_dest) - tmp.assign((const char *)m_dest, YSF_CALLSIGN_LENGTH); + tmp.assign((const char *)m_dest.get(), YSF_CALLSIGN_LENGTH); else tmp = ""; @@ -664,9 +648,6 @@ std::string CYSFPayload::getDest() void CYSFPayload::reset() { - delete[] m_source; - delete[] m_dest; - - m_source = nullptr; - m_dest = nullptr; + m_source.reset(); + m_dest.reset(); } diff --git a/src/cysfpayload.h b/src/cysfpayload.h index 1290381..3e45e6d 100644 --- a/src/cysfpayload.h +++ b/src/cysfpayload.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2016,2017 by Jonathan Naylor G4KLX + * Copyright (C) 2020 Thomas A. Early * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,13 +21,11 @@ #define YSFPayload_H #include +#include class CYSFPayload { public: - CYSFPayload(); - ~CYSFPayload(); - bool processHeaderData(unsigned char* bytes); void writeVDMode2Data(unsigned char* data, const unsigned char* dt); @@ -49,10 +48,10 @@ public: void reset(); private: - unsigned char* m_uplink; - unsigned char* m_downlink; - unsigned char* m_source; - unsigned char* m_dest; + std::unique_ptr m_uplink; + std::unique_ptr m_downlink; + std::unique_ptr m_source; + std::unique_ptr m_dest; }; #endif