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

1.1.2.1   paf         1: /** @file
                      2:        Parser: memory reference counting classes.
                      3: 
                      4:        Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
                      5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
                      6: */
                      7: 
1.1.2.5.2.8! paf         8: static const char* IDENT_MEMORY_C="$Date: 2003/03/27 15:33:45 $";
1.1.2.1   paf         9: 
                     10: #include "pa_sapi.h"
1.1.2.5.2.4  paf        11: #include "pa_common.h"
1.1.2.5   paf        12: 
1.1.2.5.2.2  paf        13: void *pa_fail_alloc(const char* what, size_t size) {
1.1.2.5.2.7  paf        14: #ifdef PA_DEBUG_DISABLE_GC
                     15:        SAPI::abort("xx");
                     16: #else
1.1.2.5.2.3  paf        17:        SAPI::abort("out of memory: failed to %s %u bytes. "
                     18:                "heap_used=%lu, heap_free=%lu, bytes_since_gc=%lu, total_bytes=%lu",
                     19:                what, size,
                     20:                GC_get_heap_size(),
                     21:                GC_get_free_bytes(),
                     22:                GC_get_bytes_since_gc(),
                     23:                GC_get_total_bytes()
1.1.2.5.2.7  paf        24:                );
                     25: #endif
1.1.2.1   paf        26:        // never reached
                     27:        return 0;
                     28: }
1.1.2.5.2.8! paf        29: 
        !            30: 
        !            31: #ifdef PA_DEBUG_GC_MEMORY
        !            32: 
        !            33: const size_t HEADTAIL_SIZE=4;
        !            34: 
        !            35: static size_t debug_size(size_t user_size) {
        !            36:        return user_size+HEADTAIL_SIZE*2;
        !            37: }
        !            38: 
        !            39: const int BEFORE_MARK=0xBEF0BEF0;
        !            40: const int AFTER_MARK=0xAFEEAFEE;
        !            41: const char PAD_MARK='\xAD';
        !            42: 
        !            43: static void* fill_return_user(void* aptr, size_t pure_size) {
        !            44:        char* ptr=(char*)aptr;
        !            45:        memcpy(ptr, &BEFORE_MARK, HEADTAIL_SIZE);
        !            46:        memcpy(ptr+pure_size-HEADTAIL_SIZE, &AFTER_MARK, HEADTAIL_SIZE);
        !            47: 
        !            48:        // pAD
        !            49:        size_t raw_size=GC_size(aptr);
        !            50:        for(size_t i=pure_size; i<raw_size; i++)
        !            51:                ptr[i]=PAD_MARK;
        !            52: 
        !            53:        return ptr+HEADTAIL_SIZE;
        !            54: }
        !            55: static void* check_return_debug(void* auser_ptr) {
        !            56:        char* user_ptr=(char*)auser_ptr;
        !            57:        char* ptr=user_ptr-HEADTAIL_SIZE;
        !            58: 
        !            59:        size_t raw_size=GC_size(ptr);
        !            60:        if(raw_size==0)
        !            61:                _asm int 3;
        !            62: 
        !            63:        size_t pure_size=raw_size;
        !            64:        while(ptr[pure_size-1]==PAD_MARK)
        !            65:                pure_size--;
        !            66: 
        !            67:        if(memcmp(ptr, &BEFORE_MARK, HEADTAIL_SIZE)!=0)
        !            68:                _asm int 3;
        !            69:        if(memcmp(ptr+pure_size-HEADTAIL_SIZE, &AFTER_MARK, HEADTAIL_SIZE)!=0)
        !            70:                _asm int 3;
        !            71: 
        !            72:        return ptr;
        !            73: }
        !            74: 
        !            75: 
        !            76: void* pa_gc_malloc(size_t size) {
        !            77:        size=debug_size(size);
        !            78:        return fill_return_user(GC_MALLOC(size), size);
        !            79: }
        !            80: 
        !            81: void* pa_gc_malloc_atomic(size_t size) {
        !            82:        size=debug_size(size);
        !            83:        return fill_return_user(GC_MALLOC_ATOMIC(size), size);
        !            84: }
        !            85: 
        !            86: void* pa_gc_realloc(void* user_ptr, size_t size) {
        !            87:        if(!GC_is_visible(user_ptr))
        !            88:                _asm int 3;
        !            89:        //printf("realloc: 0x%p -> %u\n", ptr, size);
        !            90:        size=debug_size(size);
        !            91:        return fill_return_user(
        !            92:                GC_realloc(
        !            93:                        check_return_debug(user_ptr), 
        !            94:                        size),
        !            95:                size);
        !            96: }
        !            97: void pa_gc_free(void* ptr) {
        !            98:        // ignore free
        !            99: }
        !           100: 
        !           101: #endif

E-mail: