]> git.meshlink.io Git - catta/blob - avahi-core/rrlist.c
* implement hashmap
[catta] / avahi-core / rrlist.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #include <assert.h>
23
24 #include <avahi-common/llist.h>
25 #include <avahi-common/malloc.h>
26
27 #include "rrlist.h"
28 #include "log.h"
29
30 typedef struct AvahiRecordListItem AvahiRecordListItem;
31
32 struct AvahiRecordListItem {
33     int read;
34     AvahiRecord *record;
35     int unicast_response;
36     int flush_cache;
37     int auxiliary;
38     AVAHI_LLIST_FIELDS(AvahiRecordListItem, items);
39 };
40
41
42 struct AvahiRecordList {
43     AVAHI_LLIST_HEAD(AvahiRecordListItem, read);
44     AVAHI_LLIST_HEAD(AvahiRecordListItem, unread);
45 };
46
47 AvahiRecordList *avahi_record_list_new(void) {
48     AvahiRecordList *l;
49
50     if (!(l = avahi_new(AvahiRecordList, 1))) {
51         avahi_log_error("avahi_new() failed.");
52         return NULL;
53     }
54     
55     AVAHI_LLIST_HEAD_INIT(AvahiRecordListItem, l->read);
56     AVAHI_LLIST_HEAD_INIT(AvahiRecordListItem, l->unread);
57     return l;
58 }
59
60 void avahi_record_list_free(AvahiRecordList *l) {
61     assert(l);
62
63     avahi_record_list_flush(l);
64     avahi_free(l);
65 }
66
67 static void item_free(AvahiRecordList *l, AvahiRecordListItem *i) {
68     assert(l);
69     assert(i);
70
71     if (i->read) 
72         AVAHI_LLIST_REMOVE(AvahiRecordListItem, items, l->read, i);
73     else
74         AVAHI_LLIST_REMOVE(AvahiRecordListItem, items, l->unread, i);
75     
76     avahi_record_unref(i->record);
77     avahi_free(i);
78 }
79
80 void avahi_record_list_flush(AvahiRecordList *l) {
81     assert(l);
82     
83     while (l->read)
84         item_free(l, l->read);
85     while (l->unread)
86         item_free(l, l->unread);
87 }
88
89 AvahiRecord* avahi_record_list_next(AvahiRecordList *l, int *flush_cache, int *unicast_response, int *auxiliary) {
90     AvahiRecord *r;
91     AvahiRecordListItem *i;
92
93     if (!(i = l->unread))
94         return NULL;
95
96     assert(!i->read);
97     
98     r = avahi_record_ref(i->record);
99     if (unicast_response)
100         *unicast_response = i->unicast_response;
101     if (flush_cache)
102         *flush_cache = i->flush_cache;
103     if (auxiliary)
104         *auxiliary = i->auxiliary;
105
106     AVAHI_LLIST_REMOVE(AvahiRecordListItem, items, l->unread, i);
107     AVAHI_LLIST_PREPEND(AvahiRecordListItem, items, l->read, i);
108
109     i->read = TRUE;
110     
111     return r;
112 }
113
114 static AvahiRecordListItem *get(AvahiRecordList *l, AvahiRecord *r) {
115     AvahiRecordListItem *i;
116
117     assert(l);
118     assert(r);
119     
120     for (i = l->read; i; i = i->items_next)
121         if (avahi_record_equal_no_ttl(i->record, r))
122             return i;
123
124     for (i = l->unread; i; i = i->items_next)
125         if (avahi_record_equal_no_ttl(i->record, r))
126             return i;
127
128     return NULL;
129 }
130
131 void avahi_record_list_push(AvahiRecordList *l, AvahiRecord *r, int flush_cache, int unicast_response, int auxiliary) {
132     AvahiRecordListItem *i;
133         
134     assert(l);
135     assert(r);
136
137     if (get(l, r))
138         return;
139
140     if (!(i = avahi_new(AvahiRecordListItem, 1))) {
141         avahi_log_error("avahi_new() failed.");
142         return;
143     }
144     
145     i->unicast_response = unicast_response;
146     i->flush_cache = flush_cache;
147     i->auxiliary = auxiliary;
148     i->record = avahi_record_ref(r);
149     i->read = FALSE;
150
151     AVAHI_LLIST_PREPEND(AvahiRecordListItem, items, l->unread, i);
152 }
153
154 void avahi_record_list_drop(AvahiRecordList *l, AvahiRecord *r) {
155     AvahiRecordListItem *i;
156
157     assert(l);
158     assert(r);
159
160     if (!(i = get(l, r)))
161         return;
162
163     item_free(l, i);
164 }
165
166 int avahi_record_list_is_empty(AvahiRecordList *l) {
167     assert(l);
168     
169     return !l->unread && !l->read;
170 }