Skip to content
Permalink
cbb753fee9
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
102 lines (88 sloc) 2.89 KB
#ifndef __dynarr_free_node_list_h__
#define __dynarr_free_node_list_h__
// the struct FreeSegNode will be used multiple times, so we do not want to redefine it
struct FreeSegNode{
int index;
struct FreeSegNode* next;
};
typedef struct FreeSegNode FreeSegNode;
// name of variables
#define FREESEG_LIST_NAME pFreeSegList
#define FREESEG_INVALID_LIST_NAME pFreeSegInvalidList
#define _FreeSegNodeMalloc() \
( (struct FreeSegNode*) malloc(sizeof(struct FreeSegNode)) )
// Swaps two 32-bit intergers without using temporary variables
#define _SEG_ARR_INT_SWAP(var1, var2) (\
(var1) = (FreeSegNode*) (((int) (void*) var1) ^ ((int) (void*) var2) ), \
(var2) = (FreeSegNode*) (((int) (void*) var1) ^ ((int) (void*) var2) ), \
(var1) = (FreeSegNode*) (((int) (void*) var1) ^ ((int) (void*) var2) ) )
// Given two lists A and B, moves the head of A in front of the
// head of B. Head of A must be non-null.
//
// Before:
// A-->1-2-3-4-5
// B-->a-b-c-d-e
// After:
// A-->2-3-4-5
// B-->1-a-b-c-d-e
//
#define _SEG_ARR_MOVE_LIST_HEAD(listFrom, listTo) (\
_SEG_ARR_INT_SWAP( listFrom->next, listTo), \
_SEG_ARR_INT_SWAP( listFrom, listTo ) \
)
#define _SEG_ARR_FREE_LIST_ADD_VALIDLIST(value_seg) \
( ( !FREESEG_LIST_NAME ) \
?( \
FREESEG_LIST_NAME = _FreeSegNodeMalloc(), \
/*assert(FREESEG_LIST_NAME != NULL ),*/ \
FREESEG_LIST_NAME->next = NULL, \
FREESEG_LIST_NAME->index = ((value_seg).index) \
) \
:( \
pTempFreeNode1 = _FreeSegNodeMalloc(), \
/*assert(pTempFreeNode1 != NULL ),*/ \
pTempFreeNode1->next = FREESEG_LIST_NAME, \
FREESEG_LIST_NAME = pTempFreeNode1, \
FREESEG_LIST_NAME->index = ((value_seg).index) \
)\
)
// Algorithm:
// If (an invalid node exists) Then
// move it to the front of valid list
// set its index as the index of value_seg
// Else
// Allocate new node in the valid list
//
#define _SEG_ARR_FREE_LIST_ADD(value_seg) (\
( FREESEG_INVALID_LIST_NAME ) \
? ( \
_SEG_ARR_MOVE_LIST_HEAD( FREESEG_INVALID_LIST_NAME, FREESEG_LIST_NAME ), \
FREESEG_LIST_NAME->index = ((value_seg).index) \
) \
: ( _SEG_ARR_FREE_LIST_ADD_VALIDLIST(value_seg) ) \
)
#define _SEG_ARR_REMOVE_LIST_HEAD(list) (\
pTempFreeNode1 = list, \
list = list->next, \
FREE_SAFE( pTempFreeNode1 ) )\
// Algorithm:
// If (a valid node exists) Then
// output its index, and
// move it to the front of invalid list
// Else
// output null index
//
#define _SEG_ARR_FREE_LIST_GRAB_INDEX(value_seg) (\
( FREESEG_LIST_NAME ) ?(\
(value_seg).index = FREESEG_LIST_NAME->index, \
(value_seg).nextIndex = EMPTY_INDEX, \
_SEG_ARR_MOVE_LIST_HEAD( FREESEG_LIST_NAME, FREESEG_INVALID_LIST_NAME ), \
(value_seg) \
):(\
( (value_seg) = NULL_SEG ) \
) )\
#define _SEG_ARR_FREE_LIST_CLEANUP() {\
while( FREESEG_LIST_NAME ) _SEG_ARR_REMOVE_LIST_HEAD(FREESEG_LIST_NAME); \
while( FREESEG_INVALID_LIST_NAME ) _SEG_ARR_REMOVE_LIST_HEAD(FREESEG_INVALID_LIST_NAME); \
} \
#endif