refactored client options
[m6w6/ext-http] / php_http_resource_factory.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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) 2004-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 PHP_HTTP_API php_http_resource_factory_t *php_http_resource_factory_init(php_http_resource_factory_t *f, php_http_resource_factory_ops_t *fops, void *data, void (*dtor)(void *data))
16 {
17 if (!f) {
18 f = emalloc(sizeof(*f));
19 }
20 memset(f, 0, sizeof(*f));
21
22 memcpy(&f->fops, fops, sizeof(*fops));
23
24 f->data = data;
25 f->dtor = dtor;
26
27 f->refcount = 1;
28
29 return f;
30 }
31
32 PHP_HTTP_API unsigned php_http_resource_factory_addref(php_http_resource_factory_t *rf)
33 {
34 return ++rf->refcount;
35 }
36
37 PHP_HTTP_API void php_http_resource_factory_dtor(php_http_resource_factory_t *f)
38 {
39 --f->refcount;
40
41 if (!f->refcount) {
42 if (f->dtor) {
43 f->dtor(f->data);
44 }
45 }
46 }
47
48 PHP_HTTP_API void php_http_resource_factory_free(php_http_resource_factory_t **f)
49 {
50 if (*f) {
51 php_http_resource_factory_dtor(*f);
52 if (!(*f)->refcount) {
53 efree(*f);
54 *f = NULL;
55 }
56 }
57 }
58
59 PHP_HTTP_API void *php_http_resource_factory_handle_ctor(php_http_resource_factory_t *f TSRMLS_DC)
60 {
61 if (f->fops.ctor) {
62 return f->fops.ctor(f->data TSRMLS_CC);
63 }
64 return NULL;
65 }
66
67 PHP_HTTP_API void *php_http_resource_factory_handle_copy(php_http_resource_factory_t *f, void *handle TSRMLS_DC)
68 {
69 if (f->fops.copy) {
70 return f->fops.copy(f->data, handle TSRMLS_CC);
71 }
72 return NULL;
73 }
74
75 PHP_HTTP_API void php_http_resource_factory_handle_dtor(php_http_resource_factory_t *f, void *handle TSRMLS_DC)
76 {
77 if (f->fops.dtor) {
78 f->fops.dtor(f->data, handle TSRMLS_CC);
79 }
80 }
81
82 /*
83 * Local variables:
84 * tab-width: 4
85 * c-basic-offset: 4
86 * End:
87 * vim600: noet sw=4 ts=4 fdm=marker
88 * vim<600: noet sw=4 ts=4
89 */
90