Support Watt-32 under Win32.
[platform/upstream/c-ares.git] / ares_destroy.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 #include <assert.h>
20 #include <stdlib.h>
21 #include "ares.h"
22 #include "ares_private.h"
23
24 void ares_destroy_options(struct ares_options *options)
25 {
26   int i;
27
28   free(options->servers);
29   for (i = 0; i < options->ndomains; i++)
30     free(options->domains[i]);
31   free(options->domains);
32   if(options->sortlist)
33     free(options->sortlist);
34   free(options->lookups);
35 }
36
37 void ares_destroy(ares_channel channel)
38 {
39   int i;
40   struct query *query;
41   struct list_node* list_head;
42   struct list_node* list_node;
43   
44   if (!channel)
45     return;
46
47   list_head = &(channel->all_queries);
48   for (list_node = list_head->next; list_node != list_head; )
49     {
50       query = list_node->data;
51       list_node = list_node->next;  /* since we're deleting the query */
52       query->callback(query->arg, ARES_EDESTRUCTION, 0, NULL, 0);
53       ares__free_query(query);
54     }
55 #ifndef NDEBUG
56   /* Freeing the query should remove it from all the lists in which it sits,
57    * so all query lists should be empty now.
58    */
59   assert(ares__is_list_empty(&(channel->all_queries)));
60   for (i = 0; i < ARES_QID_TABLE_SIZE; i++)
61     {
62       assert(ares__is_list_empty(&(channel->queries_by_qid[i])));
63     }
64   for (i = 0; i < ARES_TIMEOUT_TABLE_SIZE; i++)
65     {
66       assert(ares__is_list_empty(&(channel->queries_by_timeout[i])));
67     }
68 #endif
69
70   if (channel->servers) {
71     for (i = 0; i < channel->nservers; i++)
72       {
73         struct server_state *server = &channel->servers[i];
74         ares__close_sockets(channel, server);
75         assert(ares__is_list_empty(&(server->queries_to_server)));
76       }
77     free(channel->servers);
78   }
79
80   if (channel->domains) {
81     for (i = 0; i < channel->ndomains; i++)
82       free(channel->domains[i]);
83     free(channel->domains);
84   }
85
86   if(channel->sortlist)
87     free(channel->sortlist);
88
89   if (channel->lookups)
90     free(channel->lookups);
91
92   free(channel);
93 }