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 : #include "precompiled.h" 24 : 25 : #include "lib/bits.h" 26 : #include "lib/sysdep/os.h" 27 : #include "lib/sysdep/rtl.h" 28 : 29 : #include <cstdlib> 30 : 31 : // Linux glibc has posix_memalign and (obsolete) memalign 32 : // Android libc has only memalign 33 : // OS X and BSD probably do not have either. 34 : #define HAVE_POSIX_MEMALIGN (OS_LINUX && !OS_ANDROID) 35 : #define HAVE_MEMALIGN OS_LINUX 36 : 37 : #if HAVE_POSIX_MEMALIGN 38 : 39 22325 : void* rtl_AllocateAligned(size_t size, size_t alignment) 40 : { 41 22325 : void *ptr; 42 22325 : int ret = posix_memalign(&ptr, alignment, size); 43 22325 : if (ret) { 44 : // TODO: report error? 45 : return NULL; 46 : } 47 22325 : return ptr; 48 : } 49 : 50 22432 : void rtl_FreeAligned(void* alignedPointer) 51 : { 52 22432 : free(alignedPointer); 53 22432 : } 54 : 55 : #elif HAVE_MEMALIGN 56 : 57 : void* rtl_AllocateAligned(size_t size, size_t alignment) 58 : { 59 : return memalign(alignment, size); 60 : } 61 : 62 : void rtl_FreeAligned(void* alignedPointer) 63 : { 64 : free(alignedPointer); 65 : } 66 : 67 : #else // Fallback aligned allocation using malloc 68 : 69 : void* rtl_AllocateAligned(size_t size, size_t align) 70 : { 71 : // This ensures we have enough extra space to store the original pointer, 72 : // and produce an aligned buffer, assuming the platform malloc ensures at 73 : // least sizeof(void*) alignment. 74 : if (align < 2*sizeof(void*)) 75 : align = 2*sizeof(void*); 76 : 77 : void* const malloc_ptr = malloc(size + align); 78 : if (!malloc_ptr) 79 : return NULL; 80 : 81 : // Round malloc_ptr up to the next aligned address, leaving some unused 82 : // space before the pointer we'll return. The minimum alignment above 83 : // ensures we'll have at least sizeof(void*) extra space. 84 : void* const aligned_ptr = 85 : (void *)(round_down(uintptr_t(malloc_ptr), uintptr_t(align)) + align); 86 : 87 : // Just make sure we did the right thing with all the alignment hacks above. 88 : ENSURE(((void**)aligned_ptr) - 1 >= malloc_ptr); 89 : 90 : // Store the original pointer which will have to be sent to free(). 91 : ((void **)aligned_ptr)[-1] = malloc_ptr; 92 : 93 : return aligned_ptr; 94 : } 95 : 96 : void rtl_FreeAligned(void* alignedPointer) 97 : { 98 : if (alignedPointer) 99 : free(((void**)alignedPointer)[-1]); 100 : } 101 : 102 : #endif