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