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

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.10! paf         8: static const char* IDENT_MEMORY_C="$Date: 2003/04/01 13:48:32 $";
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: 
1.1.2.5.2.9  paf        33: void bug() {}
                     34: 
1.1.2.5.2.8  paf        35: const size_t HEADTAIL_SIZE=4;
                     36: 
                     37: static size_t debug_size(size_t user_size) {
                     38:        return user_size+HEADTAIL_SIZE*2;
                     39: }
                     40: 
                     41: const int BEFORE_MARK=0xBEF0BEF0;
                     42: const int AFTER_MARK=0xAFEEAFEE;
                     43: const char PAD_MARK='\xAD';
                     44: 
                     45: static void* fill_return_user(void* aptr, size_t pure_size) {
                     46:        char* ptr=(char*)aptr;
                     47:        memcpy(ptr, &BEFORE_MARK, HEADTAIL_SIZE);
                     48:        memcpy(ptr+pure_size-HEADTAIL_SIZE, &AFTER_MARK, HEADTAIL_SIZE);
                     49: 
                     50:        // pAD
                     51:        size_t raw_size=GC_size(aptr);
                     52:        for(size_t i=pure_size; i<raw_size; i++)
                     53:                ptr[i]=PAD_MARK;
1.1.2.5.2.10! paf        54: 
        !            55:        //if(ptr>=(char*)0x01c357e40 && ptr<=((char*)0x01c357e4+100))
        !            56:                //printf("valid:0x%p\n", ptr);
1.1.2.5.2.8  paf        57: 
                     58:        return ptr+HEADTAIL_SIZE;
                     59: }
                     60: static void* check_return_debug(void* auser_ptr) {
                     61:        char* user_ptr=(char*)auser_ptr;
                     62:        char* ptr=user_ptr-HEADTAIL_SIZE;
                     63: 
                     64:        size_t raw_size=GC_size(ptr);
                     65:        if(raw_size==0)
1.1.2.5.2.9  paf        66:                bug();
1.1.2.5.2.8  paf        67: 
                     68:        size_t pure_size=raw_size;
                     69:        while(ptr[pure_size-1]==PAD_MARK)
                     70:                pure_size--;
                     71: 
                     72:        if(memcmp(ptr, &BEFORE_MARK, HEADTAIL_SIZE)!=0)
1.1.2.5.2.9  paf        73:                bug();
1.1.2.5.2.8  paf        74:        if(memcmp(ptr+pure_size-HEADTAIL_SIZE, &AFTER_MARK, HEADTAIL_SIZE)!=0)
1.1.2.5.2.9  paf        75:                bug();
1.1.2.5.2.8  paf        76: 
                     77:        return ptr;
                     78: }
                     79: 
                     80: 
                     81: void* pa_gc_malloc(size_t size) {
                     82:        size=debug_size(size);
                     83:        return fill_return_user(GC_MALLOC(size), size);
                     84: }
                     85: 
                     86: void* pa_gc_malloc_atomic(size_t size) {
                     87:        size=debug_size(size);
                     88:        return fill_return_user(GC_MALLOC_ATOMIC(size), size);
                     89: }
                     90: 
                     91: void* pa_gc_realloc(void* user_ptr, size_t size) {
                     92:        if(!GC_is_visible(user_ptr))
1.1.2.5.2.9  paf        93:                bug();
1.1.2.5.2.8  paf        94:        //printf("realloc: 0x%p -> %u\n", ptr, size);
                     95:        size=debug_size(size);
                     96:        return fill_return_user(
                     97:                GC_realloc(
                     98:                        check_return_debug(user_ptr), 
                     99:                        size),
                    100:                size);
                    101: }
                    102: void pa_gc_free(void* ptr) {
                    103:        // ignore free
                    104: }
1.1.2.5.2.9  paf       105: 
1.1.2.5.2.8  paf       106: 
                    107: #endif

E-mail: