-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathmalloc.h
65 lines (60 loc) · 1.53 KB
/
malloc.h
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Replacement for malloc.h which factors out platform differences.
#include <stdlib.h>
#include "config.h"
#if defined(VGO_darwin)
// without this we are missing malloc_type_id_t on Xcode 16+
#define _DARWIN_C_SOURCE
# include <malloc/malloc.h>
#undef _DARWIN_C_SOURCE
#elif defined(VGO_freebsd)
# include <stdlib.h>
# include <malloc_np.h>
#else
# include <malloc.h>
#endif
#include <assert.h>
// Allocates a 16-aligned block. Asserts if the allocation fails.
__attribute__((unused))
static void* memalign16(size_t szB)
{
void* x;
#if defined(VGO_darwin) || defined(VGO_freebsd)
// Darwin lacks memalign, but its malloc is always 16-aligned anyway.
posix_memalign((void **)&x, 16, szB);
#else
x = memalign(16, szB);
#endif
assert(x);
assert(0 == ((16-1) & (unsigned long)x));
return x;
}
// Allocates a 32-aligned block. Asserts if the allocation fails.
__attribute__((unused))
static void* memalign32(size_t szB)
{
void* x;
#if defined(VGO_darwin) || defined(VGO_freebsd)
// Darwin lacks memalign
posix_memalign((void **)&x, 32, szB);
#else
x = memalign(32, szB);
#endif
assert(x);
assert(0 == ((32-1) & (unsigned long)x));
return x;
}
// Allocates a 64-aligned block. Asserts if the allocation fails.
__attribute__((unused))
static void* memalign64(size_t szB)
{
void* x;
#if defined(VGO_darwin) || defined(VGO_freebsd)
// Darwin lacks memalign
posix_memalign((void **)&x, 64, szB);
#else
x = memalign(64, szB);
#endif
assert(x);
assert(0 == ((64-1) & (unsigned long)x));
return x;
}