1 /*
2     +--------------------------------------------------------------------+
3     | PECL :: raphf                                                      |
4     +--------------------------------------------------------------------+
5     | Redistribution and use in source and binary forms, with or without |
6     | modification, are permitted provided that the conditions mentioned |
7     | in the accompanying LICENSE file are met.                          |
8     +--------------------------------------------------------------------+
9     | Copyright (c) 2013, Michael Wallner <mike@php.net>                 |
10     +--------------------------------------------------------------------+
11 */
12 
13 #ifdef HAVE_CONFIG_H
14 #	include "config.h"
15 #endif
16 
17 #include "php.h"
18 #include "php_ini.h"
19 #include "ext/standard/info.h"
20 #include "php_raphf.h"
21 
22 #ifndef PHP_RAPHF_TEST
23 #	define PHP_RAPHF_TEST 0
24 #endif
25 
26 struct php_persistent_handle_globals {
27 	unsigned long limit;
28 	HashTable hash;
29 };
30 
31 ZEND_BEGIN_MODULE_GLOBALS(raphf)
32 	struct php_persistent_handle_globals persistent_handle;
33 ZEND_END_MODULE_GLOBALS(raphf)
34 
35 #ifdef ZTS
36 #	define PHP_RAPHF_G ((zend_raphf_globals *) \
37 		(*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(raphf_globals_id)])
38 #else
39 #	define PHP_RAPHF_G (&raphf_globals)
40 #endif
41 
ZEND_DECLARE_MODULE_GLOBALS(raphf)42 ZEND_DECLARE_MODULE_GLOBALS(raphf)
43 
44 #ifndef PHP_RAPHF_DEBUG_PHANDLES
45 #	define PHP_RAPHF_DEBUG_PHANDLES 0
46 #endif
47 #if PHP_RAPHF_DEBUG_PHANDLES
48 #	undef inline
49 #	define inline
50 #endif
51 
52 php_resource_factory_t *php_resource_factory_init(php_resource_factory_t *f,
53 		php_resource_factory_ops_t *fops, void *data, void (*dtor)(void *data))
54 {
55 	if (!f) {
56 		f = emalloc(sizeof(*f));
57 	}
58 	memset(f, 0, sizeof(*f));
59 
60 	memcpy(&f->fops, fops, sizeof(*fops));
61 
62 	f->data = data;
63 	f->dtor = dtor;
64 
65 	f->refcount = 1;
66 
67 	return f;
68 }
69 
php_resource_factory_addref(php_resource_factory_t * rf)70 unsigned php_resource_factory_addref(php_resource_factory_t *rf)
71 {
72 	return ++rf->refcount;
73 }
74 
php_resource_factory_dtor(php_resource_factory_t * f)75 void php_resource_factory_dtor(php_resource_factory_t *f)
76 {
77 	if (!--f->refcount) {
78 		if (f->dtor) {
79 			f->dtor(f->data);
80 		}
81 	}
82 }
83 
php_resource_factory_free(php_resource_factory_t ** f)84 void php_resource_factory_free(php_resource_factory_t **f)
85 {
86 	if (*f) {
87 		php_resource_factory_dtor(*f);
88 		if (!(*f)->refcount) {
89 			efree(*f);
90 			*f = NULL;
91 		}
92 	}
93 }
94 
php_resource_factory_handle_ctor(php_resource_factory_t * f,void * init_arg)95 void *php_resource_factory_handle_ctor(php_resource_factory_t *f, void *init_arg)
96 {
97 	if (f->fops.ctor) {
98 		return f->fops.ctor(f->data, init_arg);
99 	}
100 	return NULL;
101 }
102 
php_resource_factory_handle_copy(php_resource_factory_t * f,void * handle)103 void *php_resource_factory_handle_copy(php_resource_factory_t *f, void *handle)
104 {
105 	if (f->fops.copy) {
106 		return f->fops.copy(f->data, handle);
107 	}
108 	return NULL;
109 }
110 
php_resource_factory_handle_dtor(php_resource_factory_t * f,void * handle)111 void php_resource_factory_handle_dtor(php_resource_factory_t *f, void *handle)
112 {
113 	if (f->fops.dtor) {
114 		f->fops.dtor(f->data, handle);
115 	}
116 }
117 
php_persistent_handle_resource_factory_init(php_resource_factory_t * a,php_persistent_handle_factory_t * pf)118 php_resource_factory_t *php_persistent_handle_resource_factory_init(
119 		php_resource_factory_t *a, php_persistent_handle_factory_t *pf)
120 {
121 	return php_resource_factory_init(a,
122 			php_persistent_handle_get_resource_factory_ops(), pf,
123 			(void(*)(void*)) php_persistent_handle_abandon);
124 }
125 
php_resource_factory_is_persistent(php_resource_factory_t * a)126 zend_bool php_resource_factory_is_persistent(php_resource_factory_t *a)
127 {
128 	return a->dtor == (void(*)(void *)) php_persistent_handle_abandon;
129 }
130 
php_persistent_handle_list_init(php_persistent_handle_list_t * list)131 static inline php_persistent_handle_list_t *php_persistent_handle_list_init(
132 		php_persistent_handle_list_t *list)
133 {
134 	if (!list) {
135 		list = pemalloc(sizeof(*list), 1);
136 	}
137 	list->used = 0;
138 	zend_hash_init(&list->free, 0, NULL, NULL, 1);
139 
140 	return list;
141 }
142 
php_persistent_handle_apply_stat(zval * p,int argc,va_list argv,zend_hash_key * key)143 static int php_persistent_handle_apply_stat(zval *p, int argc, va_list argv,
144 		zend_hash_key *key)
145 {
146 	php_persistent_handle_list_t *list = Z_PTR_P(p);
147 	zval zsubentry, *zentry = va_arg(argv, zval *);
148 
149 	array_init(&zsubentry);
150 	add_assoc_long_ex(&zsubentry, ZEND_STRL("used"), list->used);
151 	add_assoc_long_ex(&zsubentry, ZEND_STRL("free"),
152 			zend_hash_num_elements(&list->free));
153 	if (key->key) {
154 		add_assoc_zval_ex(zentry, key->key->val, key->key->len, &zsubentry);
155 	} else {
156 		add_index_zval(zentry, key->h, &zsubentry);
157 	}
158 	return ZEND_HASH_APPLY_KEEP;
159 }
160 
php_persistent_handle_apply_statall(zval * p,int argc,va_list argv,zend_hash_key * key)161 static int php_persistent_handle_apply_statall(zval *p, int argc, va_list argv,
162 		zend_hash_key *key)
163 {
164 	php_persistent_handle_provider_t *provider = Z_PTR_P(p);
165 	HashTable *ht = va_arg(argv, HashTable *);
166 	zval zentry;
167 
168 	array_init(&zentry);
169 
170 	zend_hash_apply_with_arguments(&provider->list.free,
171 			php_persistent_handle_apply_stat, 1, &zentry);
172 
173 	if (key->key) {
174 		zend_hash_update(ht, key->key, &zentry);
175 	} else {
176 		zend_hash_index_update(ht, key->h, &zentry);
177 	}
178 
179 	return ZEND_HASH_APPLY_KEEP;
180 }
181 
php_persistent_handle_apply_cleanup_ex(zval * p,void * arg)182 static int php_persistent_handle_apply_cleanup_ex(zval *p, void *arg)
183 {
184 	php_resource_factory_t *rf = arg;
185 	void *handle = Z_PTR_P(p);
186 
187 #if PHP_RAPHF_DEBUG_PHANDLES
188 	fprintf(stderr, "DESTROY: %p\n", handle);
189 #endif
190 	php_resource_factory_handle_dtor(rf, handle);
191 	return ZEND_HASH_APPLY_REMOVE;
192 }
193 
php_persistent_handle_apply_cleanup(zval * p,void * arg)194 static int php_persistent_handle_apply_cleanup(zval *p, void *arg)
195 {
196 	php_resource_factory_t *rf = arg;
197 	php_persistent_handle_list_t *list = Z_PTR_P(p);
198 
199 	zend_hash_apply_with_argument(&list->free,
200 			php_persistent_handle_apply_cleanup_ex, rf);
201 	if (list->used) {
202 		return ZEND_HASH_APPLY_KEEP;
203 	}
204 	zend_hash_destroy(&list->free);
205 #if PHP_RAPHF_DEBUG_PHANDLES
206 	fprintf(stderr, "LSTFREE: %p\n", list);
207 #endif
208 	pefree(list, 1);
209 	return ZEND_HASH_APPLY_REMOVE;
210 }
211 
php_persistent_handle_list_dtor(php_persistent_handle_list_t * list,php_persistent_handle_provider_t * provider)212 static inline void php_persistent_handle_list_dtor(
213 		php_persistent_handle_list_t *list,
214 		php_persistent_handle_provider_t *provider)
215 {
216 #if PHP_RAPHF_DEBUG_PHANDLES
217 	fprintf(stderr, "LSTDTOR: %p\n", list);
218 #endif
219 	zend_hash_apply_with_argument(&list->free,
220 			php_persistent_handle_apply_cleanup_ex, &provider->rf);
221 	zend_hash_destroy(&list->free);
222 }
223 
php_persistent_handle_list_free(php_persistent_handle_list_t ** list,php_persistent_handle_provider_t * provider)224 static inline void php_persistent_handle_list_free(
225 		php_persistent_handle_list_t **list,
226 		php_persistent_handle_provider_t *provider)
227 {
228 	php_persistent_handle_list_dtor(*list, provider);
229 #if PHP_RAPHF_DEBUG_PHANDLES
230 	fprintf(stderr, "LSTFREE: %p\n", *list);
231 #endif
232 	pefree(*list, 1);
233 	*list = NULL;
234 }
235 
php_persistent_handle_list_apply_dtor(zval * p,void * provider)236 static int php_persistent_handle_list_apply_dtor(zval *p, void *provider)
237 {
238 	php_persistent_handle_list_t *list = Z_PTR_P(p);
239 
240 	php_persistent_handle_list_free(&list, provider );
241 	ZVAL_PTR(p, NULL);
242 	return ZEND_HASH_APPLY_REMOVE;
243 }
244 
php_persistent_handle_list_find(php_persistent_handle_provider_t * provider,zend_string * ident,zend_bool create)245 static inline php_persistent_handle_list_t *php_persistent_handle_list_find(
246 		php_persistent_handle_provider_t *provider, zend_string *ident,
247 		zend_bool create)
248 {
249 	php_persistent_handle_list_t *list;
250 	zval *zlist = zend_symtable_find(&provider->list.free, ident);
251 
252 	if (zlist && (list = Z_PTR_P(zlist))) {
253 #if PHP_RAPHF_DEBUG_PHANDLES
254 		fprintf(stderr, "LSTFIND: %p\n", list);
255 #endif
256 		return list;
257 	}
258 
259 	if (create && (list = php_persistent_handle_list_init(NULL))) {
260 		zval p, *rv;
261 		zend_string *id;
262 
263 		ZVAL_PTR(&p, list);
264 		id = zend_string_init(ident->val, ident->len, 1);
265 		rv = zend_symtable_update(&provider->list.free, id, &p);
266 		zend_string_release(id);
267 
268 		if (rv) {
269 #if PHP_RAPHF_DEBUG_PHANDLES
270 			fprintf(stderr, "LSTFIND: %p (new)\n", list);
271 #endif
272 			return list;
273 		}
274 		php_persistent_handle_list_free(&list, provider);
275 	}
276 
277 	return NULL;
278 }
279 
php_persistent_handle_apply_cleanup_all(zval * p,int argc,va_list argv,zend_hash_key * key)280 static int php_persistent_handle_apply_cleanup_all(zval *p, int argc,
281 		va_list argv, zend_hash_key *key)
282 {
283 	php_persistent_handle_provider_t *provider = Z_PTR_P(p);
284 	zend_string *ident = va_arg(argv, zend_string *);
285 	php_persistent_handle_list_t *list;
286 
287 	if (ident && ident->len) {
288 		if ((list = php_persistent_handle_list_find(provider, ident, 0))) {
289 			zend_hash_apply_with_argument(&list->free,
290 					php_persistent_handle_apply_cleanup_ex,
291 					&provider->rf);
292 		}
293 	} else {
294 		zend_hash_apply_with_argument(&provider->list.free,
295 				php_persistent_handle_apply_cleanup, &provider->rf);
296 	}
297 
298 	return ZEND_HASH_APPLY_KEEP;
299 }
300 
php_persistent_handle_hash_dtor(zval * p)301 static void php_persistent_handle_hash_dtor(zval *p)
302 {
303 	php_persistent_handle_provider_t *provider = Z_PTR_P(p);
304 
305 	zend_hash_apply_with_argument(&provider->list.free,
306 			php_persistent_handle_list_apply_dtor, provider);
307 	zend_hash_destroy(&provider->list.free);
308 	php_resource_factory_dtor(&provider->rf);
309 	pefree(provider, 1);
310 }
311 
php_persistent_handle_provide(zend_string * name,php_resource_factory_ops_t * fops,void * data,void (* dtor)(void *))312 ZEND_RESULT_CODE php_persistent_handle_provide(zend_string *name,
313 		php_resource_factory_ops_t *fops, void *data, void (*dtor)(void *))
314 {
315 	php_persistent_handle_provider_t *provider = pemalloc(sizeof(*provider), 1);
316 
317 	if (php_persistent_handle_list_init(&provider->list)) {
318 		if (php_resource_factory_init(&provider->rf, fops, data, dtor)) {
319 			zval p, *rv;
320 			zend_string *ns;
321 
322 #if PHP_RAPHF_DEBUG_PHANDLES
323 			fprintf(stderr, "PROVIDE: %p %s\n", PHP_RAPHF_G, name_str);
324 #endif
325 
326 			ZVAL_PTR(&p, provider);
327 			ns = zend_string_init(name->val, name->len, 1);
328 			rv = zend_symtable_update(&PHP_RAPHF_G->persistent_handle.hash, ns, &p);
329 			zend_string_release(ns);
330 
331 			if (rv) {
332 				return SUCCESS;
333 			}
334 			php_resource_factory_dtor(&provider->rf);
335 		}
336 	}
337 
338 	return FAILURE;
339 }
340 
341 
php_persistent_handle_concede(php_persistent_handle_factory_t * a,zend_string * name,zend_string * ident,php_persistent_handle_wakeup_t wakeup,php_persistent_handle_retire_t retire)342 php_persistent_handle_factory_t *php_persistent_handle_concede(
343 		php_persistent_handle_factory_t *a,
344 		zend_string *name, zend_string *ident,
345 		php_persistent_handle_wakeup_t wakeup,
346 		php_persistent_handle_retire_t retire)
347 {
348 	zval *zprovider = zend_symtable_find(&PHP_RAPHF_G->persistent_handle.hash, name);
349 
350 	if (zprovider) {
351 		zend_bool free_a = 0;
352 
353 		if ((free_a = !a)) {
354 			a = emalloc(sizeof(*a));
355 		}
356 		memset(a, 0, sizeof(*a));
357 
358 		a->provider = Z_PTR_P(zprovider);
359 		a->ident = zend_string_copy(ident);
360 		a->wakeup = wakeup;
361 		a->retire = retire;
362 		a->free_on_abandon = free_a;
363 	} else {
364 		a = NULL;
365 	}
366 
367 #if PHP_RAPHF_DEBUG_PHANDLES
368 	fprintf(stderr, "CONCEDE: %p %p (%s) (%s)\n", PHP_RAPHF_G,
369 			a ? a->provider : NULL, name->val, ident->val);
370 #endif
371 
372 	return a;
373 }
374 
php_persistent_handle_abandon(php_persistent_handle_factory_t * a)375 void php_persistent_handle_abandon(php_persistent_handle_factory_t *a)
376 {
377 	zend_bool f = a->free_on_abandon;
378 
379 #if PHP_RAPHF_DEBUG_PHANDLES
380 	fprintf(stderr, "ABANDON: %p\n", a->provider);
381 #endif
382 
383 	zend_string_release(a->ident);
384 	memset(a, 0, sizeof(*a));
385 	if (f) {
386 		efree(a);
387 	}
388 }
389 
php_persistent_handle_acquire(php_persistent_handle_factory_t * a,void * init_arg)390 void *php_persistent_handle_acquire(php_persistent_handle_factory_t *a, void *init_arg)
391 {
392 	int key;
393 	zval *p;
394 	zend_ulong index;
395 	void *handle = NULL;
396 	php_persistent_handle_list_t *list;
397 
398 	list = php_persistent_handle_list_find(a->provider, a->ident, 1);
399 	if (list) {
400 		zend_hash_internal_pointer_end(&list->free);
401 		key = zend_hash_get_current_key(&list->free, NULL, &index);
402 		p = zend_hash_get_current_data(&list->free);
403 		if (p && HASH_KEY_NON_EXISTENT != key) {
404 			handle = Z_PTR_P(p);
405 			if (a->wakeup) {
406 				a->wakeup(a, &handle);
407 			}
408 			zend_hash_index_del(&list->free, index);
409 		} else {
410 			handle = php_resource_factory_handle_ctor(&a->provider->rf, init_arg);
411 		}
412 #if PHP_RAPHF_DEBUG_PHANDLES
413 		fprintf(stderr, "CREATED: %p\n", handle);
414 #endif
415 		if (handle) {
416 			++a->provider->list.used;
417 			++list->used;
418 		}
419 	}
420 
421 	return handle;
422 }
423 
php_persistent_handle_accrete(php_persistent_handle_factory_t * a,void * handle)424 void *php_persistent_handle_accrete(php_persistent_handle_factory_t *a, void *handle)
425 {
426 	void *new_handle = NULL;
427 	php_persistent_handle_list_t *list;
428 
429 	new_handle = php_resource_factory_handle_copy(&a->provider->rf, handle);
430 	if (handle) {
431 		list = php_persistent_handle_list_find(a->provider, a->ident, 1);
432 		if (list) {
433 			++list->used;
434 		}
435 		++a->provider->list.used;
436 	}
437 
438 	return new_handle;
439 }
440 
php_persistent_handle_release(php_persistent_handle_factory_t * a,void * handle)441 void php_persistent_handle_release(php_persistent_handle_factory_t *a, void *handle)
442 {
443 	php_persistent_handle_list_t *list;
444 
445 	list = php_persistent_handle_list_find(a->provider, a->ident, 1);
446 	if (list) {
447 		if (a->provider->list.used >= PHP_RAPHF_G->persistent_handle.limit) {
448 #if PHP_RAPHF_DEBUG_PHANDLES
449 			fprintf(stderr, "DESTROY: %p\n", handle);
450 #endif
451 			php_resource_factory_handle_dtor(&a->provider->rf, handle);
452 		} else {
453 			if (a->retire) {
454 				a->retire(a, &handle);
455 			}
456 			zend_hash_next_index_insert_ptr(&list->free, handle);
457 		}
458 
459 		--a->provider->list.used;
460 		--list->used;
461 	}
462 }
463 
php_persistent_handle_cleanup(zend_string * name,zend_string * ident)464 void php_persistent_handle_cleanup(zend_string *name, zend_string *ident)
465 {
466 	php_persistent_handle_provider_t *provider;
467 	php_persistent_handle_list_t *list;
468 
469 	if (name) {
470 		zval *zprovider = zend_symtable_find(&PHP_RAPHF_G->persistent_handle.hash,
471 				name);
472 
473 		if (zprovider && (provider = Z_PTR_P(zprovider))) {
474 			if (ident) {
475 				list = php_persistent_handle_list_find(provider, ident, 0);
476 				if (list) {
477 					zend_hash_apply_with_argument(&list->free,
478 							php_persistent_handle_apply_cleanup_ex,
479 							&provider->rf);
480 				}
481 			} else {
482 				zend_hash_apply_with_argument(&provider->list.free,
483 						php_persistent_handle_apply_cleanup,
484 						&provider->rf);
485 			}
486 		}
487 	} else {
488 		zend_hash_apply_with_arguments(
489 				&PHP_RAPHF_G->persistent_handle.hash,
490 				php_persistent_handle_apply_cleanup_all, 1, ident);
491 	}
492 }
493 
php_persistent_handle_statall(HashTable * ht)494 HashTable *php_persistent_handle_statall(HashTable *ht)
495 {
496 	if (zend_hash_num_elements(&PHP_RAPHF_G->persistent_handle.hash)) {
497 		if (!ht) {
498 			ALLOC_HASHTABLE(ht);
499 			zend_hash_init(ht, 0, NULL, ZVAL_PTR_DTOR, 0);
500 		}
501 		zend_hash_apply_with_arguments(
502 				&PHP_RAPHF_G->persistent_handle.hash,
503 				php_persistent_handle_apply_statall, 1, ht);
504 	} else if (ht) {
505 		ht = NULL;
506 	}
507 
508 	return ht;
509 }
510 
511 static php_resource_factory_ops_t php_persistent_handle_resource_factory_ops = {
512 	(php_resource_factory_handle_ctor_t) php_persistent_handle_acquire,
513 	(php_resource_factory_handle_copy_t) php_persistent_handle_accrete,
514 	(php_resource_factory_handle_dtor_t) php_persistent_handle_release
515 };
516 
php_persistent_handle_get_resource_factory_ops(void)517 php_resource_factory_ops_t *php_persistent_handle_get_resource_factory_ops(void)
518 {
519 	return &php_persistent_handle_resource_factory_ops;
520 }
521 
522 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_stat_persistent_handles, 0, 0, 0)
523 ZEND_END_ARG_INFO();
PHP_FUNCTION(raphf_stat_persistent_handles)524 static PHP_FUNCTION(raphf_stat_persistent_handles)
525 {
526 	if (SUCCESS == zend_parse_parameters_none()) {
527 		object_init(return_value);
528 		if (php_persistent_handle_statall(HASH_OF(return_value))) {
529 			return;
530 		}
531 		zval_dtor(return_value);
532 	}
533 	RETURN_FALSE;
534 }
535 
536 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_clean_persistent_handles, 0, 0, 0)
537 	ZEND_ARG_INFO(0, name)
538 	ZEND_ARG_INFO(0, ident)
539 ZEND_END_ARG_INFO();
PHP_FUNCTION(raphf_clean_persistent_handles)540 static PHP_FUNCTION(raphf_clean_persistent_handles)
541 {
542 	zend_string *name = NULL, *ident = NULL;
543 
544 	if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|S!S!", &name, &ident)) {
545 		php_persistent_handle_cleanup(name, ident);
546 	}
547 }
548 
549 #if PHP_RAPHF_TEST
550 #	include "php_raphf_test.c"
551 #endif
552 
553 static const zend_function_entry raphf_functions[] = {
554 	ZEND_NS_FENTRY("raphf", stat_persistent_handles,
555 			ZEND_FN(raphf_stat_persistent_handles),
556 			ai_raphf_stat_persistent_handles, 0)
557 	ZEND_NS_FENTRY("raphf", clean_persistent_handles,
558 			ZEND_FN(raphf_clean_persistent_handles),
559 			ai_raphf_clean_persistent_handles, 0)
560 #if PHP_RAPHF_TEST
561 	ZEND_NS_FENTRY("raphf", provide, ZEND_FN(raphf_provide), NULL, 0)
562 	ZEND_NS_FENTRY("raphf", conceal, ZEND_FN(raphf_conceal), NULL, 0)
563 	ZEND_NS_FENTRY("raphf", concede, ZEND_FN(raphf_concede), NULL, 0)
564 	ZEND_NS_FENTRY("raphf", dispute, ZEND_FN(raphf_dispute), NULL, 0)
565 	ZEND_NS_FENTRY("raphf", handle_ctor, ZEND_FN(raphf_handle_ctor), NULL, 0)
566 	ZEND_NS_FENTRY("raphf", handle_copy, ZEND_FN(raphf_handle_copy), NULL, 0)
567 	ZEND_NS_FENTRY("raphf", handle_dtor, ZEND_FN(raphf_handle_dtor), NULL, 0)
568 #endif
569 	{0}
570 };
571 
572 PHP_INI_BEGIN()
573 	STD_PHP_INI_ENTRY("raphf.persistent_handle.limit", "-1", PHP_INI_SYSTEM,
574 			OnUpdateLong, persistent_handle.limit, zend_raphf_globals,
575 			raphf_globals)
576 PHP_INI_END()
577 
578 static HashTable *php_persistent_handles_global_hash;
579 
PHP_GINIT_FUNCTION(raphf)580 static PHP_GINIT_FUNCTION(raphf)
581 {
582 	raphf_globals->persistent_handle.limit = -1;
583 
584 	zend_hash_init(&raphf_globals->persistent_handle.hash, 0, NULL,
585 			php_persistent_handle_hash_dtor, 1);
586 	if (php_persistent_handles_global_hash) {
587 		zend_hash_copy(&raphf_globals->persistent_handle.hash,
588 				php_persistent_handles_global_hash, NULL);
589 	}
590 }
591 
PHP_GSHUTDOWN_FUNCTION(raphf)592 static PHP_GSHUTDOWN_FUNCTION(raphf)
593 {
594 	zend_hash_destroy(&raphf_globals->persistent_handle.hash);
595 }
596 
PHP_MINIT_FUNCTION(raphf)597 PHP_MINIT_FUNCTION(raphf)
598 {
599 	php_persistent_handles_global_hash = &PHP_RAPHF_G->persistent_handle.hash;
600 
601 #if PHP_RAPHF_TEST
602 	PHP_MINIT(raphf_test)(INIT_FUNC_ARGS_PASSTHRU);
603 #endif
604 
605 	REGISTER_INI_ENTRIES();
606 	return SUCCESS;
607 }
608 
PHP_MSHUTDOWN_FUNCTION(raphf)609 PHP_MSHUTDOWN_FUNCTION(raphf)
610 {
611 #if PHP_RAPHF_TEST
612 	PHP_MSHUTDOWN(raphf_test)(SHUTDOWN_FUNC_ARGS_PASSTHRU);
613 #endif
614 
615 	UNREGISTER_INI_ENTRIES();
616 	php_persistent_handles_global_hash = NULL;
617 	return SUCCESS;
618 }
619 
php_persistent_handle_apply_info_ex(zval * p,int argc,va_list argv,zend_hash_key * key)620 static int php_persistent_handle_apply_info_ex(zval *p, int argc,
621 		va_list argv, zend_hash_key *key)
622 {
623 	php_persistent_handle_list_t *list = Z_PTR_P(p);
624 	zend_hash_key *super_key = va_arg(argv, zend_hash_key *);
625 	char used[21], free[21];
626 
627 	slprintf(used, sizeof(used), "%u", list->used);
628 	slprintf(free, sizeof(free), "%d", zend_hash_num_elements(&list->free));
629 
630 	php_info_print_table_row(4, super_key->key->val, key->key->val, used, free);
631 
632 	return ZEND_HASH_APPLY_KEEP;
633 }
634 
php_persistent_handle_apply_info(zval * p,int argc,va_list argv,zend_hash_key * key)635 static int php_persistent_handle_apply_info(zval *p, int argc,
636 		va_list argv, zend_hash_key *key)
637 {
638 	php_persistent_handle_provider_t *provider = Z_PTR_P(p);
639 
640 	zend_hash_apply_with_arguments(&provider->list.free,
641 			php_persistent_handle_apply_info_ex, 1, key);
642 
643 	return ZEND_HASH_APPLY_KEEP;
644 }
645 
PHP_MINFO_FUNCTION(raphf)646 PHP_MINFO_FUNCTION(raphf)
647 {
648 	php_info_print_table_start();
649 	php_info_print_table_header(2,
650 			"Resource and persistent handle factory support", "enabled");
651 	php_info_print_table_row(2, "Extension version", PHP_RAPHF_VERSION);
652 	php_info_print_table_end();
653 
654 	php_info_print_table_start();
655 	php_info_print_table_colspan_header(4, "Persistent handles in this "
656 #ifdef ZTS
657 			"thread"
658 #else
659 			"process"
660 #endif
661 	);
662 	php_info_print_table_header(4, "Provider", "Ident", "Used", "Free");
663 	zend_hash_apply_with_arguments(
664 			&PHP_RAPHF_G->persistent_handle.hash,
665 			php_persistent_handle_apply_info, 0);
666 	php_info_print_table_end();
667 
668 	DISPLAY_INI_ENTRIES();
669 }
670 
671 zend_module_entry raphf_module_entry = {
672 	STANDARD_MODULE_HEADER,
673 	"raphf",
674 	raphf_functions,
675 	PHP_MINIT(raphf),
676 	PHP_MSHUTDOWN(raphf),
677 	NULL,
678 	NULL,
679 	PHP_MINFO(raphf),
680 	PHP_RAPHF_VERSION,
681 	ZEND_MODULE_GLOBALS(raphf),
682 	PHP_GINIT(raphf),
683 	PHP_GSHUTDOWN(raphf),
684 	NULL,
685 	STANDARD_MODULE_PROPERTIES_EX
686 };
687 /* }}} */
688 
689 #ifdef COMPILE_DL_RAPHF
690 ZEND_GET_MODULE(raphf)
691 #endif
692 
693 /*
694  * Local variables:
695  * tab-width: 4
696  * c-basic-offset: 4
697  * End:
698  * vim600: noet sw=4 ts=4 fdm=marker
699  * vim<600: noet sw=4 ts=4
700  */
701