[libvirt] [libvirt-php PATCH 01/13] Move PHP version compat macros to utils.h

Dawid Zamirski dzrudy at gmail.com
Tue Aug 1 21:46:02 UTC 2017


---
 src/libvirt-php.h | 144 ++++----------------------------------------------
 src/util.h        | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 164 insertions(+), 134 deletions(-)

diff --git a/src/libvirt-php.h b/src/libvirt-php.h
index 7962e33..867eb60 100644
--- a/src/libvirt-php.h
+++ b/src/libvirt-php.h
@@ -119,139 +119,6 @@ typedef uint64_t arch_uint;
 #define UINTx PRIx64
 #endif
 
-#if PHP_MAJOR_VERSION >= 7
-typedef size_t strsize_t;
-typedef zend_resource virt_resource;
-typedef virt_resource *virt_resource_handle;
-
-#define VIRT_RETURN_RESOURCE(_resource) \
-    RETVAL_RES(_resource)
-
-#define VIRT_REGISTER_RESOURCE(_resource, _le_resource)          \
-    VIRT_RETURN_RESOURCE(zend_register_resource(_resource, _le_resource))
-
-#define VIRT_REGISTER_LIST_RESOURCE(_name) do { \
-    zval zret; \
-    ZVAL_RES(&zret, zend_register_resource(res_##_name, le_libvirt_##_name)); \
-    add_next_index_zval(return_value, &zret); \
-    } while(0)
-
-#define VIRT_RESOURCE_HANDLE(_resource) \
-    Z_RES_P(_resource)
-
-#define VIRT_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \
-    if ((_state = (_type)zend_fetch_resource(Z_RES_P(*_zval), _name, _le)) == NULL) { \
-        RETURN_FALSE; \
-    }
-
-#define VIRT_RETVAL_STRING(_str)    \
-    RETVAL_STRING(_str)
-#define VIRT_RETVAL_STRINGL(_str, _len) \
-    RETVAL_STRINGL(_str, _len)
-#define VIRT_RETURN_STRING(_str)    \
-    RETURN_STRING(_str)
-#define VIRT_RETURN_STRINGL(_str, _len) \
-    RETURN_STRINGL(_str, _len)
-#define VIRT_ZVAL_STRINGL(_zv, _str, _len)  \
-    ZVAL_STRINGL(_zv, _str, _len)
-#define VIRT_ADD_INDEX_STRING(_arg, _idx, _str)  \
-    add_index_string(_arg, _idx, _str)
-#define VIRT_ADD_NEXT_INDEX_STRING(_arg, _str)  \
-    add_next_index_string(_arg, _str)
-#define VIRT_ADD_ASSOC_STRING(_arg, _key, _str) \
-    add_assoc_string(_arg, _key, _str)
-#define VIRT_ADD_ASSOC_STRING_EX(_arg, _key, _key_len, _value) \
-    add_assoc_string_ex(_arg, _key, _key_len, _value)
-
-#define VIRT_FOREACH(_ht, _pos, _zv) \
-    for (zend_hash_internal_pointer_reset_ex(_ht, &_pos); \
-         (_zv = zend_hash_get_current_data_ex(_ht, &_pos)) != NULL; \
-         zend_hash_move_forward_ex(_ht, &_pos)) \
-
-#define VIRT_FOREACH_END(_dummy)
-
-#define VIRT_HASH_CURRENT_KEY_INFO(_ht, _pos, _idx, _info) \
-    do { \
-    zend_string *tmp_key_info; \
-    _info.type = zend_hash_get_current_key_ex(_ht, &tmp_key_info, &_idx, &_pos); \
-    _info.name = ZSTR_VAL(tmp_key_info); \
-    _info.length = ZSTR_LEN(tmp_key_info); \
-    } while(0)
-
-#define VIRT_ARRAY_INIT(_name) do { \
-    zval z##_name; \
-    _name = &z##_name; \
-    array_init(_name); \
-    } while(0)
-
-#else /* PHP_MAJOR_VERSION < 7 */
-typedef int strsize_t;
-typedef long zend_long;
-typedef unsigned long zend_ulong;
-typedef zend_rsrc_list_entry virt_resource;
-typedef long virt_resource_handle;
-
-#define VIRT_RETURN_RESOURCE(_resource) \
-    RETVAL_RESOURCE((long) _resource)
-
-#define VIRT_REGISTER_RESOURCE(_resource, _le_resource) \
-    ZEND_REGISTER_RESOURCE(return_value, _resource, _le_resource)
-
-#define VIRT_REGISTER_LIST_RESOURCE(_name) do { \
-    zval *zret; \
-    ALLOC_INIT_ZVAL(zret); \
-    ZEND_REGISTER_RESOURCE(zret, res_##_name, le_libvirt_##_name); \
-    add_next_index_zval(return_value, zret); \
-    } while(0)
-
-#define VIRT_RESOURCE_HANDLE(_resource) \
-    Z_LVAL_P(_resource)
-
-#define VIRT_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \
-    ZEND_FETCH_RESOURCE(_state, _type, _zval, -1, _name, _le);
-
-#define VIRT_RETVAL_STRING(_str)    \
-    RETVAL_STRING(_str, 1)
-#define VIRT_RETVAL_STRINGL(_str, _len) \
-    RETVAL_STRINGL(_str, _len, 1)
-#define VIRT_RETURN_STRING(_str)    \
-    RETURN_STRING(_str, 1)
-#define VIRT_RETURN_STRINGL(_str, _len) \
-    RETURN_STRINGL(_str, _len, 1)
-#define VIRT_ZVAL_STRINGL(_zv, _str, _len)  \
-    ZVAL_STRINGL(_zv, _str, _len, 1)
-#define VIRT_ADD_INDEX_STRING(_arg, _idx, _str)  \
-    add_index_string(_arg, _idx, _str, 1)
-#define VIRT_ADD_NEXT_INDEX_STRING(_arg, _str)  \
-    add_next_index_string(_arg, _str, 1)
-#define VIRT_ADD_ASSOC_STRING(_arg, _key, _str) \
-    add_assoc_string(_arg, _key, _str, 1)
-#define VIRT_ADD_ASSOC_STRING_EX(_arg, _key, _key_len, _value) \
-    add_assoc_string_ex(_arg, _key, _key_len, _value, 1)
-
-#define VIRT_FOREACH(_ht, _pos, _zv) \
-    { \
-    zval **pzv = &_zv; \
-    for (zend_hash_internal_pointer_reset_ex(_ht, &_pos); \
-         zend_hash_get_current_data_ex(_ht, (void **) &pzv, &_pos) == SUCCESS; \
-         zend_hash_move_forward_ex(_ht, &_pos)) { \
-        _zv = *pzv;
-
-#define VIRT_FOREACH_END(_dummy) \
-    }}
-
-#define VIRT_HASH_CURRENT_KEY_INFO(_ht, _pos, _idx, _info) \
-    do { \
-    _info.type = zend_hash_get_current_key_ex(_ht, &_info.name, &_info.length, &_idx, 0, &_pos); \
-    } while(0)
-
-#define VIRT_ARRAY_INIT(_name) do {\
-    ALLOC_INIT_ZVAL(_name); \
-    array_init(_name); \
-    } while(0)
-
-#endif /* PHP_MAJOR_VERSION < 7 */
-
 typedef struct tTokenizer {
     char **tokens;
     int numTokens;
@@ -315,6 +182,16 @@ typedef struct tVMNetwork {
     char *model;
 } tVMNetwork;
 
+/* TODO: Temporary due to code movement to util.h - will be removed when
+ *       libvirt-connection.h is separated out
+ */
+#if PHP_MAJOR_VERSION >= 7
+typedef zend_resource virt_resource;
+typedef virt_resource *virt_resource_handle;
+#else
+typedef long virt_resource_handle;
+#endif
+
 /* Libvirt-php types */
 typedef struct _php_libvirt_connection {
     virConnectPtr conn;
@@ -376,7 +253,6 @@ typedef struct _php_libvirt_hash_key_info {
 
 /* Private definitions */
 int set_logfile(char *filename, long maxsize TSRMLS_DC);
-char *get_datetime(void);
 char *get_string_from_xpath(char *xml, char *xpath, zval **val, int *retVal);
 char **get_array_from_xpath(char *xml, char *xpath, int *num);
 
diff --git a/src/util.h b/src/util.h
index c2b7324..533505d 100644
--- a/src/util.h
+++ b/src/util.h
@@ -11,6 +11,7 @@
 # define __UTIL_H__
 
 # include <stdint.h>
+# include "libvirt-php.h"
 
 # define DEBUG_SUPPORT
 
@@ -65,6 +66,159 @@
                ((uint32_t)var[2] <<  8) +   \
                ((uint32_t)var[3]))
 
+# if PHP_MAJOR_VERSION >= 7
+    typedef size_t strsize_t;
+    typedef zend_resource virt_resource;
+    typedef virt_resource *virt_resource_handle;
+
+#  define VIRT_RETURN_RESOURCE(_resource) \
+    RETVAL_RES(_resource)
+
+#  define VIRT_REGISTER_RESOURCE(_resource, _le_resource)          \
+    VIRT_RETURN_RESOURCE(zend_register_resource(_resource, _le_resource))
+
+#  define VIRT_REGISTER_LIST_RESOURCE(_name) do { \
+    zval zret; \
+    ZVAL_RES(&zret, zend_register_resource(res_##_name, le_libvirt_##_name)); \
+    add_next_index_zval(return_value, &zret); \
+    } while(0)
+
+#  define VIRT_RESOURCE_HANDLE(_resource) \
+    Z_RES_P(_resource)
+
+#  define VIRT_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \
+    if ((_state = (_type)zend_fetch_resource(Z_RES_P(*_zval), _name, _le)) == NULL) { \
+        RETURN_FALSE; \
+    }
+
+#  define VIRT_RETVAL_STRING(_str)    \
+    RETVAL_STRING(_str)
+
+#  define VIRT_RETVAL_STRINGL(_str, _len) \
+    RETVAL_STRINGL(_str, _len)
+
+#  define VIRT_RETURN_STRING(_str)    \
+    RETURN_STRING(_str)
+
+#  define VIRT_RETURN_STRINGL(_str, _len) \
+    RETURN_STRINGL(_str, _len)
+
+#  define VIRT_ZVAL_STRINGL(_zv, _str, _len)  \
+    ZVAL_STRINGL(_zv, _str, _len)
+
+#  define VIRT_ADD_INDEX_STRING(_arg, _idx, _str)  \
+    add_index_string(_arg, _idx, _str)
+
+#  define VIRT_ADD_NEXT_INDEX_STRING(_arg, _str)  \
+    add_next_index_string(_arg, _str)
+
+#  define VIRT_ADD_ASSOC_STRING(_arg, _key, _str) \
+    add_assoc_string(_arg, _key, _str)
+
+#  define VIRT_ADD_ASSOC_STRING_EX(_arg, _key, _key_len, _value) \
+    add_assoc_string_ex(_arg, _key, _key_len, _value)
+
+#  define VIRT_FOREACH(_ht, _pos, _zv) \
+    for (zend_hash_internal_pointer_reset_ex(_ht, &_pos); \
+         (_zv = zend_hash_get_current_data_ex(_ht, &_pos)) != NULL; \
+         zend_hash_move_forward_ex(_ht, &_pos)) \
+
+#  define VIRT_FOREACH_END(_dummy)
+
+#  define VIRT_HASH_CURRENT_KEY_INFO(_ht, _pos, _idx, _info) \
+    do { \
+    zend_string *tmp_key_info; \
+    _info.type = zend_hash_get_current_key_ex(_ht, &tmp_key_info, &_idx, &_pos); \
+    _info.name = ZSTR_VAL(tmp_key_info); \
+    _info.length = ZSTR_LEN(tmp_key_info); \
+    } while(0)
+
+#  define VIRT_ARRAY_INIT(_name) do { \
+    zval z##_name; \
+    _name = &z##_name; \
+    array_init(_name); \
+    } while(0)
+
+# else /* PHP_MAJOR_VERSION < 7 */
+    typedef int strsize_t;
+    typedef long zend_long;
+    typedef unsigned long zend_ulong;
+    typedef zend_rsrc_list_entry virt_resource;
+    typedef long virt_resource_handle;
+
+#  define VIRT_RETURN_RESOURCE(_resource) \
+    RETVAL_RESOURCE((long) _resource)
+
+#  define VIRT_REGISTER_RESOURCE(_resource, _le_resource) \
+    ZEND_REGISTER_RESOURCE(return_value, _resource, _le_resource)
+
+#  define VIRT_REGISTER_LIST_RESOURCE(_name) do { \
+    zval *zret; \
+    ALLOC_INIT_ZVAL(zret); \
+    ZEND_REGISTER_RESOURCE(zret, res_##_name, le_libvirt_##_name); \
+    add_next_index_zval(return_value, zret); \
+    } while(0)
+
+#  define VIRT_RESOURCE_HANDLE(_resource) \
+    Z_LVAL_P(_resource)
+
+#  define VIRT_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \
+    ZEND_FETCH_RESOURCE(_state, _type, _zval, -1, _name, _le);
+
+#  define VIRT_RETVAL_STRING(_str)    \
+    RETVAL_STRING(_str, 1)
+
+#  define VIRT_RETVAL_STRINGL(_str, _len) \
+    RETVAL_STRINGL(_str, _len, 1)
+
+#  define VIRT_RETURN_STRING(_str)    \
+    RETURN_STRING(_str, 1)
+
+#  define VIRT_RETURN_STRINGL(_str, _len) \
+    RETURN_STRINGL(_str, _len, 1)
+
+#  define VIRT_ZVAL_STRINGL(_zv, _str, _len)  \
+    ZVAL_STRINGL(_zv, _str, _len, 1)
+
+#  define VIRT_ADD_INDEX_STRING(_arg, _idx, _str)  \
+    add_index_string(_arg, _idx, _str, 1)
+
+#  define VIRT_ADD_NEXT_INDEX_STRING(_arg, _str)  \
+    add_next_index_string(_arg, _str, 1)
+
+#  define VIRT_ADD_ASSOC_STRING(_arg, _key, _str) \
+    add_assoc_string(_arg, _key, _str, 1)
+
+#  define VIRT_ADD_ASSOC_STRING_EX(_arg, _key, _key_len, _value) \
+    add_assoc_string_ex(_arg, _key, _key_len, _value, 1)
+
+#  define VIRT_FOREACH(_ht, _pos, _zv) \
+    { \
+    zval **pzv = &_zv; \
+    for (zend_hash_internal_pointer_reset_ex(_ht, &_pos); \
+         zend_hash_get_current_data_ex(_ht, (void **) &pzv, &_pos) == SUCCESS; \
+         zend_hash_move_forward_ex(_ht, &_pos)) { \
+        _zv = *pzv;
+
+#  define VIRT_FOREACH_END(_dummy) \
+    }}
+
+#  define VIRT_HASH_CURRENT_KEY_INFO(_ht, _pos, _idx, _info) \
+    do { \
+    _info.type = zend_hash_get_current_key_ex(_ht, &_info.name, &_info.length, &_idx, 0, &_pos); \
+    } while(0)
+
+#  define VIRT_ARRAY_INIT(_name) do {\
+    ALLOC_INIT_ZVAL(_name); \
+    array_init(_name); \
+    } while(0)
+
+# endif /* PHP_MAJOR_VERSION < 7 */
+
+# ifndef PHP_FE_END
+#  define PHP_FE_END {NULL, NULL, NULL}
+# endif
+
 void debugPrint(const char *source,
                 const char *fmt, ...);
 
-- 
2.13.3




More information about the libvir-list mailing list