| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2002, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile:$ |
|---|
| 11 | * $brcm_Revision:$ |
|---|
| 12 | * $brcm_Date:$ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: C Based List Header File |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * Created: 2002 by Marcus Kellerman |
|---|
| 19 | * |
|---|
| 20 | * $brcm_Log:$ |
|---|
| 21 | * |
|---|
| 22 | * Here's a code sample of how to use genericlist.h: |
|---|
| 23 | * |
|---|
| 24 | * #include "genericlist.h" |
|---|
| 25 | * |
|---|
| 26 | * typedef struct { |
|---|
| 27 | * // your structure must have a LINKS_T as the first member. |
|---|
| 28 | * LINKS_T links; |
|---|
| 29 | * int x, y, z; |
|---|
| 30 | * } MyNode; |
|---|
| 31 | * |
|---|
| 32 | * int main() { |
|---|
| 33 | * LIST_T list; |
|---|
| 34 | * MyNode *n; |
|---|
| 35 | * |
|---|
| 36 | * initl(&list); |
|---|
| 37 | * inslt(&list, createNode()); |
|---|
| 38 | * inslt(&list, createNode()); |
|---|
| 39 | * inslt(&list, createNode()); |
|---|
| 40 | * // pass the list to LHEAD, pass the current node to LNEXT. |
|---|
| 41 | * for (n = (MyNode*)LHEAD(&list); n; n = (MyNode*)LNEXT(n)) { |
|---|
| 42 | * doSomething(n); |
|---|
| 43 | * } |
|---|
| 44 | * for (n = (MyNode*)remlh(&list); n; n = (MyNode*)remlh(&list)) { |
|---|
| 45 | * deleteNode(n); |
|---|
| 46 | * } |
|---|
| 47 | * } |
|---|
| 48 | * |
|---|
| 49 | ***************************************************************************/ |
|---|
| 50 | #ifndef LIST_H |
|---|
| 51 | #define LIST_H |
|---|
| 52 | |
|---|
| 53 | #ifdef __cplusplus |
|---|
| 54 | extern "C" { |
|---|
| 55 | #endif |
|---|
| 56 | |
|---|
| 57 | /* The data structure which describes the links. */ |
|---|
| 58 | typedef struct links_struct |
|---|
| 59 | { |
|---|
| 60 | struct links_struct *p_next; |
|---|
| 61 | struct links_struct *p_prev; |
|---|
| 62 | struct links_struct *p_origin; |
|---|
| 63 | } LINKS_T; |
|---|
| 64 | |
|---|
| 65 | /* The data structure which describes the list. */ |
|---|
| 66 | typedef struct list_struct |
|---|
| 67 | { |
|---|
| 68 | LINKS_T origin_links; |
|---|
| 69 | unsigned short count; /* number of elements in the list */ |
|---|
| 70 | } LIST_T; |
|---|
| 71 | |
|---|
| 72 | /* Prototypes */ |
|---|
| 73 | void *remlh(LIST_T *); |
|---|
| 74 | void inslt(LIST_T *, void *); |
|---|
| 75 | void inslh(LIST_T *, void *); |
|---|
| 76 | void inslb(LIST_T *p_list, void *p_element, void *p_newElement); |
|---|
| 77 | void reml(LIST_T *, void *); |
|---|
| 78 | void initl(LIST_T *); |
|---|
| 79 | |
|---|
| 80 | /* Check to see if the list pointed to by (list) is empty. */ |
|---|
| 81 | #define LEMPTY(list) ((list)==0?1:(((LIST_T *)list)->count == 0 ? 1 : 0)) |
|---|
| 82 | |
|---|
| 83 | /* Return the head of a list pointed to by (list). If the list is empty, |
|---|
| 84 | ** LIST_HEAD returns zero. In actuality, an empty list has it's p_next and |
|---|
| 85 | ** p_prev pointers pointing to the list (which is (list)) for code simplistic |
|---|
| 86 | ** reasons. In this case, just check for (list) equal to p_next to determine |
|---|
| 87 | ** if the list is empty. */ |
|---|
| 88 | #define LHEAD(list) ((list)==0?0:(((void *)(list) == ((LIST_T *)list)->origin_links.p_next) ? 0 : ((LIST_T *)list)->origin_links.p_next)) |
|---|
| 89 | |
|---|
| 90 | /* Return the tail of a list pointed to by (list). If the tail is empty, |
|---|
| 91 | ** LIST_TAIL returns zero. In actuality, an empty list has it's p_next and |
|---|
| 92 | ** p_prev pointers pointing to the list (which is (list)) for code simplistic |
|---|
| 93 | ** reasons. In this case, just check for (list) equal to p_next to determine |
|---|
| 94 | ** if the list is empty. */ |
|---|
| 95 | #define LTAIL(list) ((list)==0?0:(((void *)(list) == ((LIST_T *)list)->origin_links.p_prev) ? 0 : ((LIST_T *)list)->origin_links.p_prev)) |
|---|
| 96 | |
|---|
| 97 | /* Return the number of elements in the list pointed to by (a). */ |
|---|
| 98 | #define LSIZE(list) ((list)==0?0:((LIST_T *)list)->count) |
|---|
| 99 | |
|---|
| 100 | /* Return a pointer to next element of element a. If a is the tail of the list, |
|---|
| 101 | ** LNEXT returns zero.*/ |
|---|
| 102 | #define LNEXT(link) ((link)==0?0:((((LINKS_T *)link)->p_next == ((LINKS_T *)link)->p_origin) ? 0 : ((LINKS_T *)link)->p_next)) |
|---|
| 103 | |
|---|
| 104 | /* Return a pointer to prev element of element a. If a is the head of the list, |
|---|
| 105 | ** LPREV returns zero.*/ |
|---|
| 106 | #define LPREV(link) ((link)==0?0:((((LINKS_T *)link)->p_prev == ((LINKS_T *)link)->p_origin) ? 0 : ((LINKS_T *)link)->p_prev)) |
|---|
| 107 | |
|---|
| 108 | #ifdef __cplusplus |
|---|
| 109 | } |
|---|
| 110 | #endif |
|---|
| 111 | |
|---|
| 112 | #endif /* LIST_H */ |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|