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

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

E-mail: