1 /*
2 *
3 * Copyright 2016 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19
20 #ifndef PHP7_WRAPPER_GRPC_H
21 #define PHP7_WRAPPER_GRPC_H
22
23 #if PHP_MAJOR_VERSION < 7
24
25 #define php_grpc_int int
26 #define php_grpc_long long
27 #define php_grpc_ulong ulong
28 #define php_grpc_zend_object zend_object_value
29 #define php_grpc_add_property_string(arg, name, context, b) \
30 add_property_string(arg, name, context, b)
31 #define php_grpc_add_property_stringl(res, name, str, len, b) \
32 add_property_stringl(res, name, str, len, b)
33 #define php_grpc_add_property_zval(res, name, val) \
34 add_property_zval(res, name, val)
35 #define php_grpc_add_next_index_stringl(data, str, len, b) \
36 add_next_index_stringl(data, str, len, b)
37
38 #define PHP_GRPC_RETVAL_STRING(val, dup) RETVAL_STRING(val, dup)
39 #define PHP_GRPC_RETURN_STRING(val, dup) RETURN_STRING(val, dup)
40 #define PHP_GRPC_MAKE_STD_ZVAL(pzv) MAKE_STD_ZVAL(pzv)
41 #define PHP_GRPC_FREE_STD_ZVAL(pzv)
42 #define PHP_GRPC_DELREF(zv) Z_DELREF_P(zv)
43 #define PHP_GRPC_ADD_STRING_TO_ARRAY(val, key, key_len, str, dup) \
44 add_assoc_string_ex(val, key, key_len , str, dup);
45 #define PHP_GRPC_ADD_LONG_TO_ARRAY(val, key, key_len, str) \
46 add_assoc_long_ex(val, key, key_len, str);
47 #define PHP_GRPC_ADD_BOOL_TO_ARRAY(val, key, key_len, str) \
48 add_assoc_bool_ex(val, key, key_len, str);
49 #define PHP_GRPC_ADD_LONG_TO_RETVAL(val, key, key_len, str) \
50 add_assoc_long_ex(val, key, key_len+1, str);
51
52 #define RETURN_DESTROY_ZVAL(val) \
53 RETURN_ZVAL(val, false /* Don't execute copy constructor */, \
54 true /* Dealloc original before returning */)
55
56 #define PHP_GRPC_WRAP_OBJECT_START(name) \
57 typedef struct name { \
58 zend_object std;
59 #define PHP_GRPC_WRAP_OBJECT_END(name) \
60 } name;
61
62 #define PHP_GRPC_FREE_WRAPPED_FUNC_START(class_object) \
63 void free_##class_object(void *object TSRMLS_DC) { \
64 class_object *p = (class_object *)object;
65 #define PHP_GRPC_FREE_WRAPPED_FUNC_END() \
66 zend_object_std_dtor(&p->std TSRMLS_CC); \
67 efree(p); \
68 }
69
70 #define PHP_GRPC_ALLOC_CLASS_OBJECT(class_object) \
71 class_object *intern; \
72 zend_object_value retval; \
73 intern = (class_object *)emalloc(sizeof(class_object)); \
74 memset(intern, 0, sizeof(class_object));
75
76 #define PHP_GRPC_FREE_CLASS_OBJECT(class_object, handler) \
77 retval.handle = zend_objects_store_put( \
78 intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, \
79 free_##class_object, NULL TSRMLS_CC); \
80 retval.handlers = zend_get_std_object_handlers(); \
81 return retval;
82
83 #define PHP_GRPC_HASH_FOREACH_VAL_START(ht, data) \
84 zval **tmp_data = NULL; \
85 for (zend_hash_internal_pointer_reset(ht); \
86 zend_hash_get_current_data(ht, (void**)&tmp_data) == SUCCESS; \
87 zend_hash_move_forward(ht)) { \
88 data = *tmp_data;
89
90 #define PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(ht, key, key_type, data) \
91 zval **tmp##key = NULL; \
92 ulong index##key; \
93 uint len##key; \
94 for (zend_hash_internal_pointer_reset(ht); \
95 zend_hash_get_current_data(ht, (void**)&tmp##key) == SUCCESS; \
96 zend_hash_move_forward(ht)) { \
97 key_type = zend_hash_get_current_key_ex(ht, &key, &len##key, &index##key,\
98 0, NULL); \
99 data = *tmp##key;
100
101 #define PHP_GRPC_HASH_VALPTR_TO_VAL(data) \
102 &data;
103
104 #define PHP_GRPC_HASH_FOREACH_LONG_KEY_VAL_START(ht, key, key_type, index,\
105 data) \
106 zval **tmp##key = NULL; \
107 uint len##key; \
108 for (zend_hash_internal_pointer_reset(ht); \
109 zend_hash_get_current_data(ht, (void**)&tmp##key) == SUCCESS; \
110 zend_hash_move_forward(ht)) { \
111 key_type = zend_hash_get_current_key_ex(ht, &key, &len##key, &index,\
112 0, NULL); \
113 data = *tmp##key;
114
115 #define PHP_GRPC_HASH_FOREACH_END() }
116
php_grpc_zend_hash_find(HashTable * ht,char * key,int len,void ** value)117 static inline int php_grpc_zend_hash_find(HashTable *ht, char *key, int len,
118 void **value) {
119 zval **data = NULL;
120 if (zend_hash_find(ht, key, len, (void **)&data) == SUCCESS) {
121 *value = *data;
122 return SUCCESS;
123 } else {
124 *value = NULL;
125 return FAILURE;
126 }
127 }
128
129 #define php_grpc_zend_hash_del zend_hash_del
130 #define php_grpc_zend_resource zend_rsrc_list_entry
131
132 #define PHP_GRPC_BVAL_IS_TRUE(zv) Z_LVAL_P(zv)
133 #define PHP_GRPC_VAR_SERIALIZE(buf, zv, hash) \
134 php_var_serialize(buf, &zv, hash TSRMLS_CC)
135 #define PHP_GRPC_SERIALIZED_BUF_STR(buf) buf.c
136 #define PHP_GRPC_SERIALIZED_BUF_LEN(buf) buf.len
137 #define PHP_GRPC_SHA1Update(cxt, str, len) \
138 PHP_SHA1Update(cxt, (const unsigned char *)str, len)
139 #define PHP_GRPC_PERSISTENT_LIST_FIND(plist, key, len, rsrc) \
140 zend_hash_find(plist, key, len+1, (void **)&rsrc) != FAILURE
141 #define PHP_GRPC_PERSISTENT_LIST_UPDATE(plist, key, len, rsrc) \
142 zend_hash_update(plist, key, len+1, rsrc, sizeof(php_grpc_zend_resource), \
143 NULL)
144 #define PHP_GRPC_PERSISTENT_LIST_SIZE(plist) \
145 *plist.nNumOfElements
146
147 #define PHP_GRPC_GET_CLASS_ENTRY(object) zend_get_class_entry(object TSRMLS_CC)
148
149 #define PHP_GRPC_INIT_HANDLER(class_object, handler_name)
150
151 #define PHP_GRPC_DECLARE_OBJECT_HANDLER(handler_name)
152
153 #define PHP_GRPC_GET_WRAPPED_OBJECT(class_object, zv) \
154 (class_object *)zend_object_store_get_object(zv TSRMLS_CC)
155
156 #else
157
158 #define php_grpc_int size_t
159 #define php_grpc_long zend_long
160 #define php_grpc_ulong zend_ulong
161 #define php_grpc_zend_object zend_object*
162 #define php_grpc_add_property_string(arg, name, context, b) \
163 add_property_string(arg, name, context)
164 #define php_grpc_add_property_stringl(res, name, str, len, b) \
165 add_property_stringl(res, name, str, len)
166 #define php_grpc_add_property_zval(res, name, val) do { \
167 zval tmp; \
168 tmp = *val; \
169 add_property_zval(res, name, &tmp); \
170 } while(0)
171 #define php_grpc_add_next_index_stringl(data, str, len, b) \
172 add_next_index_stringl(data, str, len)
173
174 #define PHP_GRPC_RETVAL_STRING(val, dup) RETVAL_STRING(val)
175 #define PHP_GRPC_RETURN_STRING(val, dup) RETURN_STRING(val)
176 #define PHP_GRPC_MAKE_STD_ZVAL(pzv) \
177 pzv = (zval *)emalloc(sizeof(zval));
178 #define PHP_GRPC_FREE_STD_ZVAL(pzv) efree(pzv);
179 #define PHP_GRPC_DELREF(zv)
180 #define PHP_GRPC_ADD_STRING_TO_ARRAY(val, key, key_len, str, dup) \
181 add_assoc_string_ex(val, key, key_len - 1, str);
182 #define PHP_GRPC_ADD_LONG_TO_ARRAY(val, key, key_len, str) \
183 add_assoc_long_ex(val, key, key_len - 1, str);
184 #define PHP_GRPC_ADD_BOOL_TO_ARRAY(val, key, key_len, str) \
185 add_assoc_bool_ex(val, key, key_len - 1, str);
186 #define PHP_GRPC_ADD_LONG_TO_RETVAL(val, key, key_len, str) \
187 add_assoc_long_ex(val, key, key_len, str);
188
189 #define RETURN_DESTROY_ZVAL(val) \
190 RETVAL_ZVAL(val, false /* Don't execute copy constructor */, \
191 true /* Dealloc original before returning */); \
192 efree(val); \
193 return
194
195 #define PHP_GRPC_WRAP_OBJECT_START(name) \
196 typedef struct name {
197 #define PHP_GRPC_WRAP_OBJECT_END(name) \
198 zend_object std; \
199 } name;
200
201 #define WRAPPED_OBJECT_FROM_OBJ(class_object, obj) \
202 class_object##_from_obj(obj);
203
204 #define PHP_GRPC_FREE_WRAPPED_FUNC_START(class_object) \
205 static void free_##class_object(zend_object *object) { \
206 class_object *p = WRAPPED_OBJECT_FROM_OBJ(class_object, object)
207 #define PHP_GRPC_FREE_WRAPPED_FUNC_END() \
208 zend_object_std_dtor(&p->std); \
209 }
210
211 #define PHP_GRPC_ALLOC_CLASS_OBJECT(class_object) \
212 class_object *intern; \
213 intern = ecalloc(1, sizeof(class_object) + \
214 zend_object_properties_size(class_type));
215
216 #define PHP_GRPC_FREE_CLASS_OBJECT(class_object, handler) \
217 intern->std.handlers = &handler; \
218 return &intern->std;
219
220 #define PHP_GRPC_HASH_FOREACH_VAL_START(ht, data) \
221 ZEND_HASH_FOREACH_VAL(ht, data) {
222
223 #define PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(ht, key, key_type, data) \
224 zend_string *(zs_##key); \
225 ZEND_HASH_FOREACH_STR_KEY_VAL(ht, (zs_##key), data) { \
226 if ((zs_##key) == NULL) {key = NULL; key_type = HASH_KEY_IS_LONG;} \
227 else {key = (zs_##key)->val; key_type = HASH_KEY_IS_STRING;}
228
229 #define PHP_GRPC_HASH_VALPTR_TO_VAL(data) \
230 Z_PTR_P(data);
231
232 #define PHP_GRPC_HASH_FOREACH_LONG_KEY_VAL_START(ht, key, key_type, index, \
233 data) \
234 zend_string *(zs_##key); \
235 ZEND_HASH_FOREACH_KEY_VAL(ht, index, zs_##key, data) { \
236 if ((zs_##key) == NULL) {key = NULL; key_type = HASH_KEY_IS_LONG;} \
237 else {key = (zs_##key)->val; key_type = HASH_KEY_IS_STRING;}
238
239 #define PHP_GRPC_HASH_FOREACH_END() } ZEND_HASH_FOREACH_END();
240
php_grpc_zend_hash_find(HashTable * ht,char * key,int len,void ** value)241 static inline int php_grpc_zend_hash_find(HashTable *ht, char *key, int len,
242 void **value) {
243 zval *value_tmp = zend_hash_str_find(ht, key, len -1);
244 if (value_tmp == NULL) {
245 return FAILURE;
246 } else {
247 *value = (void *)value_tmp;
248 return SUCCESS;
249 }
250 }
251
php_grpc_zend_hash_del(HashTable * ht,char * key,int len)252 static inline int php_grpc_zend_hash_del(HashTable *ht, char *key, int len) {
253 return zend_hash_str_del(ht, key, len - 1);
254 }
255 #define php_grpc_zend_resource zend_resource
256
257 #define PHP_GRPC_BVAL_IS_TRUE(zv) Z_TYPE_P(zv) == IS_TRUE
258 #define PHP_GRPC_VAR_SERIALIZE(buf, zv, hash) \
259 php_var_serialize(buf, zv, hash)
260 #define PHP_GRPC_SERIALIZED_BUF_STR(buf) ZSTR_VAL(buf.s)
261 #define PHP_GRPC_SERIALIZED_BUF_LEN(buf) ZSTR_LEN(buf.s)
262 #define PHP_GRPC_SHA1Update(cxt, str, len) \
263 PHP_SHA1Update(cxt, (unsigned char *)str, len)
264 #define PHP_GRPC_PERSISTENT_LIST_FIND(plist, key, len, rsrc) \
265 (rsrc = zend_hash_str_find_ptr(plist, key, len)) != NULL
266 #define PHP_GRPC_PERSISTENT_LIST_UPDATE(plist, key, len, rsrc) \
267 zend_hash_str_update_mem(plist, key, len, rsrc, \
268 sizeof(php_grpc_zend_resource))
269 #define PHP_GRPC_PERSISTENT_LIST_SIZE(plist) \
270 zend_array_count(plist)
271
272 #define PHP_GRPC_GET_CLASS_ENTRY(object) Z_OBJ_P(object)->ce
273
274 #define PHP_GRPC_INIT_HANDLER(class_object, handler_name) \
275 memcpy(&handler_name, zend_get_std_object_handlers(), \
276 sizeof(zend_object_handlers)); \
277 handler_name.offset = XtOffsetOf(class_object, std); \
278 handler_name.free_obj = free_##class_object
279
280 #define PHP_GRPC_DECLARE_OBJECT_HANDLER(handler_name) \
281 static zend_object_handlers handler_name;
282
283 #define PHP_GRPC_GET_WRAPPED_OBJECT(class_object, zv) \
284 class_object##_from_obj(Z_OBJ_P((zv)))
285
286 #endif /* PHP_MAJOR_VERSION */
287
288 #endif /* PHP7_WRAPPER_GRPC_H */
289