Pyrogenesis  trunk
freelist.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 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_ALLOCATORS_FREELIST
24 #define INCLUDED_ALLOCATORS_FREELIST
25 
26 #include <cstring>
27 
28 // "freelist" is a pointer to the first unused element or a sentinel.
29 // their memory holds a pointer to the previous element in the freelist
30 // (or its own address in the case of sentinels to avoid branches)
31 //
32 // rationale for the function-based interface: a class encapsulating the
33 // freelist pointer would force each header to include this header,
34 // whereas this approach only requires a void* pointer and calling
35 // mem_freelist_Sentinel from the implementation.
36 //
37 // these functions are inlined because allocation is sometimes time-critical.
38 
39 // @return the address of a sentinel element, suitable for initializing
40 // a freelist pointer. subsequent mem_freelist_Detach on that freelist
41 // will return 0.
42 void* mem_freelist_Sentinel();
43 
44 static inline void mem_freelist_AddToFront(void*& freelist, void* el)
45 {
46  ASSERT(freelist != 0);
47  ASSERT(el != 0);
48 
49  memcpy(el, &freelist, sizeof(void*));
50  freelist = el;
51 }
52 
53 // @return 0 if the freelist is empty, else a pointer that had
54 // previously been passed to mem_freelist_AddToFront.
55 static inline void* mem_freelist_Detach(void*& freelist)
56 {
57  ASSERT(freelist != 0);
58 
59  void* prev_el;
60  memcpy(&prev_el, freelist, sizeof(void*));
61  void* el = (freelist == prev_el)? 0 : freelist;
62  freelist = prev_el;
63  return el;
64 }
65 
66 #endif // #ifndef INCLUDED_ALLOCATORS_FREELIST
#define ASSERT(expr)
same as ENSURE in debug mode, does nothing in release mode.
Definition: debug.h:318
void * mem_freelist_Sentinel()
Definition: freelist.cpp:26
static void * mem_freelist_Detach(void *&freelist)
Definition: freelist.h:55
static void mem_freelist_AddToFront(void *&freelist, void *el)
Definition: freelist.h:44