Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove manual memory management to enhance memory safety #131

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove manual memory management (especially delete) to enhance memory…
… safety
jdh8 committed Jun 4, 2024
commit 3c384f6d916a83929f106e750e3d4d33e3eaff24
36 changes: 9 additions & 27 deletions src/Memory.cpp
Original file line number Diff line number Diff line change
@@ -11,21 +11,10 @@
#include "Memory.h"


Memory::Memory()
{
}


Memory::~Memory()
{
Memory::Resize(0, DDS_TT_SMALL, 0, 0);
}


void Memory::ReturnThread(const unsigned thrId)
{
memory[thrId]->transTable->ReturnAllMemory();
memory[thrId]->memUsed = Memory::MemoryInUseMB(thrId);
memory[thrId].transTable->ReturnAllMemory();
memory[thrId].memUsed = Memory::MemoryInUseMB(thrId);
}


@@ -40,12 +29,6 @@ void Memory::Resize(

if (memory.size() > n)
{
// Downsize.
for (unsigned i = n; i < memory.size(); i++)
{
delete memory[i]->transTable;
delete memory[i];
}
memory.resize(static_cast<unsigned>(n));
threadSizes.resize(static_cast<unsigned>(n));
}
@@ -57,22 +40,21 @@ void Memory::Resize(
threadSizes.resize(n);
for (unsigned i = oldSize; i < n; i++)
{
memory[i] = new ThreadData();
if (flag == DDS_TT_SMALL)
{
memory[i]->transTable = new TransTableS;
memory[i].transTable.reset(new TransTableS);
threadSizes[i] = "S";
}
else
{
memory[i]->transTable = new TransTableL;
memory[i].transTable.reset(new TransTableL);
threadSizes[i] = "L";
}

memory[i]->transTable->SetMemoryDefault(memDefault_MB);
memory[i]->transTable->SetMemoryMaximum(memMaximum_MB);
memory[i].transTable->SetMemoryDefault(memDefault_MB);
memory[i].transTable->SetMemoryMaximum(memMaximum_MB);

memory[i]->transTable->MakeTT();
memory[i].transTable->MakeTT();
}
}
}
@@ -91,13 +73,13 @@ ThreadData * Memory::GetPtr(const unsigned thrId)
cout << "Memory::GetPtr: " << thrId << " vs. " << memory.size() << endl;
exit(1);
}
return memory[thrId];
return &memory[thrId];
}


double Memory::MemoryInUseMB(const unsigned thrId) const
{
return memory[thrId]->transTable->MemoryInUse() +
return memory[thrId].transTable->MemoryInUse() +
8192. * sizeof(relRanksType) / static_cast<double>(1024.);
}

9 changes: 3 additions & 6 deletions src/Memory.h
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
#ifndef DDS_MEMORY_H
#define DDS_MEMORY_H

#include <memory>
#include <vector>

#include "TransTable.h"
@@ -78,7 +79,7 @@ struct ThreadData
// 960 KB
relRanksType rel[8192];

TransTable * transTable;
std::unique_ptr<TransTable> transTable;

Moves moves;

@@ -116,16 +117,12 @@ class Memory
{
private:

vector<ThreadData *> memory;
vector<ThreadData> memory;

vector<string> threadSizes;

public:

Memory();

~Memory();

void ReturnThread(const unsigned thrId);

void Resize(
44 changes: 15 additions & 29 deletions src/System.cpp
Original file line number Diff line number Diff line change
@@ -83,11 +83,6 @@ System::System()
}


System::~System()
{
}


void System::Reset()
{
runCat = DDS_RUN_SOLVE;
@@ -434,19 +429,16 @@ int System::RunThreadsGCD()
int System::RunThreadsBoost()
{
#ifdef DDS_THREADS_BOOST
vector<boost::thread *> threads;
vector<boost::thread> threads;

const unsigned nu = static_cast<unsigned>(numThreads);
threads.resize(nu);
threads.reserve(nu);

for (unsigned k = 0; k < nu; k++)
threads[k] = new boost::thread(fptr, k);
threads.emplace_back(fptr, k);

for (unsigned k = 0; k < nu; k++)
{
threads[k]->join();
delete threads[k];
}
for (boost::thread & t : threads)
t.join();
#endif

return RETURN_NO_FAULT;
@@ -460,23 +452,20 @@ int System::RunThreadsBoost()
int System::RunThreadsSTL()
{
#ifdef DDS_THREADS_STL
vector<thread *> threads;
vector<thread> threads;

vector<int> uniques;
vector<int> crossrefs;
(* CallbackDuplList[runCat])(* bop, uniques, crossrefs);

const unsigned nu = static_cast<unsigned>(numThreads);
threads.resize(nu);
threads.reserve(nu);

for (unsigned k = 0; k < nu; k++)
threads[k] = new thread(fptr, k);
threads.emplace_back(fptr, k);

for (unsigned k = 0; k < nu; k++)
{
threads[k]->join();
delete threads[k];
}
for (thread & t : threads)
t.join();
#endif

return RETURN_NO_FAULT;
@@ -534,19 +523,16 @@ int System::RunThreadsSTLIMPL()
int System::RunThreadsTBB()
{
#ifdef DDS_THREADS_TBB
vector<tbb::tbb_thread *> threads;
vector<tbb::tbb_thread> threads;

const unsigned nu = static_cast<unsigned>(numThreads);
threads.resize(nu);
threads.reserve(nu);

for (unsigned k = 0; k < nu; k++)
threads[k] = new tbb::tbb_thread(fptr, k);
threads.emplace_back(fptr, k);

for (unsigned k = 0; k < nu; k++)
{
threads[k]->join();
delete threads[k];
}
for (tbb::tbb_thread & t : threads)
t.join();
#endif

return RETURN_NO_FAULT;
2 changes: 1 addition & 1 deletion src/System.h
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ class System
public:
System();

~System();
~System() = default;

void Reset();