switch to freeRtos malloc + improve malloc_safe utils, fixed memleak

This commit is contained in:
2018-01-14 00:04:53 +01:00
parent 0ef1134aa0
commit ace2bd6357
21 changed files with 152 additions and 140 deletions
+3 -4
View File
@@ -87,11 +87,10 @@ static bool savebuf_ovhandler(PayloadBuilder *pb, uint32_t more)
void settings_save(void)
{
HAL_StatusTypeDef hst;
bool suc;
assert_param(save_buffer == NULL); // It must be NULL here - otherwise we have a leak
save_buffer = malloc_ck(FLASH_SAVE_BUF_LEN, &suc);
assert_param(suc);
save_buffer = malloc_ck(FLASH_SAVE_BUF_LEN);
assert_param(save_buffer != NULL);
PayloadBuilder pb = pb_start(save_buffer, FLASH_SAVE_BUF_LEN, savebuf_ovhandler);
@@ -151,7 +150,7 @@ void settings_save(void)
assert_param(hst == HAL_OK);
fls_printf("--- Flash done ---\r\n");
free(save_buffer);
free_ck(save_buffer);
save_buffer = NULL;
#if DEBUG_FLASH_WRITE
+3 -10
View File
@@ -5,6 +5,7 @@
#include "platform.h"
#include "unit.h"
#include "resources.h"
#include "unit_base.h"
char unit_tmp512[UNIT_TMP_LEN];
@@ -16,16 +17,8 @@ void clean_failed_unit(Unit *unit)
dbg("!! Init of [%s] failed!", unit->name);
// Free if it looks like it might've been allocated
if (isDynAlloc(unit->data)) {
dbg("Freeing allocated unit data");
free(unit->data);
unit->data = NULL;
}
if (isDynAlloc(unit->name)) {
dbg("Freeing allocated name");
free((void *) unit->name);
unit->name = NULL;
}
free_ck(unit->data);
free_ck(unit->name);
dbg("Releasing any held resources");
// Release any already claimed resources
+14 -20
View File
@@ -58,8 +58,8 @@ void ureg_add_type(const UnitDriver *driver)
assert_param(driver->deInit != NULL);
assert_param(driver->handleRequest != NULL);
UregEntry *re = calloc_ck(1, sizeof(UregEntry), &suc);
assert_param(suc);
UregEntry *re = calloc_ck(1, sizeof(UregEntry));
assert_param(re != NULL);
re->driver = driver;
re->next = NULL;
@@ -81,11 +81,7 @@ static void free_le_unit(UlistEntry *le)
pUnit->driver->deInit(pUnit);
// Name is not expected to be freed by the deInit() function
// - was alloc'd in the settings load loop
if (isDynAlloc(pUnit->name)) {
dbg("Freeing allocated name");
free((void *) pUnit->name);
pUnit->name = NULL;
}
free_ck(pUnit->name);
}
/** Add unit to the list, updating references as needed */
@@ -103,7 +99,6 @@ static void add_unit_to_list(UlistEntry *le)
// create a unit instance (not yet loading or initing - just pre-init)
Unit *ureg_instantiate(const char *driver_name)
{
bool suc = true;
error_t rv;
// Find type in the repository
@@ -111,8 +106,8 @@ Unit *ureg_instantiate(const char *driver_name)
while (re != NULL) {
if (streq(re->driver->name, driver_name)) {
// Create new list entry
UlistEntry *le = calloc_ck(1, sizeof(UlistEntry), &suc);
CHECK_SUC();
UlistEntry *le = calloc_ck(1, sizeof(UlistEntry));
if (le == NULL) return NULL;
le->next = NULL;
@@ -132,7 +127,7 @@ Unit *ureg_instantiate(const char *driver_name)
dbg("!! Unit type %s failed to pre-init! %s", driver_name, error_get_message(rv));
clean_failed_unit(pUnit);
free(le);
free_ck(le);
return NULL;
}
@@ -211,7 +206,7 @@ bool ureg_load_units(PayloadParser *pp)
// NAME
pp_string(pp, typebuf, 16);
pUnit->name = strdup(typebuf);
pUnit->name = strdup_ck(typebuf);
assert_param(pUnit->name);
// callsign
@@ -247,7 +242,7 @@ void ureg_remove_all_units(void)
next = le->next;
free_le_unit(le);
free(le);
free_ck(le);
le = next;
}
@@ -274,17 +269,17 @@ bool ureg_instantiate_by_ini(const char *restrict driver_name, const char *restr
char *name = NULL;
if (delim != NULL) {
// not last
name = strndup(p, delim - p);
name = strndup_ck(p, delim - p);
p = delim + 1;
} else {
// last name
name = strdup(p);
name = strdup_ck(p);
p = NULL; // quit after this loop ends
}
assert_param(name);
Unit *pUnit = ureg_instantiate(driver_name);
if (!pUnit) {
free(name);
free_ck(name);
return false;
}
@@ -514,9 +509,8 @@ void ureg_report_active_units(TF_ID frame_id)
}
msglen += count; // one byte per message for the callsign
bool suc = true;
uint8_t *buff = calloc_ck(1, msglen, &suc);
if (!suc) {
uint8_t *buff = calloc_ck(1, msglen);
if (buff == NULL) {
com_respond_error(frame_id, E_OUT_OF_MEM);
return;
}
@@ -539,7 +533,7 @@ void ureg_report_active_units(TF_ID frame_id)
com_respond_buf(frame_id, MSG_SUCCESS, buff, msglen);
}
free(buff);
free_ck(buff);
}
Unit *ureg_get_rsc_owner(Resource resource)