malloc.h revision ed07e00d438c74b7a23c01bfffde77e3968305e4
1ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// Replacement for malloc.h which factors out platform differences.
2ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
3ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <stdlib.h>
4ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#if defined(VGO_darwin)
5ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#  include <malloc/malloc.h>
6ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#else
7ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#  include <malloc.h>
8ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#endif
9ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
10ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <assert.h>
11ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
12ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// Allocates a 16-aligned block.  Asserts if the allocation fails.
13ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown__attribute__((unused))
14ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic void* memalign16(size_t szB)
15ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
16ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   void* x;
17ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#if defined(VGO_darwin)
18ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   // Darwin lacks memalign, but its malloc is always 16-aligned anyway.
19ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   x = malloc(szB);
20ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#else
21ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   x = memalign(16, szB);
22ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#endif
23ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   assert(x);
24ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   assert(0 == ((16-1) & (unsigned long)x));
25ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   return x;
26ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
27ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
28