Changeset 1554 in svn


Ignore:
Timestamp:
2014-11-08T19:18:37Z (4 years ago)
Author:
Xuefer
Message:

split head.m4 into multilpe helper file

Location:
trunk
Files:
5 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/processor/head.m4

    r1551 r1554  
    2020#include "util/xc_align.h"
    2121#include "util/xc_trace.h"
     22#include "util/xc_util.h"
    2223#include "xcache_globals.h"
    2324
     
    2526extern unsigned int zend_hash_canary;
    2627#endif
     28dnl }}}
    2729
    28 define(`SIZEOF_zend_uint', `sizeof(zend_uint)')
    29 define(`COUNTOF_zend_uint', `1')
    30 define(`SIZEOF_int', `sizeof(int)')
    31 define(`COUNTOF_int', `1')
    32 define(`SIZEOF_zend_function', `sizeof(zend_function)')
    33 define(`COUNTOF_zend_function', `1')
    34 define(`SIZEOF_zval_ptr', `sizeof(zval_ptr)')
    35 define(`COUNTOF_zval_ptr', `1')
    36 define(`SIZEOF_zval_ptr_nullable', `sizeof(zval_ptr_nullable)')
    37 define(`COUNTOF_zval_ptr_nullable', `1')
    38 define(`SIZEOF_zend_trait_alias_ptr', `sizeof(zend_trait_alias)')
    39 define(`COUNTOF_zend_trait_alias_ptr', `1')
    40 define(`SIZEOF_zend_trait_precedence_ptr', `sizeof(zend_trait_precedence)')
    41 define(`COUNTOF_zend_trait_precedence_ptr', `1')
    42 define(`SIZEOF_xc_entry_name_t', `sizeof(xc_entry_name_t)')
    43 define(`COUNTOF_xc_entry_name_t', `1')
    44 define(`SIZEOF_xc_ztstring', `sizeof(xc_ztstring)')
    45 define(`COUNTOF_xc_ztstring', `1')
     30include(__dir__`/debug-helper.m4')
     31include(__dir__`/type-helper.m4')
     32include(__dir__`/string-helper.m4')
    4633
    47 ifdef(`XCACHE_ENABLE_TEST', `
    48 #undef NDEBUG
    49 #include <assert.h>
    50 m4_errprint(`AUTOCHECK INFO: runtime autocheck Enabled (debug build)')
    51 ', `
    52 m4_errprint(`AUTOCHECK INFO: runtime autocheck Disabled (optimized build)')
     34/* {{{ call op_array ctor handler */
     35extern zend_bool xc_have_op_array_ctor;
     36static void xc_zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
     37{
     38    if (extension->op_array_ctor) {
     39        extension->op_array_ctor(op_array);
     40    }
     41}
     42/* }}} */
     43/* {{{ memsetptr */
     44IFAUTOCHECK(`dnl
     45static void *memsetptr(void *mem, void *content, size_t n)
     46{
     47    void **p = (void **) mem;
     48    void **end = (void **) ((char *) mem + n);
     49    while (p < end - sizeof(content)) {
     50        *p = content;
     51        p += sizeof(content);
     52    }
     53    if (p < end) {
     54        memset(p, -1, end - p);
     55    }
     56    return mem;
     57}
    5358')
    54 ifdef(`DEBUG_SIZE', `static int xc_totalsize = 0;')
    55 
    56 #ifndef NDEBUG
    57 #   undef inline
    58 #define inline
    59 #endif
    60 
    61 typedef zval *zval_ptr;
    62 typedef zval *zval_ptr_nullable;
    63 typedef char *xc_ztstring;
    64 #ifdef ZEND_ENGINE_2_4
    65 typedef zend_trait_alias *zend_trait_alias_ptr;
    66 typedef zend_trait_precedence *zend_trait_precedence_ptr;
    67 #endif
    68 #ifdef ZEND_ENGINE_2_3
    69 typedef int last_brk_cont_t;
    70 #else
    71 typedef zend_uint last_brk_cont_t;
    72 #endif
    73 
    74 typedef zend_uchar xc_zval_type_t;
    75 typedef int xc_op_type;
    76 typedef zend_uchar xc_opcode;
    77 #ifdef IS_UNICODE
    78 typedef UChar zstr_uchar;
    79 #endif
    80 typedef char  zstr_char;
    81 
    82 #define MAX_DUP_STR_LEN 256
    83 
    84 #define ptradd(type, ptr, ptrdiff) ((type) (((char *) (ptr)) + (ptrdiff)))
    85 #define ptrsub(ptr1, ptr2) (((char *) (ptr1)) - ((char *) (ptr2)))
    86 #ifdef NDEBUG
    87     #define notnullable(ptr) (ptr)
    88 #else
    89 static inline void *notnullable(const void *ptr)
    90 {
    91     assert(ptr);
    92     return (void *) ptr;
    93 }
    94 #endif
    95 dnl }}}
     59/* }}} */
    9660dnl {{{ _xc_processor_t
    9761typedef struct _xc_processor_t {
    9862    char *p;
    9963    size_t size;
    100     HashTable strings;
    10164    HashTable zvalptrs;
    10265    zend_bool handle_reference; /* enable if to deal with reference */
     
    12083
    12184    zend_bool readonly_protection; /* wheather it's present */
    122 IFAUTOCHECK(xc_vector_t allocsizes;)
     85
     86    STRING_HELPER_T
     87
     88    IFAUTOCHECK(xc_vector_t allocsizes;)
    12389} xc_processor_t;
    12490dnl }}}
    125 EXPORT(`typedef struct _xc_dasm_t { const zend_op_array *active_op_array_src; } xc_dasm_t;')
    126 /* {{{ memsetptr */
    127 IFAUTOCHECK(`dnl
    128 static void *memsetptr(void *mem, void *content, size_t n)
    129 {
    130     void **p = (void **) mem;
    131     void **end = (void **) ((char *) mem + n);
    132     while (p < end - sizeof(content)) {
    133         *p = content;
    134         p += sizeof(content);
    135     }
    136     if (p < end) {
    137         memset(p, -1, end - p);
    138     }
    139     return mem;
    140 }
    141 ')
    142 /* }}} */
    143 #ifdef HAVE_XCACHE_DPRINT
    144 static void xc_dprint_indent(int indent) /* {{{ */
    145 {
    146     int i;
    147     for (i = 0; i < indent; i ++) {
    148         fprintf(stderr, "  ");
    149     }
    150 }
    151 /* }}} */
    152 static void xc_dprint_str_len(const char *str, int len) /* {{{ */
    153 {
    154     const unsigned char *p = (const unsigned char *) str;
    155     int i;
    156     for (i = 0; i < len; i ++) {
    157         if (p[i] < 32 || p[i] == 127) {
    158             fprintf(stderr, "\\%03o", (unsigned int) p[i]);
    159         }
    160         else {
    161             fputc(p[i], stderr);
    162         }
    163     }
    164 }
    165 /* }}} */
    166 #endif
    167 static inline size_t xc_zstrlen_char(const_zstr s) /* {{{ */
    168 {
    169     return strlen(ZSTR_S(s));
    170 }
    171 /* }}} */
    172 #ifdef IS_UNICODE
    173 static inline size_t xc_zstrlen_uchar(zstr s) /* {{{ */
    174 {
    175     return u_strlen(ZSTR_U(s));
    176 }
    177 /* }}} */
    178 static inline size_t xc_zstrlen(int type, const_zstr s) /* {{{ */
    179 {
    180     return type == IS_UNICODE ? xc_zstrlen_uchar(s) : xc_zstrlen_char(s);
    181 }
    182 /* }}} */
    183 #else
    184 /* {{{ xc_zstrlen */
    185 #define xc_zstrlen(dummy, s) xc_zstrlen_char(s)
    186 /* }}} */
    187 #endif
    188 #undef C_RELAYLINE
    189 #define C_RELAYLINE
    190 IFAUTOCHECK(`
    191 #undef C_RELAYLINE
    192 #define C_RELAYLINE , __LINE__
    193 ')
    194 static inline void xc_calc_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline') TSRMLS_DC) { /* {{{ */
    195     pushdef(`PROCESSOR_TYPE', `calc')
    196     pushdef(`__LINE__', `relayline')
    197     size_t realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
    198     long dummy = 1;
     91STRING_HELPERS()
    19992
    200     if (realsize > MAX_DUP_STR_LEN) {
    201         ALLOC(, char, realsize)
    202     }
    203     else if (zend_u_hash_add(&processor->strings, type, str, (uint) size, (void *) &dummy, sizeof(dummy), NULL) == SUCCESS) {
    204         /* new string */
    205         ALLOC(, char, realsize)
    206     }
    207     IFAUTOCHECK(`
    208         else {
    209             dnl fprintf(stderr, "dupstr %s\n", ZSTR_S(str));
    210         }
    211     ')
    212     popdef(`__LINE__')
    213     popdef(`PROCESSOR_TYPE')
    214 }
    215 /* }}} */
    216 static inline zstr xc_store_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) { /* {{{ */
    217     pushdef(`PROCESSOR_TYPE', `store')
    218     pushdef(`__LINE__', `relayline')
    219     size_t realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
    220     zstr ret, *pret;
    221 
    222     if (realsize > MAX_DUP_STR_LEN) {
    223         ALLOC(ZSTR_V(ret), char, realsize)
    224         memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
    225         return ret;
    226     }
    227 
    228     if (zend_u_hash_find(&processor->strings, type, str, (uint) size, (void **) &pret) == SUCCESS) {
    229         TRACE("found old string %s:%ld %p", str, size, *pret);
    230         return *pret;
    231     }
    232 
    233     /* new string */
    234     ALLOC(ZSTR_V(ret), char, realsize)
    235     memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
    236     zend_u_hash_add(&processor->strings, type, str, (uint) size, (void *) &ret, sizeof(zstr), NULL);
    237     TRACE("stored new string %s:%ld %p", str, size, ret);
    238     return ret;
    239 
    240     popdef(`__LINE__')
    241     popdef(`PROCESSOR_TYPE')
    242 }
    243 /* }}} */
    244 /* {{{ xc_get_class_num
    245  * return class_index + 1
    246  */
    247 static zend_ulong xc_get_class_num(xc_processor_t *processor, zend_class_entry *ce) {
    248     zend_uint i;
    249     const xc_entry_data_php_t *php = processor->php_src;
    250     zend_class_entry *ceptr;
    251 
    252     if (processor->cache_ce == ce) {
    253         return processor->cache_class_index + 1;
    254     }
    255     for (i = 0; i < php->classinfo_cnt; i ++) {
    256         ceptr = CestToCePtr(php->classinfos[i].cest);
    257         if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) {
    258             processor->cache_ce = ceptr;
    259             processor->cache_class_index = i;
    260             assert(i <= processor->active_class_index);
    261             return i + 1;
    262         }
    263     }
    264     assert(0);
    265     return (zend_ulong) -1;
    266 }
    267 define(`xc_get_class_num', `IFSTORE(``xc_get_class_num'($@)',``xc_get_class_num' can be use in store only')')
    268 /* }}} */
    269 #ifdef ZEND_ENGINE_2
    270 static zend_class_entry *xc_get_class(xc_processor_t *processor, zend_ulong class_num) { /* {{{ */
    271     /* must be parent or currrent class */
    272     assert(class_num > 0);
    273     assert(class_num <= processor->active_class_index + 1);
    274     return CestToCePtr(processor->php_dst->classinfos[class_num - 1].cest);
    275 }
    276 define(`xc_get_class', `IFRESTORE(``xc_get_class'($@)',``xc_get_class' can be use in restore only')')
    277 /* }}} */
    278 #endif
    279 #ifdef ZEND_ENGINE_2
    280 /* fix method on store */
    281 static void xc_fix_method(xc_processor_t *processor, zend_op_array *dst TSRMLS_DC) /* {{{ */
    282 {
    283     zend_function *zf = (zend_function *) dst;
    284     zend_class_entry *ce = processor->active_class_entry_dst;
    285     const zend_class_entry *srcce = processor->active_class_entry_src;
    286 
    287     /* Fixing up the default functions for objects here since
    288      * we need to compare with the newly allocated functions
    289      *
    290      * caveat: a sub-class method can have the same name as the
    291      * parent~s constructor and create problems.
    292      */
    293 
    294     if (zf->common.fn_flags & ZEND_ACC_CTOR) {
    295         if (!ce->constructor) {
    296             ce->constructor = zf;
    297         }
    298     }
    299     else if (zf->common.fn_flags & ZEND_ACC_DTOR) {
    300         ce->destructor = zf;
    301     }
    302     else if (zf->common.fn_flags & ZEND_ACC_CLONE) {
    303         ce->clone = zf;
    304     }
    305     else {
    306     pushdef(`SET_IF_SAME_NAMEs', `
    307         SET_IF_SAME_NAME(__get);
    308         SET_IF_SAME_NAME(__set);
    309 #ifdef ZEND_ENGINE_2_1
    310         SET_IF_SAME_NAME(__unset);
    311         SET_IF_SAME_NAME(__isset);
    312 #endif
    313         SET_IF_SAME_NAME(__call);
    314 #ifdef ZEND_CALLSTATIC_FUNC_NAME
    315         SET_IF_SAME_NAME(__callstatic);
    316 #endif
    317 #if defined(ZEND_ENGINE_2_2) || PHP_MAJOR_VERSION >= 6
    318         SET_IF_SAME_NAME(__tostring);
    319 #endif
    320 #if defined(ZEND_ENGINE_2_6)
    321         SET_IF_SAME_NAME(__debugInfo);
    322 #endif
    323     ')
    324 #ifdef IS_UNICODE
    325         if (UG(unicode)) {
    326 #define SET_IF_SAME_NAME(member) \
    327             do { \
    328                 if (srcce->member && u_strcmp(ZSTR_U(zf->common.function_name), ZSTR_U(srcce->member->common.function_name)) == 0) { \
    329                     ce->member = zf; \
    330                 } \
    331             } \
    332             while(0)
    333 
    334             SET_IF_SAME_NAMEs()
    335 #undef SET_IF_SAME_NAME
    336         }
    337         else
    338 #endif
    339         do {
    340 #define SET_IF_SAME_NAME(member) \
    341             do { \
    342                 if (srcce->member && strcmp(ZSTR_S(zf->common.function_name), ZSTR_S(srcce->member->common.function_name)) == 0) { \
    343                     ce->member = zf; \
    344                 } \
    345             } \
    346             while(0)
    347 
    348             SET_IF_SAME_NAMEs()
    349 #undef SET_IF_SAME_NAME
    350         } while (0);
    351 
    352     popdef(`SET_IF_SAME_NAMEs')
    353 
    354     }
    355 }
    356 /* }}} */
    357 #endif
    358 /* {{{ call op_array ctor handler */
    359 extern zend_bool xc_have_op_array_ctor;
    360 static void xc_zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
    361 {
    362     if (extension->op_array_ctor) {
    363         extension->op_array_ctor(op_array);
    364     }
    365 }
    366 /* }}} */
    367 /* {{{ field name checker */
    368 IFAUTOCHECK(`dnl
    369 static int xc_check_names(const char *file, int line, const char *functionName, const char **assert_names, size_t assert_names_count, HashTable *done_names)
    370 {
    371     int errors = 0;
    372     if (assert_names_count) {
    373         size_t i;
    374         Bucket *b;
    375 
    376         for (i = 0; i < assert_names_count; ++i) {
    377             if (!zend_u_hash_exists(done_names, IS_STRING, assert_names[i], (uint) strlen(assert_names[i]) + 1)) {
    378                 fprintf(stderr
    379                     , "Error: missing field at %s `#'%d %s`' : %s\n"
    380                     , file, line, functionName
    381                     , assert_names[i]
    382                     );
    383                 ++errors;
    384             }
    385         }
    386 
    387         for (b = done_names->pListHead; b != NULL; b = b->pListNext) {
    388             int known = 0;
    389             for (i = 0; i < assert_names_count; ++i) {
    390                 if (strcmp(assert_names[i], BUCKET_KEY_S(b)) == 0) {
    391                     known = 1;
    392                     break;
    393                 }
    394             }
    395             if (!known) {
    396                 fprintf(stderr
    397                     , "Error: unknown field at %s `#'%d %s`' : %s\n"
    398                     , file, line, functionName
    399                     , BUCKET_KEY_S(b)
    400                     );
    401                 ++errors;
    402             }
    403         }
    404     }
    405     return errors;
    406 }
    407 ')
    408 /* }}} */
     93include(__dir__`/class-helper.m4')
  • trunk/processor/string.m4

    r1551 r1554  
    5959            ')
    6060        ')
    61         IFCALC(`xc_calc_string_n(processor, ISTYPE, SRCSTR, $3 C_RELAYLINE TSRMLS_CC);')
    62         IFSTORE(`DSTPTR = ifdef(`REALPTRTYPE', `(REALPTRTYPE() *)') ifelse(PTRTYPE,`char',`ZSTR_S',`ZSTR_U')(xc_store_string_n(processor, ISTYPE, SRCSTR, $3 C_RELAYLINE));')
     61        IFCALC(`xc_calc_string_n(processor, ISTYPE, SRCSTR, $3 RELAYLINE_CC TSRMLS_CC);')
     62        IFSTORE(`DSTPTR = ifdef(`REALPTRTYPE', `(REALPTRTYPE() *)') ifelse(PTRTYPE,`char',`ZSTR_S',`ZSTR_U')(xc_store_string_n(processor, ISTYPE, SRCSTR, $3 RELAYLINE_CC));')
    6363        IFRESTORE(`
    6464            DSTPTR = ifdef(`REALPTRTYPE', `(REALPTRTYPE() *)') STRDUP() (SRCPTR, ($3) - 1);
Note: See TracChangeset for help on using the changeset viewer.