Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * 3 : * Permission is hereby granted, free of charge, to any person obtaining 4 : * a copy of this software and associated documentation files (the 5 : * "Software"), to deal in the Software without restriction, including 6 : * without limitation the rights to use, copy, modify, merge, publish, 7 : * distribute, sublicense, and/or sell copies of the Software, and to 8 : * permit persons to whom the Software is furnished to do so, subject to 9 : * the following conditions: 10 : * 11 : * The above copyright notice and this permission notice shall be included 12 : * in all copies or substantial portions of the Software. 13 : * 14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 : */ 22 : 23 : #ifndef INCLUDED_ALIGNMENT 24 : #define INCLUDED_ALIGNMENT 25 : 26 : #include "lib/code_annotation.h" 27 : #include "lib/sysdep/compiler.h" // MSC_VERSION 28 : #include "lib/sysdep/arch.h" // ARCH_AMD64 29 : 30 : #include <cstddef> 31 : #include <cstdint> 32 : 33 : template<typename T> 34 34522 : inline bool IsAligned(T t, uintptr_t multiple) 35 : { 36 18317 : return (uintptr_t(t) % multiple) == 0; 37 : } 38 : 39 : template<size_t multiple> 40 52 : inline size_t Align(size_t n) 41 : { 42 : cassert(multiple != 0 && ((multiple & (multiple-1)) == 0)); // is power of 2 43 52 : return (n + multiple-1) & ~(multiple-1); 44 : } 45 : 46 : 47 : // bridge the differences between MSC and GCC alignment definitions. 48 : // example: ALIGNED(int, 8) myAlignedVariable = 0; 49 : #if MSC_VERSION 50 : # define ALIGNED(type, multiple) __declspec(align(multiple)) type 51 : #elif GCC_VERSION 52 : # define ALIGNED(type, multiple) type __attribute__((aligned(multiple))) 53 : #else 54 : # define ALIGNED(type, multiple) type 55 : #endif 56 : 57 : 58 : // 59 : // SIMD vector 60 : // 61 : 62 : static const size_t vectorSize = 16; 63 : #define VECTOR_ALIGNED(type) ALIGNED(type, 16) // ALIGNED() requires a literal; keep in sync with vectorSize 64 : 65 : #define ASSERT_VECTOR_MULTIPLE(size)\ 66 : ASSERT(IsAligned(size, vectorSize)) 67 : 68 : #define ASSERT_VECTOR_ALIGNED(pointer)\ 69 : ASSERT_VECTOR_MULTIPLE(pointer) 70 : 71 : 72 : // 73 : // CPU cache 74 : // 75 : 76 : static const size_t cacheLineSize = 64; // (L2) 77 : #define CACHE_ALIGNED(type) ALIGNED(type, 64) // ALIGNED() requires a literal; keep in sync with cacheLineSize 78 : 79 : 80 : 81 : 82 : // 83 : // MMU pages 84 : // 85 : 86 : #ifdef ARCH_PPC64 87 : // NOTE: ppc64 can operate in either 4k or 64k page size mode 88 : // If the define page size is larger than the active page size, 89 : // the allocator functions normally. If the defined page size 90 : // is less than the active page size, the allocator fails tests. 91 : // 92 : // Define the page size to the maximum known architectural page 93 : // size on ppc64 systems. 94 : static const size_t g_PageSize = 64 * 1024; // 64 KB 95 : #else 96 : static const size_t g_PageSize = 4 * 1024; // 4 KB 97 : #endif 98 : static const size_t g_LargePageSize = 2 * 1024 * 1024; // 2 MB 99 : 100 : 101 : // 102 : // misc 103 : // 104 : 105 : static const size_t allocationAlignment = 16; 106 : 107 : static const size_t KiB = size_t(1) << 10; 108 : static const size_t MiB = size_t(1) << 20; 109 : static const size_t GiB = size_t(1) << 30; 110 : 111 : // waio opens files with FILE_FLAG_NO_BUFFERING, so Windows requires 112 : // file offsets / buffers and sizes to be sector-aligned. querying the 113 : // actual sector size via GetDiskFreeSpace is inconvenient and slow. 114 : // we always request large blocks anyway, so just check whether inputs 115 : // are aligned to a `maximum' sector size. this catches common mistakes 116 : // before they cause scary "IO failed" errors. if the value turns out 117 : // to be too low, the Windows APIs will still complain. 118 : static const uintptr_t maxSectorSize = 0x1000; 119 : 120 : #endif // #ifndef INCLUDED_ALIGNMENT