HeapBlockDevice::erase(): free up heap memory

`HeapBlockDevice::erase()` previously performed a range and alignment
check only. This commit adds freeing of heap memory.
pull/14483/head
Lingkai Dong 2021-03-26 17:58:36 +00:00
parent 7ef9e6175f
commit 6b5e467f96
2 changed files with 13 additions and 4 deletions

View File

@ -28,9 +28,11 @@
namespace mbed {
/** Lazily allocated heap-backed block device
/** Lazily allocated heap-backed block device.
*
* Useful for simulating a block device and tests
* Useful for simulating a block device and tests.
*
* @note Each block is allocated when used, and freed when erased.
*
* @code
* #include "mbed.h"

View File

@ -44,7 +44,7 @@ HeapBlockDevice::~HeapBlockDevice()
}
delete[] _blocks;
_blocks = 0;
_blocks = nullptr;
}
}
@ -63,7 +63,7 @@ int HeapBlockDevice::init()
}
for (size_t i = 0; i < _count; i++) {
_blocks[i] = 0;
_blocks[i] = nullptr;
}
}
@ -184,6 +184,13 @@ int HeapBlockDevice::erase(bd_addr_t addr, bd_size_t size)
if (!is_valid_erase(addr, size)) {
return BD_ERROR_DEVICE_ERROR;
}
for (size_t i = 0; i < (size / _erase_size); i++) {
size_t index = addr / _erase_size + i;
delete[] _blocks[index];
_blocks[index] = nullptr;
}
return 0;
}