[LEDE-DEV] [PATCH] libubox: Fix calloc_a() to return mem aligned pointers
Ted Hess
thess at kitschensync.net
Thu Feb 23 13:20:37 PST 2017
The current implementation of calloc_a() returns packed pointers for the extra
arguments. These packed, unaligned, pointers are OK for a lot of architectures,
but not all. This patch will aligned the pointers returned in a manner congruent
with malloc(). I do not believe the extra padding overhead is all the burdensome
considering the overhead of separate malloc/calloc/free call to accomplish the
same thing.
Signed-off-by: Ted Hess <thess at kitschensync.net>
---
utils.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/utils.c b/utils.c
index 5d9d5aa..314f716 100644
--- a/utils.c
+++ b/utils.c
@@ -27,6 +27,9 @@
_addr; \
_addr = va_arg(_arg, void **), _len = _addr ? va_arg(_arg,
size_t) : 0)
+#define C_PTR_ALIGN (2*sizeof(size_t))
+#define C_PTR_MASK (-C_PTR_ALIGN)
+
void *__calloc_a(size_t len, ...)
{
va_list ap, ap1;
@@ -40,7 +43,7 @@ void *__calloc_a(size_t len, ...)
va_copy(ap1, ap);
foreach_arg(ap1, cur_addr, cur_len, &ret, len)
- alloc_len += cur_len;
+ alloc_len += (cur_len + C_PTR_ALIGN - 1 ) & C_PTR_MASK;
va_end(ap1);
ptr = calloc(1, alloc_len);
@@ -49,7 +52,7 @@ void *__calloc_a(size_t len, ...)
alloc_len = 0;
foreach_arg(ap, cur_addr, cur_len, &ret, len) {
*cur_addr = &ptr[alloc_len];
- alloc_len += cur_len;
+ alloc_len += (cur_len + C_PTR_ALIGN - 1) & C_PTR_MASK;
}
va_end(ap);
--
2.7.4
More information about the Lede-dev
mailing list