2010-02-08 17:23:39 +01:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* 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.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* memory allocator helper routines.
|
2009-04-18 18:14:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_MEM_UTIL
|
|
|
|
#define INCLUDED_MEM_UTIL
|
|
|
|
|
|
|
|
LIB_API bool mem_IsPageMultiple(uintptr_t x);
|
|
|
|
|
|
|
|
LIB_API size_t mem_RoundUpToPage(size_t size);
|
|
|
|
LIB_API size_t mem_RoundUpToAlignment(size_t size);
|
|
|
|
|
|
|
|
|
|
|
|
// very thin wrapper on top of sys/mman.h that makes the intent more obvious
|
|
|
|
// (its commit/decommit semantics are difficult to tell apart)
|
|
|
|
LIB_API LibError mem_Reserve(size_t size, u8** pp);
|
|
|
|
LIB_API LibError mem_Release(u8* p, size_t size);
|
|
|
|
LIB_API LibError mem_Commit(u8* p, size_t size, int prot);
|
|
|
|
LIB_API LibError mem_Decommit(u8* p, size_t size);
|
|
|
|
LIB_API LibError mem_Protect(u8* p, size_t size, int prot);
|
|
|
|
|
|
|
|
|
2011-03-28 14:28:29 +02:00
|
|
|
// "freelist" is a pointer to the first unused element or a sentinel.
|
|
|
|
// their memory holds a pointer to the previous element in the freelist
|
|
|
|
// (or its own address in the case of sentinels to avoid branches)
|
2010-06-05 20:52:39 +02:00
|
|
|
//
|
2009-04-18 18:14:48 +02:00
|
|
|
// rationale for the function-based interface: a class encapsulating the
|
2011-03-28 14:28:29 +02:00
|
|
|
// freelist pointer would force each header to include mem_util.h,
|
|
|
|
// whereas this approach only requires a void* pointer and calling
|
|
|
|
// mem_freelist_Init from the implementation.
|
2010-06-05 20:52:39 +02:00
|
|
|
//
|
|
|
|
// these functions are inlined because allocation is sometimes time-critical.
|
|
|
|
|
2011-03-28 14:28:29 +02:00
|
|
|
// @return the address of a sentinel element, suitable for initializing
|
|
|
|
// a freelist pointer. subsequent mem_freelist_Detach will return 0.
|
|
|
|
LIB_API void* mem_freelist_Sentinel();
|
|
|
|
|
2010-06-05 20:52:39 +02:00
|
|
|
static inline void mem_freelist_AddToFront(void*& freelist, void* el)
|
|
|
|
{
|
|
|
|
#ifndef NDEBUG
|
2011-03-28 14:28:29 +02:00
|
|
|
debug_assert(freelist != 0);
|
2010-06-05 20:52:39 +02:00
|
|
|
debug_assert(el != 0);
|
|
|
|
#endif
|
|
|
|
|
2011-03-28 14:28:29 +02:00
|
|
|
memcpy(el, &freelist, sizeof(void*));
|
2010-06-05 20:52:39 +02:00
|
|
|
freelist = el;
|
|
|
|
}
|
|
|
|
|
2011-03-28 14:28:29 +02:00
|
|
|
// @return 0 if the freelist is empty, else a pointer that had
|
|
|
|
// previously been passed to mem_freelist_AddToFront.
|
2010-06-05 20:52:39 +02:00
|
|
|
static inline void* mem_freelist_Detach(void*& freelist)
|
|
|
|
{
|
2011-03-28 14:28:29 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
debug_assert(freelist != 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void* prev_el;
|
|
|
|
memcpy(&prev_el, freelist, sizeof(void*));
|
|
|
|
void* el = (freelist == prev_el)? 0 : freelist;
|
|
|
|
freelist = prev_el;
|
2010-06-05 20:52:39 +02:00
|
|
|
return el;
|
|
|
|
}
|
2009-04-18 18:14:48 +02:00
|
|
|
|
|
|
|
#endif // #ifndef INCLUDED_MEM_UTIL
|