diff --git a/source/lib/allocators/page_aligned.cpp b/source/lib/allocators/page_aligned.cpp deleted file mode 100644 index 98bf67fab6..0000000000 --- a/source/lib/allocators/page_aligned.cpp +++ /dev/null @@ -1,115 +0,0 @@ -/* Copyright (C) 2025 Wildfire Games. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include "precompiled.h" - -#include "page_aligned.h" - -#include "lib/alignment.h" -#include "lib/debug.h" -#include "lib/posix/posix_mman.h" - -#include - -//----------------------------------------------------------------------------- - -static inline Status StatusFromMap(void* ret) -{ - if(ret != MAP_FAILED) - return INFO::OK; - WARN_RETURN(StatusFromErrno()); -} - -// "anonymous" effectively means mapping /dev/zero, but is more efficient. -// MAP_ANONYMOUS is not in SUSv3, but is a very common extension. -// unfortunately, MacOS X only defines MAP_ANON, which Solaris says is -// deprecated. workaround there: define MAP_ANONYMOUS in terms of MAP_ANON. -#ifndef MAP_ANONYMOUS -# define MAP_ANONYMOUS MAP_ANON -#endif - -static const int mmap_flags = MAP_PRIVATE|MAP_ANONYMOUS; - -Status mem_Reserve(size_t size, u8** pp) -{ - errno = 0; - void* ret = mmap(0, size, PROT_NONE, mmap_flags|MAP_NORESERVE, -1, 0); - *pp = (u8*)ret; - return StatusFromMap(ret); -} - -Status mem_Release(u8* p, size_t size) -{ - errno = 0; - if(munmap(p, size) != 0) - WARN_RETURN(StatusFromErrno()); - return 0; -} - -Status mem_Commit(u8* p, size_t size, int prot) -{ - // avoid misinterpretation by mmap. - if(prot == PROT_NONE) - WARN_RETURN(ERR::INVALID_PARAM); - - errno = 0; - void* ret = mmap(p, size, prot, mmap_flags|MAP_FIXED, -1, 0); - return StatusFromMap(ret); -} - -Status mem_Decommit(u8* p, size_t size) -{ - errno = 0; - void* ret = mmap(p, size, PROT_NONE, mmap_flags|MAP_NORESERVE|MAP_FIXED, -1, 0); - return StatusFromMap(ret); -} - -Status mem_Protect(u8* p, size_t size, int prot) -{ - errno = 0; - if(mprotect(p, size, prot) != 0) - WARN_RETURN(StatusFromErrno()); - return 0; - -} - - -//----------------------------------------------------------------------------- - -void* page_aligned_alloc(size_t size) -{ - const size_t alignedSize = Align(size); - u8* p = 0; - RETURN_0_IF_ERR(mem_Reserve(alignedSize, &p)); - RETURN_0_IF_ERR(mem_Commit(p, alignedSize, PROT_READ|PROT_WRITE)); - return p; -} - - -void page_aligned_free(void* p, size_t size) -{ - if(!p) - return; - ENSURE(IsAligned(p, g_PageSize)); - const size_t alignedSize = Align(size); - (void)mem_Release((u8*)p, alignedSize); -} diff --git a/source/lib/allocators/page_aligned.h b/source/lib/allocators/page_aligned.h deleted file mode 100644 index 866974b4f1..0000000000 --- a/source/lib/allocators/page_aligned.h +++ /dev/null @@ -1,65 +0,0 @@ -/* Copyright (C) 2025 Wildfire Games. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef INCLUDED_ALLOCATORS_PAGE_ALIGNED -#define INCLUDED_ALLOCATORS_PAGE_ALIGNED - -#include "lib/status.h" -#include "lib/types.h" - -#include - -// very thin wrapper on top of sys/mman.h that makes the intent more obvious -// (its commit/decommit semantics are difficult to tell apart) -Status mem_Reserve(size_t size, u8** pp); -Status mem_Release(u8* p, size_t size); -Status mem_Commit(u8* p, size_t size, int prot); -Status mem_Decommit(u8* p, size_t size); -Status mem_Protect(u8* p, size_t size, int prot); - - -/** - * allocate memory aligned to the system page size. - * - * this is useful for file_cache_alloc, which uses this allocator to - * get sector-aligned (hopefully; see sys_max_sector_size) IO buffers. - * - * note that this allocator is stateless and very little error checking - * can be performed. - * - * the memory is initially writable and you can use mprotect to set other - * access permissions if desired. - * - * @param unaligned_size minimum size [bytes] to allocate. - * @return page-aligned and -padded memory or 0 on error / out of memory. - **/ -void* page_aligned_alloc(size_t unaligned_size); - -/** - * free a previously allocated page-aligned region. - * - * @param p Exact value returned from page_aligned_alloc - * @param unaligned_size Exact value passed to page_aligned_alloc - **/ -void page_aligned_free(void* p, size_t unaligned_size); - -#endif // #ifndef INCLUDED_ALLOCATORS_PAGE_ALIGNED