parent
0ef1134aa0
commit
ace2bd6357
@ -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 *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 *malloc_ck_do(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; |
||||
} |
||||
|
Loading…
Reference in new issue