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

1.15      paf         1: /** @file
1.16      paf         2:        Parser: globals.
                      3: 
1.171     paf         4:        Copyright (c) 2001-2004 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.175   ! paf         8: static const char * const IDENT_GLOBALS_C="$Date: 2004/04/01 11:43:54 $";
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.157     paf        21: #include "pcre.h"
                     22: 
1.1       paf        23: #include "pa_globals.h"
1.32      paf        24: #include "pa_string.h"
1.83      parser     25: #include "pa_sapi.h"
1.101     paf        26: #include "pa_threads.h"
1.162     paf        27: #include "pa_xml_io.h"
1.163     paf        28: #include "pa_common.h"
1.70      parser     29: 
1.164     paf        30: #include "pa_cache_managers.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.175   ! paf        36: // 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
        !            41: 
1.157     paf        42: // globals
1.32      paf        43: 
1.5       paf        44: short hex_value[0x100];
1.111     paf        45: 
                     46: #ifdef XML
                     47: GdomeDOMImplementation *domimpl;
                     48: #endif
1.5       paf        49: 
                     50: static void setup_hex_value() {
1.68      parser     51:        memset(hex_value, 0, sizeof(hex_value));
1.5       paf        52:        hex_value['0'] = 0;     
                     53:        hex_value['1'] = 1;     
                     54:        hex_value['2'] = 2;     
                     55:        hex_value['3'] = 3;     
                     56:        hex_value['4'] = 4;     
                     57:        hex_value['5'] = 5;     
                     58:        hex_value['6'] = 6;     
                     59:        hex_value['7'] = 7;     
                     60:        hex_value['8'] = 8;     
                     61:        hex_value['9'] = 9;
                     62:        hex_value['A'] = 10;
                     63:        hex_value['B'] = 11;
                     64:        hex_value['C'] = 12;
                     65:        hex_value['D'] = 13;
                     66:        hex_value['E'] = 14;
                     67:        hex_value['F'] = 15;
                     68:        hex_value['a'] = 10;
                     69:        hex_value['b'] = 11;
                     70:        hex_value['c'] = 12;
                     71:        hex_value['d'] = 13;
                     72:        hex_value['e'] = 14;
                     73:        hex_value['f'] = 15;
                     74: }
1.1       paf        75: 
1.162     paf        76: 
                     77: Hash<pa_thread_t, Request*> thread_request;
                     78: void pa_register_thread_request(Request& r) {
                     79:        thread_request.put(pa_get_thread_id(), &r);
                     80: }
                     81: /// retrives request set by pa_set_request function, useful in contextless places [slow]
                     82: Request& pa_thread_request() {
                     83:        return *thread_request.get(pa_get_thread_id());
                     84: }
                     85: 
1.99      paf        86: #ifdef XML
1.101     paf        87: 
1.157     paf        88: class XML_Generic_error_info {
1.173     paf        89: public:/*internal, actually*/
1.166     paf        90:        char buf[MAX_STRING*5];
1.157     paf        91:        size_t used;
                     92: public:
                     93:        XML_Generic_error_info() {
                     94:                buf[used=0]=0;
                     95:        }
1.173     paf        96:        const char* get() {
                     97:                return used? buf: 0;
1.157     paf        98:        }
1.162     paf        99: };
1.101     paf       100: 
1.172     paf       101: static Hash<pa_thread_t, XML_Generic_error_info*> xml_generic_error_infos;
1.101     paf       102: 
1.162     paf       103: static void xmlParserGenericErrorFunc(void *  /*ctx*/, const char* msg, ...) { 
                    104: //_asm int 3;
1.157     paf       105:        pa_thread_t thread_id=pa_get_thread_id();
1.101     paf       106: 
1.172     paf       107:        XML_Generic_error_info* p;
                    108:        {
1.101     paf       109:                SYNCHRONIZED;  // find+fill blocked
                    110: 
                    111:                // first try to get existing for this thread_id
1.172     paf       112:                p=xml_generic_error_infos.get(thread_id);
1.162     paf       113:                if(!p) // occupy empty one
                    114:                        xml_generic_error_infos.put(thread_id, (p=new(PointerFreeGC) XML_Generic_error_info));
1.172     paf       115:        }
1.101     paf       116:                
1.172     paf       117:        va_list args;
                    118:        va_start(args, msg);
                    119:        p->used+=vsnprintf(p->buf+p->used, sizeof(p->buf)-p->used, msg, args);
                    120:        va_end(args);
1.101     paf       121: }
                    122: 
1.102     paf       123: bool xmlHaveGenericErrors() {
1.157     paf       124:        pa_thread_t thread_id=pa_get_thread_id();
1.102     paf       125: 
                    126:        SYNCHRONIZED;  // find blocked
                    127: 
1.162     paf       128:        return xml_generic_error_infos.get(thread_id)!=0;
1.102     paf       129: }
                    130: 
1.157     paf       131: const char* xmlGenericErrors() {
                    132:        pa_thread_t thread_id=pa_get_thread_id();
1.101     paf       133: 
                    134:        SYNCHRONIZED;  // find+free blocked
                    135: 
1.173     paf       136:        if(XML_Generic_error_info *p=xml_generic_error_infos.get(thread_id)) {
                    137:                xml_generic_error_infos.remove(thread_id);
                    138:                return p->get();
                    139:        }
1.110     paf       140: 
1.162     paf       141:        return 0; // no errors for our thread_id registered
1.150     paf       142: }
                    143: 
1.99      paf       144: #endif
                    145: 
1.83      parser    146: void pa_globals_destroy(void *) {
                    147:        try {
1.96      paf       148: #ifdef XML
                    149:                GdomeException exc;
                    150:                gdome_di_unref (domimpl, &exc);
1.160     paf       151:                // uncomment SAPI::abort below if adding potential-throw code here
1.96      paf       152: #endif
1.83      parser    153:        } catch(const Exception& e) {
1.160     paf       154: //             SAPI::abort("pa_globals_destroy failed: %s", e.comment());
1.83      parser    155:        }
                    156: }
                    157: 
                    158: 
1.157     paf       159: #ifdef XML
                    160: 
                    161: static char *pa_GC_strdup(const char *s) {
                    162:        if(!s)
                    163:                return 0;
                    164: 
                    165:        size_t size=strlen(s)+1;
1.170     paf       166:        char *result=(char *)GC_MALLOC_ATOMIC(size);
1.159     paf       167:        if(!result)
                    168:                SAPI::abort("out of memory (while duplicating XML string [size=%d])", size);
                    169: 
1.157     paf       170:        memcpy(result, s, size);
1.170     paf       171: #ifdef PA_DEBUG_XML_GC_MEMORY
                    172:        fprintf(stderr, "pa_GC_strdup(%p=%s, length=%d)=0x%p\n", s, s, size, result);
                    173: #endif
1.157     paf       174:        return result;
                    175: }
                    176: 
                    177: #ifdef PA_DEBUG_XML_GC_MEMORY
1.175   ! paf       178: void *pa_look_for[]={(void*)0x84ba980,(void*)0x8969460,(void*)0x0,(void*)0x0,
1.157     paf       179:                        (void*)0x0,(void*)0x0,(void*)0x0,(void*)0x0};
                    180: bool pa_looked(void*p) {
                    181:        for(int i=0; i<8; i++)
1.175   ! paf       182:                if(pa_look_for[i]==p) {
        !           183:                        __asm__("int $3");
1.157     paf       184:                        return true;
1.175   ! paf       185:                }
        !           186:        if((((int)p)&~0xFF)==0x89a7700) {
        !           187:                __asm__("int $3");
        !           188:                return true;
        !           189:        }
1.157     paf       190:        return false;
                    191: }
                    192: static void* pa_gc_malloc_log(size_t size){
                    193:        void *p=pa_gc_malloc(size);
                    194:         fprintf(stderr, "pa_gc_malloc_log(%d)=0x%p\n", size, p);
1.175   ! paf       195:        if(pa_looked(p))
        !           196:                fprintf(stderr,"catched debug malloc(%d)=0x%p\n", size, p);
1.157     paf       197:        return p;
                    198:         
                    199: }
                    200: static void* pa_gc_malloc_atomic_log(size_t size){
1.175   ! paf       201: #ifdef PA_WORKAROUND_BUGGY_MALLOCATOMIC_IN_LIBXML_GC_MEMORY
        !           202:        void *p=pa_gc_malloc(size);
        !           203:         fprintf(stderr, "pa_gc_malloc_atomicFAKE_log(%d)=0x%p\n", size, p);
        !           204: #else
1.157     paf       205:        void *p=pa_gc_malloc_atomic(size);
                    206:         fprintf(stderr, "pa_gc_malloc_atomic_log(%d)=0x%p\n", size, p);
1.175   ! paf       207: #endif
        !           208:        if(pa_looked(p))
        !           209:                fprintf(stderr,"catched debug malloc atomic(%d)=0x%p\n", size, p);
1.157     paf       210:        return p;
                    211: }
                    212: static void* pa_gc_realloc_log(void *ptr, size_t size){
                    213:        void *p=pa_gc_realloc(ptr, size);
                    214:         fprintf(stderr, "pa_gc_realloc_log(0x%p, %d)=0x%p\n", ptr, size, p);
1.175   ! paf       215:        if(pa_looked(p))
        !           216:                fprintf(stderr,"catched debug realloc(%d)=0x%p\n", size, p);
1.157     paf       217:        return p;
                    218: }
                    219: static void pa_gc_free_log(void *p){
1.175   ! paf       220: #ifdef PA_WORKAROUND_BUGGY_FREE_IN_LIBXML_GC_MEMORY
        !           221:         fprintf(stderr, "pa_gc_freeIGNORE_log(0x%p)\n", p);
        !           222: #else
1.157     paf       223:         fprintf(stderr, "pa_gc_free_log(0x%p)\n", p);
1.175   ! paf       224: #endif
        !           225:        if(pa_looked(p))
        !           226:                fprintf(stderr,"catched debug free(0x%p)\n", p);
        !           227: #ifndef PA_WORKAROUND_BUGGY_FREE_IN_LIBXML_GC_MEMORY
1.157     paf       228:         pa_gc_free(p);
1.175   ! paf       229: #endif
1.157     paf       230: }
1.159     paf       231: #else
                    232: 
                    233: inline void *check(void *result, const char *where, size_t size) {
                    234:        if(!result)
                    235:                SAPI::abort("out of memory (while %s [size=%d])", where, size);
                    236: 
                    237:        return result;
                    238: }
                    239: static void* pa_gc_malloc_nonull(size_t size) { 
                    240:        return check(pa_gc_malloc(size), "allocating XML compsite memory", size);
                    241: }
                    242: static void* pa_gc_malloc_atomic_nonull(size_t size) { 
1.175   ! paf       243: #ifdef PA_WORKAROUND_BUGGY_MALLOCATOMIC_IN_LIBXML_GC_MEMORY
        !           244:        return check(pa_gc_malloc(size), "allocating XML composite memory (asked atomic)", size);
        !           245: #else
1.159     paf       246:        return check(pa_gc_malloc_atomic(size), "allocating XML atomic memory", size);
1.175   ! paf       247: #endif
1.159     paf       248: }
                    249: static void* pa_gc_realloc_nonull(void* ptr, size_t size) { 
                    250:        return check(pa_gc_realloc(ptr, size), "reallocating XML memory", size);
                    251: }
                    252: 
1.175   ! paf       253: static void pa_gc_free_maybeignore(
        !           254:        void* 
        !           255: #ifndef PA_WORKAROUND_BUGGY_FREE_IN_LIBXML_GC_MEMORY
        !           256:                ptr
        !           257: #endif
        !           258:        ) {
        !           259: #ifndef PA_WORKAROUND_BUGGY_FREE_IN_LIBXML_GC_MEMORY
        !           260:        pa_gc_free(ptr);
        !           261: #endif
        !           262: }
        !           263: 
1.157     paf       264: #endif
                    265: #endif
                    266: 
                    267: void pa_CORD_oom_fn(void) {
                    268:        SAPI::abort("out of memory (while expanding string)");
                    269: }
                    270: 
                    271: /**
                    272:        @todo gc: libltdl: substitute lt_dlmalloc & co
                    273: */
                    274: static void gc_substitute_memory_management_functions() {
                    275:        // in libxml & libxslt
                    276: #ifdef XML
                    277:        // asking to use GC memory
                    278: #if LIBXML_VERSION >= 20507
                    279: #ifdef PA_DEBUG_XML_GC_MEMORY
                    280:        xmlGcMemSetup(
                    281:                /*xmlFreeFunc */pa_gc_free_log,
                    282:                /*xmlMallocFunc */pa_gc_malloc_log,
                    283:                /*xmlMallocFunc */pa_gc_malloc_atomic_log,
                    284:                /*xmlReallocFunc */pa_gc_realloc_log,
                    285:                /*xmlStrdupFunc */pa_GC_strdup);
                    286: #else
                    287:        xmlGcMemSetup(
1.175   ! paf       288:                /*xmlFreeFunc */pa_gc_free_maybeignore,
1.159     paf       289:                /*xmlMallocFunc */pa_gc_malloc_nonull,
                    290:                /*xmlMallocFunc */pa_gc_malloc_atomic_nonull,
                    291:                /*xmlReallocFunc */pa_gc_realloc_nonull,
1.157     paf       292:                /*xmlStrdupFunc */pa_GC_strdup);
                    293: #endif
1.32      paf       294: 
1.157     paf       295: #else
                    296:        xmlMemSetup(
1.175   ! paf       297:                /*xmlFreeFunc */pa_gc_free_maybeignore,
1.157     paf       298:                /*xmlMallocFunc */pa_gc_malloc,
                    299:                /*xmlReallocFunc */pa_gc_realloc,
                    300:                /*xmlStrdupFunc */pa_GC_strdup);
                    301: #endif
1.5       paf       302: 
1.157     paf       303: #endif
1.141     paf       304: 
1.157     paf       305:        // pcre
                    306:        pcre_malloc=pa_gc_malloc;
                    307:        pcre_free=pa_gc_free;
1.135     paf       308: 
1.1       paf       309: 
1.157     paf       310:        // cord
                    311:        CORD_oom_fn=pa_CORD_oom_fn;
                    312: }
1.88      paf       313: 
1.157     paf       314: /**
                    315:        @test hint on one should call this for each thread xmlSubstituteEntitiesDefault(1);
                    316: */
                    317: void pa_globals_init() {
1.164     paf       318:        // global variables 
                    319:        cache_managers=new Cache_managers;
                    320: 
                    321: 
1.157     paf       322:        // in various libraries
                    323:        gc_substitute_memory_management_functions();
1.101     paf       324: 
1.157     paf       325:        // hex value
                    326:        setup_hex_value();
1.74      parser    327: 
1.76      parser    328: #ifdef XML
1.96      paf       329:        // initializing xml libs
                    330: 
1.157     paf       331:        /* First get a DOMImplementation reference */
1.96      paf       332:        domimpl = gdome_di_mkref ();
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.157     paf       397: #ifdef XML
1.158     paf       398: #      define GNOME_LIBS "../../../../win32/gnome"
1.131     paf       399: #      pragma comment(lib, GNOME_LIBS "/glib/lib/libglib-1.3-11.lib")
1.76      parser    400: #      ifdef _DEBUG
1.157     paf       401: 
                    402: #              ifdef LIBXML_STATIC
1.168     paf       403: #                      pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/debug/lib/libxml2_a.lib")
1.157     paf       404: #              else
1.168     paf       405: #                      pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/debug/lib/libxml2.lib")
1.157     paf       406: #              endif
                    407: 
                    408: #              ifdef LIBXSLT_STATIC
1.168     paf       409: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/debug/lib/libxslt_a.lib")
1.157     paf       410: #              else
1.168     paf       411: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/debug/lib/libxslt.lib")
1.157     paf       412: #              endif
                    413: #              ifdef LIBEXSLT_STATIC
1.168     paf       414: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/debug/lib/libexslt_a.lib")
1.157     paf       415: #              else
1.168     paf       416: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/debug/lib/libexslt.lib")
1.157     paf       417: #              endif
                    418: 
                    419: #              ifdef LIBGDOME_STATIC
1.169     paf       420: #                      pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/DebugStatic/libgdome.lib")
1.157     paf       421: #              else
1.169     paf       422: #                      pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/DebugDynamic/libgdome.lib")
1.157     paf       423: #              endif
                    424: 
                    425: #else
                    426: 
                    427: #              ifdef LIBXML_STATIC
1.168     paf       428: #                      pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/release/lib/libxml2_a.lib")
1.157     paf       429: #              else
1.168     paf       430: #                      pragma comment(lib, GNOME_LIBS "/libxml2-x.x.x/win32/release/lib/libxml2.lib")
1.157     paf       431: #              endif
                    432: 
                    433: #              ifdef LIBXSLT_STATIC
1.168     paf       434: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/release/lib/libxslt_a.lib")
1.157     paf       435: #              else
1.168     paf       436: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/release/lib/libxslt.lib")
1.157     paf       437: #              endif
                    438: #              ifdef LIBEXSLT_STATIC
1.168     paf       439: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/release/lib/libexslt_a.lib")
1.157     paf       440: #              else
1.168     paf       441: #                      pragma comment(lib, GNOME_LIBS "/libxslt-x.x.x/win32/release/lib/libexslt.lib")
1.157     paf       442: #              endif
                    443: 
                    444: #              ifdef LIBGDOME_STATIC
1.169     paf       445: #                      pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/ReleaseStatic/libgdome.lib")
1.157     paf       446: #              else
1.169     paf       447: #                      pragma comment(lib, GNOME_LIBS "/gdome2-x.x.x/win32/ReleaseDynamic/libgdome.lib")
1.157     paf       448: #              endif
                    449: 
1.85      paf       450: #      endif
1.157     paf       451: #endif
                    452: 
1.85      paf       453: #endif

E-mail: