malloc memory to be 32 byte aligned in kernel
Valdis.Kletnieks at vt.edu
Valdis.Kletnieks at vt.edu
Tue Jan 28 19:15:55 EST 2014
On Tue, 28 Jan 2014 15:32:37 -0800, anish singh said:
> On Tue, Jan 28, 2014 at 11:20 AM, m silverstri
> <michael.j.silverstri at gmail.com> wrote:
> > Hi,
> >
> > I am writing a kernel driver, can you please tell me how can I
> > allocate a buffer which is 32 byte aligned?
> malloc already aligns memory for basic data types AFAIK
Yes, but even if it allocates on a long-long boundary, he can still get
hosed if it ends up on a 8-byte boundary that's *not* a 32-byte aligned.
Your best approach is probably to use the KMEM_CACHE() macro to create a slab
cache, and then allocate from that slab. See include/linux/slab.h
/*
* Please use this macro to create slab caches. Simply specify the
* name of the structure and maybe some flags that are listed above.
*
* The alignment of the struct determines object alignment. If you
* f.e. add ____cacheline_aligned_in_smp to the struct declaration
* then the objects will be properly aligned in SMP configurations.
*/
#define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct, sizeof(struct __struct), __alignof__(struct __struct), (__flags), NULL)
Oh, and you'll need to declare a 'struct foo {...} __attribute__(aligned(32))'
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140128/dfbbf46b/attachment.bin
More information about the Kernelnewbies
mailing list