Commit 28e9a0a0 28e9a0a06b996595db2dd7ad64c9adced80d3aca by Sergey Poznyakoff

Add iterator support to opool.

* include/mailutils/opool.h (mu_opool_get_iterator): New function.
* mailbox/opool.c (mu_opool_get_iterator): New function.
* mailbox/hdritr.c (hdr_data_dup): Bugfix: notify owner about the
new iterator.
1 parent 5845efba
1 2008-11-27 Sergey Poznyakoff <gray@gnu.org.ua>
2
3 Add iterator support to opool.
4
5 * include/mailutils/opool.h (mu_opool_get_iterator): New function.
6 * mailbox/opool.c (mu_opool_get_iterator): New function.
7 * mailbox/hdritr.c (hdr_data_dup): Bugfix: notify owner about the
8 new iterator.
9
1 2008-11-21 Sergey Poznyakoff <gray@gnu.org.ua> 10 2008-11-21 Sergey Poznyakoff <gray@gnu.org.ua>
2 11
3 * auth/sql.c (get_field): NULL value in an optional field is 12 * auth/sql.c (get_field): NULL value in an optional field is
......
...@@ -216,10 +216,10 @@ static struct argp argp = { ...@@ -216,10 +216,10 @@ static struct argp argp = {
216 216
217 static const char *frm_argp_capa[] = { 217 static const char *frm_argp_capa[] = {
218 "common", 218 "common",
219 "debug",
219 "license", 220 "license",
220 "mailbox", 221 "mailbox",
221 "locking", 222 "locking",
222 "debug",
223 NULL 223 NULL
224 }; 224 };
225 225
......
...@@ -75,9 +75,6 @@ static const char *capa[] = { ...@@ -75,9 +75,6 @@ static const char *capa[] = {
75 "license", 75 "license",
76 "mailbox", 76 "mailbox",
77 "locking", 77 "locking",
78 #ifdef WITH_TLS
79 "tls",
80 #endif
81 NULL 78 NULL
82 }; 79 };
83 80
...@@ -118,6 +115,9 @@ main (int argc, char **argv) ...@@ -118,6 +115,9 @@ main (int argc, char **argv)
118 115
119 /* register the formats. */ 116 /* register the formats. */
120 mu_register_all_mbox_formats (); 117 mu_register_all_mbox_formats ();
118 #ifdef WITH_TLS
119 mu_gocs_register ("tls", mu_tls_module_init);
120 #endif
121 121
122 mu_argp_init (program_version, NULL); 122 mu_argp_init (program_version, NULL);
123 if (mu_app_init (&argp, capa, NULL, argc, argv, 0, &c, NULL)) 123 if (mu_app_init (&argp, capa, NULL, argc, argv, 0, &c, NULL))
......
...@@ -68,4 +68,6 @@ void *mu_opool_head (mu_opool_t opool, size_t *psize); ...@@ -68,4 +68,6 @@ void *mu_opool_head (mu_opool_t opool, size_t *psize);
68 return p; */ 68 return p; */
69 void *mu_opool_finish (mu_opool_t opool, size_t *psize); 69 void *mu_opool_finish (mu_opool_t opool, size_t *psize);
70 70
71 int mu_opool_get_iterator (mu_opool_t opool, mu_iterator_t *piterator);
72
71 #endif 73 #endif
......
...@@ -106,10 +106,13 @@ hdr_curitem_p (void *owner, void *item) ...@@ -106,10 +106,13 @@ hdr_curitem_p (void *owner, void *item)
106 static int 106 static int
107 hdr_data_dup (void **ptr, void *owner) 107 hdr_data_dup (void **ptr, void *owner)
108 { 108 {
109 struct header_iterator *itr = owner;
110
109 *ptr = malloc (sizeof (struct header_iterator)); 111 *ptr = malloc (sizeof (struct header_iterator));
110 if (*ptr == NULL) 112 if (*ptr == NULL)
111 return ENOMEM; 113 return ENOMEM;
112 memcpy (*ptr, owner, sizeof (struct header_iterator)); 114 memcpy (*ptr, owner, sizeof (struct header_iterator));
115 mu_iterator_attach (&itr->header->itr, *ptr);
113 return 0; 116 return 0;
114 } 117 }
115 118
......
...@@ -23,10 +23,13 @@ ...@@ -23,10 +23,13 @@
23 #include <stdio.h> 23 #include <stdio.h>
24 #include <stdlib.h> 24 #include <stdlib.h>
25 #include <string.h> 25 #include <string.h>
26 #include <errno.h>
27 #include <mailutils/types.h> 26 #include <mailutils/types.h>
28 #include <mailutils/alloc.h> 27 #include <mailutils/alloc.h>
29 #include <mailutils/opool.h> 28 #include <mailutils/opool.h>
29 #include <mailutils/errno.h>
30 #include <mailutils/error.h>
31 #include <mailutils/nls.h>
32 #include <mailutils/iterator.h>
30 33
31 struct mu_opool_bucket 34 struct mu_opool_bucket
32 { 35 {
...@@ -39,6 +42,7 @@ struct mu_opool_bucket ...@@ -39,6 +42,7 @@ struct mu_opool_bucket
39 struct _mu_opool 42 struct _mu_opool
40 { 43 {
41 int memerr; 44 int memerr;
45 size_t itr_count;
42 struct mu_opool_bucket *head, *tail; 46 struct mu_opool_bucket *head, *tail;
43 struct mu_opool_bucket *free; 47 struct mu_opool_bucket *free;
44 }; 48 };
...@@ -104,6 +108,7 @@ mu_opool_create (mu_opool_t *pret, int memerr) ...@@ -104,6 +108,7 @@ mu_opool_create (mu_opool_t *pret, int memerr)
104 return ENOMEM; 108 return ENOMEM;
105 } 109 }
106 x->memerr = memerr; 110 x->memerr = memerr;
111 x->itr_count = 0;
107 x->head = x->tail = x->free = 0; 112 x->head = x->tail = x->free = 0;
108 *pret = x; 113 *pret = x;
109 return 0; 114 return 0;
...@@ -185,6 +190,8 @@ mu_opool_coalesce (mu_opool_t opool, size_t *psize) ...@@ -185,6 +190,8 @@ mu_opool_coalesce (mu_opool_t opool, size_t *psize)
185 { 190 {
186 size_t size; 191 size_t size;
187 192
193 if (opool->itr_count)
194 return MU_ERR_FAILURE;
188 if (opool->head && opool->head->next == NULL) 195 if (opool->head && opool->head->next == NULL)
189 size = opool->head->level; 196 size = opool->head->level;
190 else { 197 else {
...@@ -228,3 +235,130 @@ mu_opool_finish (mu_opool_t opool, size_t *psize) ...@@ -228,3 +235,130 @@ mu_opool_finish (mu_opool_t opool, size_t *psize)
228 return opool->free->buf; 235 return opool->free->buf;
229 } 236 }
230 237
238
239 /* Iterator support */
240 struct opool_iterator
241 {
242 mu_opool_t opool;
243 struct mu_opool_bucket *cur;
244 };
245
246 static int
247 opitr_first (void *owner)
248 {
249 struct opool_iterator *itr = owner;
250 itr->cur = itr->opool->head;
251 return 0;
252 }
253
254 static int
255 opitr_next (void *owner)
256 {
257 struct opool_iterator *itr = owner;
258 if (itr->cur)
259 {
260 itr->cur = itr->cur->next;
261 return 0;
262 }
263 return EINVAL;
264 }
265
266 static int
267 opitr_getitem (void *owner, void **pret, const void **pkey)
268 {
269 struct opool_iterator *itr = owner;
270 if (!itr->cur)
271 return MU_ERR_NOENT;
272
273 *pret = itr->cur->buf;
274 if (pkey)
275 *(size_t*) pkey = itr->cur->level;
276 return 0;
277 }
278
279 static int
280 opitr_finished_p (void *owner)
281 {
282 struct opool_iterator *itr = owner;
283 return itr->cur == NULL;
284 }
285
286 static int
287 opitr_curitem_p (void *owner, void *item)
288 {
289 struct opool_iterator *itr = owner;
290 return itr->cur && itr->cur->buf == item;
291 }
292
293 static int
294 opitr_destroy (mu_iterator_t iterator, void *data)
295 {
296 struct opool_iterator *itr = data;
297 if (itr->opool->itr_count == 0)
298 {
299 /* oops! */
300 mu_error (_("%s: INTERNAL ERROR: zero reference count"),
301 "opool_destroy");
302 }
303 else
304 itr->opool->itr_count--;
305 free (data);
306 return 0;
307 }
308
309 static int
310 opitr_data_dup (void **ptr, void *owner)
311 {
312 struct opool_iterator *itr = owner;
313
314 *ptr = malloc (sizeof (struct opool_iterator));
315 if (*ptr == NULL)
316 return ENOMEM;
317 memcpy (*ptr, owner, sizeof (struct opool_iterator));
318 itr->opool->itr_count++;
319 return 0;
320 }
321
322 int
323 mu_opool_get_iterator (mu_opool_t opool, mu_iterator_t *piterator)
324 {
325 mu_iterator_t iterator;
326 int status;
327 struct opool_iterator *itr;
328
329 if (!opool)
330 return EINVAL;
331
332 itr = calloc (1, sizeof *itr);
333 if (!itr)
334 return ENOMEM;
335 itr->opool = opool;
336 itr->cur = opool->head;
337
338 status = mu_iterator_create (&iterator, itr);
339 if (status)
340 {
341 free (itr);
342 return status;
343 }
344
345 mu_iterator_set_first (iterator, opitr_first);
346 mu_iterator_set_next (iterator, opitr_next);
347 mu_iterator_set_getitem (iterator, opitr_getitem);
348 mu_iterator_set_finished_p (iterator, opitr_finished_p);
349 mu_iterator_set_curitem_p (iterator, opitr_curitem_p);
350 mu_iterator_set_destroy (iterator, opitr_destroy);
351 mu_iterator_set_dup (iterator, opitr_data_dup);
352
353 opool->itr_count++;
354
355 *piterator = iterator;
356 return 0;
357 }
358
359
360
361
362
363
364
......