~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/sys/lists/proplist.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**@(#)Property list handling.  (Using generic lists & Props).
  2  * @(#)Prop is { int type; int count; void (*freefunc)(); void *to; }
  3  */
  4 
  5 #include <stdio.h>
  6 #include <tina/sys.h>
  7 #include <tina/sys_types.h>
  8 #include <tina/sysfuncs.h>
  9 #include <tina/lists.h>
 10 
 11 /* Alloc and init prop structure */
 12 static Prop *prop_alloc(void *ptr, int type, void (*freefunc) ( /* ??? */ ))
 13 {
 14     Prop   *prop = ts_ralloc(Prop);
 15 
 16     prop->type = type;
 17     prop->freefunc = freefunc;
 18     prop->count = 1;
 19     prop->to = ptr;
 20     return (prop);
 21 }
 22 
 23 /* Get first ref of given type */
 24 static Prop *prop_get_by_type(List * proplist, int type)
 25 {
 26     List   *lptr;
 27     Prop   *prop;
 28 
 29     for (lptr = proplist; lptr != NULL; lptr = lptr->next)
 30     {
 31         prop = (Prop *) lptr->to;
 32         if (prop!= NULL && prop->type == type)
 33             return (prop);
 34     }
 35 
 36     return (NULL);
 37 }
 38 
 39 static Prop *prop_get_by_ref(List * proplist, void *ptr)
 40 {
 41     List   *lptr;
 42     Prop   *prop;
 43 
 44     for (lptr = proplist; lptr != NULL; lptr = lptr->next)
 45     {
 46         prop = (Prop *) lptr->to;
 47         if (prop->to == ptr)
 48             return (prop);
 49     }
 50 
 51     return (NULL);
 52 }
 53 
 54 /* Add a new prop to front */
 55 List   *proplist_add(List * proplist, void *ptr, int type, void (*freefunc) ( /* ??? */ ))
 56 {
 57     List   *lptr;
 58 
 59     lptr = link_alloc((void *) prop_alloc(ptr, type, freefunc), PROP_TYPE);
 60     return (link_addtostart(proplist, lptr));
 61 }
 62 
 63 /* Add if not present */
 64 List   *proplist_addifnp(List * proplist, void *ptr, int type, void (*freefunc) ( /* ??? */ ), Bool dofree)
 65 {
 66     List   *lptr;
 67     Prop   *prop = prop_get_by_type(proplist, type);
 68 
 69     if (prop != NULL)
 70     {
 71         if (dofree == true && prop->freefunc != NULL)
 72             prop->freefunc(prop->to, prop->type);
 73         prop->to = ptr;
 74         return (proplist);
 75     }
 76     lptr = link_alloc((void *) prop_alloc(ptr, type, freefunc), PROP_TYPE);
 77     return (link_addtostart(proplist, lptr));
 78 }
 79 
 80 /* Get first prop of specified type */
 81 void   *prop_get(List * proplist, int type)
 82 {
 83     Prop   *prop = prop_get_by_type(proplist, type);
 84 
 85     return ((prop == NULL) ? NULL : prop->to);
 86 }
 87 
 88 int     prop_present(List * proplist, int type)
 89 {
 90     Prop   *prop = prop_get_by_type(proplist, type);
 91 
 92     return (prop != NULL);
 93 }
 94 
 95 int     prop_ref_present(List * proplist, void *ptr)
 96 {
 97     List   *lptr;
 98 
 99     for (lptr = proplist; lptr != NULL; lptr = lptr->next)
100         if (((Prop *) lptr->to)->to == ptr)
101             return (1);
102     return (0);
103 }
104 
105 /* Get first element of * proplist */
106 void   *prop_first(List * proplist, int *type)
107 {
108     if (proplist == NULL)
109     {
110         *type = 0;
111         return (NULL);
112     }
113     *type = ((Prop *) proplist->to)->type;
114     return (((Prop *) proplist->to)->to);
115 }
116 
117 /* Set/reset prop of specified type */
118 int     prop_set(List * proplist, void *ptr, int type, Bool dofree)
119 {
120     /* if true try to free the referenced property value */
121     Prop   *prop = prop_get_by_type(proplist, type);
122 
123     if (prop == NULL)
124         return (0);
125 
126     if (dofree == true && prop->freefunc != NULL)
127         prop->freefunc(prop->to, prop->type);
128 
129     prop->to = ptr;
130     return (1);
131 }
132 
133 List   *proplist_copy(List * proplist)
134 {
135     List   *list_copy();
136     List   *copy;
137     List   *lptr;
138 
139     copy = list_copy(proplist, (void *(*) ()) NULL, NULL);      /* copy list only (not
140                                                                  * prop) */
141 
142     for (lptr = copy; lptr != NULL; lptr = lptr->next)
143         ++((Prop *) (lptr->to))->count;
144     return (copy);
145 }
146 
147 /* Rm prop ref from list */
148 List   *proplist_rm(List * proplist, int type)
149 {
150     Prop   *prop = prop_get_by_type(proplist, type);
151     List   *list_rm_ref();
152 
153     if (prop == NULL)
154         return (proplist);
155 
156     if (--prop->count)
157         return (list_rm_ref(proplist, (void *) prop, (void (*) ()) NULL));
158     else
159         return (list_rm_ref(proplist, (void *) prop, (void (*) ()) rfree));
160 }
161 
162 /* Rm prop ref from list */
163 List   *proplist_rm_by_ref(List * proplist, void *ptr)
164 {
165     Prop   *prop = prop_get_by_ref(proplist, ptr);
166     List   *list_rm_ref();
167 
168     if (prop == NULL)
169         return (proplist);
170 
171     if (--prop->count)
172         return (list_rm_ref(proplist, (void *) prop, (void (*) ()) NULL));
173     else
174         return (list_rm_ref(proplist, (void *) prop, (void (*) ()) rfree));
175 }
176 
177 /* Rm prop ref and free prop */
178 List   *proplist_free(List * proplist, int type)
179 {
180     Prop   *prop = prop_get_by_type(proplist, type);
181     List   *list_rm_ref();
182 
183     if (prop == NULL)
184         return (proplist);
185 
186     if (--prop->count)
187         return (list_rm_ref(proplist, (void *) prop, (void (*) ()) NULL));
188     else
189     {                           /* last reference */
190         if (prop->freefunc != NULL)
191             prop->freefunc(prop->to, prop->type);
192 
193         return (list_rm_ref(proplist, (void *) prop, rfree));
194     }
195 }
196 
197 List   *proplist_free_by_ref(List * proplist, void *ptr)
198 {
199     Prop   *prop = prop_get_by_ref(proplist, ptr);
200     List   *list_rm_ref();
201 
202     if (prop == NULL)
203         return (proplist);
204 
205     if (--prop->count)
206         return (list_rm_ref(proplist, (void *) prop, (void (*) ()) NULL));
207     else
208     {                           /* last reference */
209         if (prop->freefunc != NULL)
210             prop->freefunc(prop->to, prop->type);
211 
212         return (list_rm_ref(proplist, (void *) prop, rfree));
213     }
214 }
215 
216 static void prop_rm(Prop * prop)
217 {
218     if (prop == NULL)
219         return;
220 
221     if (!(--prop->count))
222         rfree((void *) prop);
223 }
224 
225 static void prop_free(Prop * prop)
226 {
227     if (prop == NULL)
228         return;
229 
230     if (!(--prop->count))
231     {
232         if (prop->freefunc != NULL)
233             prop->freefunc(prop->to, prop->type);
234         rfree((void *) prop);
235     }
236 }
237 
238 /* Rm all references in proplist */
239 void    proplist_rmlist(List * proplist)
240 {
241     void    list_rm();
242 
243     list_rm(proplist, prop_rm);
244 }
245 
246 /* Rm the first property in the list */
247 List   *proplist_rm_first(List * proplist)
248 {
249     List   *link_rm();
250 
251     return (link_rm(proplist, prop_rm));
252 }
253 
254 /* Free whole list */
255 void    proplist_freelist(List * proplist)
256 {
257     void    list_rm();
258 
259     list_rm(proplist, prop_free);
260 }
261 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.