From e0c5c4de881f544f56371f4c457f28170b0e44cd Mon Sep 17 00:00:00 2001 From: goldsimon Date: Mon, 18 Jan 2010 17:45:41 +0000 Subject: [PATCH] Fixed bug #28679: mem_realloc calculates mem_stats wrong and added test case for it --- CHANGELOG | 3 ++ src/core/mem.c | 4 +-- test/unit/core/test_mem.c | 69 ++++++++++++++++++++++++++++++++++++++ test/unit/core/test_mem.h | 8 +++++ test/unit/lwip_unittests.c | 2 ++ 5 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 test/unit/core/test_mem.c create mode 100644 test/unit/core/test_mem.h diff --git a/CHANGELOG b/CHANGELOG index fdf8fb0d..4e786c8c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -50,6 +50,9 @@ HISTORY ++ Bugfixes: + 2010-01-18: Simon Goldschmidt + * mem.c: Fixed bug #28679: mem_realloc calculates mem_stats wrong + 2010-01-17: Simon Goldschmidt * api_lib.c, api_msg.c, (api_msg.h, api.h, sockets.c, tcpip.c): task #10102: "netconn: clean up conn->err threading issues" by adding diff --git a/src/core/mem.c b/src/core/mem.c index 093f200d..39ed926f 100644 --- a/src/core/mem.c +++ b/src/core/mem.c @@ -414,8 +414,6 @@ mem_realloc(void *rmem, mem_size_t newsize) /* protect the heap from concurrent access */ LWIP_MEM_FREE_PROTECT(); - MEM_STATS_DEC_USED(used, (size - newsize)); - mem2 = (struct mem *)&ram[mem->next]; if(mem2->used == 0) { /* The next struct is unused, we can simply move it at little */ @@ -441,6 +439,7 @@ mem_realloc(void *rmem, mem_size_t newsize) if (mem2->next != MEM_SIZE_ALIGNED) { ((struct mem *)&ram[mem2->next])->prev = ptr2; } + MEM_STATS_DEC_USED(used, (size - newsize)); /* no need to plug holes, we've already done that */ } else if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED <= size) { /* Next struct is used but there's room for another struct mem with @@ -462,6 +461,7 @@ mem_realloc(void *rmem, mem_size_t newsize) if (mem2->next != MEM_SIZE_ALIGNED) { ((struct mem *)&ram[mem2->next])->prev = ptr2; } + MEM_STATS_DEC_USED(used, (size - newsize)); /* the original mem->next is used, so no need to plug holes! */ } /* else { diff --git a/test/unit/core/test_mem.c b/test/unit/core/test_mem.c new file mode 100644 index 00000000..a032a834 --- /dev/null +++ b/test/unit/core/test_mem.c @@ -0,0 +1,69 @@ +#include "test_mem.h" + +#include "lwip/mem.h" +#include "lwip/stats.h" + +#if !LWIP_STATS || !MEM_STATS +#error "This tests needs MEM-statistics enabled" +#endif +#if LWIP_SNMP || LWIP_DNS +#error "This test needs SNMP and DNS turned off (as they malloc on init)" +#endif + +/* Setups/teardown functions */ + +static void +mem_setup(void) +{ +} + +static void +mem_teardown(void) +{ +} + + +/* Test functions */ + +/** Call mem_malloc, mem_free and mem_realloc and check stats */ +START_TEST(test_mem_one) +{ +#define SIZE1 16 +#define SIZE1_2 12 +#define SIZE2 16 + void *p1, *p2, *p3, *p4, *p5; + mem_size_t s1, s2; + LWIP_UNUSED_ARG(_i); + + fail_unless(lwip_stats.mem.used == 0); + + p1 = mem_malloc(SIZE1); + fail_unless(p1 != NULL); + fail_unless(lwip_stats.mem.used >= SIZE1); + s1 = lwip_stats.mem.used; + + p2 = mem_malloc(SIZE2); + fail_unless(p2 != NULL); + fail_unless(lwip_stats.mem.used >= SIZE2 + s1); + s2 = lwip_stats.mem.used; + + mem_realloc(p1, SIZE1_2); + + mem_free(p2); + fail_unless(lwip_stats.mem.used <= s2 - SIZE2); + + mem_free(p1); + fail_unless(lwip_stats.mem.used == 0); +} +END_TEST + + +/** Create the suite including all tests for this module */ +Suite * +mem_suite(void) +{ + TFun tests[] = { + test_mem_one, + }; + return create_suite("MEM", tests, sizeof(tests)/sizeof(TFun), mem_setup, mem_teardown); +} diff --git a/test/unit/core/test_mem.h b/test/unit/core/test_mem.h new file mode 100644 index 00000000..13803edc --- /dev/null +++ b/test/unit/core/test_mem.h @@ -0,0 +1,8 @@ +#ifndef __TEST_MEM_H__ +#define __TEST_MEM_H__ + +#include "../lwip_check.h" + +Suite *mem_suite(void); + +#endif diff --git a/test/unit/lwip_unittests.c b/test/unit/lwip_unittests.c index 2dbeb6d4..db77e1bc 100644 --- a/test/unit/lwip_unittests.c +++ b/test/unit/lwip_unittests.c @@ -3,6 +3,7 @@ #include "udp/test_udp.h" #include "tcp/test_tcp.h" #include "tcp/test_tcp_oos.h" +#include "core/test_mem.h" #include "lwip/init.h" @@ -16,6 +17,7 @@ int main() udp_suite, tcp_suite, tcp_oos_suite, + mem_suite, }; size_t num = sizeof(suites)/sizeof(void*); LWIP_ASSERT("No suites defined", num > 0); -- 2.39.2