Annotation of parser3/src/main/pa_memory.C, revision 1.1.2.5.2.9
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.9! paf 8: static const char* IDENT_MEMORY_C="$Date: 2003/04/01 12:05:46 $";
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;
54:
55: return ptr+HEADTAIL_SIZE;
56: }
57: static void* check_return_debug(void* auser_ptr) {
58: char* user_ptr=(char*)auser_ptr;
59: char* ptr=user_ptr-HEADTAIL_SIZE;
60:
61: size_t raw_size=GC_size(ptr);
62: if(raw_size==0)
1.1.2.5.2.9! paf 63: bug();
1.1.2.5.2.8 paf 64:
65: size_t pure_size=raw_size;
66: while(ptr[pure_size-1]==PAD_MARK)
67: pure_size--;
68:
69: if(memcmp(ptr, &BEFORE_MARK, HEADTAIL_SIZE)!=0)
1.1.2.5.2.9! paf 70: bug();
1.1.2.5.2.8 paf 71: if(memcmp(ptr+pure_size-HEADTAIL_SIZE, &AFTER_MARK, HEADTAIL_SIZE)!=0)
1.1.2.5.2.9! paf 72: bug();
1.1.2.5.2.8 paf 73:
74: return ptr;
75: }
76:
77:
78: void* pa_gc_malloc(size_t size) {
79: size=debug_size(size);
80: return fill_return_user(GC_MALLOC(size), size);
81: }
82:
83: void* pa_gc_malloc_atomic(size_t size) {
84: size=debug_size(size);
85: return fill_return_user(GC_MALLOC_ATOMIC(size), size);
86: }
87:
88: void* pa_gc_realloc(void* user_ptr, size_t size) {
89: if(!GC_is_visible(user_ptr))
1.1.2.5.2.9! paf 90: bug();
1.1.2.5.2.8 paf 91: //printf("realloc: 0x%p -> %u\n", ptr, size);
92: size=debug_size(size);
93: return fill_return_user(
94: GC_realloc(
95: check_return_debug(user_ptr),
96: size),
97: size);
98: }
99: void pa_gc_free(void* ptr) {
100: // ignore free
101: }
1.1.2.5.2.9! paf 102:
1.1.2.5.2.8 paf 103:
104: #endif
E-mail: