From: Intel Date: Wed, 19 Dec 2012 23:00:00 +0000 (+0100) Subject: lib: new alignment macros X-Git-Tag: spdx-start~11411 X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=70dce764786add35b88ca2ca0fc16a13a1223ca8;p=dpdk.git lib: new alignment macros Signed-off-by: Intel --- diff --git a/app/test/test_malloc.c b/app/test/test_malloc.c index 8c5133b225..365cefb54d 100644 --- a/app/test/test_malloc.c +++ b/app/test/test_malloc.c @@ -436,7 +436,7 @@ test_realloc(void) return -1; } /* calc an alignment we don't already have */ - while(RTE_ALIGN(ptr7, new_align) == ptr7) + while(RTE_PTR_ALIGN(ptr7, new_align) == ptr7) new_align *= 2; char *ptr8 = rte_realloc(ptr7, size7, new_align); if (!ptr8){ @@ -444,7 +444,7 @@ test_realloc(void) rte_free(ptr7); return -1; } - if (RTE_ALIGN(ptr8, new_align) != ptr8){ + if (RTE_PTR_ALIGN(ptr8, new_align) != ptr8){ printf("Failure to re-align data\n"); rte_free(ptr8); return -1; @@ -515,13 +515,13 @@ test_random_alloc_free(void *_ __attribute__((unused))) size_t allocated_size; while (!free_mem){ const unsigned mem_size = sizeof(struct mem_list) + \ - rte_rand() % (64 * 1024); + rte_rand() % (64 * 1024); const unsigned align = 1 << (rte_rand() % 12); /* up to 4k alignment */ struct mem_list *entry = rte_malloc(NULL, mem_size, align); if (entry == NULL) return -1; - if (RTE_ALIGN(entry, align)!= entry) + if (RTE_PTR_ALIGN(entry, align)!= entry) return -1; if (rte_malloc_validate(entry, &allocated_size) == -1 || allocated_size < mem_size) diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/include/rte_common.h index 639b79b495..42152d580f 100644 --- a/lib/librte_eal/common/include/rte_common.h +++ b/lib/librte_eal/common/include/rte_common.h @@ -51,6 +51,7 @@ extern "C" { #include #include + /*********** Macros to eliminate unused variable warnings ********/ /** @@ -111,26 +112,53 @@ rte_align_floor_int(uintptr_t ptr, uintptr_t align) * point to an address no higher than the first parameter. Second parameter * must be a power-of-two value. */ -#define RTE_ALIGN_FLOOR(ptr, align) \ +#define RTE_PTR_ALIGN_FLOOR(ptr, align) \ (typeof(ptr))rte_align_floor_int((uintptr_t)ptr, align) +/** + * Macro to align a value to a given power-of-two. The resultant value + * will be of the same type as the first parameter, and will be no + * bigger than the first parameter. Second parameter must be a + * power-of-two value. + */ +#define RTE_ALIGN_FLOOR(val, align) \ + (typeof(val))(val & (~((typeof(val))(align - 1)))) + /** * Macro to align a pointer to a given power-of-two. The resultant * pointer will be a pointer of the same type as the first parameter, and * point to an address no lower than the first parameter. Second parameter * must be a power-of-two value. */ -#define RTE_ALIGN_CEIL(ptr, align) \ - RTE_ALIGN_FLOOR(RTE_PTR_ADD(ptr, align - 1), align) +#define RTE_PTR_ALIGN_CEIL(ptr, align) \ + RTE_PTR_ALIGN_FLOOR(RTE_PTR_ADD(ptr, align - 1), align) + +/** + * Macro to align a value to a given power-of-two. The resultant value + * will be of the same type as the first parameter, and will be no lower + * than the first parameter. Second parameter must be a power-of-two + * value. + */ +#define RTE_ALIGN_CEIL(val, align) \ + RTE_ALIGN_FLOOR((val + ((typeof(val)) align - 1)), align) /** * Macro to align a pointer to a given power-of-two. The resultant * pointer will be a pointer of the same type as the first parameter, and * point to an address no lower than the first parameter. Second parameter * must be a power-of-two value. + * This function is the same as RTE_PTR_ALIGN_CEIL + */ +#define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align) + +/** + * Macro to align a value to a given power-of-two. The resultant + * value will be of the same type as the first parameter, and + * will be no lower than the first parameter. Second parameter + * must be a power-of-two value. * This function is the same as RTE_ALIGN_CEIL */ -#define RTE_ALIGN(ptr, align) RTE_ALIGN_CEIL(ptr, align) +#define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align) /** * Checks if a pointer is aligned to a given power-of-two value @@ -146,7 +174,7 @@ rte_align_floor_int(uintptr_t ptr, uintptr_t align) static inline int rte_is_aligned(void *ptr, unsigned align) { - return RTE_ALIGN(ptr, align) == ptr; + return RTE_PTR_ALIGN(ptr, align) == ptr; } /*********** Macros for compile type checks ********/ @@ -291,11 +319,11 @@ rte_str_to_size(const char *str) * This function never returns * * @param exit_code - * The exit code to be returned by the application + * The exit code to be returned by the application * @param format - * The format string to be used for printing the message. This can include - * printf format characters which will be expanded using any further parameters - * to the function. + * The format string to be used for printing the message. This can include + * printf format characters which will be expanded using any further parameters + * to the function. */ void rte_exit(int exit_code, const char *format, ...) diff --git a/lib/librte_malloc/malloc_elem.c b/lib/librte_malloc/malloc_elem.c index 18433413da..0d2fdb6bea 100644 --- a/lib/librte_malloc/malloc_elem.c +++ b/lib/librte_malloc/malloc_elem.c @@ -264,7 +264,7 @@ malloc_elem_resize(struct malloc_elem *elem, size_t size) if (elem->size - new_size > MIN_DATA_SIZE + MALLOC_ELEM_OVERHEAD){ /* now we have a big block together. Lets cut it down a bit, by splitting */ struct malloc_elem *split_pt = RTE_PTR_ADD(elem, new_size); - split_pt = RTE_ALIGN_CEIL(split_pt, CACHE_LINE_SIZE); + split_pt = RTE_PTR_ALIGN_CEIL(split_pt, CACHE_LINE_SIZE); split_elem(elem, split_pt); split_pt->state = ELEM_FREE; split_pt->next_free = elem->heap->free_head; diff --git a/lib/librte_malloc/malloc_heap.c b/lib/librte_malloc/malloc_heap.c index 4f2e6d9102..9c75d26453 100644 --- a/lib/librte_malloc/malloc_heap.c +++ b/lib/librte_malloc/malloc_heap.c @@ -53,11 +53,9 @@ #include "malloc_elem.h" #include "malloc_heap.h" -#define QUOTE_(x) #x -#define QUOTE(x) QUOTE_(x) /* since the memzone size starts with a digit, it will appear unquoted in * rte_config.h, so quote it so it can be passed to rte_str_to_size */ -#define MALLOC_MEMZONE_SIZE QUOTE(RTE_MALLOC_MEMZONE_SIZE) +#define MALLOC_MEMZONE_SIZE RTE_STR(RTE_MALLOC_MEMZONE_SIZE) /* * returns the configuration setting for the memzone size as a size_t value @@ -96,7 +94,7 @@ malloc_heap_add_memzone(struct malloc_heap *heap, size_t size, unsigned align) struct malloc_elem *start_elem = (struct malloc_elem *)mz->addr; struct malloc_elem *end_elem = RTE_PTR_ADD(mz->addr, mz_size - MALLOC_ELEM_OVERHEAD); - end_elem = RTE_ALIGN_FLOOR(end_elem, CACHE_LINE_SIZE); + end_elem = RTE_PTR_ALIGN_FLOOR(end_elem, CACHE_LINE_SIZE); const unsigned elem_size = (uintptr_t)end_elem - (uintptr_t)start_elem; malloc_elem_init(start_elem, heap, elem_size); diff --git a/lib/librte_malloc/rte_malloc.c b/lib/librte_malloc/rte_malloc.c index f3a0b7760c..607da3c98d 100644 --- a/lib/librte_malloc/rte_malloc.c +++ b/lib/librte_malloc/rte_malloc.c @@ -118,7 +118,7 @@ rte_realloc(void *ptr, size_t size, unsigned align) size = CACHE_LINE_ROUNDUP(size), align = CACHE_LINE_ROUNDUP(align); /* check alignment matches first, and if ok, see if we can resize block */ - if (RTE_ALIGN(ptr,align) == ptr && + if (RTE_PTR_ALIGN(ptr,align) == ptr && malloc_elem_resize(elem, size) == 0) return ptr;