1 // Replacement for malloc.h which factors out platform differences.
2
3 #include <stdlib.h>
4 #if defined(VGO_darwin)
5 # include <malloc/malloc.h>
6 #elif defined(VGO_dragonfly)
7 # include <stdlib.h>
8 #else
9 # include <malloc.h>
10 #endif
11
12 #include <assert.h>
13
14 // Allocates a 16-aligned block. Asserts if the allocation fails.
15 __attribute__((unused))
memalign16(size_t szB)16 static void* memalign16(size_t szB)
17 {
18 void* x;
19 #if defined(VGO_darwin) || defined(VGO_dragonfly)
20 // Darwin lacks memalign, but its malloc is always 16-aligned anyway.
21 posix_memalign((void **)&x, 16, szB);
22 #else
23 x = memalign(16, szB);
24 #endif
25 assert(x);
26 assert(0 == ((16-1) & (unsigned long)x));
27 return x;
28 }
29
30 // Allocates a 32-aligned block. Asserts if the allocation fails.
31 __attribute__((unused))
memalign32(size_t szB)32 static void* memalign32(size_t szB)
33 {
34 void* x;
35 #if defined(VGO_darwin) || defined(VGO_dragonfly)
36 // Darwin lacks memalign
37 posix_memalign((void **)&x, 32, szB);
38 #else
39 x = memalign(32, szB);
40 #endif
41 assert(x);
42 assert(0 == ((32-1) & (unsigned long)x));
43 return x;
44 }
45
46 // Allocates a 64-aligned block. Asserts if the allocation fails.
47 __attribute__((unused))
memalign64(size_t szB)48 static void* memalign64(size_t szB)
49 {
50 void* x;
51 #if defined(VGO_darwin) || defined(VGO_dragonfly)
52 // Darwin lacks memalign
53 posix_memalign((void **)&x, 64, szB);
54 #else
55 x = memalign(64, szB);
56 #endif
57 assert(x);
58 assert(0 == ((64-1) & (unsigned long)x));
59 return x;
60 }
61
62