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