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