From e0c8475561a099006a15f07d2be3eae8a6bf764a Mon Sep 17 00:00:00 2001 From: Intel Date: Mon, 3 Jun 2013 00:00:00 +0000 Subject: [PATCH] mem: find most little element from heap Allocations now return the most little element from heap matching size constraint instead of the first one available. Signed-off-by: Intel --- app/test/test_malloc.c | 20 +++++++++++--------- lib/librte_malloc/malloc_heap.c | 22 +++++++++++++++++----- lib/librte_malloc/rte_malloc.c | 2 +- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/app/test/test_malloc.c b/app/test/test_malloc.c index 819384ae6a..213d9dcde4 100644 --- a/app/test/test_malloc.c +++ b/app/test/test_malloc.c @@ -393,8 +393,15 @@ test_multi_alloc_statistics(void) void *p3 = rte_malloc_socket("add2", size,align, socket); if (!p3) return -1; + rte_malloc_get_socket_stats(socket,&second_stats); + rte_free(p2); + rte_free(p3); + + /* After freeing both allocations check stats return to original */ + rte_malloc_get_socket_stats(socket, &post_stats); + /* * Check that no new blocks added after small allocations * i.e. < RTE_MALLOC_MEMZONE_SIZE @@ -420,10 +427,8 @@ test_multi_alloc_statistics(void) return -1; } - /* 2 Free blocks smaller 11M, larger 11M + (11M - 2048) */ - if (second_stats.greatest_free_size != - (rte_str_to_size(MALLOC_MEMZONE_SIZE) * 2) - - 2048 - trailer_size) { + /* Make sure that we didn't touch our greatest chunk: 2 * 11M) */ + if (second_stats.greatest_free_size != pre_stats.greatest_free_size) { printf("Incorrect heap statistics: Greatest free size \n"); return -1; } @@ -432,10 +437,7 @@ test_multi_alloc_statistics(void) printf("Incorrect heap statistics: Free size \n"); return -1; } - rte_free(p2); - rte_free(p3); - /* After freeing both allocations check stats return to original */ - rte_malloc_get_socket_stats(socket, &post_stats); + if ((post_stats.heap_totalsz_bytes != pre_stats.heap_totalsz_bytes) && (post_stats.heap_freesz_bytes!=pre_stats.heap_freesz_bytes) && (post_stats.heap_allocsz_bytes!=pre_stats.heap_allocsz_bytes)&& @@ -450,7 +452,7 @@ test_multi_alloc_statistics(void) static int test_memzone_size_alloc(void) { - void *p1 = rte_malloc("BIG", rte_str_to_size(MALLOC_MEMZONE_SIZE) - 128, 64); + void *p1 = rte_malloc("BIG", (size_t)(rte_str_to_size(MALLOC_MEMZONE_SIZE) - 128), 64); if (!p1) return -1; rte_free(p1); diff --git a/lib/librte_malloc/malloc_heap.c b/lib/librte_malloc/malloc_heap.c index b6d83a4046..3ed502534f 100644 --- a/lib/librte_malloc/malloc_heap.c +++ b/lib/librte_malloc/malloc_heap.c @@ -153,15 +153,27 @@ static struct malloc_elem * find_suitable_element(struct malloc_heap *heap, size_t size, unsigned align, struct malloc_elem **prev) { - struct malloc_elem *elem = heap->free_head; + struct malloc_elem *elem, *min_elem, *min_prev; + size_t min_sz; + + elem = heap->free_head; + min_elem = NULL; + min_prev = NULL; + min_sz = (size_t) SIZE_MAX; *prev = NULL; + while(elem){ - if (malloc_elem_can_hold(elem, size, align)) - break; - *prev = elem; + if (malloc_elem_can_hold(elem, size, align)) { + if (min_sz > elem->size) { + min_elem = elem; + *prev = min_prev; + min_sz = elem->size; + } + } + min_prev = elem; elem = elem->next_free; } - return elem; + return (min_elem); } /* diff --git a/lib/librte_malloc/rte_malloc.c b/lib/librte_malloc/rte_malloc.c index 03db51ff6a..c0ebc4fc65 100644 --- a/lib/librte_malloc/rte_malloc.c +++ b/lib/librte_malloc/rte_malloc.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -54,7 +55,6 @@ #include #include "malloc_elem.h" #include "malloc_heap.h" -#include "malloc_heap.c" /* Free the memory space back to heap */ -- 2.20.1