Skip to content

Commit

Permalink
Merge pull request #15 from mtconnect/memory_refactor_agent
Browse files Browse the repository at this point in the history
Refactored memory management in Agent class (#14)
  • Loading branch information
MatthewPowley committed Feb 4, 2019
2 parents 53e11be + 2a32acc commit 8b21e31
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions agent/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Agent::Agent(
try
{
// Load the configuration for the Agent
m_xmlParser = new XmlParser();
m_xmlParser = make_unique<XmlParser>();
m_devices = m_xmlParser->parseFile(configXmlPath);
std::set<std::string> uuids;
for (const auto &device : m_devices)
Expand Down Expand Up @@ -94,7 +94,7 @@ Agent::Agent(
// Sequence number and sliding buffer for data
m_sequence = 1ull;
m_slidingBufferSize = 1 << bufferSize;
m_slidingBuffer = new sliding_buffer_kernel_1<ComponentEventPtr>();
m_slidingBuffer = make_unique<sliding_buffer_kernel_1<ComponentEventPtr>>();
m_slidingBuffer->set_size(bufferSize);
m_checkpointFreq = checkpointFreq.count();
m_checkpointCount = (m_slidingBufferSize / checkpointFreq.count()) + 1;
Expand Down Expand Up @@ -244,8 +244,8 @@ Device *Agent::findDeviceByUUIDorName(const std::string& idOrName)

Agent::~Agent()
{
delete m_slidingBuffer; m_slidingBuffer = nullptr;
delete m_xmlParser; m_xmlParser = nullptr;
m_slidingBuffer.reset();
m_xmlParser.reset();
delete[] m_checkpoints; m_checkpoints = nullptr;
}

Expand Down
10 changes: 5 additions & 5 deletions agent/agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ class Agent : public server_http
uint64_t m_instanceId;

// Pointer to the configuration file for node access
XmlParser *m_xmlParser;
std::unique_ptr<XmlParser> m_xmlParser;

// For access to the sequence number and sliding buffer, use the mutex
std::mutex m_sequenceLock;
Expand All @@ -365,7 +365,7 @@ class Agent : public server_http
uint64_t m_sequence;

// The sliding/circular buffer to hold all of the events/sample data
dlib::sliding_buffer_kernel_1<ComponentEventPtr> *m_slidingBuffer;
std::unique_ptr<dlib::sliding_buffer_kernel_1<ComponentEventPtr>> m_slidingBuffer;
unsigned int m_slidingBufferSize;

// Asset storage, circ buffer stores ids
Expand All @@ -388,7 +388,7 @@ class Agent : public server_http
std::vector<Adapter *> m_adapters;
std::vector<Device *> m_devices;
std::map<std::string, Device *> m_deviceNameMap;
std::map<std::string, Device *> m_deviceUuidMap;
std::map<std::string, Device *> m_deviceUuidMap;
std::map<std::string, DataItem *> m_dataItemMap;
std::map<std::string, int> m_assetCounts;

Expand Down Expand Up @@ -433,7 +433,7 @@ class Agent : public server_http

CachedFile &operator=(const CachedFile &file)
{
m_buffer.release();
m_buffer.reset();
m_buffer = std::make_unique<char[]>(file.m_size);
memcpy(m_buffer.get(), file.m_buffer.get(), file.m_size);
m_size = file.m_size;
Expand All @@ -442,7 +442,7 @@ class Agent : public server_http

void allocate(size_t size)
{
m_buffer.release();
m_buffer.reset();
m_buffer = std::make_unique<char[]>(size);
m_size = size;
}
Expand Down

0 comments on commit 8b21e31

Please sign in to comment.