LCOV - code coverage report
Current view: top level - source/lib/allocators - freelist.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 12 0.0 %
Date: 2023-01-19 00:18:29 Functions: 0 2 0.0 %

          Line data    Source code
       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           0 : static inline void mem_freelist_AddToFront(void*& freelist, void* el)
      45             : {
      46           0 :     ASSERT(freelist != 0);
      47           0 :     ASSERT(el != 0);
      48             : 
      49           0 :     memcpy(el, &freelist, sizeof(void*));
      50           0 :     freelist = el;
      51           0 : }
      52             : 
      53             : // @return 0 if the freelist is empty, else a pointer that had
      54             : // previously been passed to mem_freelist_AddToFront.
      55           0 : static inline void* mem_freelist_Detach(void*& freelist)
      56             : {
      57           0 :     ASSERT(freelist != 0);
      58             : 
      59             :     void* prev_el;
      60           0 :     memcpy(&prev_el, freelist, sizeof(void*));
      61           0 :     void* el = (freelist == prev_el)? 0 : freelist;
      62           0 :     freelist = prev_el;
      63           0 :     return el;
      64             : }
      65             : 
      66             : #endif  // #ifndef INCLUDED_ALLOCATORS_FREELIST

Generated by: LCOV version 1.13