Remove most usages of "new" and "malloc" in the client

This commit is contained in:
Christian Flach 2020-02-11 23:29:32 +01:00 committed by Johannes Pohl
parent 689f550049
commit 8c8226f2dc
9 changed files with 24 additions and 31 deletions

View file

@ -84,7 +84,7 @@ void ClientConnection::start()
SLOG(NOTICE) << "Connected to " << socket_.remote_endpoint().address().to_string() << endl; SLOG(NOTICE) << "Connected to " << socket_.remote_endpoint().address().to_string() << endl;
active_ = true; active_ = true;
sumTimeout_ = chronos::msec(0); sumTimeout_ = chronos::msec(0);
readerThread_ = new thread(&ClientConnection::reader, this); readerThread_ = make_unique<thread>(&ClientConnection::reader, this);
} }
@ -104,7 +104,6 @@ void ClientConnection::stop()
{ {
LOG(DEBUG) << "joining readerThread\n"; LOG(DEBUG) << "joining readerThread\n";
readerThread_->join(); readerThread_->join();
delete readerThread_;
} }
} }
catch (...) catch (...)

View file

@ -156,7 +156,7 @@ protected:
uint16_t reqId_; uint16_t reqId_;
std::string host_; std::string host_;
size_t port_; size_t port_;
std::thread* readerThread_; std::unique_ptr<std::thread> readerThread_;
chronos::msec sumTimeout_; chronos::msec sumTimeout_;
}; };

View file

@ -39,9 +39,9 @@
using namespace std; using namespace std;
Controller::Controller(const ClientSettings& settings, std::shared_ptr<MetadataAdapter> meta) Controller::Controller(const ClientSettings& settings, std::unique_ptr<MetadataAdapter> meta)
: MessageReceiver(), settings_(settings), active_(false), stream_(nullptr), decoder_(nullptr), player_(nullptr), meta_(meta), serverSettings_(nullptr), : MessageReceiver(), settings_(settings), active_(false), stream_(nullptr), decoder_(nullptr), player_(nullptr), meta_(std::move(meta)),
async_exception_(nullptr) serverSettings_(nullptr), async_exception_(nullptr)
{ {
} }
@ -60,18 +60,16 @@ void Controller::onMessageReceived(ClientConnection* /*connection*/, const msg::
{ {
if (stream_ && decoder_) if (stream_ && decoder_)
{ {
auto* pcmChunk = new msg::PcmChunk(sampleFormat_, 0); auto pcmChunk = make_unique<msg::PcmChunk>(sampleFormat_, 0);
pcmChunk->deserialize(baseMessage, buffer); pcmChunk->deserialize(baseMessage, buffer);
// LOG(DEBUG) << "chunk: " << pcmChunk->payloadSize << ", sampleFormat: " << sampleFormat_.rate << "\n"; // LOG(DEBUG) << "chunk: " << pcmChunk->payloadSize << ", sampleFormat: " << sampleFormat_.rate << "\n";
if (decoder_->decode(pcmChunk)) if (decoder_->decode(pcmChunk.get()))
{ {
// TODO: do decoding in thread? // TODO: do decoding in thread?
stream_->addChunk(pcmChunk); stream_->addChunk(move(pcmChunk));
// LOG(DEBUG) << ", decoded: " << pcmChunk->payloadSize << ", Duration: " << pcmChunk->getDuration() << ", sec: " << pcmChunk->timestamp.sec << // LOG(DEBUG) << ", decoded: " << pcmChunk->payloadSize << ", Duration: " << pcmChunk->getDuration() << ", sec: " << pcmChunk->timestamp.sec <<
// ", usec: " << pcmChunk->timestamp.usec/1000 << ", type: " << pcmChunk->type << "\n"; // ", usec: " << pcmChunk->timestamp.usec/1000 << ", type: " << pcmChunk->type << "\n";
} }
else
delete pcmChunk;
} }
} }
else if (baseMessage.type == message_type::kTime) else if (baseMessage.type == message_type::kTime)
@ -183,14 +181,14 @@ bool Controller::sendTimeSyncMessage(long after)
void Controller::start() void Controller::start()
{ {
clientConnection_.reset(new ClientConnection(this, settings_.server.host, settings_.server.port)); clientConnection_ = make_unique<ClientConnection>(this, settings_.server.host, settings_.server.port);
controllerThread_ = thread(&Controller::worker, this); controllerThread_ = thread(&Controller::worker, this);
} }
void Controller::run() void Controller::run()
{ {
clientConnection_.reset(new ClientConnection(this, settings_.server.host, settings_.server.port)); clientConnection_ = make_unique<ClientConnection>(this, settings_.server.host, settings_.server.port);
worker(); worker();
// controllerThread_ = thread(&Controller::worker, this); // controllerThread_ = thread(&Controller::worker, this);
} }

View file

@ -53,7 +53,7 @@
class Controller : public MessageReceiver class Controller : public MessageReceiver
{ {
public: public:
Controller(const ClientSettings& settings, std::shared_ptr<MetadataAdapter> meta); Controller(const ClientSettings& settings, std::unique_ptr<MetadataAdapter> meta);
void start(); void start();
void run(); void run();
void stop(); void stop();
@ -78,7 +78,7 @@ private:
std::shared_ptr<Stream> stream_; std::shared_ptr<Stream> stream_;
std::unique_ptr<decoder::Decoder> decoder_; std::unique_ptr<decoder::Decoder> decoder_;
std::unique_ptr<Player> player_; std::unique_ptr<Player> player_;
std::shared_ptr<MetadataAdapter> meta_; std::unique_ptr<MetadataAdapter> meta_;
std::unique_ptr<msg::ServerSettings> serverSettings_; std::unique_ptr<msg::ServerSettings> serverSettings_;
std::unique_ptr<msg::CodecHeader> headerChunk_; std::unique_ptr<msg::CodecHeader> headerChunk_;
std::mutex receiveMutex_; std::mutex receiveMutex_;

View file

@ -214,7 +214,7 @@ void metadata_callback(const FLAC__StreamDecoder* /*decoder*/, const FLAC__Strea
void error_callback(const FLAC__StreamDecoder* /*decoder*/, FLAC__StreamDecoderErrorStatus status, void* client_data) void error_callback(const FLAC__StreamDecoder* /*decoder*/, FLAC__StreamDecoderErrorStatus status, void* client_data)
{ {
SLOG(ERROR) << "Got error callback: " << FLAC__StreamDecoderErrorStatusString[status] << "\n"; SLOG(ERROR) << "Got error callback: " << FLAC__StreamDecoderErrorStatusString[status] << "\n";
static_cast<FlacDecoder*>(client_data)->lastError_ = std::unique_ptr<FLAC__StreamDecoderErrorStatus>(new FLAC__StreamDecoderErrorStatus(status)); static_cast<FlacDecoder*>(client_data)->lastError_ = std::make_unique<FLAC__StreamDecoderErrorStatus>(status);
} }
} // namespace callback } // namespace callback

View file

@ -65,13 +65,13 @@ std::vector<PcmDevice> CoreAudioPlayer::pcm_list(void)
if (AudioObjectGetPropertyDataSize(devids[i], &theAddress, 0, NULL, &propSize)) if (AudioObjectGetPropertyDataSize(devids[i], &theAddress, 0, NULL, &propSize))
continue; continue;
AudioBufferList* buflist = (AudioBufferList*)malloc(propSize); AudioBufferList* buflist = new AudioBufferList[propSize];
if (AudioObjectGetPropertyData(devids[i], &theAddress, 0, NULL, &propSize, buflist)) if (AudioObjectGetPropertyData(devids[i], &theAddress, 0, NULL, &propSize, buflist))
continue; continue;
int channels = 0; int channels = 0;
for (UInt32 i = 0; i < buflist->mNumberBuffers; ++i) for (UInt32 i = 0; i < buflist->mNumberBuffers; ++i)
channels += buflist->mBuffers[i].mNumberChannels; channels += buflist->mBuffers[i].mNumberChannels;
free(buflist); delete[] buflist;
if (channels == 0) if (channels == 0)
continue; continue;

View file

@ -189,7 +189,7 @@ int main(int argc, char** argv)
if (user_group.size() > 1) if (user_group.size() > 1)
group = user_group[1]; group = user_group[1];
} }
daemon.reset(new Daemon(user, group, pidFile)); daemon = std::make_unique<Daemon>(user, group, pidFile);
daemon->daemonize(); daemon->daemonize();
if (processPriority < -20) if (processPriority < -20)
processPriority = -20; processPriority = -20;
@ -255,12 +255,9 @@ int main(int argc, char** argv)
if (active) if (active)
{ {
// Setup metadata handling // Setup metadata handling
std::shared_ptr<MetadataAdapter> meta; auto meta(metaStderr ? std::make_unique<MetaStderrAdapter>() : std::make_unique<MetadataAdapter>());
meta.reset(new MetadataAdapter);
if (metaStderr)
meta.reset(new MetaStderrAdapter);
controller = make_shared<Controller>(settings, meta); controller = make_shared<Controller>(settings, std::move(meta));
LOG(INFO) << "Latency: " << settings.player.latency << "\n"; LOG(INFO) << "Latency: " << settings.player.latency << "\n";
controller->run(); controller->run();
// signal_handler.wait(); // signal_handler.wait();

View file

@ -84,12 +84,12 @@ void Stream::clearChunks()
} }
void Stream::addChunk(msg::PcmChunk* chunk) void Stream::addChunk(unique_ptr<msg::PcmChunk> chunk)
{ {
while (chunks_.size() * chunk->duration<cs::msec>().count() > 10000) while (chunks_.size() * chunk->duration<cs::msec>().count() > 10000)
chunks_.pop(); chunks_.pop();
chunks_.push(shared_ptr<msg::PcmChunk>(chunk)); chunks_.push(move(chunk));
// LOG(DEBUG) << "new chunk: " << chunk->duration<cs::msec>().count() << ", Chunks: " << chunks_.size() << "\n"; // LOG(DEBUG) << "new chunk: " << chunk->duration<cs::msec>().count() << ", Chunks: " << chunks_.size() << "\n";
// if (std::abs(input_rate_ - output_rate_) <= 0.0000001) // if (std::abs(input_rate_ - output_rate_) <= 0.0000001)
@ -188,11 +188,10 @@ cs::time_point_clk Stream::getNextPlayerChunk(void* outputBuffer, const cs::usec
throw 0; throw 0;
cs::time_point_clk tp = chunk_->start(); cs::time_point_clk tp = chunk_->start();
char* buffer = (char*)outputBuffer;
unsigned long read = 0; unsigned long read = 0;
while (read < framesPerBuffer) while (read < framesPerBuffer)
{ {
read += chunk_->readFrames(buffer + read * format_.frameSize, framesPerBuffer - read); read += chunk_->readFrames(static_cast<char*>(outputBuffer) + read * format_.frameSize, framesPerBuffer - read);
if (chunk_->isEndOfChunk() && !chunks_.try_pop(chunk_, timeout)) if (chunk_->isEndOfChunk() && !chunks_.try_pop(chunk_, timeout))
throw 0; throw 0;
} }
@ -206,7 +205,7 @@ cs::time_point_clk Stream::getNextPlayerChunk(void* outputBuffer, const cs::usec
return getNextPlayerChunk(outputBuffer, timeout, framesPerBuffer); return getNextPlayerChunk(outputBuffer, timeout, framesPerBuffer);
long toRead = framesPerBuffer + framesCorrection; long toRead = framesPerBuffer + framesCorrection;
char* buffer = (char*)malloc(toRead * format_.frameSize); char* buffer = new char[toRead * format_.frameSize];
cs::time_point_clk tp = getNextPlayerChunk(buffer, timeout, toRead); cs::time_point_clk tp = getNextPlayerChunk(buffer, timeout, toRead);
// if (abs(framesCorrection) > 0) // if (abs(framesCorrection) > 0)
@ -257,7 +256,7 @@ cs::time_point_clk Stream::getNextPlayerChunk(void* outputBuffer, const cs::usec
// memcpy((char*)outputBuffer + n * format_.frameSize, buffer + index * format_.frameSize, format_.frameSize); // memcpy((char*)outputBuffer + n * format_.frameSize, buffer + index * format_.frameSize, format_.frameSize);
// idx += factor; // idx += factor;
// } // }
free(buffer); delete[] buffer;
return tp; return tp;
} }

View file

@ -40,7 +40,7 @@ public:
Stream(const SampleFormat& format); Stream(const SampleFormat& format);
/// Adds PCM data to the queue /// Adds PCM data to the queue
void addChunk(msg::PcmChunk* chunk); void addChunk(std::unique_ptr<msg::PcmChunk> chunk);
void clearChunks(); void clearChunks();
/// Get PCM data, which will be played out in "outputBufferDacTime" time /// Get PCM data, which will be played out in "outputBufferDacTime" time