How allocate memory which is page size aligned?

14,885

Solution 1

There are functions for this that you're supposed to use.

If you can't, for whatever reason, then the way this is generally done is by adding the block size to the allocation size, then using integer-math trickery to round the pointer.

Something like this:

/* Note that alignment must be a power of two. */
void * allocate_aligned(size_t size, size_t alignment)
{
  const size_t mask = alignment - 1;
  const uintptr_t mem = (uintptr_t) malloc(size + alignment);
  return (void *) ((mem + mask) & ~mask);
}

This has not been very deeply tested but you get the idea.

Note that it becomes impossible to figure out the proper pointer to free() the memory later. To fix that, we would have to add some additional machinery:

typedef struct {
  void *aligned;
} AlignedMemory;

AlignedMemory * allocate_aligned2(size_t size, size_t alignment)
{
  const size_t mask = alignment - 1;
  AlignedMemory *am = malloc(sizeof *am + size + alignment);
  am->aligned = (void *) ((((uintptr_t) (am + 1)) + mask) & ~mask);
  return am;
}

This wraps the pointer trickery a bit, and gives you a pointer you can free(), but you need to dereference into the aligned pointer to get the properly aligned pointer.

Solution 2

I don't think is possible only with malloc. You can use memalign():

char *data = memalign(PAGESIZE, alloc_size);

Where PAGESIZE is the size of a page and alloc_size is the size of the memory that will be allocated.

The size of the page can be found with sysconf(_SC_PAGESIZE).

Solution 3

Use posix_memalign to obtain memory already aligned.

Note: valloc & memalign both are obsoleted.

Solution 4

Use valloc rather than malloc - it has the same signature as malloc but it allocates page-aligned memory. Note that you still free the memory subsequently using free().

Note also that valloc is technically obsolete, so consider using posix_memalign, although this is not such a simple replacement for malloc as it has a very different function signature.

Share:
14,885
Chinna
Author by

Chinna

Updated on June 07, 2022

Comments

  • Chinna
    Chinna almost 2 years

    I need to allocate memory which should be page size aligned. I need to pass this memory to an ASM code which calculates xor of all data blocks. I need to do this with malloc().

  • Damon
    Damon about 10 years
    @glglgl: Do the same thing malloc does too. Write the original address in the 4 bytes preceding the address you give back to the user.
  • Basile Starynkevitch
    Basile Starynkevitch about 10 years
    valloc is documented to be obsolete so don't use it!
  • unwind
    unwind about 10 years
    @AlterMann Hehe. Well, sure, since I need an integer and not a pointer. My usual restriction doesn't apply here, void * doesn't automatically convert to uintptr_t. :)
  • Paul R
    Paul R about 10 years
    I suspect that valloc will be around for a long time yet. Note that it's not just a Linux-ism, as it's in BSD/Mac OS X/et al also. However I've added a note about using the rather more awkward posix_memalign just for completeness.