High Performance Plasticity  0.5.0
stdlibReplacements.h
Go to the documentation of this file.
1 
7 #ifndef HPP_STDLIBREPLACEMENTS_H
8 #define HPP_STDLIBREPLACEMENTS_H
9 
10 #include <hpp/config.h>
11 
38 #ifndef HPP_HAVE_ALIGNED_ALLOC
39 static __inline__ void*
40 aligned_alloc (size_t size, size_t align)
41 {
42  void * malloc_ptr;
43  void * aligned_ptr;
44  /* Error if align is not a power of two. */
45  if (align & (align - 1))
46  {
47  errno = EINVAL;
48  return ((void*) 0);
49  }
50  if (size == 0)
51  return ((void *) 0);
52  /* Assume malloc'd pointer is aligned at least to sizeof (void*).
53  If necessary, add another sizeof (void*) to store the value
54  returned by malloc. Effectively this enforces a minimum alignment
55  of sizeof double. */
56  if (align < 2 * sizeof (void *))
57  align = 2 * sizeof (void *);
58  malloc_ptr = malloc (size + align);
59  if (!malloc_ptr)
60  return ((void *) 0);
61  /* Align We have at least sizeof (void *) space below malloc'd ptr. */
62  aligned_ptr = (void *) (((size_t) malloc_ptr + align)
63  & ~((size_t) (align) - 1));
64  /* Store the original pointer just before p. */
65  ((void **) aligned_ptr) [-1] = malloc_ptr;
66  return aligned_ptr;
67 }
68 #endif
69 
70 #endif /* HPP_STDLIBREPLACEMENTS_H */