Re:Kernel programming Q: new aligned memory

Inaky Perez Gonzalez (inaky@peloncho.fis.ucm.es)
Sat, 5 Sep 1998 21:05:55 +0200


>>>>> "Vladimir" == Vladimir Dergachev <vladimid@red.seas.upenn.edu> writes:

Hi

Vladimir> Hmmm what if this happens : say you want 16 byte alignment
Vladimir> and you chunk is 4096bytes so you get first pointer and it's
Vladimir> 8bytes in the wrong direction. Then when you allocate next
Vladimir> chunk you'll get it again 8bytes in he wrong direction and
Vladimir> so on.

No ... when I ask for another 8 bytes, I have already asked
for eight, so there's a change next one is aligned. It keeps recursing
until it gets an allocated one or it reaches the maximum depth. If
success, it frees all the unaligned ones and gives you the aligned.

Vladimir> I think much simpler way is to allocate size+alignment-1
Vladimir> bytes and then just increment the pointer so that it points
Vladimir> to the aligned part. This wastes a part of the memory
Vladimir> though. If you want to free it later you can just keep two
Vladimir> pointers instead of one - one pointer you pass to free and
Vladimir> another that you pass to the whatever procedure that wants
Vladimir> the memory.

Yeap, this is the nice way, but I have to deal with two
pointers [or do the math _each_ time I wan't to use it]. That's
ugly. I need _fast_ handling, and I can permit some more time at
allocation time if I'll save a pointer and some math.

Vladimir> PS If you really want to save memory and allocate exactly
Vladimir> size bytes, then you could use your procedure for example
Vladimir> and if it doesn't work use mine instead of returning NULL.

Two pointers ... :)

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.tux.org/lkml/faq.html