mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-25 19:46:55 +08:00
Bring the changes closer to official-stockfish/master
This commit is contained in:
81
src/misc.cpp
81
src/misc.cpp
@@ -664,85 +664,4 @@ void sleep(int ms)
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
|
||||
}
|
||||
|
||||
void* aligned_malloc(size_t size, size_t align)
|
||||
{
|
||||
void* p = _mm_malloc(size, align);
|
||||
if (p == nullptr)
|
||||
{
|
||||
std::cout << "info string can't allocate memory. sise = " << size << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
std::uint64_t get_file_size(std::fstream& fs)
|
||||
{
|
||||
auto pos = fs.tellg();
|
||||
|
||||
fs.seekg(0, fstream::end);
|
||||
const uint64_t eofPos = (uint64_t)fs.tellg();
|
||||
fs.clear(); // Otherwise, the next seek may fail.
|
||||
fs.seekg(0, fstream::beg);
|
||||
const uint64_t begPos = (uint64_t)fs.tellg();
|
||||
fs.seekg(pos);
|
||||
|
||||
return eofPos - begPos;
|
||||
}
|
||||
|
||||
int read_file_to_memory(std::string filename, std::function<void* (uint64_t)> callback_func)
|
||||
{
|
||||
fstream fs(filename, ios::in | ios::binary);
|
||||
if (fs.fail())
|
||||
return 1;
|
||||
|
||||
const uint64_t file_size = get_file_size(fs);
|
||||
//std::cout << "filename = " << filename << " , file_size = " << file_size << endl;
|
||||
|
||||
// I know the file size, so call callback_func to get a buffer for this,
|
||||
// Get the pointer.
|
||||
void* ptr = callback_func(file_size);
|
||||
|
||||
// If the buffer could not be secured, or if the file size is different from the expected file size,
|
||||
// It is supposed to return nullptr. At this time, reading is interrupted and an error is returned.
|
||||
if (ptr == nullptr)
|
||||
return 2;
|
||||
|
||||
// read in pieces
|
||||
|
||||
const uint64_t block_size = 1024 * 1024 * 1024; // number of elements to read in one read (1GB)
|
||||
for (uint64_t pos = 0; pos < file_size; pos += block_size)
|
||||
{
|
||||
// size to read this time
|
||||
uint64_t read_size = (pos + block_size < file_size) ? block_size : (file_size - pos);
|
||||
fs.read((char*)ptr + pos, read_size);
|
||||
|
||||
// Read error occurred in the middle of the file.
|
||||
if (fs.fail())
|
||||
return 2;
|
||||
|
||||
//cout << ".";
|
||||
}
|
||||
fs.close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int write_memory_to_file(std::string filename, void* ptr, uint64_t size)
|
||||
{
|
||||
fstream fs(filename, ios::out | ios::binary);
|
||||
if (fs.fail())
|
||||
return 1;
|
||||
|
||||
const uint64_t block_size = 1024 * 1024 * 1024; // number of elements to write in one write (1GB)
|
||||
for (uint64_t pos = 0; pos < size; pos += block_size)
|
||||
{
|
||||
// Memory size to write this time
|
||||
uint64_t write_size = (pos + block_size < size) ? block_size : (size - pos);
|
||||
fs.write((char*)ptr + pos, write_size);
|
||||
//cout << ".";
|
||||
}
|
||||
fs.close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace Stockfish
|
||||
|
||||
Reference in New Issue
Block a user