| 17 static ObjList *iter_next=NULL; |
17 static ObjList *iter_next=NULL; |
| 18 |
18 |
| 19 |
19 |
| 20 static void free_node(ObjList **objlist, ObjList *node) |
20 static void free_node(ObjList **objlist, ObjList *node) |
| 21 { |
21 { |
| 22 UNLINK_ITEM(*objlist, node, next, prev); |
22 UNLINK_ITEM(*objlist, node, next, prev); |
| 23 free(node); |
23 free(node); |
| 24 } |
24 } |
| 25 |
25 |
| 26 |
26 |
| 27 void watch_handler(Watch *watch, Obj *obj) |
27 void watch_handler(Watch *watch, Obj *obj) |
| 28 { |
28 { |
| 29 ObjList *node=(ObjList*)watch; |
29 ObjList *node=(ObjList*)watch; |
| 30 ObjList **list=node->list; |
30 ObjList **list=node->list; |
| 31 |
31 |
| 32 if(iter_next==node) |
32 if(iter_next==node) |
| 33 iter_next=node->next; |
33 iter_next=node->next; |
| 34 |
34 |
| 35 free_node(list, node); |
35 free_node(list, node); |
| 36 } |
36 } |
| 37 |
37 |
| 38 |
38 |
| 39 |
39 |
| 40 bool objlist_insert(ObjList **objlist, Obj *obj) |
40 bool objlist_insert(ObjList **objlist, Obj *obj) |
| 41 { |
41 { |
| 42 ObjList *node; |
42 ObjList *node; |
| 43 |
43 |
| 44 if(obj==NULL) |
44 if(obj==NULL) |
| 45 return FALSE; |
45 return FALSE; |
| 46 |
46 |
| 47 node=ALLOC(ObjList); |
47 node=ALLOC(ObjList); |
| 48 |
48 |
| 49 if(node==NULL) |
49 if(node==NULL) |
| 50 return FALSE; |
50 return FALSE; |
| 51 |
51 |
| 52 watch_init(&(node->watch)); |
52 watch_init(&(node->watch)); |
| 53 watch_setup(&(node->watch), obj, watch_handler); |
53 watch_setup(&(node->watch), obj, watch_handler); |
| 54 node->list=objlist; |
54 node->list=objlist; |
| 55 |
55 |
| 56 LINK_ITEM_FIRST(*objlist, node, next, prev); |
56 LINK_ITEM_FIRST(*objlist, node, next, prev); |
| 57 |
57 |
| 58 return TRUE; |
58 return TRUE; |
| 59 } |
59 } |
| 60 |
60 |
| 61 |
61 |
| 62 void objlist_remove(ObjList **objlist, Obj *obj) |
62 void objlist_remove(ObjList **objlist, Obj *obj) |
| 63 { |
63 { |
| 64 ObjList *node=*objlist; |
64 ObjList *node=*objlist; |
| 65 |
65 |
| 66 while(node!=NULL){ |
66 while(node!=NULL){ |
| 67 if(node->watch.obj==obj){ |
67 if(node->watch.obj==obj){ |
| 68 watch_reset(&(node->watch)); |
68 watch_reset(&(node->watch)); |
| 69 free_node(objlist, node); |
69 free_node(objlist, node); |
| 70 return; |
70 return; |
| 71 } |
71 } |
| 72 node=node->next; |
72 node=node->next; |
| 73 } |
73 } |
| 74 } |
74 } |
| 75 |
75 |
| 76 |
76 |
| 77 void objlist_clear(ObjList **objlist) |
77 void objlist_clear(ObjList **objlist) |
| 78 { |
78 { |
| 79 while(*objlist!=NULL){ |
79 while(*objlist!=NULL){ |
| 80 watch_reset(&((*objlist)->watch)); |
80 watch_reset(&((*objlist)->watch)); |
| 81 free_node(objlist, *objlist); |
81 free_node(objlist, *objlist); |
| 82 } |
82 } |
| 83 } |
83 } |
| 84 |
84 |
| 85 |
85 |
| 86 /* Warning: not thread-safe */ |
86 /* Warning: not thread-safe */ |
| 87 |
87 |
| 88 |
88 |
| 89 Obj *objlist_init_iter(ObjList *objlist) |
89 Obj *objlist_init_iter(ObjList *objlist) |
| 90 { |
90 { |
| 91 if(objlist==NULL){ |
91 if(objlist==NULL){ |
| 92 iter_next=NULL; |
92 iter_next=NULL; |
| 93 return NULL; |
93 return NULL; |
| 94 } |
94 } |
| 95 |
95 |
| 96 iter_next=objlist->next; |
96 iter_next=objlist->next; |
| 97 return objlist->watch.obj; |
97 return objlist->watch.obj; |
| 98 } |
98 } |
| 99 |
99 |
| 100 |
100 |
| 101 Obj *objlist_iter() |
101 Obj *objlist_iter() |
| 102 { |
102 { |
| 103 ObjList *ret; |
103 ObjList *ret; |
| 104 |
104 |
| 105 if(iter_next==NULL) |
105 if(iter_next==NULL) |
| 106 return NULL; |
106 return NULL; |
| 107 |
107 |
| 108 ret=iter_next; |
108 ret=iter_next; |
| 109 iter_next=iter_next->next; |
109 iter_next=iter_next->next; |
| 110 |
110 |
| 111 return ret->watch.obj; |
111 return ret->watch.obj; |
| 112 } |
112 } |