You-Cheng Syu has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/31562
Change subject: libpayload: Fix compile error with DEBUG_MALLOC ......................................................................
libpayload: Fix compile error with DEBUG_MALLOC
DEBUG_MALLOC is an option for debugging memory allocator issues. However, we would encounter a lot of compile error if we turn it on in environments with strict compile options (e.g., ChromeOS) or different architecture (e.g., ARM64).
This CL: 1. Eliminate redundant redeclaration errors in memory allocation macros. 2. Include 'stdio.h' for printing debug messages. 3. Move the declaration of 'print_malloc_map' from 'malloc.c' to 'stdlib.h' for memory allocation macros. 4. Cast arguments to unsigned int when printing debug messages to eliminate printf argument type errors.
BUG=b:125576961 TEST='emerge-kukui libpayload' succeeds with and without DEBUG_MALLOC
Change-Id: I5927f941f386d4920a5d41528b1471e5af294fcb Signed-off-by: You-Cheng Syu youcheng@google.com --- M payloads/libpayload/include/stdlib.h M payloads/libpayload/libc/malloc.c 2 files changed, 38 insertions(+), 42 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/62/31562/1
diff --git a/payloads/libpayload/include/stdlib.h b/payloads/libpayload/include/stdlib.h index a2e612d..682cbf4 100644 --- a/payloads/libpayload/include/stdlib.h +++ b/payloads/libpayload/include/stdlib.h @@ -33,6 +33,9 @@
#include <die.h> #include <stddef.h> +#if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC) && !defined(IN_MALLOC_C) +#include <stdio.h> +#endif #include <string.h>
#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1UL) @@ -45,13 +48,19 @@ * @defgroup malloc Memory allocation functions * @{ */ +void free(void *ptr); +void *malloc(size_t size); +void *calloc(size_t nmemb, size_t size); +void *realloc(void *ptr, size_t size); +void *memalign(size_t align, size_t size); +void *dma_malloc(size_t size); +void *dma_memalign(size_t align, size_t size); +void print_malloc_map(void); #if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC) && !defined(IN_MALLOC_C) -#define free(p) \ +#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("free(%p) called from %s:%s:%d...\n", \ + p, __FILE__, __func__, __LINE__); \ printf("PRE free()\n"); \ print_malloc_map(); \ free(p); \ @@ -60,11 +69,9 @@ }) #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("malloc(%u) called from %s:%s:%d...\n", \ + (unsigned int)(s), __FILE__, __func__, __LINE__); \ printf("PRE malloc\n"); \ print_malloc_map(); \ ptr = malloc(s); \ @@ -74,11 +81,10 @@ }) #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("calloc(%u, %u) called from %s:%s:%d...\n", \ + (unsigned int)(n), (unsigned int)(s), \ + __FILE__, __func__, __LINE__); \ printf("PRE calloc\n"); \ print_malloc_map(); \ ptr = calloc(n,s); \ @@ -88,11 +94,9 @@ }) #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("realloc(%p, %u) called from %s:%s:%d...\n", \ + p, (unsigned int)(s), __FILE__, __func__, __LINE__); \ printf("PRE realloc\n"); \ print_malloc_map(); \ ptr = realloc(p,s); \ @@ -102,11 +106,10 @@ }) #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("memalign(%u, %u) called from %s:%s:%d...\n", \ + (unsigned int)(a), (unsigned int)(s), \ + __FILE__, __func__, __LINE__); \ printf("PRE memalign\n"); \ print_malloc_map(); \ ptr = memalign(a,s); \ @@ -116,11 +119,9 @@ }) #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("dma_malloc(%u) called from %s:%s:%d...\n", \ + (unsigned int)(s), __FILE__, __func__, __LINE__); \ printf("PRE dma_malloc\n"); \ print_malloc_map(); \ ptr = dma_malloc(s); \ @@ -130,11 +131,10 @@ }) #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("dma_memalign(%u, %u) called from %s:%s:%d...\n", \ + (unsigned int)(a), (unsigned int)(s), \ + __FILE__, __func__, __LINE__); \ printf("PRE dma_memalign\n"); \ print_malloc_map(); \ ptr = dma_memalign(a,s); \ @@ -142,14 +142,6 @@ print_malloc_map(); \ ptr; \ }) -#else -void free(void *ptr); -void *malloc(size_t size); -void *calloc(size_t nmemb, size_t size); -void *realloc(void *ptr, size_t size); -void *memalign(size_t align, size_t size); -void *dma_malloc(size_t size); -void *dma_memalign(size_t align, size_t size); #endif void init_dma_memory(void *start, u32 size); int dma_initialized(void); diff --git a/payloads/libpayload/libc/malloc.c b/payloads/libpayload/libc/malloc.c index 595af63..c4a39b4 100644 --- a/payloads/libpayload/libc/malloc.c +++ b/payloads/libpayload/libc/malloc.c @@ -84,7 +84,6 @@ #define HAS_MAGIC(_h) (((_h) & MAGIC) == MAGIC)
static int free_aligned(void* addr, struct memory_type *type); -void print_malloc_map(void);
void init_dma_memory(void *start, u32 size) { @@ -480,7 +479,10 @@ if ((reg->alignment == align) && (reg->free >= (size + align - 1)/align)) { #if IS_ENABLED(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, %u required\n", + (unsigned int)reg->free, + (unsigned int)((size + align - 1)/align)); #endif break; } @@ -563,9 +565,10 @@
/* FIXME: Verify the size of the block. */
- printf("%s %x: %s (%x bytes)\n", type->name, + printf("%s 0x%x: %s (%u bytes)\n", type->name, (unsigned int)(ptr - type->start), - hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr)); + hdr & FLAG_FREE ? "FREE" : "USED", + (unsigned int)SIZE(hdr));
if (hdr & FLAG_FREE) free_memory += SIZE(hdr); @@ -576,7 +579,8 @@ if (free_memory && (type->minimal_free > free_memory)) type->minimal_free = free_memory; printf("%s: Maximum memory consumption: %u bytes\n", type->name, - (type->end - type->start) - HDRSIZE - type->minimal_free); + (unsigned int)(type->end - type->start - HDRSIZE - + type->minimal_free));
if (type != dma) { type = dma;