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