Renamed mem_realloc() to mem_trim() to prevent confusion with realloc()

This commit is contained in:
goldsimon 2010-02-19 16:23:46 +00:00
parent 8336796c1a
commit 52970c2459
5 changed files with 21 additions and 19 deletions

View File

@ -131,6 +131,10 @@ HISTORY
++ Bugfixes: ++ Bugfixes:
2010-02-19: Simon Goldschmidt
* mem.c/.h, pbuf.c: Renamed mem_realloc() to mem_trim() to prevent
confusion with realloc()
2010-02-15: Simon Goldschmidt/Stephane Lesage 2010-02-15: Simon Goldschmidt/Stephane Lesage
* netif.c/.h: Link status does not depend on LWIP_NETIF_LINK_CALLBACK * netif.c/.h: Link status does not depend on LWIP_NETIF_LINK_CALLBACK
(fixes bug #28899) (fixes bug #28899)

View File

@ -224,7 +224,7 @@ static volatile u8_t mem_free_count;
* one empty struct mem pointing to another empty struct mem. * one empty struct mem pointing to another empty struct mem.
* *
* @param mem this points to a struct mem which just has been freed * @param mem this points to a struct mem which just has been freed
* @internal this function is only called by mem_free() and mem_realloc() * @internal this function is only called by mem_free() and mem_trim()
* *
* This assumes access to the heap is protected by the calling function * This assumes access to the heap is protected by the calling function
* already. * already.
@ -353,9 +353,7 @@ mem_free(void *rmem)
} }
/** /**
* In contrast to its name, mem_realloc can only shrink memory, not expand it. * Shrink memory returned by mem_malloc().
* Since the only use (for now) is in pbuf_realloc (which also can only shrink),
* this shouldn't be a problem!
* *
* @param rmem pointer to memory allocated by mem_malloc the is to be shrinked * @param rmem pointer to memory allocated by mem_malloc the is to be shrinked
* @param newsize required size after shrinking (needs to be smaller than or * @param newsize required size after shrinking (needs to be smaller than or
@ -365,7 +363,7 @@ mem_free(void *rmem)
* or freed! * or freed!
*/ */
void * void *
mem_realloc(void *rmem, mem_size_t newsize) mem_trim(void *rmem, mem_size_t newsize)
{ {
mem_size_t size; mem_size_t size;
mem_size_t ptr, ptr2; mem_size_t ptr, ptr2;
@ -386,12 +384,12 @@ mem_realloc(void *rmem, mem_size_t newsize)
return NULL; return NULL;
} }
LWIP_ASSERT("mem_realloc: legal memory", (u8_t *)rmem >= (u8_t *)ram && LWIP_ASSERT("mem_trim: legal memory", (u8_t *)rmem >= (u8_t *)ram &&
(u8_t *)rmem < (u8_t *)ram_end); (u8_t *)rmem < (u8_t *)ram_end);
if ((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end) { if ((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end) {
SYS_ARCH_DECL_PROTECT(lev); SYS_ARCH_DECL_PROTECT(lev);
LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("mem_realloc: illegal memory\n")); LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("mem_trim: illegal memory\n"));
/* protect mem stats from concurrent access */ /* protect mem stats from concurrent access */
SYS_ARCH_PROTECT(lev); SYS_ARCH_PROTECT(lev);
MEM_STATS_INC(illegal); MEM_STATS_INC(illegal);
@ -404,7 +402,7 @@ mem_realloc(void *rmem, mem_size_t newsize)
ptr = (mem_size_t)((u8_t *)mem - ram); ptr = (mem_size_t)((u8_t *)mem - ram);
size = mem->next - ptr - SIZEOF_STRUCT_MEM; size = mem->next - ptr - SIZEOF_STRUCT_MEM;
LWIP_ASSERT("mem_realloc can only shrink memory", newsize <= size); LWIP_ASSERT("mem_trim can only shrink memory", newsize <= size);
if (newsize > size) { if (newsize > size) {
/* not supported */ /* not supported */
return NULL; return NULL;

View File

@ -383,8 +383,8 @@ pbuf_realloc(struct pbuf *p, u16_t new_len)
/* (other types merely adjust their length fields */ /* (other types merely adjust their length fields */
if ((q->type == PBUF_RAM) && (rem_len != q->len)) { if ((q->type == PBUF_RAM) && (rem_len != q->len)) {
/* reallocate and adjust the length of the pbuf that will be split */ /* reallocate and adjust the length of the pbuf that will be split */
q = (struct pbuf *)mem_realloc(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len); q = (struct pbuf *)mem_trim(q, (u16_t)((u8_t *)q->payload - (u8_t *)q) + rem_len);
LWIP_ASSERT("mem_realloc give q == NULL", q != NULL); LWIP_ASSERT("mem_trim returned q == NULL", q != NULL);
} }
/* adjust length fields for new last pbuf */ /* adjust length fields for new last pbuf */
q->len = rem_len; q->len = rem_len;

View File

@ -58,10 +58,10 @@ typedef size_t mem_size_t;
#ifndef mem_calloc #ifndef mem_calloc
#define mem_calloc calloc #define mem_calloc calloc
#endif #endif
/* ATTENTION: using realloc is currently not supported since the return value /* Since there is no C library allocation function to shrink memory without
is ignored, so don't define mem_realloc() to realloc()! */ moving it, define this to nothing. */
#ifndef mem_realloc #ifndef mem_trim
#define mem_realloc(mem, size) (mem) #define mem_trim(mem, size) (mem)
#endif #endif
#else /* MEM_LIBC_MALLOC */ #else /* MEM_LIBC_MALLOC */
@ -77,13 +77,13 @@ typedef u16_t mem_size_t;
#if MEM_USE_POOLS #if MEM_USE_POOLS
/** mem_init is not used when using pools instead of a heap */ /** mem_init is not used when using pools instead of a heap */
#define mem_init() #define mem_init()
/** mem_realloc is not used when using pools instead of a heap: /** mem_trim is not used when using pools instead of a heap:
we can't free part of a pool element and don't want to copy the rest */ we can't free part of a pool element and don't want to copy the rest */
#define mem_realloc(mem, size) (mem) #define mem_trim(mem, size) (mem)
#else /* MEM_USE_POOLS */ #else /* MEM_USE_POOLS */
/* lwIP alternative malloc */ /* lwIP alternative malloc */
void mem_init(void); void mem_init(void);
void *mem_realloc(void *mem, mem_size_t size); void *mem_trim(void *mem, mem_size_t size);
#endif /* MEM_USE_POOLS */ #endif /* MEM_USE_POOLS */
void *mem_malloc(mem_size_t size); void *mem_malloc(mem_size_t size);
void *mem_calloc(mem_size_t count, mem_size_t size); void *mem_calloc(mem_size_t count, mem_size_t size);

View File

@ -25,7 +25,7 @@ mem_teardown(void)
/* Test functions */ /* Test functions */
/** Call mem_malloc, mem_free and mem_realloc and check stats */ /** Call mem_malloc, mem_free and mem_trim and check stats */
START_TEST(test_mem_one) START_TEST(test_mem_one)
{ {
#define SIZE1 16 #define SIZE1 16
@ -51,7 +51,7 @@ START_TEST(test_mem_one)
fail_unless(lwip_stats.mem.used >= SIZE2 + s1); fail_unless(lwip_stats.mem.used >= SIZE2 + s1);
s2 = lwip_stats.mem.used; s2 = lwip_stats.mem.used;
mem_realloc(p1, SIZE1_2); mem_trim(p1, SIZE1_2);
mem_free(p2); mem_free(p2);
fail_unless(lwip_stats.mem.used <= s2 - SIZE2); fail_unless(lwip_stats.mem.used <= s2 - SIZE2);