-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemalign.c
More file actions
33 lines (24 loc) · 605 Bytes
/
memalign.c
File metadata and controls
33 lines (24 loc) · 605 Bytes
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
#include <stdio.h>
#include <stdlib.h>
void *aligned_memory(size_t required, size_t alignment){
void *p1;
void **p2;
p1 = malloc(alignment - 1 + sizeof(void *) + required);
p2 = (void **)(((size_t)p1 + alignment - 1 + sizeof(void *)) & ~(alignment - 1));
p2[-1] = p1;
return p2;
}
void free_aligned(void *p) {
free(((void **)p)[-1]);
}
int main (int argc, char *argv[]) {
if(argc < 3) {
printf("Wrong input\n");
exit(1);
}
int required = atoi(argv[1]);
int alignment = atoi(argv[2]);
void *aligned_p = aligned_memory(required, alignment);
printf("%p\n", aligned_p);
return 0;
}