start with a low version
[m6w6/ext-raphf] / php_raphf.c
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 ZEND_DECLARE_MODULE_GLOBALS(raphf)
23
24 typedef int STATUS;
25
26 #ifndef PHP_RAPHF_DEBUG_PHANDLES
27 # define PHP_RAPHF_DEBUG_PHANDLES 0
28 #endif
29 #if PHP_RAPHF_DEBUG_PHANDLES
30 # undef inline
31 # define inline
32 #endif
33
34 PHP_RAPHF_API php_resource_factory_t *php_resource_factory_init(php_resource_factory_t *f, php_resource_factory_ops_t *fops, void *data, void (*dtor)(void *data))
35 {
36 if (!f) {
37 f = emalloc(sizeof(*f));
38 }
39 memset(f, 0, sizeof(*f));
40
41 memcpy(&f->fops, fops, sizeof(*fops));
42
43 f->data = data;
44 f->dtor = dtor;
45
46 f->refcount = 1;
47
48 return f;
49 }
50
51 PHP_RAPHF_API unsigned php_resource_factory_addref(php_resource_factory_t *rf)
52 {
53 return ++rf->refcount;
54 }
55
56 PHP_RAPHF_API void php_resource_factory_dtor(php_resource_factory_t *f)
57 {
58 --f->refcount;
59
60 if (!f->refcount) {
61 if (f->dtor) {
62 f->dtor(f->data);
63 }
64 }
65 }
66
67 PHP_RAPHF_API void php_resource_factory_free(php_resource_factory_t **f)
68 {
69 if (*f) {
70 php_resource_factory_dtor(*f);
71 if (!(*f)->refcount) {
72 efree(*f);
73 *f = NULL;
74 }
75 }
76 }
77
78 PHP_RAPHF_API void *php_resource_factory_handle_ctor(php_resource_factory_t *f, void *init_arg TSRMLS_DC)
79 {
80 if (f->fops.ctor) {
81 return f->fops.ctor(f->data, init_arg TSRMLS_CC);
82 }
83 return NULL;
84 }
85
86 PHP_RAPHF_API void *php_resource_factory_handle_copy(php_resource_factory_t *f, void *handle TSRMLS_DC)
87 {
88 if (f->fops.copy) {
89 return f->fops.copy(f->data, handle TSRMLS_CC);
90 }
91 return NULL;
92 }
93
94 PHP_RAPHF_API void php_resource_factory_handle_dtor(php_resource_factory_t *f, void *handle TSRMLS_DC)
95 {
96 if (f->fops.dtor) {
97 f->fops.dtor(f->data, handle TSRMLS_CC);
98 }
99 }
100
101 static inline php_persistent_handle_list_t *php_persistent_handle_list_init(php_persistent_handle_list_t *list)
102 {
103 int free_list;
104
105 if ((free_list = !list)) {
106 list = pemalloc(sizeof(php_persistent_handle_list_t), 1);
107 }
108
109 list->used = 0;
110
111 if (SUCCESS != zend_hash_init(&list->free, 0, NULL, NULL, 1)) {
112 if (free_list) {
113 pefree(list, 1);
114 }
115 list = NULL;
116 }
117
118 return list;
119 }
120
121 static int php_persistent_handle_apply_stat(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
122 {
123 php_persistent_handle_list_t **list = p;
124 zval *zsubentry, *zentry = va_arg(argv, zval *);
125
126 MAKE_STD_ZVAL(zsubentry);
127 array_init(zsubentry);
128 add_assoc_long_ex(zsubentry, ZEND_STRS("used"), (*list)->used);
129 add_assoc_long_ex(zsubentry, ZEND_STRS("free"), zend_hash_num_elements(&(*list)->free));
130 add_assoc_zval_ex(zentry, key->arKey, key->nKeyLength, zsubentry);
131
132 return ZEND_HASH_APPLY_KEEP;
133 }
134
135 static int php_persistent_handle_apply_statall(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
136 {
137 php_persistent_handle_provider_t *provider = p;
138 HashTable *ht = va_arg(argv, HashTable *);
139 zval *zentry;
140
141 MAKE_STD_ZVAL(zentry);
142 array_init(zentry);
143
144 zend_hash_apply_with_arguments(&provider->list.free TSRMLS_CC, php_persistent_handle_apply_stat, 1, zentry);
145 zend_symtable_update(ht, key->arKey, key->nKeyLength, &zentry, sizeof(zval *), NULL);
146
147 return ZEND_HASH_APPLY_KEEP;
148 }
149
150 static int php_persistent_handle_apply_cleanup_ex(void *pp, void *arg TSRMLS_DC)
151 {
152 php_resource_factory_t *rf = arg;
153 void **handle = pp;
154
155 #if PHP_RAPHF_DEBUG_PHANDLES
156 fprintf(stderr, "DESTROY: %p\n", *handle);
157 #endif
158 php_resource_factory_handle_dtor(rf, *handle TSRMLS_CC);
159 return ZEND_HASH_APPLY_REMOVE;
160 }
161
162 static int php_persistent_handle_apply_cleanup(void *pp, void *arg TSRMLS_DC)
163 {
164 php_resource_factory_t *rf = arg;
165 php_persistent_handle_list_t **listp = pp;
166
167 zend_hash_apply_with_argument(&(*listp)->free, php_persistent_handle_apply_cleanup_ex, rf TSRMLS_CC);
168 if ((*listp)->used) {
169 return ZEND_HASH_APPLY_KEEP;
170 }
171 zend_hash_destroy(&(*listp)->free);
172 #if PHP_RAPHF_DEBUG_PHANDLES
173 fprintf(stderr, "LSTFREE: %p\n", *listp);
174 #endif
175 pefree(*listp, 1);
176 *listp = NULL;
177 return ZEND_HASH_APPLY_REMOVE;
178 }
179
180 static inline void php_persistent_handle_list_dtor(php_persistent_handle_list_t *list, php_persistent_handle_provider_t *provider TSRMLS_DC)
181 {
182 #if PHP_RAPHF_DEBUG_PHANDLES
183 fprintf(stderr, "LSTDTOR: %p\n", list);
184 #endif
185 zend_hash_apply_with_argument(&list->free, php_persistent_handle_apply_cleanup_ex, &provider->rf TSRMLS_CC);
186 zend_hash_destroy(&list->free);
187 }
188
189 static inline void php_persistent_handle_list_free(php_persistent_handle_list_t **list, php_persistent_handle_provider_t *provider TSRMLS_DC)
190 {
191 php_persistent_handle_list_dtor(*list, provider TSRMLS_CC);
192 #if PHP_RAPHF_DEBUG_PHANDLES
193 fprintf(stderr, "LSTFREE: %p\n", *list);
194 #endif
195 pefree(*list, 1);
196 *list = NULL;
197 }
198
199 static int php_persistent_handle_list_apply_dtor(void *listp, void *provider TSRMLS_DC)
200 {
201 php_persistent_handle_list_free(listp, provider TSRMLS_CC);
202 return ZEND_HASH_APPLY_REMOVE;
203 }
204
205 static inline php_persistent_handle_list_t *php_persistent_handle_list_find(php_persistent_handle_provider_t *provider, const char *ident_str, size_t ident_len TSRMLS_DC)
206 {
207 php_persistent_handle_list_t **list, *new_list;
208
209 if (SUCCESS == zend_symtable_find(&provider->list.free, ident_str, ident_len + 1, (void *) &list)) {
210 #if PHP_RAPHF_DEBUG_PHANDLES
211 fprintf(stderr, "LSTFIND: %p\n", *list);
212 #endif
213 return *list;
214 }
215
216 if ((new_list = php_persistent_handle_list_init(NULL))) {
217 if (SUCCESS == zend_symtable_update(&provider->list.free, ident_str, ident_len + 1, (void *) &new_list, sizeof(php_persistent_handle_list_t *), (void *) &list)) {
218 #if PHP_RAPHF_DEBUG_PHANDLES
219 fprintf(stderr, "LSTFIND: %p (new)\n", *list);
220 #endif
221 return *list;
222 }
223 php_persistent_handle_list_free(&new_list, provider TSRMLS_CC);
224 }
225
226 return NULL;
227 }
228
229 static int php_persistent_handle_apply_cleanup_all(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
230 {
231 php_persistent_handle_provider_t *provider = p;
232 const char *ident_str = va_arg(argv, const char *);
233 size_t ident_len = va_arg(argv, size_t);
234 php_persistent_handle_list_t *list;
235
236 if (ident_str && ident_len) {
237 if ((list = php_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) {
238 zend_hash_apply_with_argument(&list->free, php_persistent_handle_apply_cleanup_ex, &provider->rf TSRMLS_CC);
239 }
240 } else {
241 zend_hash_apply_with_argument(&provider->list.free, php_persistent_handle_apply_cleanup, &provider->rf TSRMLS_CC);
242 }
243
244 return ZEND_HASH_APPLY_KEEP;
245 }
246
247 static void php_persistent_handle_hash_dtor(void *p)
248 {
249 php_persistent_handle_provider_t *provider = (php_persistent_handle_provider_t *) p;
250 TSRMLS_FETCH();
251
252 zend_hash_apply_with_argument(&provider->list.free, php_persistent_handle_list_apply_dtor, provider TSRMLS_CC);
253 zend_hash_destroy(&provider->list.free);
254 php_resource_factory_dtor(&provider->rf);
255 }
256
257 PHP_RAPHF_API STATUS php_persistent_handle_provide(const char *name_str, size_t name_len, php_resource_factory_ops_t *fops, void *data, void (*dtor)(void *) TSRMLS_DC)
258 {
259 STATUS status = FAILURE;
260 php_persistent_handle_provider_t provider;
261
262 if (php_persistent_handle_list_init(&provider.list)) {
263 if (php_resource_factory_init(&provider.rf, fops, data, dtor)) {
264 #if PHP_RAPHF_DEBUG_PHANDLES
265 fprintf(stderr, "PROVIDE: %p %s\n", PHP_RAPHF_G, name_str);
266 #endif
267
268 if (SUCCESS == zend_symtable_update(&PHP_RAPHF_G->persistent_handle.hash, name_str, name_len+1, (void *) &provider, sizeof(php_persistent_handle_provider_t), NULL)) {
269 status = SUCCESS;
270 } else {
271 php_resource_factory_dtor(&provider.rf);
272 }
273 }
274 }
275
276 return status;
277 }
278
279 PHP_RAPHF_API php_persistent_handle_factory_t *php_persistent_handle_concede(php_persistent_handle_factory_t *a, const char *name_str, size_t name_len, const char *ident_str, size_t ident_len, php_persistent_handle_wakeup_t wakeup, php_persistent_handle_retire_t retire TSRMLS_DC)
280 {
281 STATUS status = FAILURE;
282 php_persistent_handle_factory_t *free_a = NULL;
283
284 if (!a) {
285 free_a = a = emalloc(sizeof(*a));
286 }
287 memset(a, 0, sizeof(*a));
288
289 status = zend_symtable_find(&PHP_RAPHF_G->persistent_handle.hash, name_str, name_len+1, (void *) &a->provider);
290
291 if (SUCCESS == status) {
292 a->ident.str = estrndup(ident_str, ident_len);
293 a->ident.len = ident_len;
294
295 a->wakeup = wakeup;
296 a->retire = retire;
297
298 if (free_a) {
299 a->free_on_abandon = 1;
300 }
301 } else {
302 if (free_a) {
303 efree(free_a);
304 }
305 a = NULL;
306 }
307
308 #if PHP_RAPHF_DEBUG_PHANDLES
309 fprintf(stderr, "CONCEDE: %p %p (%s) (%s)\n", PHP_RAPHF_G, a ? a->provider : NULL, name_str, ident_str);
310 #endif
311
312 return a;
313 }
314
315 PHP_RAPHF_API void php_persistent_handle_abandon(php_persistent_handle_factory_t *a)
316 {
317 zend_bool f = a->free_on_abandon;
318
319 #if PHP_RAPHF_DEBUG_PHANDLES
320 fprintf(stderr, "ABANDON: %p\n", a->provider);
321 #endif
322
323 STR_FREE(a->ident.str);
324 memset(a, 0, sizeof(*a));
325 if (f) {
326 efree(a);
327 }
328 }
329
330 PHP_RAPHF_API void *php_persistent_handle_acquire(php_persistent_handle_factory_t *a, void *init_arg TSRMLS_DC)
331 {
332 ulong index;
333 void **handle_ptr, *handle = NULL;
334 php_persistent_handle_list_t *list;
335
336 if ((list = php_persistent_handle_list_find(a->provider, a->ident.str, a->ident.len TSRMLS_CC))) {
337 zend_hash_internal_pointer_end(&list->free);
338 if (HASH_KEY_NON_EXISTANT != zend_hash_get_current_key(&list->free, NULL, &index, 0) && SUCCESS == zend_hash_get_current_data(&list->free, (void *) &handle_ptr)) {
339 handle = *handle_ptr;
340 if (a->wakeup) {
341 a->wakeup(a, &handle TSRMLS_CC);
342 }
343 zend_hash_index_del(&list->free, index);
344 } else {
345 handle = php_resource_factory_handle_ctor(&a->provider->rf, init_arg TSRMLS_CC);
346 }
347 #if PHP_RAPHF_DEBUG_PHANDLES
348 fprintf(stderr, "CREATED: %p\n", *handle);
349 #endif
350 if (handle) {
351 ++a->provider->list.used;
352 ++list->used;
353 }
354 }
355
356 return handle;
357 }
358
359 PHP_RAPHF_API void *php_persistent_handle_accrete(php_persistent_handle_factory_t *a, void *handle TSRMLS_DC)
360 {
361 void *new_handle = NULL;
362 php_persistent_handle_list_t *list;
363
364 if ((new_handle = php_resource_factory_handle_copy(&a->provider->rf, handle TSRMLS_CC))) {
365 if ((list = php_persistent_handle_list_find(a->provider, a->ident.str, a->ident.len TSRMLS_CC))) {
366 ++list->used;
367 }
368 ++a->provider->list.used;
369 }
370
371 return new_handle;
372 }
373
374 PHP_RAPHF_API void php_persistent_handle_release(php_persistent_handle_factory_t *a, void *handle TSRMLS_DC)
375 {
376 php_persistent_handle_list_t *list;
377
378 if ((list = php_persistent_handle_list_find(a->provider, a->ident.str, a->ident.len TSRMLS_CC))) {
379 if (a->provider->list.used >= PHP_RAPHF_G->persistent_handle.limit) {
380 #if PHP_RAPHF_DEBUG_PHANDLES
381 fprintf(stderr, "DESTROY: %p\n", *handle);
382 #endif
383 php_resource_factory_handle_dtor(&a->provider->rf, handle TSRMLS_CC);
384 } else {
385 if (a->retire) {
386 a->retire(a, &handle TSRMLS_CC);
387 }
388 zend_hash_next_index_insert(&list->free, (void *) &handle, sizeof(void *), NULL);
389 }
390
391 --a->provider->list.used;
392 --list->used;
393 }
394 }
395
396 PHP_RAPHF_API void php_persistent_handle_cleanup(const char *name_str, size_t name_len, const char *ident_str, size_t ident_len TSRMLS_DC)
397 {
398 php_persistent_handle_provider_t *provider;
399 php_persistent_handle_list_t *list;
400
401 if (name_str && name_len) {
402 if (SUCCESS == zend_symtable_find(&PHP_RAPHF_G->persistent_handle.hash, name_str, name_len+1, (void *) &provider)) {
403 if (ident_str && ident_len) {
404 if ((list = php_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) {
405 zend_hash_apply_with_argument(&list->free, php_persistent_handle_apply_cleanup_ex, &provider->rf TSRMLS_CC);
406 }
407 } else {
408 zend_hash_apply_with_argument(&provider->list.free, php_persistent_handle_apply_cleanup, &provider->rf TSRMLS_CC);
409 }
410 }
411 } else {
412 zend_hash_apply_with_arguments(&PHP_RAPHF_G->persistent_handle.hash TSRMLS_CC, php_persistent_handle_apply_cleanup_all, 2, ident_str, ident_len);
413 }
414 }
415
416 PHP_RAPHF_API HashTable *php_persistent_handle_statall(HashTable *ht TSRMLS_DC)
417 {
418 if (zend_hash_num_elements(&PHP_RAPHF_G->persistent_handle.hash)) {
419 if (!ht) {
420 ALLOC_HASHTABLE(ht);
421 zend_hash_init(ht, 0, NULL, ZVAL_PTR_DTOR, 0);
422 }
423 zend_hash_apply_with_arguments(&PHP_RAPHF_G->persistent_handle.hash TSRMLS_CC, php_persistent_handle_apply_statall, 1, ht);
424 } else if (ht) {
425 ht = NULL;
426 }
427
428 return ht;
429 }
430
431 static php_resource_factory_ops_t php_persistent_handle_resource_factory_ops = {
432 (php_resource_factory_handle_ctor_t) php_persistent_handle_acquire,
433 (php_resource_factory_handle_copy_t) php_persistent_handle_accrete,
434 (php_resource_factory_handle_dtor_t) php_persistent_handle_release
435 };
436
437 PHP_RAPHF_API php_resource_factory_ops_t *php_persistent_handle_get_resource_factory_ops(void)
438 {
439 return &php_persistent_handle_resource_factory_ops;
440 }
441
442 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_stat_persistent_handles, 0, 0, 0)
443 ZEND_END_ARG_INFO();
444 static PHP_FUNCTION(raphf_stat_persistent_handles)
445 {
446 if (SUCCESS == zend_parse_parameters_none()) {
447 object_init(return_value);
448 if (php_persistent_handle_statall(HASH_OF(return_value) TSRMLS_CC)) {
449 return;
450 }
451 zval_dtor(return_value);
452 }
453 RETURN_FALSE;
454 }
455
456 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_clean_persistent_handles, 0, 0, 0)
457 ZEND_ARG_INFO(0, name)
458 ZEND_ARG_INFO(0, ident)
459 ZEND_END_ARG_INFO();
460 static PHP_FUNCTION(raphf_clean_persistent_handles)
461 {
462 char *name_str = NULL, *ident_str = NULL;
463 int name_len = 0, ident_len = 0;
464
465 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!", &name_str, &name_len, &ident_str, &ident_len)) {
466 php_persistent_handle_cleanup(name_str, name_len, ident_str, ident_len TSRMLS_CC);
467 }
468 }
469
470 static const zend_function_entry raphf_functions[] = {
471 ZEND_NS_FENTRY("raphf", stat_persistent_handles, ZEND_FN(raphf_stat_persistent_handles), ai_raphf_stat_persistent_handles, 0)
472 ZEND_NS_FENTRY("raphf", clean_persistent_handles, ZEND_FN(raphf_clean_persistent_handles), ai_raphf_clean_persistent_handles, 0)
473 {0}
474 };
475
476 PHP_INI_BEGIN()
477 STD_PHP_INI_ENTRY("raphf.persistent_handle.limit", "-1", PHP_INI_SYSTEM, OnUpdateLong, persistent_handle.limit, zend_raphf_globals, raphf_globals)
478 PHP_INI_END()
479
480 static HashTable *php_persistent_handles_global_hash;
481
482 static void php_raphf_globals_init(zend_raphf_globals *raphf_globals)
483 {
484 raphf_globals->persistent_handle.limit = -1;
485
486 zend_hash_init(&raphf_globals->persistent_handle.hash, 0, NULL, php_persistent_handle_hash_dtor, 1);
487 if (php_persistent_handles_global_hash) {
488 zend_hash_copy(&raphf_globals->persistent_handle.hash, php_persistent_handles_global_hash, NULL, NULL, sizeof(php_persistent_handle_provider_t));
489 }
490 }
491
492 static void php_raphf_globals_dtor(zend_raphf_globals *raphf_globals)
493 {
494 zend_hash_destroy(&raphf_globals->persistent_handle.hash);
495 }
496
497 PHP_MINIT_FUNCTION(raphf)
498 {
499 ZEND_INIT_MODULE_GLOBALS(raphf, php_raphf_globals_init, php_raphf_globals_dtor);
500 php_persistent_handles_global_hash = &PHP_RAPHF_G->persistent_handle.hash;
501
502 REGISTER_INI_ENTRIES();
503 return SUCCESS;
504 }
505
506 PHP_MSHUTDOWN_FUNCTION(raphf)
507 {
508 UNREGISTER_INI_ENTRIES();
509 return SUCCESS;
510 }
511
512 static int php_persistent_handle_apply_info_ex(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
513 {
514 php_persistent_handle_list_t **list = p;
515 zend_hash_key *super_key = va_arg(argv, zend_hash_key *);
516 char used[21], free[21];
517
518 slprintf(used, sizeof(used), "%u", (*list)->used);
519 slprintf(free, sizeof(free), "%d", zend_hash_num_elements(&(*list)->free));
520
521 php_info_print_table_row(4, super_key->arKey, key->arKey, used, free);
522
523 return ZEND_HASH_APPLY_KEEP;
524 }
525
526 static int php_persistent_handle_apply_info(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
527 {
528 php_persistent_handle_provider_t *provider = p;
529
530 zend_hash_apply_with_arguments(&provider->list.free TSRMLS_CC, php_persistent_handle_apply_info_ex, 1, key);
531
532 return ZEND_HASH_APPLY_KEEP;
533 }
534
535 PHP_MINFO_FUNCTION(raphf)
536 {
537 php_info_print_table_start();
538 php_info_print_table_header(2, "Resource and persistent handle factory support", "enabled");
539 php_info_print_table_row(2, "Extension version", PHP_RAPHF_VERSION);
540 php_info_print_table_end();
541
542 php_info_print_table_start();
543 php_info_print_table_colspan_header(4, "Persistent handles in this "
544 #ifdef ZTS
545 "thread"
546 #else
547 "process"
548 #endif
549 );
550 php_info_print_table_header(4, "Provider", "Ident", "Used", "Free");
551 zend_hash_apply_with_arguments(&PHP_RAPHF_G->persistent_handle.hash TSRMLS_CC, php_persistent_handle_apply_info, 0);
552 php_info_print_table_end();
553
554 DISPLAY_INI_ENTRIES();
555 }
556
557 zend_module_entry raphf_module_entry = {
558 STANDARD_MODULE_HEADER,
559 "raphf",
560 raphf_functions,
561 PHP_MINIT(raphf),
562 PHP_MSHUTDOWN(raphf),
563 NULL,
564 NULL,
565 PHP_MINFO(raphf),
566 PHP_RAPHF_VERSION,
567 STANDARD_MODULE_PROPERTIES
568 };
569 /* }}} */
570
571 #ifdef COMPILE_DL_RAPHF
572 ZEND_GET_MODULE(raphf)
573 #endif
574
575
576 /*
577 * Local variables:
578 * tab-width: 4
579 * c-basic-offset: 4
580 * End:
581 * vim600: noet sw=4 ts=4 fdm=marker
582 * vim<600: noet sw=4 ts=4
583 */