| 1 |
#include "common_header.h" |
| 2 |
#include "src/support.h" |
| 3 |
#include <stdlib.h> |
| 4 |
#include <stdio.h> |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#ifdef RPY_ASSERT |
| 9 |
|
| 10 |
struct pypy_debug_alloc_s { |
| 11 |
struct pypy_debug_alloc_s *next; |
| 12 |
void *addr; |
| 13 |
const char *funcname; |
| 14 |
}; |
| 15 |
|
| 16 |
static struct pypy_debug_alloc_s *pypy_debug_alloc_list = NULL; |
| 17 |
|
| 18 |
RPY_EXTERN |
| 19 |
void pypy_debug_alloc_start(void *addr, const char *funcname) |
| 20 |
{ |
| 21 |
struct pypy_debug_alloc_s *p = malloc(sizeof(struct pypy_debug_alloc_s)); |
| 22 |
RPyAssert(p, "out of memory"); |
| 23 |
p->next = pypy_debug_alloc_list; |
| 24 |
p->addr = addr; |
| 25 |
p->funcname = funcname; |
| 26 |
pypy_debug_alloc_list = p; |
| 27 |
} |
| 28 |
|
| 29 |
RPY_EXTERN |
| 30 |
void pypy_debug_alloc_stop(void *addr) |
| 31 |
{ |
| 32 |
struct pypy_debug_alloc_s **p; |
| 33 |
if (!addr) |
| 34 |
return; |
| 35 |
for (p = &pypy_debug_alloc_list; *p; p = &((*p)->next)) |
| 36 |
if ((*p)->addr == addr) |
| 37 |
{ |
| 38 |
struct pypy_debug_alloc_s *dying; |
| 39 |
dying = *p; |
| 40 |
*p = dying->next; |
| 41 |
free(dying); |
| 42 |
return; |
| 43 |
} |
| 44 |
RPyAssert(0, "free() of a never-malloc()ed object"); |
| 45 |
} |
| 46 |
|
| 47 |
RPY_EXTERN |
| 48 |
void pypy_debug_alloc_results(void) |
| 49 |
{ |
| 50 |
Signed count = 0; |
| 51 |
struct pypy_debug_alloc_s *p; |
| 52 |
for (p = pypy_debug_alloc_list; p; p = p->next) |
| 53 |
count++; |
| 54 |
if (count > 0) |
| 55 |
{ |
| 56 |
char *env = getenv("PYPY_ALLOC"); |
| 57 |
fprintf(stderr, "mem.c: %ld mallocs left", count); |
| 58 |
if (env && *env) |
| 59 |
{ |
| 60 |
fprintf(stderr, " (most recent first):\n"); |
| 61 |
for (p = pypy_debug_alloc_list; p; p = p->next) |
| 62 |
fprintf(stderr, " %p %s\n", p->addr, p->funcname); |
| 63 |
} |
| 64 |
else |
| 65 |
fprintf(stderr, " (use PYPY_ALLOC=1 to see the list)\n"); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
#endif |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#ifdef PYPY_USING_BOEHM_GC |
| 75 |
|
| 76 |
struct boehm_fq_s { |
| 77 |
void *obj; |
| 78 |
struct boehm_fq_s *next; |
| 79 |
}; |
| 80 |
RPY_EXTERN void (*boehm_fq_trigger[])(void); |
| 81 |
|
| 82 |
int boehm_gc_finalizer_lock = 0; |
| 83 |
void boehm_gc_finalizer_notifier(void); |
| 84 |
|
| 85 |
#ifndef RPY_REVERSE_DEBUGGER |
| 86 |
void boehm_gc_finalizer_notifier(void) |
| 87 |
{ |
| 88 |
int i; |
| 89 |
|
| 90 |
boehm_gc_finalizer_lock++; |
| 91 |
while (GC_should_invoke_finalizers()) { |
| 92 |
if (boehm_gc_finalizer_lock > 1) { |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
break; |
| 97 |
} |
| 98 |
GC_invoke_finalizers(); |
| 99 |
} |
| 100 |
|
| 101 |
i = 0; |
| 102 |
while (boehm_fq_trigger[i]) |
| 103 |
boehm_fq_trigger[i++](); |
| 104 |
|
| 105 |
boehm_gc_finalizer_lock--; |
| 106 |
} |
| 107 |
#else |
| 108 |
|
| 109 |
RPY_EXTERN void *rpy_reverse_db_next_dead(void *); |
| 110 |
RPY_EXTERN int rpy_reverse_db_fq_register(void *); |
| 111 |
#endif |
| 112 |
|
| 113 |
static void mem_boehm_ignore(char *msg, GC_word arg) |
| 114 |
{ |
| 115 |
} |
| 116 |
|
| 117 |
void boehm_gc_startup_code(void) |
| 118 |
{ |
| 119 |
GC_init(); |
| 120 |
GC_finalizer_notifier = &boehm_gc_finalizer_notifier; |
| 121 |
GC_finalize_on_demand = 1; |
| 122 |
GC_set_warn_proc(mem_boehm_ignore); |
| 123 |
} |
| 124 |
|
| 125 |
static void boehm_fq_callback(void *obj, void *rawfqueue) |
| 126 |
{ |
| 127 |
struct boehm_fq_s **fqueue = rawfqueue; |
| 128 |
struct boehm_fq_s *node = GC_malloc(sizeof(void *) * 2); |
| 129 |
if (!node) |
| 130 |
return; |
| 131 |
node->obj = obj; |
| 132 |
node->next = *fqueue; |
| 133 |
*fqueue = node; |
| 134 |
} |
| 135 |
|
| 136 |
void boehm_fq_register(struct boehm_fq_s **fqueue, void *obj) |
| 137 |
{ |
| 138 |
#ifdef RPY_REVERSE_DEBUGGER |
| 139 |
|
| 140 |
if (rpy_reverse_db_fq_register(obj)) |
| 141 |
return; |
| 142 |
#endif |
| 143 |
GC_REGISTER_FINALIZER(obj, boehm_fq_callback, fqueue, NULL, NULL); |
| 144 |
} |
| 145 |
|
| 146 |
void *boehm_fq_next_dead(struct boehm_fq_s **fqueue) |
| 147 |
{ |
| 148 |
struct boehm_fq_s *node = *fqueue; |
| 149 |
void *result; |
| 150 |
if (node != NULL) { |
| 151 |
*fqueue = node->next; |
| 152 |
result = node->obj; |
| 153 |
} |
| 154 |
else |
| 155 |
result = NULL; |
| 156 |
#ifdef RPY_REVERSE_DEBUGGER |
| 157 |
result = rpy_reverse_db_next_dead(result); |
| 158 |
#endif |
| 159 |
return result; |
| 160 |
} |
| 161 |
#endif |