1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 16:53:09 +00:00

Add struct destructors

Avoid explicitly freeing the objects.

Because d'tor involves file unmapping, some
care must be taken to avoid accidentaly destroy
the object (even temporarly), for instance when
reordering the list.

As a side effect, we can now restore the original
main.cpp, fully in sync with master branch.
This commit is contained in:
Marco Costalba 2016-04-26 09:09:59 +02:00
parent c7c4ab8186
commit 9e24e68419
3 changed files with 34 additions and 46 deletions

View file

@ -41,13 +41,12 @@ int main(int argc, char* argv[]) {
Search::init(); Search::init();
Eval::init(); Eval::init();
Pawns::init(); Pawns::init();
Threads.init();
Tablebases::init(Options["SyzygyPath"]); Tablebases::init(Options["SyzygyPath"]);
TT.resize(Options["Hash"]); TT.resize(Options["Hash"]);
Threads.init();
UCI::loop(argc, argv); UCI::loop(argc, argv);
Threads.exit(); Threads.exit();
Tablebases::free();
return 0; return 0;
} }

View file

@ -95,6 +95,8 @@ struct TBEntry {
TBEntry_piece piece; TBEntry_piece piece;
TBEntry_pawn pawn; TBEntry_pawn pawn;
}; };
~TBEntry();
}; };
struct DTZEntry_piece { struct DTZEntry_piece {
@ -134,6 +136,8 @@ struct DTZEntry {
DTZEntry_piece piece; DTZEntry_piece piece;
DTZEntry_pawn pawn; DTZEntry_pawn pawn;
}; };
~DTZEntry();
}; };
const signed char OffdiagA1H8[] = { const signed char OffdiagA1H8[] = {
@ -468,6 +472,32 @@ public:
} }
}; };
TBEntry::~TBEntry()
{
TBFile::unmap(data, mapping);
if (has_pawns)
for (File f = FILE_A; f <= FILE_D; ++f) {
free(pawn.file[f].precomp[0]);
free(pawn.file[f].precomp[1]);
}
else {
free(piece.precomp[0]);
free(piece.precomp[1]);
}
}
DTZEntry::~DTZEntry()
{
TBFile::unmap(data, mapping);
if (has_pawns)
for (File f = FILE_A; f <= FILE_D; ++f)
free(pawn.file[f].precomp);
else
free(piece.precomp);
}
// Given a position with 6 or fewer pieces, produce a text string // Given a position with 6 or fewer pieces, produce a text string
// of the form KQPvKRP, where "KQP" represents the white pieces if // of the form KQPvKRP, where "KQP" represents the white pieces if
// mirror == false and the black pieces if mirror == true. // mirror == false and the black pieces if mirror == true.
@ -483,32 +513,6 @@ std::string file_name(const Position& pos, bool mirror)
return mirror ? b + 'v' + w : w + 'v' + b; return mirror ? b + 'v' + w : w + 'v' + b;
} }
void free_wdl_entry(TBEntry& entry)
{
TBFile::unmap(entry.data, entry.mapping);
if (entry.has_pawns)
for (File f = FILE_A; f <= FILE_D; ++f) {
free(entry.pawn.file[f].precomp[0]);
free(entry.pawn.file[f].precomp[1]);
}
else {
free(entry.piece.precomp[0]);
free(entry.piece.precomp[1]);
}
}
void free_dtz_entry(DTZEntry& entry)
{
TBFile::unmap(entry.data, entry.mapping);
if (entry.has_pawns)
for (File f = FILE_A; f <= FILE_D; ++f)
free(entry.pawn.file[f].precomp);
else
free(entry.piece.precomp);
}
void HashTable::insert(const std::vector<PieceType>& pieces) void HashTable::insert(const std::vector<PieceType>& pieces)
{ {
StateInfo st; StateInfo st;
@ -1416,8 +1420,8 @@ int probe_dtz_table(const Position& pos, int wdl, int *success)
// Enforce "Most Recently Used" (MRU) order for DTZ_list // Enforce "Most Recently Used" (MRU) order for DTZ_list
for (auto it = DTZ_list.begin(); it != DTZ_list.end(); ++it) for (auto it = DTZ_list.begin(); it != DTZ_list.end(); ++it)
if (it->keys[0] == key) { if (it->keys[0] == key) {
DTZ_list.push_front(*it); // Move to front without deleting the element
DTZ_list.erase(it); DTZ_list.splice(DTZ_list.begin(),DTZ_list, it);
break; break;
} }
@ -1470,12 +1474,9 @@ int probe_dtz_table(const Position& pos, int wdl, int *success)
// Keep list size within 64 entries // Keep list size within 64 entries
// FIXME remove it when we will know what we are doing // FIXME remove it when we will know what we are doing
if (DTZ_list.size() > 64) if (DTZ_list.size() > 64)
{
free_dtz_entry(DTZ_list.back());
DTZ_list.pop_back(); DTZ_list.pop_back();
} }
} }
}
DTZEntry* ptr = &DTZ_list.front(); DTZEntry* ptr = &DTZ_list.front();
@ -1881,24 +1882,13 @@ int probe_dtz(Position& pos, int *success)
} // namespace } // namespace
void Tablebases::free() void Tablebases::init(const std::string& paths)
{ {
for (auto& e : TB_entry)
free_wdl_entry(e);
for (auto& e : DTZ_list)
free_dtz_entry(e);
DTZ_list.clear(); DTZ_list.clear();
TB_entry.clear(); TB_entry.clear();
TBHash.clear(); TBHash.clear();
MaxCardinality = 0; MaxCardinality = 0;
}
void Tablebases::init(const std::string& paths)
{
Tablebases::free();
TBPaths = paths; TBPaths = paths;
if (TBPaths.empty() || TBPaths == "<empty>") if (TBPaths.empty() || TBPaths == "<empty>")

View file

@ -16,7 +16,6 @@ enum WDLScore {
extern int MaxCardinality; extern int MaxCardinality;
void init(const std::string& paths); void init(const std::string& paths);
void free();
WDLScore probe_wdl(Position& pos, int* success); WDLScore probe_wdl(Position& pos, int* success);
bool root_probe(Position& pos, Search::RootMoves& rootMoves, Value& score); bool root_probe(Position& pos, Search::RootMoves& rootMoves, Value& score);
bool root_probe_wdl(Position& pos, Search::RootMoves& rootMoves, Value& score); bool root_probe_wdl(Position& pos, Search::RootMoves& rootMoves, Value& score);