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