dynamic allocation of aligned buffer

Greetings,

is there library routine that would allow me to allocate buffer of
‘size’ bytes aligned at ‘alignment’ boundary (for example,
512 bytes buffer aligned at 4 byte boundary) ? Both’size’
and ‘alignment’ are powers of 2.

If I want to align pointer upward to ‘alignment’ boundary (where
‘alignment’ is power of 2), is this the reliable way to do it:

#define ALIGN_PTR_UPWARD(p,alignment)
((void *) ((char *)p + ((int)p % alignment)))

Thanks in advance,
Andray

Andray Kaganovsky <andreykNOSPAM@home.com> wrote in
news:Xns917CCA223C3CBandreykhomecom@209.226.137.7:

Greetings,

If I want to align pointer upward to ‘alignment’ boundary (where
‘alignment’ is power of 2), is this the reliable way to do it:

#define ALIGN_PTR_UPWARD(p,alignment)
((void *) ((char *)p + ((int)p % alignment)))

Thanks in advance,
Andray

[AndrayK]. Oops … it should be:

#define ALIGN_PTR_UPWARD(p,alignment)
((void *) ((char *)p + alignment - ((int)p % alignment)))

Andray

There is (undocumented) posix_memalign() which does just that. It is
from one of draft POSIX specs. Grep headers :wink:
The malloc() currently aligns on 4, but that will be changed to 8 (i’ve
heard). As a general rule, malloc() in all OSes aligns to what compiler
thinks is size of the largest primitive datatype.

  • igor

Andray Kaganovsky wrote:

Greetings,

is there library routine that would allow me to allocate buffer of
‘size’ bytes aligned at ‘alignment’ boundary (for example,
512 bytes buffer aligned at 4 byte boundary) ? Both’size’
and ‘alignment’ are powers of 2.

If I want to align pointer upward to ‘alignment’ boundary (where
‘alignment’ is power of 2), is this the reliable way to do it:

#define ALIGN_PTR_UPWARD(p,alignment)
((void *) ((char *)p + ((int)p % alignment)))

Thanks in advance,
Andray

posix_memalign() will be doc’d for 6.2.0…
Although there’s more doc work to be done,
here’s what we’ve got so far:

posix_memalign() Allocate aligned memory
Synopsis:
#include <stdlib.h>

int posix_memalign( void ** memptr,
size_t alignment,
size_t size );
Library:
libc

Description:
The posix_memalign() function allocates size bytes aligned on a boundary specified by alignment.
It returns a pointer to the allocated memory in memptr.
The value of alignment must be a multiple of size(void*).

Returns:
0 Success.
-1 An error occurred (errno is set).
Errors:
EINVAL The value of alignment isn’t a multiple of size( void *).
ENOMEM There’s insufficient memory available with the requested alignment.

Classification:
POSIX

Safety:
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

See also:
errno, free(), malloc()



Igor Kovalenko <Igor.Kovalenko@motorola.com> wrote:

There is (undocumented) posix_memalign() which does just that. It is
from one of draft POSIX specs. Grep headers > :wink:
The malloc() currently aligns on 4, but that will be changed to 8 (i’ve
heard). As a general rule, malloc() in all OSes aligns to what compiler
thinks is size of the largest primitive datatype.

  • igor

Andray Kaganovsky wrote:

Greetings,

is there library routine that would allow me to allocate buffer of
‘size’ bytes aligned at ‘alignment’ boundary (for example,
512 bytes buffer aligned at 4 byte boundary) ? Both’size’
and ‘alignment’ are powers of 2.

If I want to align pointer upward to ‘alignment’ boundary (where
‘alignment’ is power of 2), is this the reliable way to do it:

#define ALIGN_PTR_UPWARD(p,alignment)
((void *) ((char *)p + ((int)p % alignment)))

Thanks in advance,
Andray

Donna Kinsman wrote:

posix_memalign() will be doc’d for 6.2.0…
Although there’s more doc work to be done,
here’s what we’ve got so far:

posix_memalign() Allocate aligned memory
Synopsis:
#include <stdlib.h

int posix_memalign( void ** memptr,
size_t alignment,
size_t size );
Library:
libc

Description:
The posix_memalign() function allocates size bytes aligned on a boundary specified by alignment.
It returns a pointer to the allocated memory in memptr.
The value of alignment must be a multiple of size(void*).

Returns:
0 Success.
-1 An error occurred (errno is set).
Errors:
EINVAL The value of alignment isn’t a multiple of size( void *).
ENOMEM There’s insufficient memory available with the requested alignment.

Classification:
POSIX

Safety:
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

See also:
errno, free(), malloc()

Can I assume the buffer allocated by posix_memalign() is contiguous in
physical memory?

And is it true even in the case size > 4KB ?

Which is the best way to obtain the physical memory address of the
allocated buffer?

Thanks

“Davide Ancri” <davidea@prisma-eng.it> wrote in message
news:3C32D754.87B8E285@prisma-eng.it

Can I assume the buffer allocated by posix_memalign() is contiguous in
physical memory?

No, it would be contiguous in the virtual address space, not the physical
one. Of course if the amount is under 4K, you could make an assumption it’s
physically contiguous (since physical memory is used in pages size hunks)
but I would tend to avoid that assumption, since other platforms (or future
systems) may not do 4K page sizes.

Which is the best way to obtain the physical memory address of the
allocated buffer?

You can get the physical address of the start of the buffer using
mem_offset() with fd=NOFD.

-Adam