Fix libpbd tests.
[ardour.git] / libs / pbd / malign.cc
1 #include "libpbd-config.h"
2
3 #include <cstring>
4 #include <cerrno>
5
6 #include "pbd/malign.h"
7 #include "pbd/error.h"
8
9 #include "i18n.h"
10
11 using namespace PBD;
12
13 #ifdef __x86_64__
14 static const int CPU_CACHE_ALIGN = 64;
15 #else
16 static const int CPU_CACHE_ALIGN = 16; /* arguably 32 on most arches, but it matters less */
17 #endif
18
19 int cache_aligned_malloc (void** memptr, size_t size)
20 {
21 #ifdef NO_POSIX_MEMALIGN
22         if (((*memptr) = malloc (size)) == 0) {
23                 fatal << string_compose (_("Memory allocation error: malloc (%1 * %2) failed (%3)"),
24                                          CPU_CACHE_ALIGN, size, strerror (errno)) << endmsg;
25                 return errno;
26         } else {
27                 return 0;
28         }
29 #else
30         if (posix_memalign (memptr, CPU_CACHE_ALIGN, size)) {
31                 fatal << string_compose (_("Memory allocation error: posix_memalign (%1 * %2) failed (%3)"),
32                                          CPU_CACHE_ALIGN, size, strerror (errno)) << endmsg;
33         }
34
35         return 0;
36 #endif  
37 }