sandbox: Align RAM buffer to the machine page size
authorSimon Glass <sjg@chromium.org>
Sat, 15 Sep 2018 06:50:54 +0000 (00:50 -0600)
committerAlexander Graf <agraf@suse.de>
Sun, 23 Sep 2018 19:55:30 +0000 (21:55 +0200)
At present the sandbox RAM buffer is not aligned to any particular
address boundary. This makes the internal pointers somewhat random with
respect to the associated RAM buffer addresses.

Align the buffer to the page size of the machine to help with this. Note
that there is a header at the start of the allocated pointer. To avoid
returning a pointer which is not aligned to a page boundary, we waste
almost an entire page of memory for each allocation.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>
arch/sandbox/cpu/os.c

index 5839932..a1a982a 100644 (file)
@@ -143,14 +143,15 @@ void os_tty_raw(int fd, bool allow_sigs)
 void *os_malloc(size_t length)
 {
        struct os_mem_hdr *hdr;
+       int page_size = getpagesize();
 
-       hdr = mmap(NULL, length + sizeof(*hdr), PROT_READ | PROT_WRITE,
-                  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+       hdr = mmap(NULL, length + page_size,
+                  PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        if (hdr == MAP_FAILED)
                return NULL;
        hdr->length = length;
 
-       return hdr + 1;
+       return (void *)hdr + page_size;
 }
 
 void os_free(void *ptr)