lwip-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-devel] Purpose of memory alignment check


From: Boštjan Meglič
Subject: Re: [lwip-devel] Purpose of memory alignment check
Date: Tue, 13 Mar 2012 13:27:16 +0100
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2

Hi Simon,

thank you for the explanation.

On our architecture sizeof(void *) returns 2; that is 2 words. MEM_ALIGNMENT is set to 2.

So, shouldn't the number 4 be replaced with MEM_ALIGNMENT?
#define IS_SOCK_ADDR_ALIGNED(name) ((((mem_ptr_t)(name)) % MEM_ALIGNMENT) == 0)

Bostjan


On 13.3.2012 13:01, Simon Goldschmidt wrote:
"Boštjan Meglič"<address@hidden>  wrote:
In the lwip code src/api/sockets.c, line 117, what is the use of the
following macro? Why should the name be aligned?
#define IS_SOCK_ADDR_ALIGNED(name)      ((((mem_ptr_t)(name)) % 4) == 0)

Because 'struct sockaddr' is only used as a 'base class' for the socket API: 
you'll never instantiate a 'struct sockaddr': instead, you'll declare your 
address variables as 'struct sockaddr_in' (or 'struct sockaddr_in6' ofr IPv6) 
and cast them to 'struct sockaddr*' when passing to socket functions.

The problem here is that 'struct sockaddr' consists of u8_t members only, while 
'struct sockaddr_in' includes u32_t members. On most platforms, this results in 
'struct sockaddr' having no alignment requirement while 'struct sockaddr_in' 
has an alignment requirement of 4. That's why we check that the address is 
aligned.

There are however some platforms (like Intel, for example) which can access 
unaligned pointers, so this might work for you...

On our system (Texas Instruments DSP 5509) the code fails *sometimes* on
[..]
My question is, could there be a problem later on if I remove the memory
alignment check?

I can't help you there, since I don't know that platform.

Simon



reply via email to

[Prev in Thread] Current Thread [Next in Thread]