Support Watt-32 under Win32.
[platform/upstream/c-ares.git] / ares_llist.c
1 /* $Id$ */
2
3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4  *
5  * Permission to use, copy, modify, and distribute this
6  * software and its documentation for any purpose and without
7  * fee is hereby granted, provided that the above copyright
8  * notice appear in all copies and that both that copyright
9  * notice and this permission notice appear in supporting
10  * documentation, and that the name of M.I.T. not be used in
11  * advertising or publicity pertaining to distribution of the
12  * software without specific, written prior permission.
13  * M.I.T. makes no representations about the suitability of
14  * this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  */
17
18 #include "setup.h"
19
20 #include "ares.h"
21 #include "ares_private.h"
22
23 /* Routines for managing doubly-linked circular linked lists with a
24  * dummy head.
25  */
26
27 /* Initialize a new head node */
28 void ares__init_list_head(struct list_node* head) {
29   head->prev = head;
30   head->next = head;
31   head->data = NULL;
32 }
33
34 /* Initialize a list node */
35 void ares__init_list_node(struct list_node* node, void* d) {
36   node->prev = NULL;
37   node->next = NULL;
38   node->data = d;
39 }
40
41 /* Returns true iff the given list is empty */
42 int ares__is_list_empty(struct list_node* head) {
43   return ((head->next == head) && (head->prev == head));
44 }
45
46 /* Inserts new_node before old_node */
47 void ares__insert_in_list(struct list_node* new_node,
48                           struct list_node* old_node) {
49   new_node->next = old_node;
50   new_node->prev = old_node->prev;
51   old_node->prev->next = new_node;
52   old_node->prev = new_node;
53 }
54
55 /* Removes the node from the list it's in, if any */
56 void ares__remove_from_list(struct list_node* node) {
57   if (node->next != NULL) {
58     node->prev->next = node->next;
59     node->next->prev = node->prev;
60     node->prev = NULL;
61     node->next = NULL;
62   }
63 }
64
65 /* Swap the contents of two lists */
66 void ares__swap_lists(struct list_node* head_a,
67                       struct list_node* head_b) {
68   int is_a_empty = ares__is_list_empty(head_a);
69   int is_b_empty = ares__is_list_empty(head_b);
70   struct list_node old_a = *head_a;
71   struct list_node old_b = *head_b;
72
73   if (is_a_empty) {
74     ares__init_list_head(head_b);
75   } else {
76     *head_b = old_a;
77     old_a.next->prev = head_b;
78     old_a.prev->next = head_b;
79   }
80   if (is_b_empty) {
81     ares__init_list_head(head_a);
82   } else {
83     *head_a = old_b;
84     old_b.next->prev = head_a;
85     old_b.prev->next = head_a;
86   }
87 }