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

Revision 237, 14.4 kB (checked in by bart, 2 years ago)

* Make clean routine support dir hashing. Again, this should be tested on win32!
* Use a define for the magic string in the eAccelerator file headers
* Bump up version to 0.9.5-rc1
* Put -O2 back on for -rc1

  • Property svn:eol-style set to native
  • Property svn:keywords set to svn:eol-style
Line 
1 /*
2    +----------------------------------------------------------------------+
3    | eAccelerator project                                                 |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 2004 - 2006 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    $Id: eaccelerator.h 178 2006-03-06 09:08:40Z bart $
26 */
27
28 #ifndef INCLUDED_EACCELERATOR_H
29 #define INCLUDED_EACCELERATOR_H
30
31 #include "php.h"
32 #include "zend.h"
33 #include "zend_API.h"
34 #include "zend_extensions.h"
35
36 #if !defined(ZEND_WIN32) && defined(HAVE_CONFIG_H)
37 #  if ZEND_MODULE_API_NO >= 20001222
38 #    include "config.h"
39 #  else
40 #    include "php_config.h"
41 #  endif
42 #endif
43
44 #if PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 1
45 #   define ZEND_ENGINE_2_1
46 #endif
47
48 /* fixes compile errors on php5.1 */
49 #ifdef STR_EMPTY_ALLOC
50 #define empty_string STR_EMPTY_ALLOC()
51 #endif
52
53 #if !defined(ZEND_WIN32) && defined(WITH_EACCELERATOR_USE_INODE)
54 /* UnDefine if your filesystem doesn't support inodes */
55 #  define EACCELERATOR_USE_INODE
56 #endif
57
58 #ifdef WITH_EACCELERATOR_CRASH_DETECTION
59 #  include <signal.h>
60 #endif
61
62 #define EACCELERATOR_MM_FILE "/tmp/eaccelerator"
63
64 #ifdef EACCELERATOR_WITHOUT_FILE_LOCKING
65 #  ifndef LOCK_SH
66 #    define LOCK_SH 1
67 #    define LOCK_EX 2
68 #    define LOCK_UN 8
69 #  endif
70 #  define EACCELERATOR_FLOCK(FILE,OP)
71 #else
72 #  ifndef ZEND_WIN32
73 #    ifdef HAVE_FLOCK
74 #          include <sys/file.h>
75 #      define EACCELERATOR_FLOCK(FILE,OP) flock((FILE),(OP))
76 #    else
77 #      ifndef LOCK_SH
78 #        define LOCK_SH 1
79 #        define LOCK_EX 2
80 #        define LOCK_UN 8
81 #      endif
82 #      define EACCELERATOR_FLOCK(FILE,OP)
83 #    endif
84 #  else
85 #    define LOCK_SH 0
86 #    define LOCK_EX 1
87 #    define LOCK_UN 2
88 #    define EACCELERATOR_FLOCK(FILE,OP) {OVERLAPPED offset = {0,0,0,0,NULL};\
89                                    if ((OP) == LOCK_EX) {\
90                                      LockFileEx((HANDLE)_get_osfhandle(FILE), \
91                                        LOCKFILE_EXCLUSIVE_LOCK, 0,\
92                                        1, 0, &offset);\
93                                    } else if ((OP) == LOCK_SH) {\
94                                      LockFileEx((HANDLE)_get_osfhandle(FILE), \
95                                        0, 0,\
96                                        1, 0, &offset);\
97                                    } else if ((OP) == LOCK_UN) {\
98                                      UnlockFileEx((HANDLE)_get_osfhandle(FILE), \
99                                        0,\
100                                        1, 0, &offset);\
101                                    }}
102 #  endif
103 #endif
104
105 #if !defined(_INTPTR_T_DEFINED) && ZEND_WIN32
106         typedef intptr_t;
107         #define _INTPTR_T_DEFINED
108 #endif
109
110 #ifdef HAVE_EACCELERATOR
111 #ifndef HAVE_EACCELERATOR_STANDALONE_LOADER
112
113 #include "mm.h"
114
115 #ifdef ZEND_WIN32
116 #  include <process.h>
117 #  ifndef S_ISREG
118 #    define S_ISREG(mode) (((mode)&S_IFMT) & S_IFREG)
119 #  endif
120 #  ifndef S_IRUSR
121 #    define S_IRUSR S_IREAD
122 #  endif
123 #  ifndef S_IWUSR
124 #    define S_IWUSR S_IWRITE
125 #  endif
126 #else
127 #  include <dirent.h>
128 #endif
129
130 #ifdef ZTS
131 #  define ZTS_LOCK()    tsrm_mutex_lock(ea_mutex)
132 #  define ZTS_UNLOCK()  tsrm_mutex_unlock(ea_mutex)
133 #else
134 #  define ZTS_LOCK()
135 #  define ZTS_UNLOCK()
136 #endif
137
138 #if defined(EACCELERATOR_PROTECT_SHM)
139 #  define EACCELERATOR_PROTECT()    do {mm_protect(eaccelerator_mm_instance->mm, MM_PROT_READ);} while(0)
140 #  define EACCELERATOR_UNPROTECT()  do {mm_protect(eaccelerator_mm_instance->mm, MM_PROT_READ|MM_PROT_WRITE);} while(0)
141 #else
142 #  define EACCELERATOR_PROTECT()
143 #  define EACCELERATOR_UNPROTECT()
144 #endif
145
146 #define EACCELERATOR_LOCK_RW()    do {ZTS_LOCK(); mm_lock(eaccelerator_mm_instance->mm, MM_LOCK_RW);} while(0)
147 #define EACCELERATOR_LOCK_RD()    do {ZTS_LOCK(); mm_lock(eaccelerator_mm_instance->mm, MM_LOCK_RD);} while(0)
148 #define EACCELERATOR_UNLOCK()     do {mm_unlock(eaccelerator_mm_instance->mm); ZTS_UNLOCK();} while(0)
149 #define EACCELERATOR_UNLOCK_RW()  EACCELERATOR_UNLOCK()
150 #define EACCELERATOR_UNLOCK_RD()  EACCELERATOR_UNLOCK()
151
152 #define EACCELERATOR_BLOCK_INTERRUPTIONS()   HANDLE_BLOCK_INTERRUPTIONS()
153 #define EACCELERATOR_UNBLOCK_INTERRUPTIONS() HANDLE_UNBLOCK_INTERRUPTIONS()
154
155 #define EACCELERATOR_HASH_LEVEL 2
156 #define EA_HASH_SIZE      512
157 #define EA_USER_HASH_SIZE 512
158
159 #define EA_HASH_MAX       (EA_HASH_SIZE-1)
160 #define EA_USER_HASH_MAX  (EA_USER_HASH_SIZE-1)
161
162 #define eaccelerator_malloc(size)        mm_malloc_lock(eaccelerator_mm_instance->mm, size)
163 #define eaccelerator_free(x)             mm_free_lock(eaccelerator_mm_instance->mm, x)
164 #define eaccelerator_malloc_nolock(size) mm_malloc_nolock(eaccelerator_mm_instance->mm, size)
165 #define eaccelerator_free_nolock(x)      mm_free_nolock(eaccelerator_mm_instance->mm, x)
166
167 #if (defined (__GNUC__) && __GNUC__ >= 2)
168 #define EACCELERATOR_PLATFORM_ALIGNMENT (__alignof__ (align_test))
169 #else
170 #define EACCELERATOR_PLATFORM_ALIGNMENT (sizeof(align_union))
171 #endif
172
173 #define EACCELERATOR_ALIGN(n) (n) = (void*)((((size_t)(n)-1) & ~(EACCELERATOR_PLATFORM_ALIGNMENT-1)) + EACCELERATOR_PLATFORM_ALIGNMENT)
174
175 #define MAX_DUP_STR_LEN 256
176
177 /******************************************************************************/
178
179 #endif /* HAVE_EACCELERATOR_STANDALONE_LOADER */
180
181 #ifndef offsetof
182 #  define offsetof(str,fld) ((size_t)&(((str*)NULL)->fld))
183 #endif
184
185 typedef struct _eaccelerator_op_array {
186         zend_uchar type;
187 #ifdef ZEND_ENGINE_2
188         zend_bool uses_this;
189 #else
190         zend_bool uses_globals;
191 #endif
192         zend_bool return_reference;
193 #ifdef ZEND_ENGINE_2
194         zend_uint num_args;
195         zend_uint required_num_args;
196         zend_arg_info *arg_info;
197         zend_bool pass_rest_by_reference;
198 #else
199         zend_uchar *arg_types;
200 #endif
201         char *function_name;
202 #ifdef ZEND_ENGINE_2
203         char *scope_name;
204         int scope_name_len;
205         zend_uint fn_flags;
206 #endif
207         zend_op *opcodes;
208         zend_uint last;
209 #ifdef ZEND_ENGINE_2_1
210         zend_compiled_variable *vars;
211     int last_var;
212 #endif
213         zend_uint T;
214         zend_brk_cont_element *brk_cont_array;
215         zend_uint last_brk_cont;
216 #ifdef ZEND_ENGINE_2
217         zend_try_catch_element *try_catch_array;
218         int last_try_catch;
219 #endif
220         HashTable *static_variables;
221         char *filename;
222 #ifdef ZEND_ENGINE_2
223         zend_uint line_start;
224         zend_uint line_end;
225 #endif
226 } eaccelerator_op_array;
227
228 typedef struct _eaccelerator_class_entry {
229         char type;
230         char *name;
231         zend_uint name_length;
232         char *parent;
233         HashTable function_table;
234         HashTable default_properties;
235 #ifdef ZEND_ENGINE_2
236         HashTable properties_info;
237 #  ifdef ZEND_ENGINE_2_1
238         HashTable default_static_members;
239 #  endif
240         HashTable *static_members;
241         HashTable constants_table;
242         zend_uint ce_flags;
243         zend_uint num_interfaces;
244
245         char *filename;
246         zend_uint line_start;
247         zend_uint line_end;
248 #endif
249 } eaccelerator_class_entry;
250
251 /*
252  * To cache functions and classes.
253  */
254 typedef struct _mm_fc_entry {
255         void *fc;
256         struct _mm_fc_entry *next;
257         int htablen;
258         char htabkey[1];                        /* must be last element */
259 } mm_fc_entry;
260
261 #ifndef HAVE_EACCELERATOR_STANDALONE_LOADER
262
263 /*
264  * A mm_cache_entry is a bucket for one PHP script file.
265  * Nested  functions and classes which defined in the file goes
266  * into the list of mm_fc_entry.
267  */
268 typedef struct _mm_cache_entry {
269         struct _mm_cache_entry *next;
270 #ifdef EACCELERATOR_USE_INODE
271         dev_t st_dev;                           /* file's device                     */
272         ino_t st_ino;                           /* file's inode                      */
273 #else
274         unsigned int hv;                        /* hash value                        */
275 #endif
276         off_t filesize;                         /* file size */
277         time_t mtime;                           /* file last modification time       */
278         time_t ttl;                                     /* expiration time                   */
279         int size;                                       /* entry size (bytes)                */
280         int nhits;                                      /* hits count                        */
281         int nreloads;                           /* count of reloads                  */
282         int use_cnt;                            /* how many processes uses the entry */
283         eaccelerator_op_array *op_array;        /* script's global scope code        */
284         mm_fc_entry *f_head;            /* list of nested functions          */
285         mm_fc_entry *c_head;            /* list of nested classes            */
286         zend_bool removed;                      /* the entry is scheduled to remove  */
287         char realfilename[1];           /* real file name (must be last el.) */
288 } mm_cache_entry;
289
290 /*
291  * bucket for user's cache
292  */
293 typedef struct _mm_user_cache_entry {
294         struct _mm_user_cache_entry *next;
295         unsigned int hv;                        /* hash value                  */
296         long ttl;                                       /* expiration time             */
297         long create;
298         int size;
299         zval value;                                     /* value                       */
300         char key[1];                            /* key value (must be last el) */
301 } mm_user_cache_entry;
302
303 /*
304  * Linked list of mm_cache_entry which are used by process/thread
305  */
306 typedef struct _mm_used_entry {
307         struct _mm_used_entry *next;
308         mm_cache_entry *entry;
309 } mm_used_entry;
310
311 /*
312  * Linked list of locks
313  */
314 typedef struct _mm_lock_entry {
315         struct _mm_lock_entry *next;
316         pid_t pid;
317 #ifdef ZTS
318         THREAD_T thread;
319 #endif
320         char key[1];
321 } mm_lock_entry;
322
323 typedef struct _mm_file_header {
324         char magic[8];                          /* "EACCELERATOR" */
325         int eaccelerator_version;
326         int zend_version;
327         int php_version;
328         int size;
329         time_t mtime;
330         unsigned int crc32;
331 } mm_file_header;
332
333 typedef struct {
334         MM *mm;
335         pid_t owner;
336         size_t total;
337         unsigned int hash_cnt;
338         unsigned int user_hash_cnt;
339         zend_bool enabled;
340         zend_bool optimizer_enabled;
341         unsigned int rem_cnt;
342         time_t last_prune;
343         mm_cache_entry *removed;
344         mm_lock_entry *locks;
345
346         mm_cache_entry *hash[EA_HASH_SIZE];
347         mm_user_cache_entry *user_hash[EA_USER_HASH_SIZE];
348 } eaccelerator_mm;
349
350 /*
351  * Where to cache
352  */
353 typedef enum _eaccelerator_cache_place {
354         eaccelerator_shm_and_disk,      /* in shm and in disk */
355         eaccelerator_shm,                       /* in shm, but if it is not possible then on disk */
356         eaccelerator_shm_only,          /* in shm only  */
357         eaccelerator_disk_only,         /* on disk only */
358         eaccelerator_none                       /* don't cache  */
359 } eaccelerator_cache_place;
360
361 typedef union align_union {
362   double d;
363   void *v;
364   int (*func)(int);
365   long l;
366 } align_union;
367
368 /******************************************************************************/
369
370 #ifdef ZTS
371 #  ifdef __APPLE__
372 /* Workaround to prevent 'multiple definitions of symbol' during build on OSX */
373 static MUTEX_T ea_mutex;
374 #  else
375 MUTEX_T ea_mutex;
376 #  endif
377 #endif
378
379 /* needed to compile eA as a static php module */
380 extern zend_module_entry eaccelerator_module_entry;
381 #define phpext_eaccelerator_ptr &eaccelerator_module_entry
382
383
384 void format_size (char *s, unsigned int size, int legend);
385 void eaccelerator_prune (time_t t);
386
387 int eaccelerator_lock (const char *key, int key_len TSRMLS_DC);
388 int eaccelerator_unlock (const char *key, int key_len TSRMLS_DC);
389
390 void *eaccelerator_malloc2 (size_t size TSRMLS_DC);
391
392 unsigned int eaccelerator_crc32 (const char *p, size_t n);
393 int eaccelerator_md5 (char *s, const char *prefix, const char *key TSRMLS_DC);
394
395 unsigned int hash_mm(const char *data, int len);
396
397 #  ifdef WITH_EACCELERATOR_OPTIMIZER
398 void eaccelerator_optimize (zend_op_array * op_array);
399 #  endif
400
401 #ifdef WITH_EACCELERATOR_ENCODER
402 PHP_FUNCTION (eaccelerator_encode);
403 #endif
404 #endif /* HAVE_EACCELERATOR_STANDALONE_LOADER */
405
406 #ifdef ZTS
407 #  define EAG(v) TSRMG(eaccelerator_globals_id, zend_eaccelerator_globals*, v)
408 #else
409 #  define EAG(v) (eaccelerator_globals.v)
410 #endif
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 char *allowed_admin_path;
450 HashTable strings;
451 HashTable restored;
452 zend_class_entry *class_entry;
453 mm_cond_entry *cond_list;
454 zend_uint refcount_helper;
455 char hostname[32];
456 #ifdef WITH_EACCELERATOR_CRASH_DETECTION
457 #ifdef SIGSEGV
458 void (*original_sigsegv_handler) (int);
459 #endif
460 #ifdef SIGFPE
461 void (*original_sigfpe_handler) (int);
462 #endif
463 #ifdef SIGBUS
464 void (*original_sigbus_handler) (int);
465 #endif
466 #ifdef SIGILL
467 void (*original_sigill_handler) (int);
468 #endif
469 #ifdef SIGABRT
470 void (*original_sigabrt_handler) (int);
471 #endif
472 #endif
473 #ifdef DEBUG
474 int xpad;
475 int profile_level;
476 long self_time[256];
477 #endif
478 #ifdef WITH_EACCELERATOR_SESSIONS
479 char *session;
480 #endif
481 ZEND_END_MODULE_GLOBALS (eaccelerator)
482
483 ZEND_EXTERN_MODULE_GLOBALS (eaccelerator)
484
485 #define EACCELERATOR_EXTENSION_NAME "eAccelerator"
486 #define EACCELERATOR_LOADER_EXTENSION_NAME "eLoader"
487
488 #define EA_MAGIC "EACCELERATOR"
489
490 #define EA_ENCODER_VERSION   0x00000004
491 #define EA_ENCODER_END       0x00
492 #define EA_ENCODER_NAMESPACE 0x01
493 #define EA_ENCODER_CLASS     0x02
494 #define EA_ENCODER_FUNCTION  0x03
495
496 #define EACCELERATOR_VERSION_GUID   "PHPE8EDA1B6-806A-4851-B1C8-A6B4712F44FB"
497 #define EACCELERATOR_LOGO_GUID      "PHPE6F78DE9-13E4-4dee-8518-5FA2DACEA803"
498 #define EACCELERATOR_VERSION_STRING ("eAccelerator " EACCELERATOR_VERSION " (PHP " PHP_VERSION ")")
499
500 #endif          /* HAVE_EACCELERATOR */
501 #endif          /* #ifndef INCLUDED_EACCELERATOR_H */
Note: See TracBrowser for help on using the browser.