Re:Kernel programming Q: new aligned memory

Vladimir Dergachev (vladimid@red.seas.upenn.edu)
Sat, 5 Sep 1998 08:54:29 -0400 (EDT)


>
> The point is, if the size of the object is smaller then the
> alignment, ask for a chunk sized as the alignment, else the size. The
> rounding to the next power of two will do the same when
> allocating.
>
> I've successfully allocated _everyone_ of them, even under
> heavy load, but I am not sure it will always success. So I implemented
> a workaround for the case it failed.
>
> If not aligned, it will recursively allocate more chunks,
> until one falls into the wanted alignment; then it will free them and
> let you with the aligned one. It's ugly, but it works. There's a depth
> limit, just in case ...
>

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

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

Vladimir Dergachev

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

> It's dirty, could be optimized, etc, etc ... but hey, I did it
> in five minutes :)
>
> 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
>

-
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