Skip to content

Commit f1adef8

Browse files
committed
Moved deallocation of mem block device to destructor
Enables persistant storage between de/reinitialization cycles
1 parent 5a803a7 commit f1adef8

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

features/filesystem/bd/MemBlockDevice.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,34 @@ MemBlockDevice::MemBlockDevice(bd_count_t count, bd_size_t size)
2626

2727
MemBlockDevice::~MemBlockDevice()
2828
{
29-
// nop if uninitialized
30-
deinit();
29+
if (_blocks) {
30+
for (bd_count_t i = 0; i < _count; i++) {
31+
free(_blocks[i]);
32+
}
33+
34+
free(_blocks);
35+
_blocks = 0;
36+
}
3137
}
3238

3339
bd_error_t MemBlockDevice::init()
3440
{
35-
_blocks = (uint8_t**)malloc(_count*sizeof(uint8_t*));
3641
if (!_blocks) {
37-
return BD_ERROR_DEVICE_ERROR;
42+
_blocks = (uint8_t**)malloc(_count*sizeof(uint8_t*));
43+
if (!_blocks) {
44+
return BD_ERROR_DEVICE_ERROR;
45+
}
46+
47+
memset(_blocks, 0, _count*sizeof(uint8_t*));
3848
}
3949

40-
memset(_blocks, 0, _count*sizeof(uint8_t*));
4150
return BD_ERROR_OK;
4251
}
4352

4453
bd_error_t MemBlockDevice::deinit()
4554
{
46-
if (_blocks) {
47-
for (bd_count_t i = 0; i < _count; i++) {
48-
free(_blocks[i]);
49-
}
50-
51-
free(_blocks);
52-
_blocks = 0;
53-
}
54-
55+
// Memory is lazily cleaned up in destructor to allow
56+
// data to live across de/reinitialization
5557
return BD_ERROR_OK;
5658
}
5759

0 commit comments

Comments
 (0)