Annotation of parser3/src/main/pa_memory.C, revision 1.8
1.2 paf 1: /** @file
2: Parser: memory reference counting classes.
3:
1.6 paf 4: Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com)
1.2 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
6: */
7:
1.8 ! paf 8: static const char * const IDENT_MEMORY_C="$Date: 2005/12/16 10:15:12 $";
1.2 paf 9:
10: #include "pa_sapi.h"
11: #include "pa_common.h"
12:
13: void *pa_fail_alloc(const char* what, size_t size) {
14: #ifdef PA_DEBUG_DISABLE_GC
15: SAPI::die("out of memory (in pa_fail_alloc)");
16: #else
17: SAPI::die("out of memory: failed to %s %u bytes. "
1.4 paf 18: "heap_used=%u, heap_free=%u, bytes_since_gc=%u, total_bytes=%u",
1.2 paf 19: what, size,
20: GC_get_heap_size(),
21: GC_get_free_bytes(),
22: GC_get_bytes_since_gc(),
23: GC_get_total_bytes()
24: );
25: #endif
26: // never reached
27: return 0;
28: }
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:
1.8 ! paf 42: #error this code is known not to work: it ruins something with nonGC_DEBUG realloc
! 43:
1.7 paf 44: static void* fill_return_user(void* aptr, size_t /*pure_size*/) {
1.2 paf 45: char* ptr=(char*)aptr;
46: memcpy(ptr, &BEFORE_MARK, HEADTAIL_SIZE);
1.7 paf 47: // memcpy(ptr+HEADTAIL_SIZE+pure_size, &AFTER_MARK, HEADTAIL_SIZE);
1.2 paf 48:
49: //if(ptr>=(char*)0x01c357e40 && ptr<=((char*)0x01c357e4+100))
50: //printf("valid:0x%p\n", ptr);
51:
52: return ptr+HEADTAIL_SIZE;
53: }
54: static void* check_return_debug(void* auser_ptr) {
55: char* user_ptr=(char*)auser_ptr;
56: char* ptr=user_ptr-HEADTAIL_SIZE;
57:
1.7 paf 58: assert(GC_size(ptr));
1.2 paf 59: assert(memcmp(ptr, &BEFORE_MARK, HEADTAIL_SIZE)==0);
1.7 paf 60: // don't know real size. can put it to head someday
61: // assert(memcmp(ptr+HEADTAIL_SIZE+pure_size, &AFTER_MARK, HEADTAIL_SIZE)==0);
1.2 paf 62:
63: return ptr;
64: }
65:
66:
67: void* pa_gc_malloc(size_t size) {
68: size=debug_size(size);
69: return fill_return_user(GC_MALLOC(size), size);
70: }
71:
72: void* pa_gc_malloc_atomic(size_t size) {
73: size=debug_size(size);
74: return fill_return_user(GC_MALLOC_ATOMIC(size), size);
75: }
76:
77: void* pa_gc_realloc(void* user_ptr, size_t size) {
78: GC_is_visible(user_ptr);
79:
80: //printf("realloc: 0x%p -> %u\n", ptr, size);
81: size=debug_size(size);
82: return fill_return_user(
83: GC_realloc(
84: check_return_debug(user_ptr),
85: size),
86: size);
87: }
1.7 paf 88: void pa_gc_free(void* user_ptr) {
89: GC_is_visible(user_ptr);
90: check_return_debug(user_ptr);
1.2 paf 91: // ignore free
92: }
93:
94:
95: #endif
E-mail: