Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
alignment.h
Go to the documentation of this file.
1 #ifndef INCLUDED_ALIGNMENT
2 #define INCLUDED_ALIGNMENT
3 
4 #include "lib/sysdep/compiler.h" // MSC_VERSION
5 #include "lib/sysdep/arch.h" // ARCH_AMD64
6 
7 template<typename T>
8 inline bool IsAligned(T t, uintptr_t multiple)
9 {
10  return (uintptr_t(t) % multiple) == 0;
11 }
12 
13 template<size_t multiple>
14 inline size_t Align(size_t n)
15 {
16  cassert_dependent(multiple != 0 && ((multiple & (multiple-1)) == 0)); // is power of 2
17  return (n + multiple-1) & ~(multiple-1);
18 }
19 
20 
21 // bridge the differences between MSC and GCC alignment definitions.
22 // example: ALIGNED(int, 8) myAlignedVariable = 0;
23 #if MSC_VERSION
24 # define ALIGNED(type, multiple) __declspec(align(multiple)) type
25 #elif GCC_VERSION
26 # define ALIGNED(type, multiple) type __attribute__((aligned(multiple)))
27 #else
28 # define ALIGNED(type, multiple) type
29 #endif
30 
31 
32 //
33 // SIMD vector
34 //
35 
36 static const size_t vectorSize = 16;
37 #define VECTOR_ALIGNED(type) ALIGNED(type, 16) // ALIGNED() requires a literal; keep in sync with vectorSize
38 
39 #define ASSERT_VECTOR_MULTIPLE(size)\
40  ASSERT(IsAligned(size, vectorSize))
41 
42 #define ASSERT_VECTOR_ALIGNED(pointer)\
43  ASSERT_VECTOR_MULTIPLE(pointer);\
44  ASSUME_ALIGNED(pointer, vectorSize)
45 
46 
47 //
48 // CPU cache
49 //
50 
51 static const size_t cacheLineSize = 64; // (L2)
52 #define CACHE_ALIGNED(type) ALIGNED(type, 64) // ALIGNED() requires a literal; keep in sync with cacheLineSize
53 
54 
55 
56 
57 //
58 // MMU pages
59 //
60 
61 static const size_t pageSize = 0x1000; // 4 KB
62 static const size_t largePageSize = 0x200000; // 2 MB
63 
64 
65 //
66 // misc
67 //
68 
69 static const size_t allocationAlignment = 16;
70 
71 static const size_t KiB = size_t(1) << 10;
72 static const size_t MiB = size_t(1) << 20;
73 static const size_t GiB = size_t(1) << 30;
74 
75 // waio opens files with FILE_FLAG_NO_BUFFERING, so Windows requires
76 // file offsets / buffers and sizes to be sector-aligned. querying the
77 // actual sector size via GetDiskFreeSpace is inconvenient and slow.
78 // we always request large blocks anyway, so just check whether inputs
79 // are aligned to a `maximum' sector size. this catches common mistakes
80 // before they cause scary "IO failed" errors. if the value turns out
81 // to be too low, the Windows APIs will still complain.
82 static const uintptr_t maxSectorSize = 0x1000;
83 
84 #endif // #ifndef INCLUDED_ALIGNMENT
static const size_t pageSize
Definition: alignment.h:61
size_t Align(size_t n)
Definition: alignment.h:14
static const uintptr_t maxSectorSize
Definition: alignment.h:82
#define cassert_dependent(expr)
Compile-time assertion.
bool IsAligned(T t, uintptr_t multiple)
Definition: alignment.h:8
static const size_t GiB
Definition: alignment.h:73
static const size_t KiB
Definition: alignment.h:71
static const size_t MiB
Definition: alignment.h:72
#define T(string_literal)
Definition: secure_crt.cpp:70
static const size_t largePageSize
Definition: alignment.h:62
static const size_t cacheLineSize
Definition: alignment.h:51
static const size_t vectorSize
Definition: alignment.h:36
static const size_t allocationAlignment
Definition: alignment.h:69