Make fio -Wshadow clean
[fio.git] / memalign.c
CommitLineData
91aea6ec
JA
1#include <stdlib.h>
2#include <assert.h>
e43606c2 3#include <inttypes.h>
91aea6ec
JA
4
5#include "memalign.h"
6
7struct align_footer {
8 unsigned int offset;
9};
10
11#define PTR_ALIGN(ptr, mask) \
e43606c2 12 (char *) (((uintptr_t) ((ptr) + (mask)) & ~(mask)))
91aea6ec
JA
13
14void *fio_memalign(size_t alignment, size_t size)
15{
16 struct align_footer *f;
17 void *ptr, *ret = NULL;
18
19 assert(!(alignment & (alignment - 1)));
20
21 ptr = malloc(size + alignment + size + sizeof(*f) - 1);
22 if (ptr) {
5fb55c77 23 ret = PTR_ALIGN(ptr, alignment - 1);
91aea6ec 24 f = ret + size;
e43606c2 25 f->offset = (uintptr_t) ret - (uintptr_t) ptr;
91aea6ec
JA
26 }
27
28 return ret;
29}
30
31void fio_memfree(void *ptr, size_t size)
32{
33 struct align_footer *f = ptr + size;
34
35 free(ptr - f->offset);
36}