File: memalign.c

package info (click to toggle)
fio 2.1.11-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,712 kB
  • ctags: 5,643
  • sloc: ansic: 44,396; sh: 1,489; python: 400; makefile: 342
file content (36 lines) | stat: -rw-r--r-- 663 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdlib.h>
#include <assert.h>
#include <inttypes.h>

#include "memalign.h"

struct align_footer {
	unsigned int offset;
};

#define PTR_ALIGN(ptr, mask)	\
	(char *) (((uintptr_t) ((ptr) + (mask)) & ~(mask)))

void *fio_memalign(size_t alignment, size_t size)
{
	struct align_footer *f;
	void *ptr, *ret = NULL;

	assert(!(alignment & (alignment - 1)));

	ptr = malloc(size + alignment + size + sizeof(*f) - 1);
	if (ptr) {
		ret = PTR_ALIGN(ptr, alignment);
		f = ret + size;
		f->offset = (uintptr_t) ret - (uintptr_t) ptr;
	}

	return ret;
}

void fio_memfree(void *ptr, size_t size)
{
	struct align_footer *f = ptr + size;

	free(ptr - f->offset);
}