[SUMMARY] How to get aligned memory

Inaky Perez Gonzalez (inaky@peloncho.fis.ucm.es)
Mon, 3 Aug 1998 18:03:53 +0200


Hi

First of all, thanks to everyone who answered my question [how
to get aligned memory when kmalloc()ing]. I've received tons of
answers (~20 :) which I'll summarize in this message for anyothers
convenience:

- Ask for a size of one less the alignment you need (align to 32
bytes, ask for 31, 32 should do too) and round up the result
ANDing 0xfffffff0 and ORing 0x00000010. Needs to keep the
original pointer to free the thing up [or remember your data
is at (pointer & ~0xf) | 0x10.

- Get a page and make an specialized allocator. It wastes a
page, but it is a nice idea if you want to optimize on cache
hits for SMP operation. Ralf <ralf@uni-koblenz.de> suggested
this.

- Make the allocator without wasting the whole page, just
getting, for example, a quarter kbyte.

The final sollution I used was quite awkward and I'm not sure
it will work on every arch. I did some testings and realized that
kmallocs where aligned to the next power of two of the size [or at
least on the sizes I tried on my kernel]. I modified the allocation
code for the need-to-be-aligned structures to requests double their
size (8 to 16 bytes) and an assertion check to verify it's aligned to
16-bytes. In all the test runs I've made, the code worked flawlessly
and all the allocations aligned perfectly to 16 bytes without any more
tweakings.

This is nice as a temporary sollution, but I still have to
find a better one (perhaps the MM code can be tweaked to allow asking
aligned memory).

Again, thanks to everybody who answered!

-- 

Linux-USB! http://peloncho.fis.ucm.es/~inaky/USB.html - - Inaky Perez Gonzalez -- PGP pubkey fingerprint - inaky@peloncho.fis.ucm.es -- 8E 34 3A 62 64 99 E2 44 - http://peloncho.fis.ucm.es/~inaky -- AD 7B 30 D9 DD FF 3E 4C - --------------------------------- -- ----------------------- - The loneliness of the long distance runner .....

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.altern.org/andrebalsa/doc/lkml-faq.html