Skip to content

Commit 9b76379

Browse files
committed
Add zend_bit_enum
1 parent 4691739 commit 9b76379

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

Zend/zend_bit_enum.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Zend Engine |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 2.00 of the Zend license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.zend.com/license/2_00.txt. |
11+
| If you did not receive a copy of the Zend license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@zend.com so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifndef ZEND_BIT_ENUM_H
18+
#define ZEND_BIT_ENUM_H
19+
20+
#define _ZEND_BIT_ENUM_CASE(name, value) name = (value),
21+
#define _ZEND_BIT_ENUM_MASK(name, value) (value) |
22+
23+
#define ZEND_BIT_ENUM(name, CASES) \
24+
typedef enum { \
25+
CASES(_ZEND_BIT_ENUM_CASE) \
26+
} name; \
27+
static const int name##_mask = CASES(_ZEND_BIT_ENUM_MASK) 0; \
28+
static zend_always_inline name name##_init(int bits) { \
29+
ZEND_ASSERT((bits & name##_mask) == bits); \
30+
return (name) bits; \
31+
}
32+
33+
#endif

ext/ffi/ffi.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "zend_exceptions.h"
2626
#include "zend_closures.h"
2727
#include "main/SAPI.h"
28+
#include "Zend/zend_bit_enum.h"
2829

2930
#include <ffi.h>
3031

@@ -98,11 +99,11 @@ typedef enum _zend_ffi_type_kind {
9899

99100
#include "ffi_arginfo.h"
100101

101-
typedef enum _zend_ffi_flags {
102-
ZEND_FFI_FLAG_CONST = (1 << 0),
103-
ZEND_FFI_FLAG_OWNED = (1 << 1),
104-
ZEND_FFI_FLAG_PERSISTENT = (1 << 2),
105-
} zend_ffi_flags;
102+
#define ZEND_FFI_FLAGS_CASES(_) \
103+
_(ZEND_FFI_FLAG_CONST, 1 << 0) \
104+
_(ZEND_FFI_FLAG_OWNED, 1 << 1) \
105+
_(ZEND_FFI_FLAG_PERSISTENT, 1 << 2)
106+
ZEND_BIT_ENUM(zend_ffi_flags, ZEND_FFI_FLAGS_CASES);
106107

107108
struct _zend_ffi_type {
108109
zend_ffi_type_kind kind;
@@ -261,7 +262,7 @@ static zend_object *zend_ffi_cdata_new(zend_class_entry *class_type) /* {{{ */
261262

262263
cdata->type = NULL;
263264
cdata->ptr = NULL;
264-
cdata->flags = (zend_ffi_flags)0;
265+
cdata->flags = zend_ffi_flags_init(0);
265266

266267
return &cdata->std;
267268
}
@@ -1110,7 +1111,7 @@ static zval *zend_ffi_cdata_get(zend_object *obj, zend_string *member, int read_
11101111
return &EG(uninitialized_zval);
11111112
}
11121113

1113-
zend_ffi_cdata_to_zval(cdata, cdata->ptr, type, BP_VAR_R, rv, (zend_ffi_flags)0, 0, 0);
1114+
zend_ffi_cdata_to_zval(cdata, cdata->ptr, type, BP_VAR_R, rv, zend_ffi_flags_init(0), 0, 0);
11141115
return rv;
11151116
}
11161117
/* }}} */
@@ -2833,7 +2834,7 @@ static ZEND_FUNCTION(ffi_trampoline) /* {{{ */
28332834
}
28342835

28352836
if (ZEND_FFI_TYPE(type->func.ret_type)->kind != ZEND_FFI_TYPE_VOID) {
2836-
zend_ffi_cdata_to_zval(NULL, ret, ZEND_FFI_TYPE(type->func.ret_type), BP_VAR_R, return_value, (zend_ffi_flags)0, 1, 0);
2837+
zend_ffi_cdata_to_zval(NULL, ret, ZEND_FFI_TYPE(type->func.ret_type), BP_VAR_R, return_value, zend_ffi_flags_init(0), 1, 0);
28372838
} else {
28382839
ZVAL_NULL(return_value);
28392840
}
@@ -3868,7 +3869,7 @@ ZEND_METHOD(FFI, free) /* {{{ */
38683869
} else if (!(cdata->flags & ZEND_FFI_FLAG_OWNED)) {
38693870
pefree(cdata->ptr, cdata->flags & ZEND_FFI_FLAG_PERSISTENT);
38703871
cdata->ptr = NULL;
3871-
cdata->flags = (zend_ffi_flags)(cdata->flags & ~(ZEND_FFI_FLAG_OWNED|ZEND_FFI_FLAG_PERSISTENT));
3872+
cdata->flags = zend_ffi_flags_init(cdata->flags & ~(ZEND_FFI_FLAG_OWNED|ZEND_FFI_FLAG_PERSISTENT));
38723873
cdata->std.handlers = &zend_ffi_cdata_free_handlers;
38733874
} else {
38743875
zend_throw_error(zend_ffi_exception_ce, "free() non a C pointer");
@@ -4042,7 +4043,7 @@ ZEND_METHOD(FFI, cast) /* {{{ */
40424043
if (old_cdata->flags & ZEND_FFI_FLAG_OWNED) {
40434044
if (GC_REFCOUNT(&old_cdata->std) == 1 && Z_REFCOUNT_P(arg) == 1) {
40444045
/* transfer ownership */
4045-
old_cdata->flags = (zend_ffi_flags)(old_cdata->flags & ~ZEND_FFI_FLAG_OWNED);
4046+
old_cdata->flags = zend_ffi_flags_init(old_cdata->flags & ~ZEND_FFI_FLAG_OWNED);
40464047
cdata->flags |= ZEND_FFI_FLAG_OWNED;
40474048
} else {
40484049
//???zend_throw_error(zend_ffi_exception_ce, "Attempt to cast owned C pointer");
@@ -4279,7 +4280,7 @@ ZEND_METHOD(FFI, addr) /* {{{ */
42794280
}
42804281
if (cdata->flags & ZEND_FFI_FLAG_OWNED) {
42814282
/* transfer ownership */
4282-
cdata->flags = (zend_ffi_flags)(cdata->flags & ~ZEND_FFI_FLAG_OWNED);
4283+
cdata->flags = zend_ffi_flags_init(cdata->flags & ~ZEND_FFI_FLAG_OWNED);
42834284
new_cdata->flags |= ZEND_FFI_FLAG_OWNED;
42844285
}
42854286
}

0 commit comments

Comments
 (0)