Debian Codegraph @

rpython/translator/c/src/mem.h

raw symbols SCIP
1
2 /************************************************************/
3 /*** C header subsection: operations on LowLevelTypes ***/
4
5 #include <string.h>
6
7 /* used by rpython.rlib.rstack */
8 #define OP_STACK_CURRENT(r) r = (Signed)&r
9
10
11 #define OP_RAW_MALLOC(size, zero, result) { \
12 if (zero) \
13 result = calloc(size, 1); \
14 else \
15 result = malloc(size); \
16 if (result != NULL) { \
17 COUNT_MALLOC; \
18 } \
19 }
20
21 #define OP_RAW_FREE(p, r) free(p); COUNT_FREE;
22
23 #define OP_RAW_MEMCLEAR(p, size, r) memset((void*)p, 0, size)
24 #define OP_RAW_MEMSET(p, byte, size, r) memset((void*)p, byte, size)
25
26 #define OP_RAW_MALLOC_USAGE(size, r) r = size
27
28 #if defined(MS_WINDOWS) && !defined(__MINGW32__)
29 #define alloca _alloca
30 #endif
31
32 #define OP_RAW_MEMCOPY(x,y,size,r) memcpy(y,x,size);
33 #define OP_RAW_MEMMOVE(x,y,size,r) memmove(y,x,size);
34
35 /************************************************************/
36
37 #define OP_FREE(p) OP_RAW_FREE(p, do_not_use)
38
39 #ifndef COUNT_OP_MALLOCS
40
41 #define COUNT_MALLOC /* nothing */
42 #define COUNT_FREE /* nothing */
43 #define pypy_malloc_counters_results() /* nothing */
44
45 #else /* COUNT_OP_MALLOCS */
46
47 static int count_mallocs=0, count_frees=0;
48
49 #define COUNT_MALLOC count_mallocs++
50 #define COUNT_FREE count_frees++
51
52 #define pypy_malloc_counters_results() \
53 printf("MALLOC COUNTERS: %d %d\n", count_mallocs, count_frees)
54
55 #endif /* COUNT_OP_MALLOCS */
56
57
58 #ifndef RPY_COUNT_FIELDACCESS
59
60 #define pypy_print_field_stats() /* nothing */
61
62 #else /* RPY_COUNT_FIELDACCESS */
63
64 #define pypy_print_field_stats() _pypy_print_field_stats()
65
66 #endif /* RPY_COUNT_FIELDACCESS */
67
68 /*** tracking raw mallocs and frees for debugging ***/
69
70 #ifndef RPY_ASSERT
71
72 # define OP_TRACK_ALLOC_START(addr, r) /* nothing */
73 # define OP_TRACK_ALLOC_STOP(addr, r) /* nothing */
74 # define pypy_debug_alloc_results() /* nothing */
75
76 #else /* RPY_ASSERT */
77
78 # define OP_TRACK_ALLOC_START(addr, r) pypy_debug_alloc_start(addr, \
79 __FUNCTION__)
80 # define OP_TRACK_ALLOC_STOP(addr, r) pypy_debug_alloc_stop(addr)
81
82 RPY_EXTERN void pypy_debug_alloc_start(void*, const char*);
83 RPY_EXTERN void pypy_debug_alloc_stop(void*);
84 RPY_EXTERN void pypy_debug_alloc_results(void);
85
86 #endif /* RPY_ASSERT */
87
88 /* for Boehm GC */
89
90 #ifdef PYPY_USING_BOEHM_GC
91
92 #define BOEHM_MALLOC_0_0 GC_MALLOC
93 #define BOEHM_MALLOC_1_0 GC_MALLOC_ATOMIC
94 #define BOEHM_MALLOC_0_1 GC_MALLOC
95 #define BOEHM_MALLOC_1_1 GC_MALLOC_ATOMIC
96 /* #define BOEHM_MALLOC_0_1 GC_MALLOC_IGNORE_OFF_PAGE */
97 /* #define BOEHM_MALLOC_1_1 GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE */
98
99 #define OP_BOEHM_ZERO_MALLOC(size, r, restype, is_atomic, is_varsize) { \
100 r = (restype) BOEHM_MALLOC_ ## is_atomic ## _ ## is_varsize (size); \
101 if (r && is_atomic) /* the non-atomic versions return cleared memory */ \
102 memset((void*) r, 0, size); \
103 }
104
105 #define OP_BOEHM_DISAPPEARING_LINK(link, obj, r) \
106 if (GC_base(obj) == NULL) \
107 ; /* 'obj' is probably a prebuilt object - it makes no */ \
108 /* sense to register it then, and it crashes Boehm in */ \
109 /* quite obscure ways */ \
110 else \
111 GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj)
112
113 RPY_EXTERN int boehm_gc_finalizer_lock;
114 RPY_EXTERN void boehm_gc_startup_code(void);
115 RPY_EXTERN void boehm_gc_finalizer_notifier(void);
116 struct boehm_fq_s;
117 RPY_EXTERN struct boehm_fq_s *boehm_fq_queues[];
118 RPY_EXTERN void (*boehm_fq_trigger[])(void);
119 RPY_EXTERN void boehm_fq_register(struct boehm_fq_s **, void *);
120 RPY_EXTERN void *boehm_fq_next_dead(struct boehm_fq_s **);
121
122 #define OP_GC__DISABLE_FINALIZERS(r) boehm_gc_finalizer_lock++
123 #define OP_GC__ENABLE_FINALIZERS(r) (boehm_gc_finalizer_lock--, \
124 boehm_gc_finalizer_notifier())
125 #define OP_GC__DISABLE(r) /* nothing */
126 #define OP_GC__ENABLE(r) /* nothing */
127 #define OP_GC__ISENABLED(r) r = 1
128
129 #define OP_BOEHM_FQ_REGISTER(tagindex, obj, r) \
130 boehm_fq_register(boehm_fq_queues + tagindex, obj)
131 #define OP_BOEHM_FQ_NEXT_DEAD(tagindex, r) \
132 r = boehm_fq_next_dead(boehm_fq_queues + tagindex)
133
134 #endif /* PYPY_USING_BOEHM_GC */
135
136
137 #ifdef PYPY_USING_NO_GC_AT_ALL
138 #define OP_BOEHM_ZERO_MALLOC(size, r, restype, is_atomic, is_varsize) \
139 r = (restype) calloc(1, size);
140 #define OP_BOEHM_DISAPPEARING_LINK(link, obj, r) /* nothing */
141 #define OP_GC__DISABLE_FINALIZERS(r) /* nothing */
142 #define OP_GC__ENABLE_FINALIZERS(r) /* nothing */
143 #define OP_GC__DISABLE(r) /* nothing */
144 #define OP_GC__ENABLE(r) /* nothing */
145 #define GC_REGISTER_FINALIZER(a, b, c, d, e) /* nothing */
146 #define GC_gcollect() /* nothing */
147 #define GC_set_max_heap_size(a) /* nothing */
148 #define OP_GC_FQ_REGISTER(tag, obj, r) /* nothing */
149 #define OP_GC_FQ_NEXT_DEAD(tag, r) (r = NULL)
150 #endif
151
152 #if (defined(PYPY_USING_BOEHM_GC) || defined(PYPY_USING_NO_GC_AT_ALL)) && !defined(PYPY_BOEHM_WITH_HEADER)
153 # define RPY_SIZE_OF_GCHEADER 0
154 #else
155 # define RPY_SIZE_OF_GCHEADER sizeof(struct pypy_header0)
156 #endif
157
158 /************************************************************/
159 /* weakref support */
160
161 #define OP_CAST_PTR_TO_WEAKREFPTR(x, r) r = x
162 #define OP_CAST_WEAKREFPTR_TO_PTR(x, r) r = x
163
164 /************************************************************/
165 /* dummy version of these operations, e.g. with Boehm */
166
167 #define OP_GC_GET_RPY_ROOTS(r) r = 0
168 #define OP_GC_GET_RPY_REFERENTS(x, r) r = 0
169 #define OP_GC_GET_RPY_MEMORY_USAGE(x, r) r = -1
170 #define OP_GC_GET_RPY_TYPE_INDEX(x, r) r = -1
171 #define OP_GC_IS_RPY_INSTANCE(x, r) r = 0
172 #define OP_GC_DUMP_RPY_HEAP(fd, r) r = 0
173 #define OP_GC_SET_EXTRA_THRESHOLD(x, r) /* nothing */
174 #define OP_GC_IGNORE_FINALIZER(x, r) /* nothing */
175 #define OP_GC_INCREASE_ROOT_STACK_DEPTH(x, r) /* nothing */
176
177 /****************************/
178 /* misc stuff */
179 /****************************/
180
181 #ifndef _MSC_VER
182 # define pypy_asm_keepalive(v) asm volatile ("/* keepalive %0 */" : : \
183 "g" (v))
184 #else
185 # ifndef _WIN64
186 # define pypy_asm_keepalive(v) __asm { }
187 # else
188 /* is there something cheaper? */
189 # define pypy_asm_keepalive(v) _ReadWriteBarrier();
190 # endif
191 #endif