]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/commitdiff
Fixed bug #28679: mem_realloc calculates mem_stats wrong and added test case for it
authorgoldsimon <goldsimon>
Mon, 18 Jan 2010 17:45:41 +0000 (17:45 +0000)
committergoldsimon <goldsimon>
Mon, 18 Jan 2010 17:45:41 +0000 (17:45 +0000)
CHANGELOG
src/core/mem.c
test/unit/core/test_mem.c [new file with mode: 0644]
test/unit/core/test_mem.h [new file with mode: 0644]
test/unit/lwip_unittests.c

index fdf8fb0d3a6a7cd517f206f3f3fdf38ae09c0006..4e786c8cd48b057d1d182b097237cf233e47ad09 100644 (file)
--- 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
index 093f200da6aed890d102c1af0ef1e8d87af6b6d5..39ed926f4efe1e861d7e3d73e2efe46ade9ede3e 100644 (file)
@@ -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 (file)
index 0000000..a032a83
--- /dev/null
@@ -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 (file)
index 0000000..13803ed
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef __TEST_MEM_H__
+#define __TEST_MEM_H__
+
+#include "../lwip_check.h"
+
+Suite *mem_suite(void);
+
+#endif
index 2dbeb6d45a7855858e3e269f194051eb6cf20ae7..db77e1bcca067574006905fe789dd50712b988cb 100644 (file)
@@ -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);