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
24 #define php_grpc_int size_t
25 #define php_grpc_long zend_long
26 #define php_grpc_ulong zend_ulong
27 #define php_grpc_zend_object zend_object*
28 #define php_grpc_add_property_string(arg, name, context, b) \
29 add_property_string(arg, name, context)
30 #define php_grpc_add_property_stringl(res, name, str, len, b) \
31 add_property_stringl(res, name, str, len)
32 #define php_grpc_add_property_zval(res, name, val) do { \
33 zval tmp; \
34 tmp = *val; \
35 add_property_zval(res, name, &tmp); \
36 } while(0)
37 #define php_grpc_add_next_index_stringl(data, str, len, b) \
38 add_next_index_stringl(data, str, len)
39
40 #define PHP_GRPC_RETVAL_STRING(val, dup) RETVAL_STRING(val)
41 #define PHP_GRPC_RETURN_STRING(val, dup) RETURN_STRING(val)
42 #define PHP_GRPC_MAKE_STD_ZVAL(pzv) \
43 pzv = (zval *)emalloc(sizeof(zval));
44 #define PHP_GRPC_FREE_STD_ZVAL(pzv) efree(pzv);
45 #define PHP_GRPC_DELREF(zv)
46 #define PHP_GRPC_ADD_STRING_TO_ARRAY(val, key, key_len, str, dup) \
47 add_assoc_string_ex(val, key, key_len - 1, str);
48 #define PHP_GRPC_ADD_LONG_TO_ARRAY(val, key, key_len, str) \
49 add_assoc_long_ex(val, key, key_len - 1, str);
50 #define PHP_GRPC_ADD_BOOL_TO_ARRAY(val, key, key_len, str) \
51 add_assoc_bool_ex(val, key, key_len - 1, str);
52 #define PHP_GRPC_ADD_LONG_TO_RETVAL(val, key, key_len, str) \
53 add_assoc_long_ex(val, key, key_len, str);
54
55 #define RETURN_DESTROY_ZVAL(val) \
56 RETVAL_ZVAL(val, false /* Don't execute copy constructor */, \
57 true /* Dealloc original before returning */); \
58 efree(val); \
59 return
60
61 #define PHP_GRPC_WRAP_OBJECT_START(name) \
62 typedef struct name {
63 #define PHP_GRPC_WRAP_OBJECT_END(name) \
64 zend_object std; \
65 } name;
66
67 #define WRAPPED_OBJECT_FROM_OBJ(class_object, obj) \
68 class_object##_from_obj(obj);
69
70 #define PHP_GRPC_FREE_WRAPPED_FUNC_START(class_object) \
71 static void free_##class_object(zend_object *object) { \
72 class_object *p = WRAPPED_OBJECT_FROM_OBJ(class_object, object)
73 #define PHP_GRPC_FREE_WRAPPED_FUNC_END() \
74 zend_object_std_dtor(&p->std); \
75 }
76
77 #define PHP_GRPC_ALLOC_CLASS_OBJECT(class_object) \
78 class_object *intern; \
79 intern = ecalloc(1, sizeof(class_object) + \
80 zend_object_properties_size(class_type));
81
82 #define PHP_GRPC_FREE_CLASS_OBJECT(class_object, handler) \
83 intern->std.handlers = &handler; \
84 return &intern->std;
85
86 #define PHP_GRPC_HASH_FOREACH_VAL_START(ht, data) \
87 ZEND_HASH_FOREACH_VAL(ht, data) {
88
89 #define PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(ht, key, key_type, data) \
90 zend_string *(zs_##key); \
91 ZEND_HASH_FOREACH_STR_KEY_VAL(ht, (zs_##key), data) { \
92 if ((zs_##key) == NULL) {key = NULL; key_type = HASH_KEY_IS_LONG;} \
93 else {key = (zs_##key)->val; key_type = HASH_KEY_IS_STRING;}
94
95 #define PHP_GRPC_HASH_VALPTR_TO_VAL(data) \
96 Z_PTR_P(data);
97
98 #define PHP_GRPC_HASH_FOREACH_LONG_KEY_VAL_START(ht, key, key_type, index, \
99 data) \
100 zend_string *(zs_##key); \
101 ZEND_HASH_FOREACH_KEY_VAL(ht, index, zs_##key, data) { \
102 if ((zs_##key) == NULL) {key = NULL; key_type = HASH_KEY_IS_LONG;} \
103 else {key = (zs_##key)->val; key_type = HASH_KEY_IS_STRING;}
104
105 #define PHP_GRPC_HASH_FOREACH_END() } ZEND_HASH_FOREACH_END();
106
php_grpc_zend_hash_find(HashTable * ht,char * key,int len,void ** value)107 static inline int php_grpc_zend_hash_find(HashTable *ht, char *key, int len,
108 void **value) {
109 zval *value_tmp = zend_hash_str_find(ht, key, len -1);
110 if (value_tmp == NULL) {
111 return FAILURE;
112 } else {
113 *value = (void *)value_tmp;
114 return SUCCESS;
115 }
116 }
117
php_grpc_zend_hash_del(HashTable * ht,char * key,int len)118 static inline int php_grpc_zend_hash_del(HashTable *ht, char *key, int len) {
119 return zend_hash_str_del(ht, key, len - 1);
120 }
121 #define php_grpc_zend_resource zend_resource
122
123 #define PHP_GRPC_BVAL_IS_TRUE(zv) Z_TYPE_P(zv) == IS_TRUE
124 #define PHP_GRPC_VAR_SERIALIZE(buf, zv, hash) \
125 php_var_serialize(buf, zv, hash)
126 #define PHP_GRPC_SERIALIZED_BUF_STR(buf) ZSTR_VAL(buf.s)
127 #define PHP_GRPC_SERIALIZED_BUF_LEN(buf) ZSTR_LEN(buf.s)
128 #define PHP_GRPC_SHA1Update(cxt, str, len) \
129 PHP_SHA1Update(cxt, (unsigned char *)str, len)
130 #define PHP_GRPC_PERSISTENT_LIST_FIND(plist, key, len, rsrc) \
131 (rsrc = zend_hash_str_find_ptr(plist, key, len)) != NULL
132 #define PHP_GRPC_PERSISTENT_LIST_UPDATE(plist, key, len, rsrc) \
133 zend_hash_str_update_mem(plist, key, len, rsrc, \
134 sizeof(php_grpc_zend_resource))
135 #define PHP_GRPC_PERSISTENT_LIST_SIZE(plist) \
136 zend_array_count(plist)
137
138 #define PHP_GRPC_GET_CLASS_ENTRY(object) Z_OBJ_P(object)->ce
139
140 #define PHP_GRPC_INIT_HANDLER(class_object, handler_name) \
141 memcpy(&handler_name, zend_get_std_object_handlers(), \
142 sizeof(zend_object_handlers)); \
143 handler_name.offset = XtOffsetOf(class_object, std); \
144 handler_name.free_obj = free_##class_object
145
146 #define PHP_GRPC_DECLARE_OBJECT_HANDLER(handler_name) \
147 static zend_object_handlers handler_name;
148
149 #define PHP_GRPC_GET_WRAPPED_OBJECT(class_object, zv) \
150 class_object##_from_obj(Z_OBJ_P((zv)))
151
152 #endif /* PHP7_WRAPPER_GRPC_H */
153