-----Original Message----- From: Yousong Zhou
Sent: Thursday, February 23, 2017 7:15 PM
To: Ted Hess
Cc: lede-dev
Subject: Re: [LEDE-DEV] [PATCH] libubox: Fix calloc_a() to return mem aligned 
pointers

On 24 February 2017 at 05:20, Ted Hess <th...@kitschensync.net> wrote:
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 <th...@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)
+

sizeof(long) should be used for C_PTR_ALIGN, though I cannot find the
quote at the moment...

               yousong

I picked the expression from malloc in the musl sources. No hard preferences, but it does do proper alignment for 64-bit systems and other sensitive data-types AFAICT.

/ted


_______________________________________________
Lede-dev mailing list
Lede-dev@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/lede-dev

Reply via email to