Skip to content

Commit 43289c7

Browse files
author
Veijo Pesonen
committed
SFDP: converts to smart pointers
C++14-ify memory allocation.
1 parent e40ce82 commit 43289c7

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

drivers/source/SFDP.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
*/
1717

1818
#include <algorithm>
19-
#include <cstdint>
20-
#include <cstdlib>
21-
#include <cstring>
19+
#include <memory>
20+
#include <stdint.h>
21+
#include <stdlib.h>
22+
#include <string.h>
23+
24+
#include "platform/mbed_error.h"
2225
#include "drivers/internal/SFDP.h"
2326

2427
#if (DEVICE_SPI || DEVICE_QSPI)
@@ -179,14 +182,6 @@ int sfdp_parse_headers(Callback<int(bd_addr_t, void *, bd_size_t)> sfdp_reader,
179182

180183
int sfdp_parse_sector_map_table(Callback<int(bd_addr_t, void *, bd_size_t)> sfdp_reader, sfdp_hdr_info &sfdp_info)
181184
{
182-
/* The number of
183-
* - sector map configuration detection commands
184-
* - configurations
185-
* - regions in each configuration
186-
* is variable -> the size of this table is variable
187-
*/
188-
uint8_t *smptbl_buff;
189-
int status = 0;
190185
uint32_t tmp_region_size = 0;
191186
uint8_t type_mask;
192187
int prev_boundary = 0;
@@ -199,37 +194,41 @@ int sfdp_parse_sector_map_table(Callback<int(bd_addr_t, void *, bd_size_t)> sfdp
199194

200195
if (!sfdp_info.smptbl.addr || !sfdp_info.smptbl.size) {
201196
tr_debug("No Sector Map Table");
202-
return 0;
197+
return MBED_SUCCESS;
203198
}
204199

205-
smptbl_buff = (uint8_t *)malloc(sfdp_info.smptbl.size);
200+
/* The number of
201+
* - sector map configuration detection commands
202+
* - configurations
203+
* - regions in each configuration
204+
* is variable -> the size of this table is variable
205+
*/
206+
auto smptbl_buff = std::make_unique<uint8_t[]>(sfdp_info.smptbl.size);
206207
if (!smptbl_buff) {
207208
tr_error("Failed to allocate memory");
208209
return -1;
209210
}
210211

211212
tr_debug("Parsing Sector Map Table - addr: 0x%" PRIx32 ", Size: %d", sfdp_info.smptbl.addr, sfdp_info.smptbl.size);
212213

213-
status = sfdp_reader(sfdp_info.smptbl.addr, smptbl_buff, sfdp_info.smptbl.size);
214+
int status = sfdp_reader(sfdp_info.smptbl.addr, smptbl_buff.get(), sfdp_info.smptbl.size);
214215
if (status < 0) {
215216
tr_error("Sector Map: Table retrieval failed");
216-
goto EXIT;
217+
return -1;
217218
}
218219

219220
// Currently we support only Single Map Descriptor
220221
if (!((smptbl_buff[0] & 0x3) == 0x03) && (smptbl_buff[1] == 0x0)) {
221222
tr_error("Sector Map: Supporting Only Single Map Descriptor (not map commands)");
222-
status = -1;
223-
goto EXIT;
223+
return -1;
224224
}
225225

226226
sfdp_info.smptbl.region_cnt = smptbl_buff[2] + 1;
227227
if (sfdp_info.smptbl.region_cnt > SFDP_SECTOR_MAP_MAX_REGIONS) {
228228
tr_error("Sector Map: Supporting up to %d regions, current setup to %d regions - fail",
229229
SFDP_SECTOR_MAP_MAX_REGIONS,
230230
sfdp_info.smptbl.region_cnt);
231-
status = -1;
232-
goto EXIT;
231+
return -1;
233232
}
234233

235234
// Loop through Regions and set for each one: size, supported erase types, high boundary offset
@@ -259,10 +258,7 @@ int sfdp_parse_sector_map_table(Callback<int(bd_addr_t, void *, bd_size_t)> sfdp
259258
type_mask = type_mask << 1;
260259
}
261260

262-
EXIT:
263-
free(smptbl_buff);
264-
265-
return status;
261+
return 0;
266262
}
267263

268264
size_t sfdp_detect_page_size(uint8_t *basic_param_table_ptr, size_t basic_param_table_size)

0 commit comments

Comments
 (0)