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

1.15      paf         1: /** @file
1.16      paf         2:        Parser: globals.
                      3: 
1.198     moko        4:        Copyright (c) 2001-2017 Art. Lebedev Studio (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.102     paf         8: #include "pa_config_includes.h"
                      9: 
                     10: #ifdef XML
1.157     paf        11: #include "libxml/xmlversion.h"
1.102     paf        12: #include "libxslt/extensions.h"
                     13: #include "libxslt/xsltutils.h"
1.116     paf        14: extern "C" {
1.102     paf        15: #include "libexslt/exslt.h"
1.193     moko       16: }
1.102     paf        17: #endif
                     18: 
1.1       paf        19: #include "pa_globals.h"
1.203     moko       20: #include "pa_socks.h"
1.83      parser     21: #include "pa_sapi.h"
1.101     paf        22: #include "pa_threads.h"
1.162     paf        23: #include "pa_xml_io.h"
1.163     paf        24: #include "pa_common.h"
1.196     moko       25: #include "pa_symbols.h"
1.70      parser     26: 
1.164     paf        27: #include "pa_cache_managers.h"
                     28: 
1.188     moko       29: #include "ltdl.h"
1.182     misha      30: #include "pcre.h"
                     31: 
1.204   ! moko       32: volatile const char * IDENT_PA_GLOBALS_C="$Id: pa_globals.C,v 1.203 2019/12/25 22:01:03 moko Exp $" IDENT_PA_GLOBALS_H IDENT_PA_SAPI_H;
1.187     moko       33: 
1.157     paf        34: // defines
1.155     paf        35: 
1.157     paf        36: //#define PA_DEBUG_XML_GC_MEMORY
1.95      paf        37: 
1.157     paf        38: // globals
1.32      paf        39: 
1.204   ! moko       40: short hex_value[0x100] = {
        !            41:          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        !            42:          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        !            43:          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        !            44:          0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  0,  0,  0,  0,  0,  0,
        !            45:          0, 10, 11, 12, 13, 14, 15,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        !            46:          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        !            47:          0, 10, 11, 12, 13, 14, 15,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        !            48: };
1.1       paf        49: 
1.185     moko       50: THREAD_LOCAL Request* thread_request=NULL;
1.162     paf        51: 
                     52: void pa_register_thread_request(Request& r) {
1.185     moko       53:        thread_request=&r;
1.162     paf        54: }
                     55: /// retrives request set by pa_set_request function, useful in contextless places [slow]
                     56: Request& pa_thread_request() {
1.185     moko       57:        return *thread_request;
1.162     paf        58: }
1.176     paf        59: 
1.162     paf        60: 
1.99      paf        61: #ifdef XML
1.101     paf        62: 
1.157     paf        63: class XML_Generic_error_info {
1.173     paf        64: public:/*internal, actually*/
1.166     paf        65:        char buf[MAX_STRING*5];
1.157     paf        66:        size_t used;
                     67: public:
                     68:        XML_Generic_error_info() {
                     69:                buf[used=0]=0;
                     70:        }
1.173     paf        71:        const char* get() {
                     72:                return used? buf: 0;
1.157     paf        73:        }
1.162     paf        74: };
1.101     paf        75: 
1.186     moko       76: THREAD_LOCAL XML_Generic_error_info* xml_generic_error_info = NULL;
1.101     paf        77: 
1.162     paf        78: static void xmlParserGenericErrorFunc(void *  /*ctx*/, const char* msg, ...) { 
1.172     paf        79:        XML_Generic_error_info* p;
1.186     moko       80:        
                     81:        if(!(p=xml_generic_error_info)) // occupy empty one
                     82:                p=xml_generic_error_info=new(PointerFreeGC) XML_Generic_error_info;
1.101     paf        83:                
1.172     paf        84:        va_list args;
                     85:        va_start(args, msg);
                     86:        p->used+=vsnprintf(p->buf+p->used, sizeof(p->buf)-p->used, msg, args);
                     87:        va_end(args);
1.101     paf        88: }
                     89: 
1.102     paf        90: bool xmlHaveGenericErrors() {
1.186     moko       91:        return xml_generic_error_info!=0;
1.102     paf        92: }
                     93: 
1.157     paf        94: const char* xmlGenericErrors() {
1.186     moko       95:        if(XML_Generic_error_info *p=xml_generic_error_info) {
                     96:                xml_generic_error_info=0;
1.173     paf        97:                return p->get();
                     98:        }
1.110     paf        99: 
1.162     paf       100:        return 0; // no errors for our thread_id registered
1.150     paf       101: }
                    102: 
1.195     moko      103: #endif // XML
1.99      paf       104: 
1.157     paf       105: #ifdef XML
                    106: 
                    107: static char *pa_GC_strdup(const char *s) {
                    108:        if(!s)
                    109:                return 0;
                    110: 
                    111:        size_t size=strlen(s)+1;
1.199     moko      112:        char *result=(char *)GC_MALLOC_ATOMIC(size);
1.195     moko      113: 
1.159     paf       114:        if(!result)
1.184     misha     115:                pa_fail_alloc("duplicate XML string",size);
1.159     paf       116: 
1.157     paf       117:        memcpy(result, s, size);
1.170     paf       118: #ifdef PA_DEBUG_XML_GC_MEMORY
                    119:        fprintf(stderr, "pa_GC_strdup(%p=%s, length=%d)=0x%p\n", s, s, size, result);
                    120: #endif
1.157     paf       121:        return result;
                    122: }
                    123: 
                    124: #ifdef PA_DEBUG_XML_GC_MEMORY
1.191     moko      125: 
1.157     paf       126: static void* pa_gc_malloc_log(size_t size){
1.199     moko      127:        void *p=GC_MALLOC(size);
1.157     paf       128:         fprintf(stderr, "pa_gc_malloc_log(%d)=0x%p\n", size, p);
                    129:        return p;
                    130:         
                    131: }
1.191     moko      132: 
1.157     paf       133: static void* pa_gc_malloc_atomic_log(size_t size){
1.199     moko      134:        void *p=GC_MALLOC_ATOMIC(size);
1.157     paf       135:         fprintf(stderr, "pa_gc_malloc_atomic_log(%d)=0x%p\n", size, p);
                    136:        return p;
                    137: }
1.191     moko      138: 
1.157     paf       139: static void* pa_gc_realloc_log(void *ptr, size_t size){
1.199     moko      140:        void *p=GC_REALLOC(ptr, size);
1.157     paf       141:         fprintf(stderr, "pa_gc_realloc_log(0x%p, %d)=0x%p\n", ptr, size, p);
                    142:        return p;
                    143: }
1.191     moko      144: 
1.157     paf       145: static void pa_gc_free_log(void *p){
                    146:         fprintf(stderr, "pa_gc_free_log(0x%p)\n", p);
1.199     moko      147:         GC_FREE(p);
1.157     paf       148: }
1.191     moko      149: 
1.159     paf       150: #else
                    151: 
                    152: inline void *check(void *result, const char *where, size_t size) {
                    153:        if(!result)
1.184     misha     154:                pa_fail_alloc(where, size);
1.159     paf       155:        return result;
                    156: }
1.191     moko      157: 
1.159     paf       158: static void* pa_gc_malloc_nonull(size_t size) { 
1.199     moko      159:        return check(GC_MALLOC(size), "allocate XML compsite memory", size);
1.159     paf       160: }
1.191     moko      161: 
1.159     paf       162: static void* pa_gc_malloc_atomic_nonull(size_t size) { 
1.199     moko      163:        return check(GC_MALLOC_ATOMIC(size), "allocate XML atomic memory", size);
1.159     paf       164: }
1.191     moko      165: 
1.159     paf       166: static void* pa_gc_realloc_nonull(void* ptr, size_t size) { 
1.199     moko      167:        return check(GC_REALLOC(ptr, size), "reallocate XML memory", size);
1.159     paf       168: }
                    169: 
1.191     moko      170: static void pa_gc_free_maybeignore(void* ptr) {
1.199     moko      171:        GC_FREE(ptr);
1.175     paf       172: }
                    173: 
1.157     paf       174: #endif
1.195     moko      175: 
                    176: #endif // XML
1.157     paf       177: 
                    178: void pa_CORD_oom_fn(void) {
1.184     misha     179:        pa_fail_alloc("expand string", 0);
1.157     paf       180: }
                    181: 
                    182: /**
                    183:        @todo gc: libltdl: substitute lt_dlmalloc & co
                    184: */
                    185: static void gc_substitute_memory_management_functions() {
                    186:        // in libxml & libxslt
                    187: #ifdef XML
                    188:        // asking to use GC memory
                    189: #ifdef PA_DEBUG_XML_GC_MEMORY
                    190:        xmlGcMemSetup(
                    191:                /*xmlFreeFunc */pa_gc_free_log,
                    192:                /*xmlMallocFunc */pa_gc_malloc_log,
                    193:                /*xmlMallocFunc */pa_gc_malloc_atomic_log,
                    194:                /*xmlReallocFunc */pa_gc_realloc_log,
                    195:                /*xmlStrdupFunc */pa_GC_strdup);
                    196: #else
                    197:        xmlGcMemSetup(
1.175     paf       198:                /*xmlFreeFunc */pa_gc_free_maybeignore,
1.159     paf       199:                /*xmlMallocFunc */pa_gc_malloc_nonull,
                    200:                /*xmlMallocFunc */pa_gc_malloc_atomic_nonull,
                    201:                /*xmlReallocFunc */pa_gc_realloc_nonull,
1.157     paf       202:                /*xmlStrdupFunc */pa_GC_strdup);
                    203: #endif
1.32      paf       204: 
1.157     paf       205: #endif
1.141     paf       206: 
1.157     paf       207:        // pcre
1.199     moko      208:        pcre_malloc=pa_malloc;
                    209:        pcre_free=pa_free;
1.135     paf       210: 
1.157     paf       211:        // cord
                    212:        CORD_oom_fn=pa_CORD_oom_fn;
                    213: }
1.88      paf       214: 
1.157     paf       215: /**
                    216:        @test hint on one should call this for each thread xmlSubstituteEntitiesDefault(1);
                    217: */
                    218: void pa_globals_init() {
1.203     moko      219:        // init socks
                    220:        pa_socks_init();
                    221: 
                    222:        // global variables
1.164     paf       223:        cache_managers=new Cache_managers;
                    224: 
1.157     paf       225:        // in various libraries
                    226:        gc_substitute_memory_management_functions();
1.101     paf       227: 
1.196     moko      228: #ifdef SYMBOLS_CACHING
                    229:        // symbols cache
                    230:        Symbols::init();
                    231: #endif
                    232: 
1.76      parser    233: #ifdef XML
1.96      paf       234:        // initializing xml libs
                    235: 
1.191     moko      236:        // Register the EXSLT extensions and the test module
1.157     paf       237:        exsltRegisterAll();
                    238:        xsltRegisterTestModule();
                    239:        xmlDefaultSAXHandlerInit();
1.191     moko      240: 
                    241:        // disable CDATA from being built in the document tree
1.157     paf       242:        // never added yet  xmlDefaultSAXHandler.cdataBlock = NULL;
                    243:        
1.191     moko      244:        // Initialization function for the XML parser. This is not reentrant. 
                    245:        // Call once before processing in case of use in multithreaded programs.
1.99      paf       246:        xmlInitParser();
1.107     paf       247: 
                    248:        // 1. this is needed for proper parsing of stylesheets
                    249:        // there were a situation where honest entity ruined innocent xpath compilation
                    250:        // doc says "you sould turn it on on stylesheet load" without deepening into details
                    251:        // 2. when dom tree with entites goes under transform text nodes 
                    252:        // got [erroreosly] cut on first entity occurance
1.109     paf       253:        // --
1.107     paf       254:        // that is why this is:
                    255:        xmlSubstituteEntitiesDefault(1);
1.100     paf       256:        
                    257:        // Bit in the loadsubset context field to tell to do ID/REFs lookups 
                    258:        xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
                    259:        // Bit in the loadsubset context field to tell to do complete the elements attributes lists 
                    260:        // with the ones defaulted from the DTDs 
1.157     paf       261:        xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
1.138     paf       262: 
                    263:        // validate each document after load/create (?)
1.191     moko      264:        // xmlDoValidityCheckingDefaultValue = 1;
1.99      paf       265: 
1.191     moko      266:        // regretfully this not only replaces entities on parse, but also on generate   xmlSubstituteEntitiesDefault(1);
1.105     paf       267:        // never switched this on xmlIndentTreeOutput=1;
1.104     paf       268: 
1.101     paf       269:        xmlSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.102     paf       270:        xsltSetGenericErrorFunc(0, xmlParserGenericErrorFunc);
1.191     moko      271: 
1.195     moko      272: //     FILE *f=fopen("xslt.log", "wt");
1.105     paf       273: //     xsltSetGenericDebugFunc(f/*stderr*/, 0);
1.110     paf       274: 
1.162     paf       275:        pa_xml_io_init();
1.157     paf       276: #endif
1.174     paf       277: }
                    278: 
1.188     moko      279: static bool is_dlinited=false;
                    280: 
1.174     paf       281: void pa_globals_done() {
1.191     moko      282:        delete cache_managers;
                    283:        cache_managers=0;
                    284: 
1.188     moko      285:        if(is_dlinited)
                    286:                lt_dlexit();
1.203     moko      287: 
                    288:        pa_socks_done();
1.188     moko      289: }
                    290: 
                    291: void pa_dlinit() {
                    292:        if(!is_dlinited){
                    293:                if(lt_dlinit())
1.192     moko      294:                        throw Exception(0,0,"preparation for dynamic library loading failed, %s", lt_dlerror());
1.188     moko      295:                is_dlinited=true;
                    296:        }
1.157     paf       297: }
                    298: 
                    299: #ifdef _MSC_VER
                    300: 
1.200     moko      301: #define PREFIX "../../../../win32/"
1.191     moko      302: 
                    303: #ifdef _DEBUG
1.200     moko      304: #define CONFIGURATION "Debug"
                    305: #else
                    306: #define CONFIGURATION "Release"
                    307: #endif
                    308: 
                    309: #ifdef _WIN64
                    310: #define PLATFORM_64 "x64/"
                    311: #define PLATFORM_32 ""
1.191     moko      312: #else
1.200     moko      313: #define PLATFORM_64 ""
                    314: #define PLATFORM_32 "win32/"
1.191     moko      315: #endif
1.96      paf       316: 
1.201     moko      317: #pragma comment(lib, PREFIX "pcre/" PLATFORM_64 CONFIGURATION "/pcre.lib")
1.200     moko      318: 
                    319: #ifndef PA_DEBUG_DISABLE_GC
                    320: 
                    321: #pragma comment(lib, PREFIX "gc/" PLATFORM_64 CONFIGURATION "/gc.lib")
                    322: 
1.195     moko      323: #endif // PA_DEBUG_DISABLE_GC
1.76      parser    324: 
1.191     moko      325: 
                    326: #ifdef XML
                    327: 
1.200     moko      328: #define GNOME_LIBS PREFIX "gnome"
1.181     paf       329: 
1.200     moko      330: #define LIB_XML PREFIX "gnome/libxml2-x.x.x/" PLATFORM_64 PLATFORM_32 CONFIGURATION "/lib/"
                    331: #define LIB_XSLT PREFIX "gnome/libxslt-x.x.x/" PLATFORM_64 PLATFORM_32 CONFIGURATION "/lib/"
1.181     paf       332: 
1.202     moko      333: #ifdef XML_STATIC
1.191     moko      334: #pragma comment(lib, LIB_XML "libxml2_a.lib")
1.202     moko      335: #pragma comment(lib, LIB_XSLT "libxslt_a.lib")
                    336: #pragma comment(lib, LIB_XSLT "libexslt_a.lib")
1.191     moko      337: #else
                    338: #pragma comment(lib, LIB_XML "libxml2.lib")
                    339: #pragma comment(lib, LIB_XSLT "libxslt.lib")
                    340: #pragma comment(lib, LIB_XSLT "libexslt.lib")
                    341: #endif
1.157     paf       342: 
1.195     moko      343: #endif // XML
1.157     paf       344: 
1.197     moko      345: // defines for VS2015 to link with gc/xml libs compiled in the previous VS versions
                    346: #if _MSC_VER >= 1900
                    347: #pragma comment(lib,"legacy_stdio_definitions.lib")
                    348: extern "C" { FILE _iob[3] = { *stdin, *stdout, *stderr }; }
                    349: #endif
                    350: 
1.195     moko      351: #endif // _MSC_VER

E-mail: