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
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 0 additions & 5 deletions src/ABstats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ ABstats::ABstats()
}


ABstats::~ABstats()
{
}


void ABstats::Reset()
{
for (int depth = 0; depth < DDS_MAXDEPTH; depth++)
Expand Down
2 changes: 1 addition & 1 deletion src/ABstats.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class ABstats

ABstats();

~ABstats();
~ABstats() = default;

void Reset();

Expand Down
36 changes: 9 additions & 27 deletions src/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand All @@ -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));
}
Expand All @@ -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();
}
}
}
Expand All @@ -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.);
}

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

#include <memory>
#include <vector>

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

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

Moves moves;

Expand Down Expand Up @@ -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(
Expand Down
5 changes: 0 additions & 5 deletions src/Moves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ Moves::Moves()
}


Moves::~Moves()
{
}


void Moves::Init(
const int tricks,
const int relStartHand,
Expand Down
2 changes: 1 addition & 1 deletion src/Moves.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class Moves
public:
Moves();

~Moves();
~Moves() = default;

void Init(
const int tricks,
Expand Down
5 changes: 0 additions & 5 deletions src/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ void Scheduler::InitTimes()
#endif


Scheduler::~Scheduler()
{
}


void Scheduler::Reset()
{
for (int b = 0; b < MAXNOOFBOARDS; b++)
Expand Down
2 changes: 1 addition & 1 deletion src/Scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class Scheduler

Scheduler();

~Scheduler();
~Scheduler() = default;

void RegisterThreads(
const int n);
Expand Down
44 changes: 15 additions & 29 deletions src/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ System::System()
}


System::~System()
{
}


void System::Reset()
{
runCat = DDS_RUN_SOLVE;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class System
public:
System();

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

void Reset();

Expand Down
5 changes: 0 additions & 5 deletions src/ThreadMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ ThreadMgr::ThreadMgr()
}


ThreadMgr::~ThreadMgr()
{
}


void ThreadMgr::Reset(const int nThreads)
{
const unsigned n = static_cast<unsigned>(nThreads);
Expand Down
2 changes: 1 addition & 1 deletion src/ThreadMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ThreadMgr

ThreadMgr();

~ThreadMgr();
~ThreadMgr() = default;

void Reset(const int nThreads);

Expand Down
5 changes: 0 additions & 5 deletions src/TimeStat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ TimeStat::TimeStat()
}


TimeStat::~TimeStat()
{
}


void TimeStat::Reset()
{
number = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/TimeStat.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TimeStat

TimeStat();

~TimeStat();
~TimeStat() = default;

void Reset();

Expand Down
11 changes: 0 additions & 11 deletions src/TimeStatList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@
#include "TimeStatList.h"


TimeStatList::TimeStatList()
{
TimeStatList::Reset();
}


TimeStatList::~TimeStatList()
{
}


void TimeStatList::Reset()
{
}
Expand Down
4 changes: 0 additions & 4 deletions src/TimeStatList.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ class TimeStatList

public:

TimeStatList();

~TimeStatList();

void Reset();

void Init(
Expand Down
5 changes: 0 additions & 5 deletions src/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ Timer::Timer()
}


Timer::~Timer()
{
}


void Timer::Reset()
{
name = "";
Expand Down
2 changes: 1 addition & 1 deletion src/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Timer

Timer();

~Timer();
~Timer() = default;

void Reset();

Expand Down
5 changes: 0 additions & 5 deletions src/TimerGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ TimerGroup::TimerGroup()
}


TimerGroup::~TimerGroup()
{
}


void TimerGroup::Reset()
{
timers.resize(TIMER_DEPTH);
Expand Down
2 changes: 1 addition & 1 deletion src/TimerGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TimerGroup

TimerGroup();

~TimerGroup();
~TimerGroup() = default;

void Reset();

Expand Down
Loading