Fixed bug #28679: mem_realloc calculates mem_stats wrong and added test case for it

This commit is contained in:
goldsimon 2010-01-18 17:45:41 +00:00
parent 149f21dc58
commit e0c5c4de88
5 changed files with 84 additions and 2 deletions

View File

@ -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

View 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 {

69
test/unit/core/test_mem.c Normal file
View File

@ -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);
}

View File

@ -0,0 +1,8 @@
#ifndef __TEST_MEM_H__
#define __TEST_MEM_H__
#include "../lwip_check.h"
Suite *mem_suite(void);
#endif

View 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);