简体   繁体   中英

How allocate memory which is page size aligned?

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() .

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.

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) .

Use posix_memalign to obtain memory already aligned.

Note: valloc & memalign both are obsoleted.

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.

// Linux-oriented, // See my trickery to arrange yourself

I just wrote a program which has to allocate hardware-page-aligned memory ; here follows the body of the program containing the main() function :

The main() function calls an Func_a() function, which calls an Func_b() function which calls aforementioned Func_a() function, and so on ... ... recursively and without stop condition, thus indefinitely, burning the program stack, eventually causing SIGSEGV.

Should this help you :

#include "Func_A.h"
#include "Func_B.h"


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define STACK_SIZE (1024 * 1024)

int main(void)
{
  // Allocate a 2nd stack for the SIG_SEGV signal handler
  long pgsz = sysconf(_SC_PAGESIZE);
  printf("Page size is %ld\n", pgsz);

  long stacksize = STACK_SIZE;

  long sigstkpages = (STACK_SIZE + (pgsz - 1) ) / pgsz;

  void* newstack = calloc(sigstkpages, pgsz);
  if (NULL == newstack)
  {
    perror("calloc()\n");
    exit(2);
  }
  newstack = (void*) (((((long) newstack) + (pgsz - 1)) / pgsz) * pgsz);
  void* newstackhigh = (void*) (((long) newstack) + (stacksize - 1));
  stacksize = (newstackhigh - newstack) + 1;
  printf("Stack size is %ld\n", stacksize);

  printf("New Stack spans %p-%p  (%ld pages)\n", newstack, newstackhigh, sigstkpages);

  a();
}

And its output is :

Page size is 4096 Stack size is 1048576 New Stack spans 0x7f28435cd000-0x7f28436ccfff (256 pages) Erreur de segmentation (core dumped)

The coredump is expected here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM