root/ext/opcache/ZendAccelerator.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


   1 /*
   2    +----------------------------------------------------------------------+
   3    | Zend OPcache                                                         |
   4    +----------------------------------------------------------------------+
   5    | Copyright (c) 1998-2016 The PHP Group                                |
   6    +----------------------------------------------------------------------+
   7    | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt                                  |
  11    | If you did not receive a copy of the PHP license and are unable to   |
  12    | obtain it through the world-wide-web, please send a note to          |
  13    | license@php.net so we can mail you a copy immediately.               |
  14    +----------------------------------------------------------------------+
  15    | Authors: Andi Gutmans <andi@zend.com>                                |
  16    |          Zeev Suraski <zeev@zend.com>                                |
  17    |          Stanislav Malyshev <stas@zend.com>                          |
  18    |          Dmitry Stogov <dmitry@zend.com>                             |
  19    +----------------------------------------------------------------------+
  20 */
  21 
  22 #ifndef ZEND_ACCELERATOR_H
  23 #define ZEND_ACCELERATOR_H
  24 
  25 #ifdef HAVE_CONFIG_H
  26 # include <config.h>
  27 #endif
  28 
  29 #define ACCELERATOR_PRODUCT_NAME        "Zend OPcache"
  30 #define PHP_ZENDOPCACHE_VERSION "7.0.6-dev"
  31 #define ACCELERATOR_VERSION PHP_ZENDOPCACHE_VERSION
  32 /* 2 - added Profiler support, on 20010712 */
  33 /* 3 - added support for Optimizer's encoded-only-files mode */
  34 /* 4 - works with the new Optimizer, that supports the file format with licenses */
  35 /* 5 - API 4 didn't really work with the license-enabled file format.  v5 does. */
  36 /* 6 - Monitor was removed from ZendPlatform.so, to a module of its own */
  37 /* 7 - Optimizer was embedded into Accelerator */
  38 /* 8 - Standalone Open Source Zend OPcache */
  39 #define ACCELERATOR_API_NO 8
  40 
  41 #if ZEND_WIN32
  42 # include "zend_config.w32.h"
  43 #else
  44 #include "zend_config.h"
  45 # include <sys/time.h>
  46 # include <sys/resource.h>
  47 #endif
  48 
  49 #if HAVE_UNISTD_H
  50 # include "unistd.h"
  51 #endif
  52 
  53 #include "zend_extensions.h"
  54 #include "zend_compile.h"
  55 
  56 #include "Optimizer/zend_optimizer.h"
  57 #include "zend_accelerator_hash.h"
  58 #include "zend_accelerator_debug.h"
  59 
  60 #ifndef PHPAPI
  61 # ifdef ZEND_WIN32
  62 #  define PHPAPI __declspec(dllimport)
  63 # else
  64 #  define PHPAPI
  65 # endif
  66 #endif
  67 
  68 #ifndef ZEND_EXT_API
  69 # if WIN32|WINNT
  70 #  define ZEND_EXT_API __declspec(dllexport)
  71 # elif defined(__GNUC__) && __GNUC__ >= 4
  72 #  define ZEND_EXT_API __attribute__ ((visibility("default")))
  73 # else
  74 #  define ZEND_EXT_API
  75 # endif
  76 #endif
  77 
  78 #ifdef ZEND_WIN32
  79 # ifndef MAXPATHLEN
  80 #  define MAXPATHLEN     _MAX_PATH
  81 # endif
  82 # include <direct.h>
  83 #else
  84 # ifndef MAXPATHLEN
  85 #  define MAXPATHLEN     4096
  86 # endif
  87 # include <sys/param.h>
  88 #endif
  89 
  90 #define PHP_5_0_X_API_NO                220040412
  91 #define PHP_5_1_X_API_NO                220051025
  92 #define PHP_5_2_X_API_NO                220060519
  93 #define PHP_5_3_X_API_NO                220090626
  94 #define PHP_5_4_X_API_NO                220100525
  95 #define PHP_5_5_X_API_NO                220121212
  96 #define PHP_5_6_X_API_NO                220131226
  97 
  98 /*** file locking ***/
  99 #ifndef ZEND_WIN32
 100 extern int lock_file;
 101 
 102 # if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (defined(__APPLE__) && defined(__MACH__)/* Darwin */) || defined(__OpenBSD__) || defined(__NetBSD__)
 103 #  define FLOCK_STRUCTURE(name, type, whence, start, len) \
 104                 struct flock name = {start, len, -1, type, whence}
 105 # elif defined(__svr4__)
 106 #  define FLOCK_STRUCTURE(name, type, whence, start, len) \
 107                 struct flock name = {type, whence, start, len}
 108 # elif defined(__linux__) || defined(__hpux) || defined(__GNU__)
 109 #  define FLOCK_STRUCTURE(name, type, whence, start, len) \
 110                 struct flock name = {type, whence, start, len, 0}
 111 # elif defined(_AIX)
 112 #  if defined(_LARGE_FILES) || defined(__64BIT__)
 113 #   define FLOCK_STRUCTURE(name, type, whence, start, len) \
 114                 struct flock name = {type, whence, 0, 0, 0, start, len }
 115 #  else
 116 #   define FLOCK_STRUCTURE(name, type, whence, start, len) \
 117                 struct flock name = {type, whence, start, len}
 118 #  endif
 119 # elif defined(HAVE_FLOCK_BSD)
 120 #  define FLOCK_STRUCTURE(name, type, whence, start, len) \
 121                 struct flock name = {start, len, -1, type, whence}
 122 # elif defined(HAVE_FLOCK_LINUX)
 123 #  define FLOCK_STRUCTURE(name, type, whence, start, len) \
 124                 struct flock name = {type, whence, start, len}
 125 # else
 126 #  error "Don't know how to define struct flock"
 127 # endif
 128 #endif
 129 
 130 #if ZEND_EXTENSION_API_NO < PHP_5_3_X_API_NO
 131         #define Z_REFCOUNT_P(pz)                                (pz)->refcount
 132         #define Z_SET_REFCOUNT_P(pz, v)                 (pz)->refcount = (v)
 133         #define Z_ADDREF_P(pz)                                  ++((pz)->refcount)
 134         #define Z_DELREF_P(pz)                                  --((pz)->refcount)
 135         #define Z_ISREF_P(pz)                                   (pz)->is_ref
 136         #define Z_SET_ISREF_P(pz)                               Z_SET_ISREF_TO_P(pz, 1)
 137         #define Z_UNSET_ISREF_P(pz)                             Z_SET_ISREF_TO_P(pz, 0)
 138         #define Z_SET_ISREF_TO_P(pz, isref)             (pz)->is_ref = (isref)
 139         #define PZ_REFCOUNT_P(pz)                               (pz)->refcount
 140         #define PZ_SET_REFCOUNT_P(pz, v)                (pz)->refcount = (v)
 141         #define PZ_ADDREF_P(pz)                                 ++((pz)->refcount)
 142         #define PZ_DELREF_P(pz)                                 --((pz)->refcount)
 143         #define PZ_ISREF_P(pz)                                  (pz)->is_ref
 144         #define PZ_SET_ISREF_P(pz)                              Z_SET_ISREF_TO_P(pz, 1)
 145         #define PZ_UNSET_ISREF_P(pz)                    Z_SET_ISREF_TO_P(pz, 0)
 146         #define PZ_SET_ISREF_TO_P(pz, isref)    (pz)->is_ref = (isref)
 147 #else
 148         #define PZ_REFCOUNT_P(pz)                               (pz)->refcount__gc
 149         #define PZ_SET_REFCOUNT_P(pz, v)                (pz)->refcount__gc = (v)
 150         #define PZ_ADDREF_P(pz)                                 ++((pz)->refcount__gc)
 151         #define PZ_DELREF_P(pz)                                 --((pz)->refcount__gc)
 152         #define PZ_ISREF_P(pz)                                  (pz)->is_ref__gc
 153         #define PZ_SET_ISREF_P(pz)                              Z_SET_ISREF_TO_P(pz, 1)
 154         #define PZ_UNSET_ISREF_P(pz)                    Z_SET_ISREF_TO_P(pz, 0)
 155         #define PZ_SET_ISREF_TO_P(pz, isref)    (pz)->is_ref__gc = (isref)
 156 #endif
 157 
 158 #if ZEND_EXTENSION_API_NO < PHP_5_3_X_API_NO
 159 # ifdef ALLOCA_FLAG
 160         #define DO_ALLOCA(x)    do_alloca_with_limit(x, use_heap)
 161         #define FREE_ALLOCA(x)  free_alloca_with_limit(x, use_heap)
 162 # else
 163         #define ALLOCA_FLAG(x)
 164         #define DO_ALLOCA(x)    do_alloca(x)
 165         #define FREE_ALLOCA(x)  free_alloca(x)
 166 # endif
 167 #else
 168         #define DO_ALLOCA(x)    do_alloca(x, use_heap)
 169         #define FREE_ALLOCA(x)  free_alloca(x, use_heap)
 170 #endif
 171 
 172 
 173 #if ZEND_WIN32
 174 typedef unsigned __int64 accel_time_t;
 175 #else
 176 typedef time_t accel_time_t;
 177 #endif
 178 
 179 typedef enum _zend_accel_restart_reason {
 180         ACCEL_RESTART_OOM,    /* restart because of out of memory */
 181         ACCEL_RESTART_HASH,   /* restart because of hash overflow */
 182         ACCEL_RESTART_USER    /* restart scheduled by opcache_reset() */
 183 } zend_accel_restart_reason;
 184 
 185 typedef struct _zend_persistent_script {
 186         ulong          hash_value;
 187         char          *full_path;              /* full real path with resolved symlinks */
 188         unsigned int   full_path_len;
 189         zend_op_array  main_op_array;
 190         HashTable      function_table;
 191         HashTable      class_table;
 192         long           compiler_halt_offset;   /* position of __HALT_COMPILER or -1 */
 193         int            ping_auto_globals_mask; /* which autoglobals are used by the script */
 194         accel_time_t   timestamp;              /* the script modification time */
 195         zend_bool      corrupted;
 196 #if ZEND_EXTENSION_API_NO < PHP_5_3_X_API_NO
 197         zend_uint      early_binding;          /* the linked list of delayed declarations */
 198 #endif
 199 
 200         void          *mem;                    /* shared memory area used by script structures */
 201         size_t         size;                   /* size of used shared memory */
 202 
 203         /* All entries that shouldn't be counted in the ADLER32
 204          * checksum must be declared in this struct
 205          */
 206         struct zend_persistent_script_dynamic_members {
 207                 time_t       last_used;
 208                 ulong        hits;
 209                 unsigned int memory_consumption;
 210                 unsigned int checksum;
 211                 time_t       revalidate;
 212         } dynamic_members;
 213 } zend_persistent_script;
 214 
 215 typedef struct _zend_accel_directives {
 216         long           memory_consumption;
 217         long           max_accelerated_files;
 218         double         max_wasted_percentage;
 219         char          *user_blacklist_filename;
 220         long           consistency_checks;
 221         long           force_restart_timeout;
 222         zend_bool      use_cwd;
 223         zend_bool      ignore_dups;
 224         zend_bool      validate_timestamps;
 225         zend_bool      revalidate_path;
 226         zend_bool      save_comments;
 227         zend_bool      load_comments;
 228         zend_bool      fast_shutdown;
 229         zend_bool      protect_memory;
 230         zend_bool      file_override_enabled;
 231         zend_bool      inherited_hack;
 232         zend_bool      enable_cli;
 233         unsigned long  revalidate_freq;
 234         unsigned long  file_update_protection;
 235         char          *error_log;
 236 #ifdef ZEND_WIN32
 237         char          *mmap_base;
 238 #endif
 239         char          *memory_model;
 240         long           log_verbosity_level;
 241 
 242         long           optimization_level;
 243         long           max_file_size;
 244 #if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO
 245         long           interned_strings_buffer;
 246 #endif
 247         char          *restrict_api;
 248 } zend_accel_directives;
 249 
 250 typedef struct _zend_accel_globals {
 251     /* copy of CG(function_table) used for compilation scripts into cache */
 252     /* initially it contains only internal functions */
 253         HashTable               function_table;
 254         int                     internal_functions_count;
 255         int                     counted;   /* the process uses shared memory */
 256         zend_bool               enabled;
 257         zend_bool               locked;    /* thread obtained exclusive lock */
 258         HashTable               bind_hash; /* prototype and zval lookup table */
 259         zend_accel_directives   accel_directives;
 260         char                   *cwd;              /* current working directory or NULL */
 261         int                     cwd_len;          /* "cwd" string length */
 262         char                   *include_path_key; /* one letter key of current "include_path" */
 263         char                   *include_path;     /* current section of "include_path" directive */
 264         int                     include_path_len; /* "include_path" string length */
 265         int                     include_path_check;
 266         time_t                  request_time;
 267         /* preallocated shared-memory block to save current script */
 268         void                   *mem;
 269         /* cache to save hash lookup on the same INCLUDE opcode */
 270         zend_op                *cache_opline;
 271         zend_persistent_script *cache_persistent_script;
 272         /* preallocated buffer for keys */
 273         int                     key_len;
 274         char                    key[MAXPATHLEN * 8];
 275 } zend_accel_globals;
 276 
 277 typedef struct _zend_accel_shared_globals {
 278         /* Cache Data Structures */
 279         unsigned long   hits;
 280         unsigned long   misses;
 281         unsigned long   blacklist_misses;
 282         unsigned long   oom_restarts;     /* number of restarts because of out of memory */
 283         unsigned long   hash_restarts;    /* number of restarts because of hash overflow */
 284         unsigned long   manual_restarts;  /* number of restarts scheduled by opcache_reset() */
 285         zend_accel_hash hash;             /* hash table for cached scripts */
 286         zend_accel_hash include_paths;    /* used "include_path" values    */
 287 
 288         /* Directives & Maintenance */
 289         time_t          start_time;
 290         time_t          last_restart_time;
 291         time_t          force_restart_time;
 292         zend_bool       accelerator_enabled;
 293         zend_bool       restart_pending;
 294         zend_accel_restart_reason restart_reason;
 295         zend_bool       cache_status_before_restart;
 296 #ifdef ZEND_WIN32
 297     unsigned long   mem_usage;
 298     unsigned long   restart_in;
 299 #endif
 300         zend_bool       restart_in_progress;
 301 #if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO
 302         /* Interned Strings Support */
 303         char           *interned_strings_start;
 304         char           *interned_strings_top;
 305         char           *interned_strings_end;
 306         HashTable       interned_strings;
 307         struct {
 308                 Bucket **arBuckets;
 309                 Bucket  *pListHead;
 310                 Bucket  *pListTail;
 311                 char    *top;
 312         } interned_strings_saved_state;
 313 #endif
 314 } zend_accel_shared_globals;
 315 
 316 extern zend_bool accel_startup_ok;
 317 
 318 extern zend_accel_shared_globals *accel_shared_globals;
 319 #define ZCSG(element)   (accel_shared_globals->element)
 320 
 321 #ifdef ZTS
 322 # define ZCG(v) TSRMG(accel_globals_id, zend_accel_globals *, v)
 323 extern int accel_globals_id;
 324 #else
 325 # define ZCG(v) (accel_globals.v)
 326 extern zend_accel_globals accel_globals;
 327 #endif
 328 
 329 extern char *zps_api_failure_reason;
 330 
 331 void accel_shutdown(TSRMLS_D);
 332 void zend_accel_schedule_restart(zend_accel_restart_reason reason TSRMLS_DC);
 333 void zend_accel_schedule_restart_if_necessary(zend_accel_restart_reason reason TSRMLS_DC);
 334 int  validate_timestamp_and_record(zend_persistent_script *persistent_script, zend_file_handle *file_handle TSRMLS_DC);
 335 int  zend_accel_invalidate(const char *filename, int filename_len, zend_bool force TSRMLS_DC);
 336 int  zend_accel_script_optimize(zend_persistent_script *persistent_script TSRMLS_DC);
 337 int  accelerator_shm_read_lock(TSRMLS_D);
 338 void accelerator_shm_read_unlock(TSRMLS_D);
 339 
 340 char *accel_make_persistent_key_ex(zend_file_handle *file_handle, int path_length, int *key_len TSRMLS_DC);
 341 zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC);
 342 
 343 #if !defined(ZEND_DECLARE_INHERITED_CLASS_DELAYED)
 344 # define ZEND_DECLARE_INHERITED_CLASS_DELAYED 145
 345 #endif
 346 
 347 #define ZEND_DECLARE_INHERITED_CLASS_DELAYED_FLAG 0x80
 348 
 349 #if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO
 350 
 351 const char *accel_new_interned_string(const char *arKey, int nKeyLength, int free_src TSRMLS_DC);
 352 
 353 # define interned_free(s) do { \
 354                 if (!IS_INTERNED(s)) { \
 355                         free(s); \
 356                 } \
 357         } while (0)
 358 # define interned_efree(s) do { \
 359                 if (!IS_INTERNED(s)) { \
 360                         efree(s); \
 361                 } \
 362         } while (0)
 363 # define interned_estrndup(s, n) \
 364         (IS_INTERNED(s) ? (s) : estrndup(s, n))
 365 # define ZEND_RESULT_TYPE(opline)       (opline)->result_type
 366 # define ZEND_RESULT(opline)            (opline)->result
 367 # define ZEND_OP1_TYPE(opline)          (opline)->op1_type
 368 # define ZEND_OP1(opline)                       (opline)->op1
 369 # define ZEND_OP1_CONST(opline)         (*(opline)->op1.zv)
 370 # define ZEND_OP1_LITERAL(opline)       (op_array)->literals[(opline)->op1.constant].constant
 371 # define ZEND_OP2_TYPE(opline)          (opline)->op2_type
 372 # define ZEND_OP2(opline)                       (opline)->op2
 373 # define ZEND_OP2_CONST(opline)         (*(opline)->op2.zv)
 374 # define ZEND_OP2_LITERAL(opline)       (op_array)->literals[(opline)->op2.constant].constant
 375 # define ZEND_DONE_PASS_TWO(op_array)   (((op_array)->fn_flags & ZEND_ACC_DONE_PASS_TWO) != 0)
 376 # define ZEND_CE_FILENAME(ce)                   (ce)->info.user.filename
 377 # define ZEND_CE_DOC_COMMENT(ce)        (ce)->info.user.doc_comment
 378 # define ZEND_CE_DOC_COMMENT_LEN(ce)    (ce)->info.user.doc_comment_len
 379 #else
 380 # define IS_INTERNED(s)                         0
 381 # define interned_free(s)                       free(s)
 382 # define interned_efree(s)                      efree(s)
 383 # define interned_estrndup(s, n)        estrndup(s, n)
 384 # define ZEND_RESULT_TYPE(opline)       (opline)->result.op_type
 385 # define ZEND_RESULT(opline)            (opline)->result.u
 386 # define ZEND_OP1_TYPE(opline)          (opline)->op1.op_type
 387 # define ZEND_OP1(opline)                       (opline)->op1.u
 388 # define ZEND_OP1_CONST(opline)         (opline)->op1.u.constant
 389 # define ZEND_OP1_LITERAL(opline)       (opline)->op1.u.constant
 390 # define ZEND_OP2_TYPE(opline)          (opline)->op2.op_type
 391 # define ZEND_OP2(opline)                       (opline)->op2.u
 392 # define ZEND_OP2_CONST(opline)         (opline)->op2.u.constant
 393 # define ZEND_OP2_LITERAL(opline)       (opline)->op2.u.constant
 394 # define ZEND_DONE_PASS_TWO(op_array)   ((op_array)->done_pass_two != 0)
 395 # define ZEND_CE_FILENAME(ce)                   (ce)->filename
 396 # define ZEND_CE_DOC_COMMENT(ce)        (ce)->doc_comment
 397 # define ZEND_CE_DOC_COMMENT_LEN(ce)    (ce)->doc_comment_len
 398 #endif
 399 
 400 #endif /* ZEND_ACCELERATOR_H */

/* [<][>][^][v][top][bottom][index][help] */