root/eaccelerator/branches/0.9.4/eaccelerator.h

Revision 215, 14.7 kB (checked in by bart, 2 years ago)

* Branched the 0.9.4 release to fix win32
* Fixes from Simon Westwood

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