nvram: use smart pointers, fix initialization.

This commit is contained in:
Maxim Poliakovski 2023-04-17 00:03:36 +02:00
parent 2f562e7761
commit 09da0c6109
2 changed files with 7 additions and 9 deletions

View File

@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
Copyright (C) 2018-23 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@ -46,15 +46,14 @@ NVram::NVram(std::string file_name, uint32_t ram_size)
this->file_name = file_name;
this->ram_size = ram_size;
this->storage = new uint8_t[ram_size];
// allocate memory storage and fill it with zeroes
this->storage = std::unique_ptr<uint8_t[]>(new uint8_t[ram_size] ());
this->init();
}
NVram::~NVram() {
this->save();
if (this->storage)
delete this->storage;
}
uint8_t NVram::read_byte(uint32_t offset) {
@ -74,9 +73,8 @@ void NVram::init() {
if (f.fail() || !f.read(sig, sizeof(NVRAM_FILE_ID)) ||
!f.read((char*)&data_size, sizeof(data_size)) ||
memcmp(sig, NVRAM_FILE_ID, sizeof(NVRAM_FILE_ID)) || data_size != this->ram_size ||
!f.read((char*)this->storage, this->ram_size)) {
!f.read((char*)this->storage.get(), this->ram_size)) {
LOG_F(WARNING, "Could not restore NVRAM content from the given file.");
memset(this->storage, 0, sizeof(this->ram_size));
}
f.close();
@ -90,7 +88,7 @@ void NVram::save() {
f.write((char*)&this->ram_size, sizeof(this->ram_size));
/* write NVRAM content */
f.write((char*)this->storage, this->ram_size);
f.write((char*)this->storage.get(), this->ram_size);
f.close();
}

View File

@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
Copyright (C) 2018-23 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@ -49,7 +49,7 @@ public:
private:
std::string file_name; // file name for the backing file
uint16_t ram_size; // NVRAM size
uint8_t* storage;
std::unique_ptr<uint8_t[]> storage;
void init();
void save();