Skip to content

Commit 6b5e467

Browse files
committed
HeapBlockDevice::erase(): free up heap memory
`HeapBlockDevice::erase()` previously performed a range and alignment check only. This commit adds freeing of heap memory.
1 parent 7ef9e61 commit 6b5e467

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

storage/blockdevice/include/blockdevice/HeapBlockDevice.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828

2929
namespace mbed {
3030

31-
/** Lazily allocated heap-backed block device
31+
/** Lazily allocated heap-backed block device.
3232
*
33-
* Useful for simulating a block device and tests
33+
* Useful for simulating a block device and tests.
34+
*
35+
* @note Each block is allocated when used, and freed when erased.
3436
*
3537
* @code
3638
* #include "mbed.h"

storage/blockdevice/source/HeapBlockDevice.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ HeapBlockDevice::~HeapBlockDevice()
4444
}
4545

4646
delete[] _blocks;
47-
_blocks = 0;
47+
_blocks = nullptr;
4848
}
4949
}
5050

@@ -63,7 +63,7 @@ int HeapBlockDevice::init()
6363
}
6464

6565
for (size_t i = 0; i < _count; i++) {
66-
_blocks[i] = 0;
66+
_blocks[i] = nullptr;
6767
}
6868
}
6969

@@ -184,6 +184,13 @@ int HeapBlockDevice::erase(bd_addr_t addr, bd_size_t size)
184184
if (!is_valid_erase(addr, size)) {
185185
return BD_ERROR_DEVICE_ERROR;
186186
}
187+
188+
for (size_t i = 0; i < (size / _erase_size); i++) {
189+
size_t index = addr / _erase_size + i;
190+
delete[] _blocks[index];
191+
_blocks[index] = nullptr;
192+
}
193+
187194
return 0;
188195
}
189196

0 commit comments

Comments
 (0)