root/eaccelerator/tags/0.9.4-rc1/eaccelerator.h

Revision 124, 14.7 kB (checked in by zoeloelip, 3 years ago)

* ea_store.c and ea_restore.c functions clean up
* extraced restore_class_parent and restore_class_methods from

restore_class_entry to share code with loader.c

* extracted opcode handling in encode/decode_op_array into

encode/decode_op to make code more readable

* make decode_class_entry use new functions in ea_restore.c
* readded line-number encoding
* bumped up encoder version to 4 until new loader is in place

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2    +----------------------------------------------------------------------+
3    | eAccelerator project                                                 |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 2004 - 2005 eAccelerator                               |
6    | http://eaccelerator.net                                                  |
7    +----------------------------------------------------------------------+
8    | This program is free software; you can redistribute it and/or        |
9    | modify it under the terms of the GNU General Public License          |
10    | as published by the Free Software Foundation; either version 2       |
11    | of the License, or (at your option) any later version.               |
12    |                                                                      |
13    | This program is distributed in the hope that it will be useful,      |
14    | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
15    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
16    | GNU General Public License for more details.                         |
17    |                                                                      |
18    | You should have received a copy of the GNU General Public License    |
19    | along with this program; if not, write to the Free Software          |
20    | Foundation, Inc., 59 Temple Place - Suite 330, Boston,               |
21    | MA  02111-1307, USA.                                                 |
22    |                                                                      |
23    | A copy is availble at http://www.gnu.org/copyleft/gpl.txt            |
24    +----------------------------------------------------------------------+
25    | Author(s): Dmitry Stogov <dstogov@users.sourceforge.net>             |
26    |            Everaldo Canuto <everaldo_canuto@yahoo.com.br>            |
27    +----------------------------------------------------------------------+
28    $Id$
29 */
30
31 #ifndef INCLUDED_EACCELERATOR_H
32 #define INCLUDED_EACCELERATOR_H
33
34 #include "php.h"
35 #include "zend.h"
36 #include "zend_API.h"
37 #include "zend_extensions.h"
38
39 #if !defined(ZEND_WIN32) && defined(HAVE_CONFIG_H)
40 #  if ZEND_MODULE_API_NO >= 20001222
41 #    include "config.h"
42 #  else
43 #    include "php_config.h"
44 #  endif
45 #endif
46
47 /* fixes compile errors on php5.1 */
48 #ifdef STR_EMPTY_ALLOC
49 #define empty_string STR_EMPTY_ALLOC()
50 #endif
51
52 #if !defined(ZEND_WIN32) && defined(WITH_EACCELERATOR_USE_INODE)
53 /* UnDefine if your filesystem doesn't support inodes */
54 #  define EACCELERATOR_USE_INODE
55 #endif
56
57 #ifdef WITH_EACCELERATOR_CRASH_DETECTION
58 #  include <signal.h>
59 #endif
60
61 #define EACCELERATOR_MM_FILE "/tmp/eaccelerator"
62
63 #ifdef HAVE_EACCELERATOR
64 #ifndef HAVE_EACCELERATOR_STANDALONE_LOADER
65
66 #include "mm.h"
67
68 #ifdef EACCELERATOR_WITHOUT_FILE_LOCKING
69 #  ifndef LOCK_SH
70 #    define LOCK_SH 1
71 #    define LOCK_EX 2
72 #    define LOCK_UN 8
73 #  endif
74 #  define EACCELERATOR_FLOCK(FILE,OP)
75 #else
76 #  ifndef ZEND_WIN32
77 #    ifdef HAVE_FLOCK
78 #          include <sys/file.h>
79 #      define EACCELERATOR_FLOCK(FILE,OP) flock((FILE),(OP))
80 #    else
81 #      ifndef LOCK_SH
82 #        define LOCK_SH 1
83 #        define LOCK_EX 2
84 #        define LOCK_UN 8
85 #      endif
86 #      define EACCELERATOR_FLOCK(FILE,OP)
87 #    endif
88 #  else
89 #    define LOCK_SH 0
90 #    define LOCK_EX 1
91 #    define LOCK_UN 2
92 #    define EACCELERATOR_FLOCK(FILE,OP) {OVERLAPPED offset = {0,0,0,0,NULL};\
93                                    if ((OP) == LOCK_EX) {\
94                                      LockFileEx((HANDLE)_get_osfhandle(FILE), \
95                                        LOCKFILE_EXCLUSIVE_LOCK, 0,\
96                                        1, 0, &offset);\
97                                    } else if ((OP) == LOCK_SH) {\
98                                      LockFileEx((HANDLE)_get_osfhandle(FILE), \
99                                        0, 0,\
100                                        1, 0, &offset);\
101                                    } else if ((OP) == LOCK_UN) {\
102                                      UnlockFileEx((HANDLE)_get_osfhandle(FILE), \
103                                        0,\
104                                        1, 0, &offset);\
105                                    }}
106 #  endif
107 #endif
108
109 #ifdef ZEND_WIN32
110 #  include <process.h>
111 #  ifndef S_ISREG
112 #    define S_ISREG(mode) (((mode)&S_IFMT) & S_IFREG)
113 #  endif
114 #  ifndef S_IRUSR
115 #    define S_IRUSR S_IREAD
116 #  endif
117 #  ifndef S_IWUSR
118 #    define S_IWUSR S_IWRITE
119 #  endif
120 #else
121 #  include <dirent.h>
122 #endif
123
124 #ifdef ZTS
125 #  define ZTS_LOCK()    tsrm_mutex_lock(mm_mutex)
126 #  define ZTS_UNLOCK()  tsrm_mutex_unlock(mm_mutex)
127 #else
128 #  define ZTS_LOCK()
129 #  define ZTS_UNLOCK()
130 #endif
131
132 #if defined(EACCELERATOR_PROTECT_SHM)
133 #  define EACCELERATOR_PROTECT()    do {mm_protect(eaccelerator_mm_instance->mm, MM_PROT_READ);} while(0)
134 #  define EACCELERATOR_UNPROTECT()  do {mm_protect(eaccelerator_mm_instance->mm, MM_PROT_READ|MM_PROT_WRITE);} while(0)
135 #else
136 #  define EACCELERATOR_PROTECT()
137 #  define EACCELERATOR_UNPROTECT()
138 #endif
139
140 #define EACCELERATOR_LOCK_RW()    do {ZTS_LOCK(); mm_lock(eaccelerator_mm_instance->mm, MM_LOCK_RW);} while(0)
141 #define EACCELERATOR_LOCK_RD()    do {ZTS_LOCK(); mm_lock(eaccelerator_mm_instance->mm, MM_LOCK_RD);} while(0)
142 #define EACCELERATOR_UNLOCK()     do {mm_unlock(eaccelerator_mm_instance->mm); ZTS_UNLOCK();} while(0)
143 #define EACCELERATOR_UNLOCK_RW()  EACCELERATOR_UNLOCK()
144 #define EACCELERATOR_UNLOCK_RD()  EACCELERATOR_UNLOCK()
145
146 #define EACCELERATOR_BLOCK_INTERRUPTIONS()   HANDLE_BLOCK_INTERRUPTIONS()
147 #define EACCELERATOR_UNBLOCK_INTERRUPTIONS() HANDLE_UNBLOCK_INTERRUPTIONS()
148
149 #define MM_HASH_SIZE      256
150 #define MM_USER_HASH_SIZE 256
151 #define MM_HASH_MAX       (MM_HASH_SIZE-1)
152 #define MM_USER_HASH_MAX  (MM_USER_HASH_SIZE-1)
153
154 #define eaccelerator_malloc(size)        mm_malloc_lock(eaccelerator_mm_instance->mm, size)
155 #define eaccelerator_free(x)             mm_free_lock(eaccelerator_mm_instance->mm, x)
156 #define eaccelerator_malloc_nolock(size) mm_malloc_nolock(eaccelerator_mm_instance->mm, size)
157 #define eaccelerator_free_nolock(x)      mm_free_nolock(eaccelerator_mm_instance->mm, x)
158
159 #if (defined (__GNUC__) && __GNUC__ >= 2)
160 #define EACCELERATOR_PLATFORM_ALIGNMENT (__alignof__ (align_test))
161 #else
162 #define EACCELERATOR_PLATFORM_ALIGNMENT (sizeof(align_union))
163 #endif
164
165 #define EACCELERATOR_ALIGN(n) (n) = (void*)((((size_t)(n)-1) & ~(EACCELERATOR_PLATFORM_ALIGNMENT-1)) + EACCELERATOR_PLATFORM_ALIGNMENT)
166
167 #define MAX_DUP_STR_LEN 256
168
169 #ifndef offsetof
170 #  define offsetof(str,fld) ((size_t)&(((str*)NULL)->fld))
171 #endif
172
173 /******************************************************************************/
174
175 typedef struct _eaccelerator_op_array {
176         zend_uchar type;
177 #ifdef ZEND_ENGINE_2
178         zend_bool uses_this;
179 #else
180         zend_bool uses_globals;
181 #endif
182         zend_bool return_reference;
183 #ifdef ZEND_ENGINE_2
184         zend_uint num_args;
185         zend_uint required_num_args;
186         zend_arg_info *arg_info;
187         zend_bool pass_rest_by_reference;
188 #else
189         zend_uchar *arg_types;
190 #endif
191         char *function_name;
192         char *function_name_lc;
193 #ifdef ZEND_ENGINE_2
194         char *scope_name;
195         int scope_name_len;
196         zend_uint fn_flags;
197 #endif
198         zend_op *opcodes;
199         zend_uint last;
200         zend_uint T;
201         zend_brk_cont_element *brk_cont_array;
202         zend_uint last_brk_cont;
203 #ifdef ZEND_ENGINE_2
204         zend_try_catch_element *try_catch_array;
205         int last_try_catch;
206 #endif
207         HashTable *static_variables;
208         char *filename;
209 #ifdef ZEND_ENGINE_2
210         zend_uint line_start;
211         zend_uint line_end;
212         char *doc_comment;
213         zend_uint doc_comment_len;
214 #endif
215 } eaccelerator_op_array;
216
217 typedef struct _eaccelerator_class_entry {
218         char type;
219         char *name;
220         char *name_lc;
221         uint name_length;
222         char *parent;
223         HashTable function_table;
224         HashTable default_properties;
225 #ifdef ZEND_ENGINE_2
226         zend_uint ce_flags;
227         HashTable *static_members;
228         HashTable properties_info;
229         HashTable constants_table;
230         zend_uint num_interfaces;
231         char **interfaces;
232         zend_class_iterator_funcs iterator_funcs;
233
234          zend_object_value (*create_object) (zend_class_entry *
235                                                                                  class_type TSRMLS_DC);
236         zend_object_iterator *(*get_iterator) (zend_class_entry * ce,
237                                                                                    zval * object TSRMLS_DC);
238         int (*interface_gets_implemented) (zend_class_entry * iface, zend_class_entry * class_type TSRMLS_DC);  /* a class implements this interface */
239
240         char *filename;
241         zend_uint line_start;
242         zend_uint line_end;
243         char *doc_comment;
244         zend_uint doc_comment_len;
245 #endif
246 } eaccelerator_class_entry;
247
248 /*
249  * To cache functions and classes.
250  */
251 typedef struct _mm_fc_entry {
252         void *fc;
253         struct _mm_fc_entry *next;
254         int htablen;
255         char htabkey[1];                        /* must be last element */
256 } mm_fc_entry;
257
258 /*
259  * A mm_cache_entry is a bucket for one PHP script file.
260  * Nested  functions and classes which defined in the file goes
261  * into the list of mm_fc_entry.
262  */
263 typedef struct _mm_cache_entry {
264         struct _mm_cache_entry *next;
265 #ifdef EACCELERATOR_USE_INODE
266         dev_t st_dev;                           /* file's device                     */
267         ino_t st_ino;                           /* file's inode                      */
268 #else
269         unsigned int hv;                        /* hash value                        */
270 #endif
271         off_t filesize;                         /* file size */
272         time_t mtime;                           /* file last modification time       */
273         time_t ttl;                                     /* expiration time                   */
274         int size;                                       /* entry size (bytes)                */
275         int nhits;                                      /* hits count                        */
276         int nreloads;                           /* count of reloads                  */
277         int use_cnt;                            /* how many processes uses the entry */
278         eaccelerator_op_array *op_array;        /* script's global scope code        */
279         mm_fc_entry *f_head;            /* list of nested functions          */
280         mm_fc_entry *c_head;            /* list of nested classes            */
281         zend_bool removed;                      /* the entry is scheduled to remove  */
282         char realfilename[1];           /* real file name (must be last el.) */
283 } mm_cache_entry;
284
285 /*
286  * bucket for user's cache
287  */
288 typedef struct _mm_user_cache_entry {
289         struct _mm_user_cache_entry *next;
290         unsigned int hv;                        /* hash value                  */
291         long ttl;                                       /* expiration time             */
292         int size;
293         zval value;                                     /* value                       */
294         char key[1];                            /* key value (must be last el) */
295 } mm_user_cache_entry;
296
297 /*
298  * Linked list of mm_cache_entry which are used by process/thread
299  */
300 typedef struct _mm_used_entry {
301         struct _mm_used_entry *next;
302         mm_cache_entry *entry;
303 } mm_used_entry;
304
305 /*
306  * Linked list of locks
307  */
308 typedef struct _mm_lock_entry {
309         struct _mm_lock_entry *next;
310         pid_t pid;
311 #ifdef ZTS
312         THREAD_T thread;
313 #endif
314         char key[1];
315 } mm_lock_entry;
316
317 typedef struct _mm_file_header {
318         char magic[8];                          /* "EACCELERATOR" */
319         int eaccelerator_version;
320         int zend_version;
321         int php_version;
322         int size;
323         time_t mtime;
324         unsigned int crc32;
325 } mm_file_header;
326
327 typedef struct {
328         MM *mm;
329         pid_t owner;
330         size_t total;
331         unsigned int hash_cnt;
332         unsigned int user_hash_cnt;
333         zend_bool enabled;
334         zend_bool optimizer_enabled;
335         unsigned int rem_cnt;
336         time_t last_prune;
337         mm_cache_entry *removed;
338         mm_lock_entry *locks;
339
340         mm_cache_entry *hash[MM_HASH_SIZE];
341         mm_user_cache_entry *user_hash[MM_USER_HASH_SIZE];
342 } eaccelerator_mm;
343
344 /*
345  * Where to cache
346  */
347 typedef enum _eaccelerator_cache_place {
348         eaccelerator_shm_and_disk,      /* in shm and in disk */
349         eaccelerator_shm,                       /* in shm, but if it is not possible then on disk */
350         eaccelerator_shm_only,          /* in shm only  */
351         eaccelerator_disk_only,         /* on disk only */
352         eaccelerator_none                       /* don't cache  */
353 } eaccelerator_cache_place;
354
355 typedef union align_union {
356   double d;
357   void *v;
358   int (*func)(int);
359   long l;
360 } align_union;
361
362 /******************************************************************************/
363
364 #ifdef ZTS
365 MUTEX_T mm_mutex;
366 #endif
367
368 /* needed to compile eA as a static php module */
369 extern zend_module_entry eaccelerator_module_entry;
370 #define phpext_eaccelerator_ptr &eaccelerator_module_entry
371
372
373 void format_size (char *s, unsigned int size, int legend);
374 void eaccelerator_prune (time_t t);
375
376 int eaccelerator_lock (const char *key, int key_len TSRMLS_DC);
377 int eaccelerator_unlock (const char *key, int key_len TSRMLS_DC);
378
379 void *eaccelerator_malloc2 (size_t size TSRMLS_DC);
380
381 unsigned int eaccelerator_crc32 (const char *p, size_t n);
382 int eaccelerator_md5 (char *s, const char *prefix, const char *key TSRMLS_DC);
383
384 void restore_zval (zval * TSRMLS_DC);
385 void calc_zval (zval * z TSRMLS_DC);
386 void store_zval (zval * z TSRMLS_DC);
387 void fixup_zval (zval * z TSRMLS_DC);
388
389 unsigned int hash_mm(const char *data, int len);
390
391 #  ifdef WITH_EACCELERATOR_EXECUTOR
392 ZEND_DLEXPORT void eaccelerator_execute (zend_op_array * op_array TSRMLS_DC);
393 #  endif
394
395 #  ifdef WITH_EACCELERATOR_OPTIMIZER
396 void eaccelerator_optimize (zend_op_array * op_array);
397 #  endif
398
399 #ifdef WITH_EACCELERATOR_ENCODER
400 PHP_FUNCTION (eaccelerator_encode);
401 #endif
402 #endif /* HAVE_EACCELERATOR_LOADER_STANDALONE */
403
404 #ifdef ZTS
405 #  define EAG(v) TSRMG(eaccelerator_globals_id, zend_eaccelerator_globals*, v)
406 #else
407 #  define EAG(v) (eaccelerator_globals.v)
408 #endif
409
410 #define MMCG(v) EAG(v)
411
412 /*
413  * conditional filter
414  */
415 typedef struct _mm_cond_entry {
416         char *str;
417         int len;
418         zend_bool not;
419         struct _mm_cond_entry *next;
420 } mm_cond_entry;
421
422 #ifdef WITH_EACCELERATOR_LOADER
423 zend_op_array *eaccelerator_load (char *src, int src_len TSRMLS_DC);
424 PHP_FUNCTION (eaccelerator_load);
425 PHP_FUNCTION (_eaccelerator_loader_file);
426 PHP_FUNCTION (_eaccelerator_loader_line);
427 #endif
428
429 /*
430  * Globals (different for each process/thread)
431  */
432 ZEND_BEGIN_MODULE_GLOBALS (eaccelerator)
433 void *used_entries;                             /* list of files which are used     */
434                                                                 /* by process/thread                */
435 zend_bool enabled;
436 zend_bool optimizer_enabled;
437 zend_bool compression_enabled;
438 zend_bool compiler;
439 zend_bool encoder;
440 zend_bool compress;
441 zend_bool compress_content;
442 zend_bool in_request;
443 zend_llist *content_headers;
444 long compress_level;
445 char *cache_dir;
446 char *eaccelerator_log_file;
447 char *name_space;
448 char *mem;
449 HashTable strings;
450 zend_class_entry *class_entry;
451 mm_cond_entry *cond_list;
452 zend_uint refcount_helper;
453 char hostname[32];
454 #ifdef WITH_EACCELERATOR_CRASH_DETECTION
455 #ifdef SIGSEGV
456 void (*original_sigsegv_handler) (int);
457 #endif
458 #ifdef SIGFPE
459 void (*original_sigfpe_handler) (int);
460 #endif
461 #ifdef SIGBUS
462 void (*original_sigbus_handler) (int);
463 #endif
464 #ifdef SIGILL
465 void (*original_sigill_handler) (int);
466 #endif
467 #ifdef SIGABRT
468 void (*original_sigabrt_handler) (int);
469 #endif
470 #endif
471 #ifdef DEBUG
472 int xpad;
473 int profile_level;
474 long self_time[256];
475 #endif
476 #ifdef WITH_EACCELERATOR_SESSIONS
477 char *session;
478 #endif
479 ZEND_END_MODULE_GLOBALS (eaccelerator)
480
481 ZEND_EXTERN_MODULE_GLOBALS (eaccelerator)
482
483 #define EACCELERATOR_EXTENSION_NAME "eAccelerator"
484 #define EACCELERATOR_LOADER_EXTENSION_NAME "eLoader"
485
486 #define EA_ENCODER_VERSION   0x00000004
487 #define EA_ENCODER_END       0x00
488 #define EA_ENCODER_NAMESPACE 0x01
489 #define EA_ENCODER_CLASS     0x02
490 #define EA_ENCODER_FUNCTION  0x03
491
492 #define EACCELERATOR_VERSION_GUID   "PHPE8EDA1B6-806A-4851-B1C8-A6B4712F44FB"
493 #define EACCELERATOR_LOGO_GUID      "PHPE6F78DE9-13E4-4dee-8518-5FA2DACEA803"
494 #define EACCELERATOR_VERSION_STRING ("eAccelerator " EACCELERATOR_VERSION " (PHP " PHP_VERSION ")")
495
496 #endif          /* HAVE_EACCELERATOR */
497 #endif          /* #ifndef INCLUDED_EACCELERATOR_H */
Note: See TracBrowser for help on using the browser.