What is the memory alignment of the pointer returned by mem alloc() in mem.c?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

What is the memory alignment of the pointer returned by mem alloc() in mem.c?

yanhc519-2
Hi all.

What is the memory alignment of the pointer returned by mem_alloc() in mem.c?
Since the only argument of mem_alloc() is size, so what alignment will mem_alloc() choose?

In http://man7.org/linux/man-pages/man3/malloc.3.html, it says "The malloc() and calloc() functions return a pointer to the allocated memory, which is suitably aligned for any built-in type."
Since the largest type is double or int64 which are 8 bytes, so I guess malloc() in linux will choose alignment of 8 bytes.

Is this guess also applied to mem_alloc() in LwIP? 
That is, does mem_alloc() in LwIP also choose alignment of 8 bytes?

Thanks!
Best regard,

yan

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: What is the memory alignment of the pointer returned by mem alloc() in mem.c?

yanhc519-2
Could anyone give me some advice?

I recently use mem_alloc() to allocate a structure memory which have a int64 member. However, mem_alloc() returns a memory poniter which is 4 byte aligned which cause a memory unaligned fault.
The code is as follows,

ifp=mem_alloc(sizeof(structure interface));

The structure intrface has a member flags which is int64. The size of structure interface is 112 which is a multiple of 8 while the address returned is 0x720ef324 which is not 8 byte aligned. So when I want to access ifp->flags, an unaligned fault will be triggered.

Could any give some hint?
Thanks!
Best regards,

yan



On 05/25/2019 22:19, [hidden email] wrote:
Hi all.

What is the memory alignment of the pointer returned by mem_alloc() in mem.c?
Since the only argument of mem_alloc() is size, so what alignment will mem_alloc() choose?

In http://man7.org/linux/man-pages/man3/malloc.3.html, it says "The malloc() and calloc() functions return a pointer to the allocated memory, which is suitably aligned for any built-in type."
Since the largest type is double or int64 which are 8 bytes, so I guess malloc() in linux will choose alignment of 8 bytes.

Is this guess also applied to mem_alloc() in LwIP? 
That is, does mem_alloc() in LwIP also choose alignment of 8 bytes?

Thanks!
Best regard,

yan

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: What is the memory alignment of the pointer returned by mem alloc() in mem.c?

Terry Barnaby

In your lwipopts.h what have you set for MEM_ALIGNMENT ?

// MEM_ALIGNMENT: should be set to the alignment of the CPU for which lwIP is compiled.
#define MEM_ALIGNMENT           4

Terry

On 27/05/2019 04:17, [hidden email] wrote:
Could anyone give me some advice?

I recently use mem_alloc() to allocate a structure memory which have a int64 member. However, mem_alloc() returns a memory poniter which is 4 byte aligned which cause a memory unaligned fault.
The code is as follows,

ifp=mem_alloc(sizeof(structure interface));

The structure intrface has a member flags which is int64. The size of structure interface is 112 which is a multiple of 8 while the address returned is 0x720ef324 which is not 8 byte aligned. So when I want to access ifp->flags, an unaligned fault will be triggered.

Could any give some hint?
Thanks!
Best regards,

yan



On 05/25/2019 22:19, [hidden email] wrote:
Hi all.

What is the memory alignment of the pointer returned by mem_alloc() in mem.c?
Since the only argument of mem_alloc() is size, so what alignment will mem_alloc() choose?

In http://man7.org/linux/man-pages/man3/malloc.3.html, it says "The malloc() and calloc() functions return a pointer to the allocated memory, which is suitably aligned for any built-in type."
Since the largest type is double or int64 which are 8 bytes, so I guess malloc() in linux will choose alignment of 8 bytes.

Is this guess also applied to mem_alloc() in LwIP? 
That is, does mem_alloc() in LwIP also choose alignment of 8 bytes?

Thanks!
Best regard,

yan

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: What is the memory alignment of the pointer returned by mem alloc() in mem.c?

yanhc519-2
Hi, Terry. Thanks for your reply.
The memory alignment I set in lwipopts.h is 4 as follows. Since I am using a 32 bit processor.

#define MEM_ALIGNMENT           4



On 05/27/2019 13:16, [hidden email] wrote:

In your lwipopts.h what have you set for MEM_ALIGNMENT ?

// MEM_ALIGNMENT: should be set to the alignment of the CPU for which lwIP is compiled.
#define MEM_ALIGNMENT           4

Terry

On 27/05/2019 04:17, [hidden email] wrote:
Could anyone give me some advice?

I recently use mem_alloc() to allocate a structure memory which have a int64 member. However, mem_alloc() returns a memory poniter which is 4 byte aligned which cause a memory unaligned fault.
The code is as follows,

ifp=mem_alloc(sizeof(structure interface));

The structure intrface has a member flags which is int64. The size of structure interface is 112 which is a multiple of 8 while the address returned is 0x720ef324 which is not 8 byte aligned. So when I want to access ifp->flags, an unaligned fault will be triggered.

Could any give some hint?
Thanks!
Best regards,

yan



On 05/25/2019 22:19, [hidden email] wrote:
Hi all.

What is the memory alignment of the pointer returned by mem_alloc() in mem.c?
Since the only argument of mem_alloc() is size, so what alignment will mem_alloc() choose?

In http://man7.org/linux/man-pages/man3/malloc.3.html, it says "The malloc() and calloc() functions return a pointer to the allocated memory, which is suitably aligned for any built-in type."
Since the largest type is double or int64 which are 8 bytes, so I guess malloc() in linux will choose alignment of 8 bytes.

Is this guess also applied to mem_alloc() in LwIP? 
That is, does mem_alloc() in LwIP also choose alignment of 8 bytes?

Thanks!
Best regard,

yan

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: What is the memory alignment of the pointer returned by mem alloc() in mem.c?

Terry Barnaby

But what is your CPU type ?

Some CPU's can load/save an 8 Byte (64 bit) value (such as uint64_t, double) on a 4 Byte boundary, while some others cannot. I suspect you need to set MEM_ALIGNMENT to 8 for your CPU.

Terry

On 27/05/2019 06:49, [hidden email] wrote:
Hi, Terry. Thanks for your reply.
The memory alignment I set in lwipopts.h is 4 as follows. Since I am using a 32 bit processor.

#define MEM_ALIGNMENT           4



On 05/27/2019 13:16, [hidden email] wrote:

In your lwipopts.h what have you set for MEM_ALIGNMENT ?

// MEM_ALIGNMENT: should be set to the alignment of the CPU for which lwIP is compiled.
#define MEM_ALIGNMENT           4

Terry

On 27/05/2019 04:17, [hidden email] wrote:
Could anyone give me some advice?

I recently use mem_alloc() to allocate a structure memory which have a int64 member. However, mem_alloc() returns a memory poniter which is 4 byte aligned which cause a memory unaligned fault.
The code is as follows,

ifp=mem_alloc(sizeof(structure interface));

The structure intrface has a member flags which is int64. The size of structure interface is 112 which is a multiple of 8 while the address returned is 0x720ef324 which is not 8 byte aligned. So when I want to access ifp->flags, an unaligned fault will be triggered.

Could any give some hint?
Thanks!
Best regards,

yan



On 05/25/2019 22:19, [hidden email] wrote:
Hi all.

What is the memory alignment of the pointer returned by mem_alloc() in mem.c?
Since the only argument of mem_alloc() is size, so what alignment will mem_alloc() choose?

In http://man7.org/linux/man-pages/man3/malloc.3.html, it says "The malloc() and calloc() functions return a pointer to the allocated memory, which is suitably aligned for any built-in type."
Since the largest type is double or int64 which are 8 bytes, so I guess malloc() in linux will choose alignment of 8 bytes.

Is this guess also applied to mem_alloc() in LwIP? 
That is, does mem_alloc() in LwIP also choose alignment of 8 bytes?

Thanks!
Best regard,

yan

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: What is the memory alignment of the pointer returned by mem alloc() in mem.c?

yanhc519-2
Hi Terry.
My cpu is a processor based on sparc v8 architecture. I have checked the sparc v8 manual and leared that the processor uses LDD instruction to load double word (64 bit) and it requires the address is double word (64 bit) aligned.

However, in another application when I use mem_malloc() to allocate memory for structure interface, the returned memory pointer is actually 8 byte aligned.

So, I am wondering whether there is a memory alignment assumption for mem_malloc().
When I set MEM_ALIGNMENT to 4, why sometimes mem_malloc() returns a 4 byte aligned address and sometimes it returns a 8 byte address?

Nevertheless, your suggestion to set MEM_ALIGNMENT to 8 seems to be a good solution to this problem.

yan


On 05/27/2019 16:18, [hidden email] wrote:

But what is your CPU type ?

Some CPU's can load/save an 8 Byte (64 bit) value (such as uint64_t, double) on a 4 Byte boundary, while some others cannot. I suspect you need to set MEM_ALIGNMENT to 8 for your CPU.

Terry

On 27/05/2019 06:49, [hidden email] wrote:
Hi, Terry. Thanks for your reply.
The memory alignment I set in lwipopts.h is 4 as follows. Since I am using a 32 bit processor.

#define MEM_ALIGNMENT           4



On 05/27/2019 13:16, [hidden email] wrote:

In your lwipopts.h what have you set for MEM_ALIGNMENT ?

// MEM_ALIGNMENT: should be set to the alignment of the CPU for which lwIP is compiled.
#define MEM_ALIGNMENT           4

Terry

On 27/05/2019 04:17, [hidden email] wrote:
Could anyone give me some advice?

I recently use mem_alloc() to allocate a structure memory which have a int64 member. However, mem_alloc() returns a memory poniter which is 4 byte aligned which cause a memory unaligned fault.
The code is as follows,

ifp=mem_alloc(sizeof(structure interface));

The structure intrface has a member flags which is int64. The size of structure interface is 112 which is a multiple of 8 while the address returned is 0x720ef324 which is not 8 byte aligned. So when I want to access ifp->flags, an unaligned fault will be triggered.

Could any give some hint?
Thanks!
Best regards,

yan



On 05/25/2019 22:19, [hidden email] wrote:
Hi all.

What is the memory alignment of the pointer returned by mem_alloc() in mem.c?
Since the only argument of mem_alloc() is size, so what alignment will mem_alloc() choose?

In http://man7.org/linux/man-pages/man3/malloc.3.html, it says "The malloc() and calloc() functions return a pointer to the allocated memory, which is suitably aligned for any built-in type."
Since the largest type is double or int64 which are 8 bytes, so I guess malloc() in linux will choose alignment of 8 bytes.

Is this guess also applied to mem_alloc() in LwIP? 
That is, does mem_alloc() in LwIP also choose alignment of 8 bytes?

Thanks!
Best regard,

yan

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: What is the memory alignment of the pointer returned by mem alloc() in mem.c?

Terry Barnaby

"When I set MEM_ALIGNMENT to 4, why sometimes mem_malloc() returns a 4 byte aligned address and sometimes it returns a 8 byte address?" Well when set to 4 Byte alignment, you will likely have a 50/50 chance of the address being 8 Byte aligned (depends on the size of items you are allocating, however it will always be 4 Byte aligned).

Terry

On 27/05/2019 13:55, [hidden email] wrote:
Hi Terry.
My cpu is a processor based on sparc v8 architecture. I have checked the sparc v8 manual and leared that the processor uses LDD instruction to load double word (64 bit) and it requires the address is double word (64 bit) aligned.

However, in another application when I use mem_malloc() to allocate memory for structure interface, the returned memory pointer is actually 8 byte aligned.

So, I am wondering whether there is a memory alignment assumption for mem_malloc().
When I set MEM_ALIGNMENT to 4, why sometimes mem_malloc() returns a 4 byte aligned address and sometimes it returns a 8 byte address?

Nevertheless, your suggestion to set MEM_ALIGNMENT to 8 seems to be a good solution to this problem.

yan


On 05/27/2019 16:18, [hidden email] wrote:

But what is your CPU type ?

Some CPU's can load/save an 8 Byte (64 bit) value (such as uint64_t, double) on a 4 Byte boundary, while some others cannot. I suspect you need to set MEM_ALIGNMENT to 8 for your CPU.

Terry

On 27/05/2019 06:49, [hidden email] wrote:
Hi, Terry. Thanks for your reply.
The memory alignment I set in lwipopts.h is 4 as follows. Since I am using a 32 bit processor.

#define MEM_ALIGNMENT           4



On 05/27/2019 13:16, [hidden email] wrote:

In your lwipopts.h what have you set for MEM_ALIGNMENT ?

// MEM_ALIGNMENT: should be set to the alignment of the CPU for which lwIP is compiled.
#define MEM_ALIGNMENT           4

Terry

On 27/05/2019 04:17, [hidden email] wrote:
Could anyone give me some advice?

I recently use mem_alloc() to allocate a structure memory which have a int64 member. However, mem_alloc() returns a memory poniter which is 4 byte aligned which cause a memory unaligned fault.
The code is as follows,

ifp=mem_alloc(sizeof(structure interface));

The structure intrface has a member flags which is int64. The size of structure interface is 112 which is a multiple of 8 while the address returned is 0x720ef324 which is not 8 byte aligned. So when I want to access ifp->flags, an unaligned fault will be triggered.

Could any give some hint?
Thanks!
Best regards,

yan



On 05/25/2019 22:19, [hidden email] wrote:
Hi all.

What is the memory alignment of the pointer returned by mem_alloc() in mem.c?
Since the only argument of mem_alloc() is size, so what alignment will mem_alloc() choose?

In http://man7.org/linux/man-pages/man3/malloc.3.html, it says "The malloc() and calloc() functions return a pointer to the allocated memory, which is suitably aligned for any built-in type."
Since the largest type is double or int64 which are 8 bytes, so I guess malloc() in linux will choose alignment of 8 bytes.

Is this guess also applied to mem_alloc() in LwIP? 
That is, does mem_alloc() in LwIP also choose alignment of 8 bytes?

Thanks!
Best regard,

yan

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users
Reply | Threaded
Open this post in threaded view
|

Re: What is the memory alignment of the pointer returned by mem alloc() in mem.c?

yanhc519-2
Thanks Terry!

On 05/27/2019 21:04, [hidden email] wrote:

"When I set MEM_ALIGNMENT to 4, why sometimes mem_malloc() returns a 4 byte aligned address and sometimes it returns a 8 byte address?" Well when set to 4 Byte alignment, you will likely have a 50/50 chance of the address being 8 Byte aligned (depends on the size of items you are allocating, however it will always be 4 Byte aligned).

Terry

On 27/05/2019 13:55, [hidden email] wrote:
Hi Terry.
My cpu is a processor based on sparc v8 architecture. I have checked the sparc v8 manual and leared that the processor uses LDD instruction to load double word (64 bit) and it requires the address is double word (64 bit) aligned.

However, in another application when I use mem_malloc() to allocate memory for structure interface, the returned memory pointer is actually 8 byte aligned.

So, I am wondering whether there is a memory alignment assumption for mem_malloc().
When I set MEM_ALIGNMENT to 4, why sometimes mem_malloc() returns a 4 byte aligned address and sometimes it returns a 8 byte address?

Nevertheless, your suggestion to set MEM_ALIGNMENT to 8 seems to be a good solution to this problem.

yan


On 05/27/2019 16:18, [hidden email] wrote:

But what is your CPU type ?

Some CPU's can load/save an 8 Byte (64 bit) value (such as uint64_t, double) on a 4 Byte boundary, while some others cannot. I suspect you need to set MEM_ALIGNMENT to 8 for your CPU.

Terry

On 27/05/2019 06:49, [hidden email] wrote:
Hi, Terry. Thanks for your reply.
The memory alignment I set in lwipopts.h is 4 as follows. Since I am using a 32 bit processor.

#define MEM_ALIGNMENT           4



On 05/27/2019 13:16, [hidden email] wrote:

In your lwipopts.h what have you set for MEM_ALIGNMENT ?

// MEM_ALIGNMENT: should be set to the alignment of the CPU for which lwIP is compiled.
#define MEM_ALIGNMENT           4

Terry

On 27/05/2019 04:17, [hidden email] wrote:
Could anyone give me some advice?

I recently use mem_alloc() to allocate a structure memory which have a int64 member. However, mem_alloc() returns a memory poniter which is 4 byte aligned which cause a memory unaligned fault.
The code is as follows,

ifp=mem_alloc(sizeof(structure interface));

The structure intrface has a member flags which is int64. The size of structure interface is 112 which is a multiple of 8 while the address returned is 0x720ef324 which is not 8 byte aligned. So when I want to access ifp->flags, an unaligned fault will be triggered.

Could any give some hint?
Thanks!
Best regards,

yan



On 05/25/2019 22:19, [hidden email] wrote:
Hi all.

What is the memory alignment of the pointer returned by mem_alloc() in mem.c?
Since the only argument of mem_alloc() is size, so what alignment will mem_alloc() choose?

In http://man7.org/linux/man-pages/man3/malloc.3.html, it says "The malloc() and calloc() functions return a pointer to the allocated memory, which is suitably aligned for any built-in type."
Since the largest type is double or int64 which are 8 bytes, so I guess malloc() in linux will choose alignment of 8 bytes.

Is this guess also applied to mem_alloc() in LwIP? 
That is, does mem_alloc() in LwIP also choose alignment of 8 bytes?

Thanks!
Best regard,

yan

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users

_______________________________________________
lwip-users mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/lwip-users