libpayload: Fix CONFIG_LP_DEBUG_MALLOC for 64-bit archs

New compilers are a little more stringent about defining the same
prototype more than once, so some of our CONFIG_LP_DEBUG_MALLOC wrappers
don't quite work the way they are written anymore. Also, several of the
printf()s weren't written 64-bit safe. And let's add some
double-evaluation safety while I'm here anyway... and I have no idea why
this ever depended on CONFIG_LP_USB, that just seems like a typo.

Change-Id: Ib54ebc3cfba99f372690365b78c7ceb372c0bd45
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/14921
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
This commit is contained in:
Julius Werner 2016-05-19 13:15:16 -07:00 committed by Patrick Georgi
parent 651d8dd4f6
commit f5b76fe9e9
3 changed files with 91 additions and 102 deletions

View File

@ -415,7 +415,6 @@ menu "Debugging"
config DEBUG_MALLOC config DEBUG_MALLOC
bool "Debug memory allocator" bool "Debug memory allocator"
depends on USB
default n default n
help help
Select this option if you want to debug the memory allocator. This Select this option if you want to debug the memory allocator. This

View File

@ -45,104 +45,6 @@
* @defgroup malloc Memory allocation functions * @defgroup malloc Memory allocation functions
* @{ * @{
*/ */
#if CONFIG(LP_DEBUG_MALLOC) && !defined(IN_MALLOC_C)
#define free(p) \
({ \
extern void print_malloc_map(void); \
extern void free(void *); \
printf("free(%p) called from %s:%s:%d...\n", p, __FILE__, __func__, \
__LINE__);\
printf("PRE free()\n"); \
print_malloc_map(); \
free(p); \
printf("POST free()\n"); \
print_malloc_map(); \
})
#define malloc(s) \
({ \
extern void print_malloc_map(void); \
extern void *malloc(size_t); \
void *ptr; \
printf("malloc(%u) called from %s:%s:%d...\n", s, __FILE__, __func__, \
__LINE__);\
printf("PRE malloc\n"); \
print_malloc_map(); \
ptr = malloc(s); \
printf("POST malloc (ptr = %p)\n", ptr); \
print_malloc_map(); \
ptr; \
})
#define calloc(n,s) \
({ \
extern void print_malloc_map(void); \
extern void *calloc(size_t,size_t); \
void *ptr; \
printf("calloc(%u, %u) called from %s:%s:%d...\n", n, s, __FILE__, \
__func__, __LINE__);\
printf("PRE calloc\n"); \
print_malloc_map(); \
ptr = calloc(n,s); \
printf("POST calloc (ptr = %p)\n", ptr); \
print_malloc_map(); \
ptr; \
})
#define realloc(p,s) \
({ \
extern void print_malloc_map(void); \
extern void *realloc(void*,size_t); \
void *ptr; \
printf("realloc(%p, %u) called from %s:%s:%d...\n", p, s, __FILE__, \
__func__, __LINE__);\
printf("PRE realloc\n"); \
print_malloc_map(); \
ptr = realloc(p,s); \
printf("POST realloc (ptr = %p)\n", ptr); \
print_malloc_map(); \
ptr; \
})
#define memalign(a,s) \
({ \
extern void print_malloc_map(void); \
extern void *memalign(size_t, size_t); \
void *ptr; \
printf("memalign(%u, %u) called from %s:%s:%d...\n", a, s, __FILE__, \
__func__, __LINE__);\
printf("PRE memalign\n"); \
print_malloc_map(); \
ptr = memalign(a,s); \
printf("POST memalign (ptr = %p)\n", ptr); \
print_malloc_map(); \
ptr; \
})
#define dma_malloc(s) \
({ \
extern void print_malloc_map(void); \
extern void *dma_malloc(size_t); \
void *ptr; \
printf("dma_malloc(%u) called from %s:%s:%d...\n", s, __FILE__, \
__func__, __LINE__);\
printf("PRE dma_malloc\n"); \
print_malloc_map(); \
ptr = dma_malloc(s); \
printf("POST dma_malloc (ptr = %p)\n", ptr); \
print_malloc_map(); \
ptr; \
})
#define dma_memalign(a,s) \
({ \
extern void print_malloc_map(void); \
extern void *dma_memalign(size_t, size_t); \
void *ptr; \
printf("dma_memalign(%u, %u) called from %s:%s:%d...\n", a, s, \
__FILE__, __func__, __LINE__);\
printf("PRE dma_memalign\n"); \
print_malloc_map(); \
ptr = dma_memalign(a,s); \
printf("POST dma_memalign (ptr = %p)\n", ptr); \
print_malloc_map(); \
ptr; \
})
#else
void free(void *ptr); void free(void *ptr);
void *malloc(size_t size); void *malloc(size_t size);
void *calloc(size_t nmemb, size_t size); void *calloc(size_t nmemb, size_t size);
@ -150,7 +52,95 @@ void *realloc(void *ptr, size_t size);
void *memalign(size_t align, size_t size); void *memalign(size_t align, size_t size);
void *dma_malloc(size_t size); void *dma_malloc(size_t size);
void *dma_memalign(size_t align, size_t size); void *dma_memalign(size_t align, size_t size);
#if CONFIG(LP_DEBUG_MALLOC) && !defined(IN_MALLOC_C)
#include <stdio.h>
void print_malloc_map(void);
#define free(p) ({ \
void *__p = p; \
printf("free(%p) called from %s:%s:%d...\n", __p, __FILE__, __func__, \
__LINE__);\
printf("PRE free()\n"); \
print_malloc_map(); \
free(__p); \
printf("POST free()\n"); \
print_malloc_map(); \
})
#define malloc(s) ({ \
size_t __s = s; \
void *ptr; \
printf("malloc(%zu) called from %s:%s:%d...\n", __s, __FILE__, \
__func__, __LINE__);\
printf("PRE malloc\n"); \
print_malloc_map(); \
ptr = malloc(__s); \
printf("POST malloc (ptr = %p)\n", ptr); \
print_malloc_map(); \
ptr; \
})
#define calloc(n, s) ({ \
size_t __n = n, __s = s; \
void *ptr; \
printf("calloc(%zu, %zu) called from %s:%s:%d...\n", __n, __s, \
__FILE__, __func__, __LINE__);\
printf("PRE calloc\n"); \
print_malloc_map(); \
ptr = calloc(__n, __s); \
printf("POST calloc (ptr = %p)\n", ptr); \
print_malloc_map(); \
ptr; \
})
#define realloc(p, s) ({ \
void *__p = p; \
size_t __s = s; \
void *ptr; \
printf("realloc(%p, %zu) called from %s:%s:%d...\n", __p, __s, \
__FILE__, __func__, __LINE__);\
printf("PRE realloc\n"); \
print_malloc_map(); \
ptr = realloc(__p, __s); \
printf("POST realloc (ptr = %p)\n", ptr); \
print_malloc_map(); \
ptr; \
})
#define memalign(a, s) ({ \
size_t __a = a, __s = s; \
void *ptr; \
printf("memalign(%zu, %zu) called from %s:%s:%d...\n", __a, __s, \
__FILE__, __func__, __LINE__);\
printf("PRE memalign\n"); \
print_malloc_map(); \
ptr = memalign(__a, __s); \
printf("POST memalign (ptr = %p)\n", ptr); \
print_malloc_map(); \
ptr; \
})
#define dma_malloc(s) ({ \
size_t __s = s; \
void *ptr; \
printf("dma_malloc(%zu) called from %s:%s:%d...\n", __s, __FILE__, \
__func__, __LINE__);\
printf("PRE dma_malloc\n"); \
print_malloc_map(); \
ptr = dma_malloc(__s); \
printf("POST dma_malloc (ptr = %p)\n", ptr); \
print_malloc_map(); \
ptr; \
})
#define dma_memalign(a, s) ({ \
size_t __a = a, __s = s; \
void *ptr; \
printf("dma_memalign(%zu, %zu) called from %s:%s:%d...\n", __a, __s, \
__FILE__, __func__, __LINE__);\
printf("PRE dma_memalign\n"); \
print_malloc_map(); \
ptr = dma_memalign(__a, __s); \
printf("POST dma_memalign (ptr = %p)\n", ptr); \
print_malloc_map(); \
ptr; \
})
#endif #endif
void init_dma_memory(void *start, u32 size); void init_dma_memory(void *start, u32 size);
int dma_initialized(void); int dma_initialized(void);
int dma_coherent(void *ptr); int dma_coherent(void *ptr);

View File

@ -480,7 +480,7 @@ look_further:
if ((reg->alignment == align) && (reg->free >= (size + align - 1)/align)) if ((reg->alignment == align) && (reg->free >= (size + align - 1)/align))
{ {
#if CONFIG(LP_DEBUG_MALLOC) #if CONFIG(LP_DEBUG_MALLOC)
printf(" found memalign region. %x free, %x required\n", reg->free, (size + align - 1)/align); printf(" found memalign region. %u free, %zu required\n", reg->free, (size + align - 1)/align);
#endif #endif
break; break;
} }
@ -563,7 +563,7 @@ again:
/* FIXME: Verify the size of the block. */ /* FIXME: Verify the size of the block. */
printf("%s %x: %s (%x bytes)\n", type->name, printf("%s %x: %s (%llx bytes)\n", type->name,
(unsigned int)(ptr - type->start), (unsigned int)(ptr - type->start),
hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr)); hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr));
@ -575,7 +575,7 @@ again:
if (free_memory && (type->minimal_free > free_memory)) if (free_memory && (type->minimal_free > free_memory))
type->minimal_free = free_memory; type->minimal_free = free_memory;
printf("%s: Maximum memory consumption: %u bytes\n", type->name, printf("%s: Maximum memory consumption: %zu bytes\n", type->name,
(type->end - type->start) - HDRSIZE - type->minimal_free); (type->end - type->start) - HDRSIZE - type->minimal_free);
if (type != dma) { if (type != dma) {