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