Annotation of parser3/src/main/pa_memory.C, revision 1.25

1.2       paf         1: /** @file
                      2:        Parser: memory reference counting classes.
                      3: 
1.24      moko        4:        Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.18      moko        5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.2       paf         6: */
                      7: 
                      8: #include "pa_sapi.h"
                      9: #include "pa_common.h"
1.17      moko       10: #include "pa_threads.h"
1.2       paf        11: 
1.25    ! moko       12: volatile const char * IDENT_PA_MEMORY_C="$Id: pa_memory.C,v 1.24 2026/04/25 13:38:46 moko Exp $" IDENT_PA_MEMORY_H;
1.9       moko       13: 
1.2       paf        14: void *pa_fail_alloc(const char* what, size_t size) {
                     15: #ifdef PA_DEBUG_DISABLE_GC
                     16:        SAPI::die("out of memory (in pa_fail_alloc)");
                     17: #else
1.12      moko       18:        SAPI::die("out of memory: failed to %s %u bytes. heap_used=%u, heap_free=%u, bytes_since_gc=%u, total_bytes=%u",
                     19:                what, size, GC_get_heap_size(), GC_get_free_bytes(), GC_get_bytes_since_gc(), GC_get_total_bytes());
1.2       paf        20: #endif
                     21:        // never reached
                     22:        return 0;
                     23: }
1.12      moko       24: 
1.23      moko       25: #ifndef PA_DEBUG_DISABLE_GC
                     26: // Under Windows pa_fail_alloc was referenced in function GC_default_on_abort in gc.lib(misc.c) and thus should be extern "C"
                     27: extern "C" void *pa_fail_alloc(const char* what) {
1.21      moko       28:        SAPI::die("fatal memory error: %s. heap_used=%u, heap_free=%u, bytes_since_gc=%u, total_bytes=%u",
1.23      moko       29:                what ? what : "pre-exit", GC_get_heap_size(), GC_get_free_bytes(), GC_get_bytes_since_gc(), GC_get_total_bytes());
1.21      moko       30:        // never reached
                     31:        return 0;
                     32: }
                     33: #endif
                     34: 
1.17      moko       35: int pa_free_space_divisor = 0;
                     36: 
                     37: void pa_gc_collect(bool forced){
                     38: #ifndef PA_DEBUG_DISABLE_GC
                     39:     int divisor = pa_free_space_divisor; // as it can change during collect in multithreaded enviroment
                     40:     if(!divisor) GC_enable();
                     41:     if(!divisor || forced) GC_gcollect();
                     42:     if(!divisor) GC_disable();
                     43: #endif
                     44: }
                     45: 
                     46: void pa_gc_set_free_space_divisor(int divisor){
                     47: #ifndef PA_DEBUG_DISABLE_GC
                     48:        if(divisor != pa_free_space_divisor){
                     49:                SYNCHRONIZED;
                     50:                if(pa_free_space_divisor){
                     51:                        if(!divisor) GC_disable();
                     52:                } else {
                     53:                        if(divisor) GC_enable();
                     54:                }
                     55:                if(divisor)
                     56:                        GC_set_free_space_divisor(divisor);
                     57:                pa_free_space_divisor = divisor;
                     58:        }
                     59: #endif
                     60: }
1.25    ! moko       61: 
        !            62: static THREAD_LOCAL Temp_gc_references* thread_gc_references=NULL; // this is stack object, GC won't collect it
        !            63: 
        !            64: Temp_gc_references::Temp_gc_references(): index(0), previous(thread_gc_references) {
        !            65:        memset(items, 0, sizeof(items));
        !            66:        thread_gc_references=this;
        !            67: }
        !            68: 
        !            69: Temp_gc_references::~Temp_gc_references() {
        !            70:        thread_gc_references=previous;
        !            71: }
        !            72: 
        !            73: const char* pa_gc_reference(const char* data) {
        !            74:        if(data && thread_gc_references)
        !            75:                thread_gc_references->keep(data);
        !            76:        return data;
        !            77: }
        !            78: 
        !            79: const String* pa_gc_reference(const String* data) {
        !            80:        if(data && thread_gc_references)
        !            81:                thread_gc_references->keep(data);
        !            82:        return data;
        !            83: }

E-mail: