Annotation of parser3/src/main/pa_globals.C, revision 1.185

1.15      paf         1: /** @file
1.16      paf         2:        Parser: globals.
                      3: 
1.177     paf         4:        Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com)
1.113     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.133     paf         6: */
1.16      paf         7: 
1.185   ! moko        8: static const char * const IDENT_GLOBALS_C="$Date: 2009-10-13 06:31:59 $";
1.1       paf         9: 
1.102     paf        10: #include "pa_config_includes.h"
                     11: 
                     12: #ifdef XML
1.157     paf        13: #include "libxml/xmlversion.h"
1.102     paf        14: #include "libxslt/extensions.h"
                     15: #include "libxslt/xsltutils.h"
1.116     paf        16: extern "C" {
1.102     paf        17: #include "libexslt/exslt.h"
1.116     paf        18: };
1.102     paf        19: #endif
                     20: 
1.1       paf        21: #include "pa_globals.h"
1.32      paf        22: #include "pa_string.h"
1.83      parser     23: #include "pa_sapi.h"
1.101     paf        24: #include "pa_threads.h"
1.162     paf        25: #include "pa_xml_io.h"
1.163     paf        26: #include "pa_common.h"
1.70      parser     27: 
1.164     paf        28: #include "pa_cache_managers.h"
                     29: 
1.182     misha      30: #include "pcre.h"
                     31: 
1.157     paf        32: // defines
1.155     paf        33: 
1.157     paf        34: //#define PA_DEBUG_XML_GC_MEMORY
1.95      paf        35: 
1.178     paf        36: //20051130 trying to remove this, author claims that fixed a lot there // 20040920 for now both workarounds needed. wait for new libxml/xsl versions
                     37: // // there is a problem with testcase, it's unstable. 
                     38: // // see paf@six/bug20040920/cgi-bin/t for it-showed-bug-on-20040920-day
                     39: // #define PA_WORKAROUND_BUGGY_FREE_IN_LIBXML_GC_MEMORY
                     40: // #define PA_WORKAROUND_BUGGY_MALLOCATOMIC_IN_LIBXML_GC_MEMORY
1.175     paf        41: 
1.157     paf        42: // globals
1.32      paf        43: 
1.5       paf        44: short hex_value[0x100];
1.111     paf        45: 
1.5       paf        46: static void setup_hex_value() {
1.68      parser     47:        memset(hex_value, 0, sizeof(hex_value));
1.5       paf        48:        hex_value['0'] = 0;     
                     49:        hex_value['1'] = 1;     
                     50:        hex_value['2'] = 2;     
                     51:        hex_value['3'] = 3;     
                     52:        hex_value['4'] = 4;     
                     53:        hex_value['5'] = 5;     
                     54:        hex_value['6'] = 6;     
                     55:        hex_value['7'] = 7;     
                     56:        hex_value['8'] = 8;     
                     57:        hex_value['9'] = 9;
                     58:        hex_value['A'] = 10;
                     59:        hex_value['B'] = 11;
                     60:        hex_value['C'] = 12;
                     61:        hex_value['D'] = 13;
                     62:        hex_value['E'] = 14;
                     63:        hex_value['F'] = 15;
                     64:        hex_value['a'] = 10;
                     65:        hex_value['b'] = 11;
                     66:        hex_value['c'] = 12;
                     67:        hex_value['d'] = 13;
                     68:        hex_value['e'] = 14;
                     69:        hex_value['f'] = 15;
                     70: }
1.1       paf        71: 
1.185   ! moko       72: THREAD_LOCAL Request* thread_request=NULL;
1.162     paf        73: 
                     74: void pa_register_thread_request(Request& r) {
1.185   ! moko       75:        thread_request=&r;
1.162     paf        76: }
                     77: /// retrives request set by pa_set_request function, useful in contextless places [slow]
                     78: Request& pa_thread_request() {
1.185   ! moko       79:        return *thread_request;
1.162     paf        80: }
1.176     paf        81: 
                     82: #ifdef PA_RELEASE_ASSERTS
                     83: void pa_release_assert(const char* str, const char* file, int line) {
                     84:        SAPI::die("%s at %s:%d", str, file, line); 
                     85: }
                     86: #endif
                     87: 
1.162     paf        88: 
1.99      paf        89: #ifdef XML
1.101     paf        90: 
1.157     paf        91: class XML_Generic_error_info {
1.173     paf        92: public:/*internal, actually*/
1.166     paf        93:        char buf[MAX_STRING*5];
1.157     paf        94:        size_t used;
                     95: public:
                     96:        XML_Generic_error_info() {
                     97:                buf[used=0]=0;
                     98:        }
1.173     paf        99:        const char* get() {
                    100:                return used? buf: 0;
1.157     paf       101:        }
1.162     paf       102: };
1.101     paf       103: 
1.172     paf       104: static Hash<pa_thread_t, XML_Generic_error_info*> xml_generic_error_infos;
1.101     paf       105: 
1.162     paf       106: static void xmlParserGenericErrorFunc(void *  /*ctx*/, const char* msg, ...) { 
                    107: //_asm int 3;
1.157     paf       108:        pa_thread_t thread_id=pa_get_thread_id();
1.101     paf       109: 
1.172     paf       110:        XML_Generic_error_info* p;
                    111:        {
1.101     paf       112:                SYNCHRONIZED;  // find+fill blocked
                    113: 
                    114:                // first try to get existing for this thread_id
1.172     paf       115:                p=xml_generic_error_infos.get(thread_id);
1.162     paf       116:                if(!p) // occupy empty one
                    117:                        xml_generic_error_infos.put(thread_id, (p=new(PointerFreeGC) XML_Generic_error_info));
1.172     paf       118:        }
1.101     paf       119:                
1.172     paf       120:        va_list args;
                    121:        va_start(args, msg);
                    122:        p->used+=vsnprintf(p->buf+p->used, sizeof(p->buf)-p->used, msg, args);
                    123:        va_end(args);
1.101     paf       124: }
                    125: 
1.102     paf       126: bool xmlHaveGenericErrors() {
1.157     paf       127:        pa_thread_t thread_id=pa_get_thread_id();
1.102     paf       128: 
                    129:        SYNCHRONIZED;  // find blocked
                    130: 
1.162     paf       131:        return xml_generic_error_infos.get(thread_id)!=0;
1.102     paf       132: }
                    133: 
1.157     paf       134: const char* xmlGenericErrors() {
                    135:        pa_thread_t thread_id=pa_get_thread_id();
1.101     paf       136: 
                    137:        SYNCHRONIZED;  // find+free blocked
                    138: 
1.173     paf       139:        if(XML_Generic_error_info *p=xml_generic_error_infos.get(thread_id)) {
                    140:                xml_generic_error_infos.remove(thread_id);
                    141:                return p->get();
                    142:        }
1.110     paf       143: 
1.162     paf       144:        return 0; // no errors for our thread_id registered
1.150     paf       145: }
                    146: 
1.99      paf       147: #endif
                    148: 
1.83      parser    149: void pa_globals_destroy(void *) {
1.180     paf       150: /*
1.83      parser    151:        try {
1.96      paf       152: #ifdef XML
                    153: #endif
1.180     paf       154:        } catch(.../*const Exception& e* /) {
1.160     paf       155: //             SAPI::abort("pa_globals_destroy failed: %s", e.comment());
1.83      parser    156:        }
1.180     paf       157: */
1.83      parser    158: }
                    159: 
                    160: 
1.157     paf       161: #ifdef XML
                    162: 
                    163: static char *pa_GC_strdup(const char *s) {
                    164:        if(!s)
                    165:                return 0;
                    166: 
                    167:        size_t size=strlen(s)+1;
1.170     paf       168:        char *result=(char *)GC_MALLOC_ATOMIC(size);
1.159     paf       169:        if(!result)
1.184     misha     170:                pa_fail_alloc("duplicate XML string",size);
1.159     paf       171: 
1.157     paf       172:        memcpy(result, s, size);
1.170     paf       173: #ifdef PA_DEBUG_XML_GC_MEMORY
                    174:        fprintf(stderr, "pa_GC_strdup(%p=%s, length=%d)=0x%p\n", s, s, size, result);
                    175: #endif
1.157     paf       176:        return result;
                    177: }
                    178: 
                    179: #ifdef PA_DEBUG_XML_GC_MEMORY
1.175     paf       180: void *pa_look_for[]={(void*)0x84ba980,(void*)0x8969460,(void*)0x0,(void*)0x0,
1.157     paf       181:                        (void*)0x0,(void*)0x0,(void*)0x0,(void*)0x0};
                    182: bool pa_looked(void*p) {
                    183:        for(int i=0; i<8; i++)
1.175     paf       184:                if(pa_look_for[i]==p) {
                    185:                        __asm__("int $3");
1.157     paf       186:                        return true;
1.175     paf       187:                }
                    188:        if((((int)p)&~0xFF)==0x89a7700) {
                    189:                __asm__("int $3");
                    190:                return true;
                    191:        }
1.157     paf       192:        return false;
                    193: }
                    194: static void* pa_gc_malloc_log(size_t size){
                    195:        void *p=pa_gc_malloc(size);
                    196:         fprintf(stderr, "pa_gc_malloc_log(%d)=0x%p\n", size, p);
1.175     paf       197:        if(pa_looked(p))
                    198:                fprintf(stderr,"catched debug malloc(%d)=0x%p\n", size, p);
1.157     paf       199:        return p;
                    200:         
                    201: }
                    202: static void* pa_gc_malloc_atomic_log(size_t size){
1.175     paf       203: #ifdef PA_WORKAROUND_BUGGY_MALLOCATOMIC_IN_LIBXML_GC_MEMORY
                    204:        void *p=pa_gc_malloc(size);
                    205:         fprintf(stderr, "pa_gc_malloc_atomicFAKE_log(%d)=0x%p\n", size, p);
                    206: #else
1.157     paf       207:        void *p=pa_gc_malloc_atomic(size);
                    208:         fprintf(stderr, "pa_gc_malloc_atomic_log(%d)=0x%p\n", size, p);
1.175     paf       209: #endif
                    210:        if(pa_looked(p))
                    211:                fprintf(stderr,"catched debug malloc atomic(%d)=0x%p\n", size, p);
1.157     paf       212:        return p;
                    213: }
                    214: static void* pa_gc_realloc_log(void *ptr, size_t size){
                    215:        void *p=pa_gc_realloc(ptr, size);
                    216:         fprintf(stderr, "pa_gc_realloc_log(0x%p, %d)=0x%p\n", ptr, size, p);
1.175     paf       217:        if(pa_looked(p))
                    218:                fprintf(stderr,"catched debug realloc(%d)=0x%p\n", size, p);
1.157     paf       219:        return p;
                    220: }
                    221: static void pa_gc_free_log(void *p){
1.175     paf       222: #ifdef PA_WORKAROUND_BUGGY_FREE_IN_LIBXML_GC_MEMORY
                    223:         fprintf(stderr, "pa_gc_freeIGNORE_log(0x%p)\n", p);
                    224: #else
1.157     paf       225:         fprintf(stderr, "pa_gc_free_log(0x%p)\n", p);
1.175     paf       226: #endif
                    227:        if(pa_looked(p))
                    228:                fprintf(stderr,"catched debug free(0x%p)\n", p);
                    229: #ifndef PA_WORKAROUND_BUGGY_FREE_IN_LIBXML_GC_MEMORY
1.157     paf       230:         pa_gc_free(p);
1.175     paf       231: #endif
1.157     paf       232: }
1.159     paf       233: #else
                    234: 
                    235: inline void *check(void *result, const char *where, size_t size) {
                    236:        if(!result)
1.184     misha     237:                pa_fail_alloc(where, size);
1.159     paf       238: 
                    239:        return result;
                    240: }
                    241: static void* pa_gc_malloc_nonull(size_t size) { 
1.184     misha     242:        return check(pa_gc_malloc(size), "allocate XML compsite memory", size);
1.159     paf       243: }
                    244: static void* pa_gc_malloc_atomic_nonull(size_t size) { 
1.175     paf       245: #ifdef PA_WORKAROUND_BUGGY_MALLOCATOMIC_IN_LIBXML_GC_MEMORY
1.184     misha     246:        return check(pa_gc_malloc(size), "allocate XML composite memory (asked atomic)", size);
1.175     paf       247: #else
1.184     misha     248:        return check(pa_gc_malloc_atomic(size), "allocate XML atomic memory", size);
1.175     paf       249: #endif
1.159     paf       250: }
                    251: static void* pa_gc_realloc_nonull(void* ptr, size_t size) { 
1.184     misha     252:        return check(pa_gc_realloc(ptr, size), "reallocate XML memory", size);
1.159     paf       253: }
                    254: 
1.175     paf       255: static void pa_gc_free_maybeignore(
                    256:        void* 
                    257: #ifndef PA_WORKAROUND_BUGGY_FREE_IN_LIBXML_GC_MEMORY
                    258:                ptr
                    259: #endif
                    260:        ) {
                    261: #ifndef PA_WORKAROUND_BUGGY_FREE_IN_LIBXML_GC_MEMORY
                    262:        pa_gc_free(ptr);
                    263: #endif
                    264: }
                    265: 
1.157     paf       266: #endif
                    267: #endif
                    268: 
                    269: void pa_CORD_oom_fn(void) {
1.184     misha     270:        pa_fail_alloc("expand string", 0);
1.157     paf       271: }
                    272: 
                    273: /**
                    274:        @todo gc: libltdl: substitute lt_dlmalloc & co
                    275: */
                    276: static void gc_substitute_memory_management_functions() {
                    277:        // in libxml & libxslt
                    278: #ifdef XML
                    279:        // asking to use GC memory
                    280: #if LIBXML_VERSION >= 20507
                    281: #ifdef PA_DEBUG_XML_GC_MEMORY
                    282:        xmlGcMemSetup(
                    283:                /*xmlFreeFunc */pa_gc_free_log,
                    284:                /*xmlMallocFunc */pa_gc_malloc_log,
                    285:                /*xmlMallocFunc */pa_gc_malloc_atomic_log,
                    286:                /*xmlReallocFunc */pa_gc_realloc_log,
                    287:                /*xmlStrdupFunc */pa_GC_strdup);
                    288: #else
                    289:        xmlGcMemSetup(
1.175     paf       290:                /*xmlFreeFunc */pa_gc_free_maybeignore,
1.159     paf       291:                /*xmlMallocFunc */pa_gc_malloc_nonull,
                    292:                /*xmlMallocFunc */pa_gc_malloc_atomic_nonull,
                    293:                /*xmlReallocFunc */pa_gc_realloc_nonull,
1.157     paf       294:                /*xmlStrdupFunc */pa_GC_strdup);
                    295: #endif
1.32      paf       296: 
1.157     paf       297: #else
                    298:        xmlMemSetup(
1.175     paf       299:                /*xmlFreeFunc */pa_gc_free_maybeignore,
1.157     paf       300:                /*xmlMallocFunc */pa_gc_malloc,
                    301:                /*xmlReallocFunc */pa_gc_realloc,
                    302:                /*xmlStrdupFunc */pa_GC_strdup);
                    303: #endif
1.5       paf       304: 
1.157     paf       305: #endif
1.141     paf       306: 
1.157     paf       307:        // pcre
1.183     misha     308:        pcre_malloc=pa_gc_malloc;
                    309:        pcre_free=pa_gc_free;
1.135     paf       310: 
1.1       paf       311: 
1.157     paf       312:        // cord
                    313:        CORD_oom_fn=pa_CORD_oom_fn;
                    314: }
1.88      paf       315: 
1.157     paf       316: /**
                    317:        @test hint on one should call this for each thread xmlSubstituteEntitiesDefault(1);
                    318: */
                    319: void pa_globals_init() {
1.164     paf       320:        // global variables 
                    321:        cache_managers=new Cache_managers;
                    322: 
                    323: 
1.157     paf       324:        // in various libraries
                    325:        gc_substitute_memory_management_functions();
1.101     paf       326: 
1.157     paf       327:        // hex value
                    328:        setup_hex_value();
1.74      parser    329: 
1.76      parser    330: #ifdef XML
1.96      paf       331:        // initializing xml libs
                    332: 
1.157     paf       333:        /*
                    334:        * Register the EXSLT extensions and the test module
                    335:        */
                    336:        exsltRegisterAll();
                    337:        xsltRegisterTestModule();
                    338:        xmlDefaultSAXHandlerInit();
                    339:        /*
                    340:        * disable CDATA from being built in the document tree
                    341:        */
                    342:        // never added yet  xmlDefaultSAXHandler.cdataBlock = NULL;
                    343:        
1.99      paf       344:        /*
                    345:         * Initialization function for the XML parser.
                    346:         * This is not reentrant. Call once before processing in case of
                    347:         * use in multithreaded programs.
                    348:        */
                    349:        xmlInitParser();
1.107     paf       350: 
                    351:        // 1. this is needed for proper parsing of stylesheets
                    352:        // there were a situation where honest entity ruined innocent xpath compilation
                    353:        // doc says "you sould turn it on on stylesheet load" without deepening into details
                    354:        // 2. when dom tree with entites goes under transform text nodes 
                    355:        // got [erroreosly] cut on first entity occurance
1.109     paf       356:        // --
1.107     paf       357:        // that is why this is:
                    358:        xmlSubstituteEntitiesDefault(1);
1.100     paf       359:        
                    360:        // Bit in the loadsubset context field to tell to do ID/REFs lookups 
                    361:        xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
                    362:        // Bit in the loadsubset context field to tell to do complete the elements attributes lists 
                    363:        // with the ones defaulted from the DTDs 
1.157     paf       364:        xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
1.138     paf       365: 
                    366:        // validate each document after load/create (?)
                    367:        //xmlDoValidityCheckingDefaultValue = 1;
1.99      paf       368: 
1.104     paf       369: //regretfully this not only replaces entities on parse, but also on generate   xmlSubstituteEntitiesDefault(1);
1.105     paf       370:        // never switched this on xmlIndentTreeOutput=1;
1.104     paf       371: 
1.101     paf       372:        xmlSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.102     paf       373:        xsltSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.105     paf       374: //     FILE *f=fopen("y:\\xslt.log", "wt");
                    375: //     xsltSetGenericDebugFunc(f/*stderr*/, 0);
1.110     paf       376: 
1.162     paf       377:        pa_xml_io_init();
1.157     paf       378: #endif
1.174     paf       379: }
                    380: 
                    381: void pa_globals_done() {
                    382:        delete cache_managers;  cache_managers=0;
1.157     paf       383: }
                    384: 
                    385: #ifdef _MSC_VER
                    386: 
                    387: #ifndef PA_DEBUG_DISABLE_GC
                    388: #      define GC_LIB "../../../../win32/gc"
                    389: #      ifdef _DEBUG
                    390: #              pragma comment(lib, GC_LIB "/Debug/gc.lib")
                    391: #      else
                    392: #              pragma comment(lib, GC_LIB "/Release/gc.lib")
                    393: #      endif
1.96      paf       394: 
1.76      parser    395: #endif
                    396: 
1.181     paf       397: #define GNOME_LIBS "../../../../win32/gnome"
                    398: 
                    399: #ifdef WITH_MAILRECEIVE
                    400: #      pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
                    401: #endif
                    402: 
1.157     paf       403: #ifdef XML
1.76      parser    404: #      ifdef _DEBUG
1.157     paf       405: 
                    406: #              ifdef LIBXML_STATIC
1.168     paf       407: #                      pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/debug/lib/libxml2_a.lib")
1.157     paf       408: #              else
1.168     paf       409: #                      pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/debug/lib/libxml2.lib")
1.157     paf       410: #              endif
                    411: 
                    412: #              ifdef LIBXSLT_STATIC
1.168     paf       413: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/debug/lib/libxslt_a.lib")
1.157     paf       414: #              else
1.168     paf       415: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/debug/lib/libxslt.lib")
1.157     paf       416: #              endif
                    417: #              ifdef LIBEXSLT_STATIC
1.168     paf       418: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/debug/lib/libexslt_a.lib")
1.157     paf       419: #              else
1.168     paf       420: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/debug/lib/libexslt.lib")
1.157     paf       421: #              endif
                    422: 
                    423: #else
                    424: 
                    425: #              ifdef LIBXML_STATIC
1.168     paf       426: #                      pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/release/lib/libxml2_a.lib")
1.157     paf       427: #              else
1.168     paf       428: #                      pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/release/lib/libxml2.lib")
1.157     paf       429: #              endif
                    430: 
                    431: #              ifdef LIBXSLT_STATIC
1.168     paf       432: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/release/lib/libxslt_a.lib")
1.157     paf       433: #              else
1.168     paf       434: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/release/lib/libxslt.lib")
1.157     paf       435: #              endif
                    436: #              ifdef LIBEXSLT_STATIC
1.168     paf       437: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/release/lib/libexslt_a.lib")
1.157     paf       438: #              else
1.168     paf       439: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/release/lib/libexslt.lib")
1.157     paf       440: #              endif
                    441: 
1.85      paf       442: #      endif
1.157     paf       443: #endif
                    444: 
1.85      paf       445: #endif

E-mail: