3 /* Copyright 1998 by the Massachusetts Institute of Technology.
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.
21 #include "ares_private.h"
23 /* Routines for managing doubly-linked circular linked lists with a
27 /* Initialize a new head node */
28 void ares__init_list_head(struct list_node* head) {
34 /* Initialize a list node */
35 void ares__init_list_node(struct list_node* node, void* d) {
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));
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;
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;
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;
74 ares__init_list_head(head_b);
77 old_a.next->prev = head_b;
78 old_a.prev->next = head_b;
81 ares__init_list_head(head_a);
84 old_b.next->prev = head_a;
85 old_b.prev->next = head_a;