1
2 /* $Id: fpm_cleanup.c,v 1.8 2008/05/24 17:38:47 anight Exp $ */
3 /* (c) 2007,2008 Andrei Nigmatulin */
4
5 #include "fpm_config.h"
6
7 #include <stdlib.h>
8
9 #include "fpm_arrays.h"
10 #include "fpm_cleanup.h"
11
12 struct cleanup_s {
13 int type;
14 void (*cleanup)(int, void *);
15 void *arg;
16 };
17
18 static struct fpm_array_s cleanups = { .sz = sizeof(struct cleanup_s) };
19
fpm_cleanup_add(int type,void (* cleanup)(int,void *),void * arg)20 int fpm_cleanup_add(int type, void (*cleanup)(int, void *), void *arg) /* {{{ */
21 {
22 struct cleanup_s *c;
23
24 c = fpm_array_push(&cleanups);
25
26 if (!c) {
27 return -1;
28 }
29
30 c->type = type;
31 c->cleanup = cleanup;
32 c->arg = arg;
33
34 return 0;
35 }
36 /* }}} */
37
fpm_cleanups_run(int type)38 void fpm_cleanups_run(int type) /* {{{ */
39 {
40 struct cleanup_s *c = fpm_array_item_last(&cleanups);
41 int cl = cleanups.used;
42
43 for ( ; cl--; c--) {
44 if (c->type & type) {
45 c->cleanup(type, c->arg);
46 }
47 }
48
49 fpm_array_free(&cleanups);
50 }
51 /* }}} */
52
53