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
-11
View File
@@ -12,17 +12,6 @@ static inline bool inIRQ(void)
return __get_IPSR() != 0;
}
register char *__SP asm("sp");
static inline bool isDynAlloc(const void *obj)
{
// this is the 0x20000000-something address that should correspond to heap bottom
extern char heapstart __asm("end");
return ((uint32_t)obj >= (uint32_t)&heapstart)
&& ((uint32_t)obj < (uint32_t)__SP);
}
/** Tight asm loop */
#define __asm_loop(cycles) \
do { \
+3 -5
View File
@@ -23,17 +23,15 @@ char *iwbuffer = NULL;
void iw_begin(void)
{
assert_param(iwbuffer == NULL);
bool suc = true;
iwbuffer = malloc_ck(IWBUFFER_LEN, &suc);
assert_param(suc);
iwbuffer = malloc_ck(IWBUFFER_LEN);
assert_param(iwbuffer != NULL);
}
/** Release the helper buffer */
void iw_end(void)
{
assert_param(iwbuffer != NULL);
free(iwbuffer);
iwbuffer = NULL;
free_ck(iwbuffer);
}
#define IW_VPRINTF() do { \
+26 -30
View File
@@ -1,48 +1,44 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include "platform.h"
#include "debug.h"
#include "stm32_assert.h"
#include "malloc_safe.h"
#if 1
void *malloc_safe_do(size_t size, const char *file, uint32_t line)
void *malloc_ck_do(size_t size, const char *file, uint32_t line)
{
void *mem = malloc(size);
if (mem == NULL) abort_msg("MALLOC FAILED", file, line);
return mem;
}
void *calloc_safe_do(size_t nmemb, size_t size, const char *file, uint32_t line)
{
void *mem = calloc(size, nmemb);
if (mem == NULL) abort_msg("CALLOC FAILED", file, line);
return mem;
}
void *malloc_ck_do(size_t size, bool *suc, const char *file, uint32_t line)
{
void *mem = malloc(size);
void *mem = pvPortMalloc(size);
_malloc_trace(size, mem, file, line);
if (mem == NULL) {
warn_msg("MALLOC FAILED", file, line);
*suc = false;
mem = NULL;
}
return mem;
}
void *calloc_ck_do(size_t nmemb, size_t size, bool *suc, const char *file, uint32_t line)
void *calloc_ck_do(size_t nmemb, size_t size, const char *file, uint32_t line)
{
void *mem = calloc(size, nmemb);
void *mem = pvPortMalloc(size*nmemb);
_malloc_trace(nmemb*size, mem, file, line);
if (mem == NULL) {
warn_msg("CALLOC FAILED", file, line);
*suc = false;
mem = NULL;
}
memset(mem, 0, size*nmemb);
return mem;
}
#endif
char *strdup_ck_do(const char *s, const char* file, uint32_t line)
{
size_t len = strlen(s) + 1;
void *new = malloc_ck_do(len, file, line);
if (new == NULL) return NULL;
return (char *) memcpy (new, s, len);
}
char *strndup_ck_do(const char *s, uint32_t len, const char* file, uint32_t line)
{
// TODO verify - this was not tested
size_t alen = MIN(strlen(s) + 1, len);
uint8_t *new = malloc_ck_do(alen, file, line);
if (new == NULL) return NULL;
memcpy (new, s, alen-1);
new[alen-1] = '\0';
return (char *) new;
}
+29 -8
View File
@@ -9,14 +9,35 @@
#include <stdint.h>
#include <stdbool.h>
void *malloc_safe_do(size_t size, const char* file, uint32_t line) __attribute__((malloc));
void *calloc_safe_do(size_t nmemb, size_t size, const char* file, uint32_t line) __attribute__((malloc));
void *malloc_ck_do(size_t size, bool *suc, const char* file, uint32_t line) __attribute__((malloc));
void *calloc_ck_do(size_t nmemb, size_t size, bool *suc, const char* file, uint32_t line) __attribute__((malloc));
void *malloc_ck_do(size_t size, const char* file, uint32_t line) __attribute__((malloc));
void *calloc_ck_do(size_t nmemb, size_t size, const char* file, uint32_t line) __attribute__((malloc));
char *strdup_ck_do(const char *s, const char* file, uint32_t line) __attribute__((malloc));
char *strndup_ck_do(const char *s, uint32_t len, const char* file, uint32_t line) __attribute__((malloc));
#define malloc_s(size) malloc_safe_do(size, __BASE_FILE__, __LINE__)
#define calloc_s(nmemb, size) calloc_safe_do(nmemb, size, __BASE_FILE__, __LINE__)
#define malloc_ck(size, suc) malloc_ck_do(size, suc, __BASE_FILE__, __LINE__)
#define calloc_ck(nmemb, size, suc) calloc_ck_do(nmemb, size, suc, __BASE_FILE__, __LINE__)
#if DEBUG_MALLOC
#define _malloc_trace(len, obj, file, line) do { PRINTF("~ malloc(%d) -> 0x%p at ", len, obj); PUTS(file); PUTCHAR(':'); PRINTF("%d\r\n", (int)line); } while (0)
#define _free_trace(obj, file, line) do { PRINTF("~ free(0x%p) at ", obj); PUTS(file); PUTCHAR(':'); PRINTF("%d\r\n", (int)line); } while (0)
#define malloc_ck(size) malloc_ck_do(size, __BASE_FILE__, __LINE__)
#define calloc_ck(nmemb, size) calloc_ck_do(nmemb, size, __BASE_FILE__, __LINE__)
#define strdup_ck(s) strdup_ck_do(s, __BASE_FILE__, __LINE__)
#define strndup_ck(s, len) strndup_ck_do(s, (uint32_t)(len), __BASE_FILE__, __LINE__)
#else
#define _malloc_trace(len, obj, file, line)
#define _free_trace(obj, file, line)
#define malloc_ck(size) malloc_ck_do(size, "", 0)
#define calloc_ck(nmemb, size) calloc_ck_do(nmemb, size, "", 0)
#define strdup_ck(s) strdup_ck_do(s, "", 0)
#define strndup_ck(s, len) strndup_ck_do(s, (uint32_t)(len), "", 0)
#endif
/**
* Free an allocated object, and assign it to NULL.
*/
#define free_ck(obj) do { \
_free_trace(obj, __BASE_FILE__, __LINE__); \
if ((obj) != NULL) vPortFree((void *)(obj)); \
obj = NULL; \
} while (0)
#endif // MALLOC_SAFE_H
+3 -2
View File
@@ -23,6 +23,7 @@
/**** pts: sam2p-specific defines ****/
#include "snprintf.h"
#include "malloc_safe.h"
/* #include <stdarg.h> -- from snprintf.h */
#ifdef NULL /* as on Mac OS/X 10.5.7 <stdlib.h> */
# undef NULL
@@ -32,7 +33,7 @@
# define malloc ::operator new
#else
# include <stdlib.h> /* malloc() */
#include <debug.h>
#include "debug.h"
#endif
#define size_t size_t /* normally: int, unsigned */
@@ -834,7 +835,7 @@ size_t vasprintf(char **ptr, const char *format, va_list ap)
ret = vsnprintf((char*)NULL, 0, format, ap);
if (ret+1 <= 1) return ret; /* pts: bit of old unsigned trick... */
if (NULL==(*ptr = (char *)malloc(ret+1))) return (size_t)-1;
if (NULL==(*ptr = (char *)malloc_ck(ret+1))) return (size_t)-1;
ret = vsnprintf(*ptr, ret+1, format, ap);
return ret;