|
| 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_REFCOUNTED_H |
| 18 | +#define ZEND_REFCOUNTED_H |
| 19 | + |
| 20 | +#include "zend_portability.h" |
| 21 | +#include "zend_rc_debug.h" |
| 22 | +#include "zend_type_code.h" |
| 23 | + |
| 24 | +#include <stdint.h> |
| 25 | + |
| 26 | +#define GC_TYPE_MASK 0x0000000f |
| 27 | +#define GC_FLAGS_MASK 0x000003f0 |
| 28 | +#define GC_INFO_MASK 0xfffffc00 |
| 29 | +#define GC_FLAGS_SHIFT 0 |
| 30 | +#define GC_INFO_SHIFT 10 |
| 31 | + |
| 32 | +/* zval_gc_flags(zval.value->gc.u.type_info) (common flags) */ |
| 33 | +#define GC_NOT_COLLECTABLE (1<<4) |
| 34 | +#define GC_PROTECTED (1<<5) /* used for recursion detection */ |
| 35 | +#define GC_IMMUTABLE (1<<6) /* can't be changed in place */ |
| 36 | +#define GC_PERSISTENT (1<<7) /* allocated using malloc */ |
| 37 | +#define GC_PERSISTENT_LOCAL (1<<8) /* persistent, but thread-local */ |
| 38 | + |
| 39 | +#define GC_TYPE_INFO(p) (p)->gc.u.type_info |
| 40 | +#define GC_TYPE(p) zval_gc_type(GC_TYPE_INFO(p)) |
| 41 | +#define GC_FLAGS(p) zval_gc_flags(GC_TYPE_INFO(p)) |
| 42 | +#define GC_INFO(p) zval_gc_info(GC_TYPE_INFO(p)) |
| 43 | + |
| 44 | +#define GC_ADD_FLAGS(p, flags) do { \ |
| 45 | + GC_TYPE_INFO(p) |= (flags) << GC_FLAGS_SHIFT; \ |
| 46 | + } while (0) |
| 47 | +#define GC_DEL_FLAGS(p, flags) do { \ |
| 48 | + GC_TYPE_INFO(p) &= ~((flags) << GC_FLAGS_SHIFT); \ |
| 49 | + } while (0) |
| 50 | + |
| 51 | +#define GC_REFCOUNT(p) zend_gc_refcount(&(p)->gc) |
| 52 | +#define GC_SET_REFCOUNT(p, rc) zend_gc_set_refcount(&(p)->gc, rc) |
| 53 | +#define GC_ADDREF(p) zend_gc_addref(&(p)->gc) |
| 54 | +#define GC_DELREF(p) zend_gc_delref(&(p)->gc) |
| 55 | +#define GC_ADDREF_EX(p, rc) zend_gc_addref_ex(&(p)->gc, rc) |
| 56 | +#define GC_DELREF_EX(p, rc) zend_gc_delref_ex(&(p)->gc, rc) |
| 57 | +#define GC_TRY_ADDREF(p) zend_gc_try_addref(&(p)->gc) |
| 58 | +#define GC_TRY_DELREF(p) zend_gc_try_delref(&(p)->gc) |
| 59 | + |
| 60 | +#define GC_NULL (IS_NULL | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT)) |
| 61 | +#define GC_STRING (IS_STRING | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT)) |
| 62 | +#define GC_ARRAY IS_ARRAY |
| 63 | +#define GC_OBJECT IS_OBJECT |
| 64 | +#define GC_RESOURCE (IS_RESOURCE | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT)) |
| 65 | +#define GC_REFERENCE (IS_REFERENCE | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT)) |
| 66 | +#define GC_CONSTANT_AST (IS_CONSTANT_AST | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT)) |
| 67 | + |
| 68 | +typedef struct _zend_refcounted_h { |
| 69 | + uint32_t refcount; /* reference counter 32-bit */ |
| 70 | + union { |
| 71 | + uint32_t type_info; |
| 72 | + } u; |
| 73 | +} zend_refcounted_h; |
| 74 | + |
| 75 | +typedef struct _zend_refcounted { |
| 76 | + zend_refcounted_h gc; |
| 77 | +} zend_refcounted; |
| 78 | + |
| 79 | +static zend_always_inline uint8_t zval_gc_type(uint32_t gc_type_info) { |
| 80 | + return (gc_type_info & GC_TYPE_MASK); |
| 81 | +} |
| 82 | + |
| 83 | +static zend_always_inline uint32_t zval_gc_flags(uint32_t gc_type_info) { |
| 84 | + return (gc_type_info >> GC_FLAGS_SHIFT) & (GC_FLAGS_MASK >> GC_FLAGS_SHIFT); |
| 85 | +} |
| 86 | + |
| 87 | +static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) { |
| 88 | + return (gc_type_info >> GC_INFO_SHIFT); |
| 89 | +} |
| 90 | + |
| 91 | +static zend_always_inline uint32_t zend_gc_refcount(const zend_refcounted_h *p) { |
| 92 | + return p->refcount; |
| 93 | +} |
| 94 | + |
| 95 | +static zend_always_inline uint32_t zend_gc_set_refcount(zend_refcounted_h *p, uint32_t rc) { |
| 96 | + p->refcount = rc; |
| 97 | + return p->refcount; |
| 98 | +} |
| 99 | + |
| 100 | +static zend_always_inline uint32_t zend_gc_addref(zend_refcounted_h *p) { |
| 101 | + ZEND_RC_MOD_CHECK(p); |
| 102 | + return ++(p->refcount); |
| 103 | +} |
| 104 | + |
| 105 | +static zend_always_inline void zend_gc_try_addref(zend_refcounted_h *p) { |
| 106 | + if (!(p->u.type_info & GC_IMMUTABLE)) { |
| 107 | + ZEND_RC_MOD_CHECK(p); |
| 108 | + ++p->refcount; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +static zend_always_inline void zend_gc_try_delref(zend_refcounted_h *p) { |
| 113 | + if (!(p->u.type_info & GC_IMMUTABLE)) { |
| 114 | + ZEND_RC_MOD_CHECK(p); |
| 115 | + --p->refcount; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +static zend_always_inline uint32_t zend_gc_delref(zend_refcounted_h *p) { |
| 120 | + ZEND_ASSERT(p->refcount > 0); |
| 121 | + ZEND_RC_MOD_CHECK(p); |
| 122 | + return --(p->refcount); |
| 123 | +} |
| 124 | + |
| 125 | +static zend_always_inline uint32_t zend_gc_addref_ex(zend_refcounted_h *p, uint32_t rc) { |
| 126 | + ZEND_RC_MOD_CHECK(p); |
| 127 | + p->refcount += rc; |
| 128 | + return p->refcount; |
| 129 | +} |
| 130 | + |
| 131 | +static zend_always_inline uint32_t zend_gc_delref_ex(zend_refcounted_h *p, uint32_t rc) { |
| 132 | + ZEND_RC_MOD_CHECK(p); |
| 133 | + p->refcount -= rc; |
| 134 | + return p->refcount; |
| 135 | +} |
| 136 | + |
| 137 | +#endif /* ZEND_REFCOUNTED_H */ |
0 commit comments