Back to index

php5  5.3.10
zend_gc.h
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Zend Engine                                                          |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 2.00 of the Zend license,     |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.zend.com/license/2_00.txt.                                |
00011    | If you did not receive a copy of the Zend license and are unable to  |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@zend.com so we can mail you a copy immediately.              |
00014    +----------------------------------------------------------------------+
00015    | Authors: David Wang <planetbeing@gmail.com>                          |
00016    |          Dmitry Stogov <dmitry@zend.com>                             |
00017    +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: zend_gc.h 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #ifndef ZEND_GC_H
00023 #define ZEND_GC_H
00024 
00025 #ifndef GC_BENCH
00026 # define GC_BENCH 0
00027 #endif
00028 
00029 #if GC_BENCH
00030 # define GC_BENCH_INC(counter) GC_G(counter)++
00031 # define GC_BENCH_DEC(counter) GC_G(counter)--
00032 # define GC_BENCH_PEAK(peak, counter) do {              \
00033               if (GC_G(counter) > GC_G(peak)) {         \
00034                      GC_G(peak) = GC_G(counter);               \
00035               }                                                                     \
00036        } while (0)
00037 #else
00038 # define GC_BENCH_INC(counter)
00039 # define GC_BENCH_DEC(counter)
00040 # define GC_BENCH_PEAK(peak, counter)
00041 #endif
00042 
00043 #define GC_COLOR  0x03
00044 
00045 #define GC_BLACK  0x00
00046 #define GC_WHITE  0x01
00047 #define GC_GREY   0x02
00048 #define GC_PURPLE 0x03
00049 
00050 #define GC_ADDRESS(v) \
00051        ((gc_root_buffer*)(((zend_uintptr_t)(v)) & ~GC_COLOR))
00052 #define GC_SET_ADDRESS(v, a) \
00053        (v) = ((gc_root_buffer*)((((zend_uintptr_t)(v)) & GC_COLOR) | ((zend_uintptr_t)(a))))
00054 #define GC_GET_COLOR(v) \
00055        (((zend_uintptr_t)(v)) & GC_COLOR)
00056 #define GC_SET_COLOR(v, c) \
00057        (v) = ((gc_root_buffer*)((((zend_uintptr_t)(v)) & ~GC_COLOR) | (c)))
00058 #define GC_SET_BLACK(v) \
00059        (v) = ((gc_root_buffer*)(((zend_uintptr_t)(v)) & ~GC_COLOR))
00060 #define GC_SET_PURPLE(v) \
00061        (v) = ((gc_root_buffer*)(((zend_uintptr_t)(v)) | GC_PURPLE))
00062 
00063 #define GC_ZVAL_INIT(z) \
00064        ((zval_gc_info*)(z))->u.buffered = NULL
00065 #define GC_ZVAL_ADDRESS(v) \
00066        GC_ADDRESS(((zval_gc_info*)(v))->u.buffered)
00067 #define GC_ZVAL_SET_ADDRESS(v, a) \
00068        GC_SET_ADDRESS(((zval_gc_info*)(v))->u.buffered, (a))
00069 #define GC_ZVAL_GET_COLOR(v) \
00070        GC_GET_COLOR(((zval_gc_info*)(v))->u.buffered)
00071 #define GC_ZVAL_SET_COLOR(v, c) \
00072        GC_SET_COLOR(((zval_gc_info*)(v))->u.buffered, (c))
00073 #define GC_ZVAL_SET_BLACK(v) \
00074        GC_SET_BLACK(((zval_gc_info*)(v))->u.buffered)
00075 #define GC_ZVAL_SET_PURPLE(v) \
00076        GC_SET_PURPLE(((zval_gc_info*)(v))->u.buffered)
00077 
00078 #define GC_OBJ_INIT(z) \
00079        (z)->buffered = NULL
00080 
00081 typedef struct _gc_root_buffer {
00082        struct _gc_root_buffer   *prev;           /* double-linked list               */
00083        struct _gc_root_buffer   *next;
00084        zend_object_handle        handle;  /* must be 0 for zval               */
00085        union {
00086               zval                 *pz;
00087               zend_object_handlers *handlers;
00088        } u;
00089 } gc_root_buffer;
00090 
00091 typedef struct _zval_gc_info {
00092        zval z;
00093        union {
00094               gc_root_buffer       *buffered;
00095               struct _zval_gc_info *next;
00096        } u;
00097 } zval_gc_info;
00098 
00099 typedef struct _zend_gc_globals {
00100        zend_bool         gc_enabled;
00101        zend_bool         gc_active;
00102 
00103        gc_root_buffer   *buf;                           /* preallocated arrays of buffers   */
00104        gc_root_buffer    roots;                  /* list of possible roots of cycles */
00105        gc_root_buffer   *unused;                 /* list of unused buffers           */
00106        gc_root_buffer   *first_unused;           /* pointer to first unused buffer   */
00107        gc_root_buffer   *last_unused;            /* pointer to last unused buffer    */
00108 
00109        zval_gc_info     *zval_to_free;           /* temporaryt list of zvals to free */
00110        zval_gc_info     *free_list;
00111        zval_gc_info     *next_to_free;
00112 
00113        zend_uint gc_runs;
00114        zend_uint collected;
00115 
00116 #if GC_BENCH
00117        zend_uint root_buf_length;
00118        zend_uint root_buf_peak;
00119        zend_uint zval_possible_root;
00120        zend_uint zobj_possible_root;
00121        zend_uint zval_buffered;
00122        zend_uint zobj_buffered;
00123        zend_uint zval_remove_from_buffer;
00124        zend_uint zobj_remove_from_buffer;
00125        zend_uint zval_marked_grey;
00126        zend_uint zobj_marked_grey;
00127 #endif
00128 
00129 } zend_gc_globals;
00130 
00131 #ifdef ZTS
00132 BEGIN_EXTERN_C()
00133 ZEND_API extern int gc_globals_id;
00134 END_EXTERN_C()
00135 #define GC_G(v) TSRMG(gc_globals_id, zend_gc_globals *, v)
00136 #else
00137 #define GC_G(v) (gc_globals.v)
00138 extern ZEND_API zend_gc_globals gc_globals;
00139 #endif
00140 
00141 BEGIN_EXTERN_C()
00142 ZEND_API int  gc_collect_cycles(TSRMLS_D);
00143 ZEND_API void gc_zval_possible_root(zval *zv TSRMLS_DC);
00144 ZEND_API void gc_zobj_possible_root(zval *zv TSRMLS_DC);
00145 ZEND_API void gc_remove_zval_from_buffer(zval *zv TSRMLS_DC);
00146 ZEND_API void gc_globals_ctor(TSRMLS_D);
00147 ZEND_API void gc_globals_dtor(TSRMLS_D);
00148 ZEND_API void gc_init(TSRMLS_D);
00149 ZEND_API void gc_reset(TSRMLS_D);
00150 END_EXTERN_C()
00151 
00152 #define GC_ZVAL_CHECK_POSSIBLE_ROOT(z) \
00153        gc_zval_check_possible_root((z) TSRMLS_CC)
00154 
00155 #define GC_REMOVE_FROM_BUFFER(current) \
00156        gc_remove_from_buffer((current) TSRMLS_CC)
00157 
00158 #define GC_REMOVE_ZVAL_FROM_BUFFER(z)                                 \
00159        if (GC_ADDRESS(((zval_gc_info*)z)->u.buffered)) {       \
00160               gc_remove_zval_from_buffer(z TSRMLS_CC);         \
00161        }
00162 
00163 #define GC_ZOBJ_CHECK_POSSIBLE_ROOT(zobject)                                                             \
00164        do {                                                                                                                          \
00165               if (EXPECTED(EG(objects_store).object_buckets != NULL) &&                           \
00166                   EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zobject)].valid) {       \
00167                      gc_zobj_possible_root(zobject TSRMLS_CC);                                           \
00168               }                                                                                                                             \
00169        } while (0)
00170 
00171 #define GC_REMOVE_ZOBJ_FROM_BUFFER(obj)                                                           \
00172        do {                                                                                                            \
00173               if (GC_ADDRESS((obj)->buffered) && !GC_G(gc_active)) {                \
00174                      GC_BENCH_INC(zobj_remove_from_buffer);                                       \
00175                      GC_REMOVE_FROM_BUFFER(GC_ADDRESS((obj)->buffered));                   \
00176                      (obj)->buffered = NULL;                                                                    \
00177               }                                                                                                               \
00178        } while (0)
00179 
00180 static zend_always_inline void gc_zval_check_possible_root(zval *z TSRMLS_DC)
00181 {
00182        if (z->type == IS_ARRAY || z->type == IS_OBJECT) {
00183               gc_zval_possible_root(z TSRMLS_CC);
00184        }
00185 }
00186 
00187 static zend_always_inline void gc_remove_from_buffer(gc_root_buffer *root TSRMLS_DC)
00188 {
00189        root->next->prev = root->prev;
00190        root->prev->next = root->next;
00191        root->prev = GC_G(unused);
00192        GC_G(unused) = root;
00193        GC_BENCH_DEC(root_buf_length);
00194 }
00195 
00196 #define ALLOC_PERMANENT_ZVAL(z)                                              \
00197        do {                                                                                \
00198               (z) = (zval*)malloc(sizeof(zval_gc_info));              \
00199               GC_ZVAL_INIT(z);                                                      \
00200        } while (0)
00201 
00202 /* The following macroses override macroses from zend_alloc.h */
00203 #undef  ALLOC_ZVAL
00204 #define ALLOC_ZVAL(z)                                                               \
00205        do {                                                                                \
00206               (z) = (zval*)emalloc(sizeof(zval_gc_info));             \
00207               GC_ZVAL_INIT(z);                                                      \
00208        } while (0)
00209 
00210 #undef  FREE_ZVAL
00211 #define FREE_ZVAL(z)                                                                \
00212        do {                                                                                \
00213            GC_REMOVE_ZVAL_FROM_BUFFER(z);                             \
00214               efree(z);                                                                    \
00215        } while (0)
00216 
00217 #undef  ALLOC_ZVAL_REL
00218 #define ALLOC_ZVAL_REL(z)                                                    \
00219        do {                                                                                \
00220               (z) = (zval*)emalloc_rel(sizeof(zval_gc_info));  \
00221               GC_ZVAL_INIT(z);                                                      \
00222        } while (0)
00223 
00224 #undef  FREE_ZVAL_REL
00225 #define FREE_ZVAL_REL(z)                                                     \
00226        do {                                                                                \
00227            GC_REMOVE_ZVAL_FROM_BUFFER(z);                             \
00228               efree_rel(z);                                                         \
00229        } while (0)
00230 
00231 #define FREE_ZVAL_EX(z)                                                             \
00232        efree(z)
00233 
00234 #define FREE_ZVAL_REL_EX(z)                                                  \
00235        efree_rel(z)
00236 
00237 #endif /* ZEND_GC_H */
00238 
00239 /*
00240  * Local variables:
00241  * tab-width: 4
00242  * c-basic-offset: 4
00243  * indent-tabs-mode: t
00244  * End:
00245  */