From b91e47b5185ba70fff0151c34ad8923564a07d81 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Thu, 7 Jul 2016 13:35:13 +0200 Subject: [PATCH] Implement portable and overridable allocation of memory buffers Fixes bug #48300 (Private mempools allocate foreign memory), bug #48354 (Portable alignment defines/include required for static allocation) and bug #47092 (Tag memory buffers like memp_memory_xxx and ram_heap with a macro so that attributes can be attached to their definitions) Signed-off-by: Simon Goldschmidt --- src/core/ipv4/ip_frag.c | 2 +- src/core/mem.c | 2 +- src/include/lwip/arch.h | 14 ++++++++++++++ src/include/lwip/memp.h | 4 ++-- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/core/ipv4/ip_frag.c b/src/core/ipv4/ip_frag.c index a6474335..bb0599e6 100644 --- a/src/core/ipv4/ip_frag.c +++ b/src/core/ipv4/ip_frag.c @@ -640,7 +640,7 @@ nullreturn: #if IP_FRAG #if IP_FRAG_USES_STATIC_BUF -static u8_t buf[LWIP_MEM_ALIGN_SIZE(IP_FRAG_MAX_MTU + MEM_ALIGNMENT - 1)]; +static LWIP_DECLARE_MEMORY_ALIGNED(buf, IP_FRAG_MAX_MTU); #else /* IP_FRAG_USES_STATIC_BUF */ #if !LWIP_NETIF_TX_SINGLE_PBUF diff --git a/src/core/mem.c b/src/core/mem.c index 9ca9e3c4..d3d97e35 100644 --- a/src/core/mem.c +++ b/src/core/mem.c @@ -202,7 +202,7 @@ struct mem { * how that space is calculated). */ #ifndef LWIP_RAM_HEAP_POINTER /** the heap. we need one struct mem at the end and some room for alignment */ -u8_t ram_heap[MEM_SIZE_ALIGNED + (2U*SIZEOF_STRUCT_MEM) + MEM_ALIGNMENT]; +LWIP_DECLARE_MEMORY_ALIGNED(ram_heap, MEM_SIZE_ALIGNED + (2U*SIZEOF_STRUCT_MEM)); #define LWIP_RAM_HEAP_POINTER ram_heap #endif /* LWIP_RAM_HEAP_POINTER */ diff --git a/src/include/lwip/arch.h b/src/include/lwip/arch.h index 41b123f6..ca708b3c 100644 --- a/src/include/lwip/arch.h +++ b/src/include/lwip/arch.h @@ -110,6 +110,20 @@ typedef uintptr_t mem_ptr_t; #endif #endif +/** Allocates a memory buffer of specified size that is of sufficient size to align + * its start address using LWIP_MEM_ALIGN. + * You can declare your own version here e.g. to enforce alignment without adding + * trailing padding bytes (see LWIP_MEM_ALIGN_BUFFER) or your own section placement + * requirements. + * e.g. if you use gcc and need 32 bit alignment: + * #define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u8_t variable_name[size] __attribute__((aligned(4))) + * or more portable: + * #define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u32_t variable_name[(size + sizeof(u32_t) - 1) / sizeof(u32_t)] + */ +#ifndef LWIP_DECLARE_MEMORY_ALIGNED +#define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u8_t variable_name[LWIP_MEM_ALIGN_BUFFER(size)] +#endif + #ifdef __cplusplus extern "C" { #endif diff --git a/src/include/lwip/memp.h b/src/include/lwip/memp.h index d6c29657..285d3b8b 100644 --- a/src/include/lwip/memp.h +++ b/src/include/lwip/memp.h @@ -90,8 +90,8 @@ extern const struct memp_desc* const memp_pools[MEMP_MAX]; #else /* MEMP_MEM_MALLOC */ -#define LWIP_MEMPOOL_DECLARE(name,num,size,desc) u8_t memp_memory_ ## name ## _base \ - [((num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size))) + (MEM_ALIGNMENT-1)]; \ +#define LWIP_MEMPOOL_DECLARE(name,num,size,desc) \ + LWIP_DECLARE_MEMORY_ALIGNED(memp_memory_ ## name ## _base, ((num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size)))); \ \ static struct memp *memp_tab_ ## name; \ \