]>
git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSShared/GenLinkedList.c
1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 Contains: implementation of generic linked lists.
25 #include "GenLinkedList.h"
28 // Return the link pointer contained within element e at offset o.
29 #define GETLINK( e, o) ( *(void**)((char*) (e) + (o)) )
31 // Assign the link pointer l to element e at offset o.
32 #define ASSIGNLINK( e, l, o) ( *((void**)((char*) (e) + (o))) = (l))
35 // GenLinkedList /////////////////////////////////////////////////////////////
37 void InitLinkedList( GenLinkedList
*pList
, size_t linkOffset
)
38 /* Initialize the block of memory pointed to by pList as a linked list. */
42 pList
->LinkOffset
= linkOffset
;
46 void AddToTail( GenLinkedList
*pList
, void *elem
)
47 /* Add a linked list element to the tail of the list. */
50 ASSIGNLINK( pList
->Tail
, elem
, pList
->LinkOffset
);
53 ASSIGNLINK( elem
, NULL
, pList
->LinkOffset
);
59 void AddToHead( GenLinkedList
*pList
, void *elem
)
60 /* Add a linked list element to the head of the list. */
62 ASSIGNLINK( elem
, pList
->Head
, pList
->LinkOffset
);
63 if ( pList
->Tail
== NULL
)
70 int RemoveFromList( GenLinkedList
*pList
, void *elem
)
71 /* Remove a linked list element from the list. Return 0 if it was not found. */
72 /* If the element is removed, its link will be set to NULL. */
74 void *iElem
, *lastElem
;
76 for ( iElem
= pList
->Head
, lastElem
= NULL
; iElem
; iElem
= GETLINK( iElem
, pList
->LinkOffset
)) {
78 if ( lastElem
) { // somewhere past the head
79 ASSIGNLINK( lastElem
, GETLINK( elem
, pList
->LinkOffset
), pList
->LinkOffset
);
80 } else { // at the head
81 pList
->Head
= GETLINK( elem
, pList
->LinkOffset
);
83 if ( pList
->Tail
== elem
)
84 pList
->Tail
= lastElem
? lastElem
: NULL
;
85 ASSIGNLINK( elem
, NULL
, pList
->LinkOffset
); // maybe catch a stale reference bug.
95 int ReplaceElem( GenLinkedList
*pList
, void *elemInList
, void *newElem
)
96 /* Replace an element in the list with a new element, in the same position. */
98 void *iElem
, *lastElem
;
100 if ( elemInList
== NULL
|| newElem
== NULL
)
103 for ( iElem
= pList
->Head
, lastElem
= NULL
; iElem
; iElem
= GETLINK( iElem
, pList
->LinkOffset
))
105 if ( iElem
== elemInList
)
107 ASSIGNLINK( newElem
, GETLINK( elemInList
, pList
->LinkOffset
), pList
->LinkOffset
);
108 if ( lastElem
) // somewhere past the head
110 ASSIGNLINK( lastElem
, newElem
, pList
->LinkOffset
);
114 pList
->Head
= newElem
;
116 if ( pList
->Tail
== elemInList
)
117 pList
->Tail
= newElem
;
127 // GenDoubleLinkedList /////////////////////////////////////////////////////////
129 void InitDoubleLinkedList( GenDoubleLinkedList
*pList
, size_t fwdLinkOffset
,
130 size_t backLinkOffset
)
131 /* Initialize the block of memory pointed to by pList as a double linked list. */
135 pList
->FwdLinkOffset
= fwdLinkOffset
;
136 pList
->BackLinkOffset
= backLinkOffset
;
140 void DLLAddToHead( GenDoubleLinkedList
*pList
, void *elem
)
141 /* Add a linked list element to the head of the list. */
147 // fix up the forward links
148 ASSIGNLINK( elem
, pList
->Head
, pList
->FwdLinkOffset
);
151 // fix up the backward links
153 ASSIGNLINK( pNext
, elem
, pList
->BackLinkOffset
);
156 ASSIGNLINK( elem
, NULL
, pList
->BackLinkOffset
);
160 void DLLRemoveFromList( GenDoubleLinkedList
*pList
, void *elem
)
161 /* Remove a linked list element from the list. */
162 /* When the element is removed, its link will be set to NULL. */
166 pNext
= GETLINK( elem
, pList
->FwdLinkOffset
);
167 pPrev
= GETLINK( elem
, pList
->BackLinkOffset
);
169 // fix up the forward links
171 ASSIGNLINK( pPrev
, pNext
, pList
->FwdLinkOffset
);
175 // fix up the backward links
177 ASSIGNLINK( pNext
, pPrev
, pList
->BackLinkOffset
);
181 ASSIGNLINK( elem
, NULL
, pList
->FwdLinkOffset
);
182 ASSIGNLINK( elem
, NULL
, pList
->BackLinkOffset
);
186 // GenLinkedOffsetList /////////////////////////////////////////////////////
188 // Extract the Next offset from element
189 #define GETOFFSET( e, o) ( *(size_t*)((char*) (e) + (o)) )
191 static void AssignOffsetLink( void *elem
, void *link
, size_t linkOffset
);
194 static void AssignOffsetLink( void *elem
, void *link
, size_t linkOffset
)
195 // Assign link to elem as an offset from elem. Assign 0 to elem if link is NULL.
197 GETOFFSET( elem
, linkOffset
) = link
? (size_t) link
- (size_t) elem
: 0;
201 void *GetHeadPtr( GenLinkedOffsetList
*pList
)
202 /* Return a pointer to the head element of a list, or NULL if none. */
204 return pList
->Head
? ( (char*) (pList
) + pList
->Head
) : NULL
;
208 void *GetTailPtr( GenLinkedOffsetList
*pList
)
209 /* Return a pointer to the tail element of a list, or NULL if none. */
211 return pList
->Tail
? ( (char*) (pList
) + pList
->Tail
) : NULL
;
215 void *GetOffsetLink( GenLinkedOffsetList
*pList
, void *elem
)
216 /* Return the link pointer contained within element e for pList, or NULL if it is 0. */
220 nextOffset
= GETOFFSET( elem
, pList
->LinkOffset
);
222 return nextOffset
? (char*) elem
+ nextOffset
: NULL
;
226 void InitLinkedOffsetList( GenLinkedOffsetList
*pList
, size_t linkOffset
)
227 /* Initialize the block of memory pointed to by pList as a linked list. */
231 pList
->LinkOffset
= linkOffset
;
235 void OffsetAddToTail( GenLinkedOffsetList
*pList
, void *elem
)
236 /* Add a linked list element to the tail of the list. */
239 AssignOffsetLink( GetTailPtr( pList
), elem
, pList
->LinkOffset
);
241 pList
->Head
= (size_t) elem
- (size_t) pList
;
242 AssignOffsetLink( elem
, NULL
, pList
->LinkOffset
);
244 pList
->Tail
= (size_t) elem
- (size_t) pList
;
248 void OffsetAddToHead( GenLinkedOffsetList
*pList
, void *elem
)
249 /* Add a linked list element to the head of the list. */
251 AssignOffsetLink( elem
, GetHeadPtr( pList
), pList
->LinkOffset
);
252 if ( pList
->Tail
== 0)
253 pList
->Tail
= (size_t) elem
- (size_t) pList
;
255 pList
->Head
= (size_t) elem
- (size_t) pList
;
259 int OffsetRemoveFromList( GenLinkedOffsetList
*pList
, void *elem
)
260 /* Remove a linked list element from the list. Return 0 if it was not found. */
261 /* If the element is removed, its link will be set to NULL. */
263 void *iElem
, *lastElem
;
265 for ( iElem
= GetHeadPtr( pList
), lastElem
= NULL
; iElem
;
266 iElem
= GetOffsetLink( pList
, iElem
))
268 if ( iElem
== elem
) {
269 if ( lastElem
) { // somewhere past the head
270 AssignOffsetLink( lastElem
, GetOffsetLink( pList
, elem
), pList
->LinkOffset
);
271 } else { // at the head
272 iElem
= GetOffsetLink( pList
, elem
);
273 pList
->Head
= iElem
? (size_t) iElem
- (size_t) pList
: 0;
275 if ( GetTailPtr( pList
) == elem
)
276 pList
->Tail
= lastElem
? (size_t) lastElem
- (size_t) pList
: 0;
277 AssignOffsetLink( elem
, NULL
, pList
->LinkOffset
); // maybe catch a stale reference bug.
287 int OffsetReplaceElem( GenLinkedOffsetList
*pList
, void *elemInList
, void *newElem
)
288 /* Replace an element in the list with a new element, in the same position. */
290 void *iElem
, *lastElem
;
292 if ( elemInList
== NULL
|| newElem
== NULL
)
295 for ( iElem
= GetHeadPtr( pList
), lastElem
= NULL
; iElem
;
296 iElem
= GetOffsetLink( pList
, iElem
))
298 if ( iElem
== elemInList
)
300 AssignOffsetLink( newElem
, GetOffsetLink( pList
, elemInList
), pList
->LinkOffset
);
301 if ( lastElem
) // somewhere past the head
303 AssignOffsetLink( lastElem
, newElem
, pList
->LinkOffset
);
307 pList
->Head
= (size_t) newElem
- (size_t) pList
;
309 if ( GetTailPtr( pList
) == elemInList
)
310 pList
->Tail
= (size_t) newElem
- (size_t) pList
;