/***************************************************************************** * * @File: LIST.C - C Based List C File * @Author: Marcus Kellerman * * Broadcom Corporation Confidential * Copyright 2000 - All Rights Reserved. * ****************************************************************************/ #include "genericlist.h" /******************************************************************************* ** External Functions *******************************************************************************/ /******************************************************************************* ** Name: initl ** Description: Initialize a list. ** ** In: p_list pointer to a list. ** Out: None. ** Ret: None. *******************************************************************************/ void initl(LIST_T *p_list) { if( !p_list ) return; p_list->origin_links.p_next = (LINKS_T *)p_list; p_list->origin_links.p_prev = (LINKS_T *)p_list; p_list->origin_links.p_origin = (LINKS_T *)p_list; p_list->count = 0; } /******************************************************************************* ** Name: inslt ** Description: Insert an element to the tail of a list. ** ** In: p_list Pointer to a list. ** p_element Pointer to an element. ** Out: None. ** Ret: None. *******************************************************************************/ void inslt(LIST_T *p_list, void *p_element) { if( !p_list || !p_element ) return; ((LINKS_T *)p_element)->p_origin = (LINKS_T *)p_list; ((LINKS_T *)p_element)->p_next = p_list->origin_links.p_prev->p_next; ((LINKS_T *)p_element)->p_prev = p_list->origin_links.p_prev; ((LINKS_T *)(p_list->origin_links.p_prev))->p_next = (LINKS_T *)p_element; p_list->origin_links.p_prev = (LINKS_T *)p_element; p_list->count++; } /******************************************************************************* ** Name: inslh ** Description: Insert an element to the head of a list. ** ** In: p_list Pointer to a list. ** p_element Pointer to an element. ** Out: None. ** Ret: None. *******************************************************************************/ void inslh(LIST_T *p_list, void *p_element) { if( !p_list || !p_element ) return; ((LINKS_T *)p_element)->p_origin = (LINKS_T *)p_list; ((LINKS_T *)p_element)->p_next = p_list->origin_links.p_next; ((LINKS_T *)p_element)->p_prev = p_list->origin_links.p_next->p_prev; ((LINKS_T *)(p_list->origin_links.p_next))->p_prev = (LINKS_T *)p_element; p_list->origin_links.p_next = (LINKS_T *)p_element; p_list->count++; } /******************************************************************************* ** Name: remlh ** Description: Remove an element from the head of a list. ** ** In: p_list Pointer to a list. ** Out: None. ** Ret: Pointer to the removed element, if it exists. Otherwise return 0. *******************************************************************************/ void *remlh(LIST_T *p_list) { if( !p_list ) return 0; if (p_list->count) { LINKS_T *p_element = p_list->origin_links.p_next; /* Update the list's next pointer. */ ((LINKS_T *)p_list)->p_next = ((LINKS_T *)p_list)->p_next->p_next; /* Update the previous pointer for the new head of the list. */ ((LINKS_T *)p_list)->p_next->p_prev = (LINKS_T *)p_list; /* Decrement the count. */ p_list->count--; /* Return a pointer to the newly removed element. */ return p_element; } else { /* The list is empty, return a pointer to 0. */ return 0; } } /******************************************************************************* ** Name: reml ** Description: Remove an element from anywhere on a list. ** ** In: p_list Pointer to a list. ** p_element Pointer to an element in that list. ** Out: None. ** Ret: None. *******************************************************************************/ void reml(LIST_T *p_list, void *p_element) { if( !p_list || !p_element ) return; (((LINKS_T *)p_element)->p_prev)->p_next = ((LINKS_T *)p_element)->p_next; (((LINKS_T *)p_element)->p_next)->p_prev = ((LINKS_T *)p_element)->p_prev; p_list->count--; } /******************************************************************************* ** Name: inslb ** Description: Insert an element before another element in the list. ** ** In: p_list Pointer to a list. ** p_element Pointer to an element in that list. ** p_newElement Pointer to the element to insert ** Out: None. ** Ret: None. *******************************************************************************/ void inslb(LIST_T *p_list, void *p_element, void *p_newElement) { if( !p_list || !p_element || !p_newElement ) return; ((LINKS_T *)p_newElement)->p_origin = (LINKS_T *)p_list; ((LINKS_T *)p_newElement)->p_next = (LINKS_T *)p_element; ((LINKS_T *)p_newElement)->p_prev = ((LINKS_T *)p_element)->p_prev; (((LINKS_T *)p_element)->p_prev)->p_next = (LINKS_T *)p_newElement; ((LINKS_T *)p_element)->p_prev = (LINKS_T *)p_newElement; p_list->count++; }