2023-07-07 12:20:59 +09:30
|
|
|
/* debug_alloc.h
|
|
|
|
|
*
|
|
|
|
|
* Some macros which can report on malloc results.
|
|
|
|
|
*
|
|
|
|
|
* Enable with "-D DEBUG_ALLOC"
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef DEBUG_ALLOC_H
|
|
|
|
|
#define DEBUG_ALLOC_H
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
// Debug calls
|
|
|
|
|
|
|
|
|
|
#ifdef CORTEX_M4
|
2023-07-14 12:36:50 +09:30
|
|
|
extern char *__heap_end;
|
|
|
|
|
register char *sp asm("sp");
|
2023-07-07 12:20:59 +09:30
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(__EMBEDDED__)
|
2023-07-14 12:36:50 +09:30
|
|
|
extern void *codec2_malloc(size_t size);
|
|
|
|
|
extern void *codec2_calloc(size_t nmemb, size_t size);
|
|
|
|
|
extern void codec2_free(void *ptr);
|
2023-07-07 12:20:59 +09:30
|
|
|
#else
|
|
|
|
|
#define codec2_malloc(size) (malloc(size))
|
|
|
|
|
#define codec2_calloc(nmemb, size) (calloc(nmemb, size))
|
|
|
|
|
#define codec2_free(ptr) (free(ptr))
|
2023-07-14 12:36:50 +09:30
|
|
|
#endif // defined(__EMBEDDED__)
|
2023-07-07 12:20:59 +09:30
|
|
|
|
2023-07-14 12:36:50 +09:30
|
|
|
static inline void *DEBUG_MALLOC(const char *func, size_t size) {
|
|
|
|
|
void *ptr = codec2_malloc(size);
|
|
|
|
|
fprintf(stderr, "MALLOC: %s %p %d", func, ptr, (int)size);
|
2023-07-07 12:20:59 +09:30
|
|
|
#ifdef CORTEX_M4
|
|
|
|
|
|
2023-07-14 12:36:50 +09:30
|
|
|
fprintf(stderr, " : sp %p ", sp);
|
2023-07-07 12:20:59 +09:30
|
|
|
#endif
|
2023-07-14 12:36:50 +09:30
|
|
|
if (!ptr) fprintf(stderr, " ** FAILED **");
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
return (ptr);
|
|
|
|
|
}
|
2023-07-07 12:20:59 +09:30
|
|
|
|
2023-07-14 12:36:50 +09:30
|
|
|
static inline void *DEBUG_CALLOC(const char *func, size_t nmemb, size_t size) {
|
|
|
|
|
void *ptr = codec2_calloc(nmemb, size);
|
|
|
|
|
fprintf(stderr, "CALLOC: %s %p %d %d", func, ptr, (int)nmemb, (int)size);
|
2023-07-07 12:20:59 +09:30
|
|
|
#ifdef CORTEX_M4
|
2023-07-14 12:36:50 +09:30
|
|
|
fprintf(stderr, " : sp %p ", sp);
|
2023-07-07 12:20:59 +09:30
|
|
|
#endif
|
2023-07-14 12:36:50 +09:30
|
|
|
if (!ptr) fprintf(stderr, " ** FAILED **");
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
return (ptr);
|
|
|
|
|
}
|
|
|
|
|
static inline void DEBUG_FREE(const char *func, void *ptr) {
|
|
|
|
|
codec2_free(ptr);
|
|
|
|
|
fprintf(stderr, "FREE: %s %p\n", func, ptr);
|
|
|
|
|
}
|
2023-07-07 12:20:59 +09:30
|
|
|
|
|
|
|
|
#ifdef DEBUG_ALLOC
|
2023-07-14 12:36:50 +09:30
|
|
|
#define MALLOC(size) DEBUG_MALLOC(__func__, size)
|
|
|
|
|
#define CALLOC(nmemb, size) DEBUG_CALLOC(__func__, nmemb, size)
|
|
|
|
|
#define FREE(ptr) DEBUG_FREE(__func__, ptr)
|
|
|
|
|
#else // DEBUG_ALLOC
|
2023-07-07 12:20:59 +09:30
|
|
|
// Default to normal calls
|
2023-07-14 12:36:50 +09:30
|
|
|
#define MALLOC(size) codec2_malloc(size)
|
2023-07-07 12:20:59 +09:30
|
|
|
|
2023-07-14 12:36:50 +09:30
|
|
|
#define CALLOC(nmemb, size) codec2_calloc(nmemb, size)
|
2023-07-07 12:20:59 +09:30
|
|
|
|
2023-07-14 12:36:50 +09:30
|
|
|
#define FREE(ptr) codec2_free(ptr)
|
2023-07-07 12:20:59 +09:30
|
|
|
|
2023-07-14 12:36:50 +09:30
|
|
|
#endif // DEBUG_ALLOC
|
2023-07-07 12:20:59 +09:30
|
|
|
|
2023-07-14 12:36:50 +09:30
|
|
|
#endif // DEBUG_ALLOC_H
|