1
1
#include " rust_internal.h"
2
2
#include " memory_region.h"
3
3
4
- // NB: please do not commit code with this uncommented. It's
5
- // hugely expensive and should only be used as a last resort.
6
- //
7
- // #define TRACK_ALLOCATIONS
8
-
9
- #define PTR_SIZE (sizeof (void *))
10
- #define ALIGN_PTR (x ) (((x)+PTR_SIZE-1 )/PTR_SIZE*PTR_SIZE)
11
- #define HEADER_SIZE ALIGN_PTR (sizeof (alloc_header))
12
- #define MAGIC 0xbadc0ffe
4
+ #if RUSTRT_TRACK_ALLOCATIONS >= 1
5
+ # define PTR_SIZE (sizeof (void *))
6
+ # define ALIGN_PTR (x ) (((x)+PTR_SIZE-1 )/PTR_SIZE*PTR_SIZE)
7
+ # define HEADER_SIZE ALIGN_PTR (sizeof (alloc_header))
8
+ # define MAGIC 0xbadc0ffe
9
+ #else
10
+ # define HEADER_SIZE 0
11
+ #endif
13
12
14
13
memory_region::alloc_header *memory_region::get_header (void *mem) {
15
14
return (alloc_header *)((char *)mem - HEADER_SIZE);
16
15
}
17
16
18
17
void *memory_region::get_data (alloc_header *ptr) {
19
- assert (ptr->magic == MAGIC);
20
18
return (void *)((char *)ptr + HEADER_SIZE);
21
19
}
22
20
@@ -46,7 +44,11 @@ void memory_region::free(void *mem) {
46
44
// printf("free: ptr 0x%" PRIxPTR" region=%p\n", (uintptr_t) mem, this);
47
45
if (!mem) { return ; }
48
46
alloc_header *alloc = get_header (mem);
47
+
48
+ # if RUSTRT_TRACK_ALLOCATIONS >= 1
49
49
assert (alloc->magic == MAGIC);
50
+ # endif
51
+
50
52
if (_live_allocations < 1 ) {
51
53
_srv->fatal (" live_allocs < 1" , __FILE__, __LINE__, " " );
52
54
}
@@ -56,18 +58,22 @@ void memory_region::free(void *mem) {
56
58
}
57
59
58
60
void *
59
- memory_region::realloc (void *mem, size_t size ) {
61
+ memory_region::realloc (void *mem, size_t orig_size ) {
60
62
if (_synchronized) { _lock.lock (); }
61
63
if (!mem) {
62
64
add_alloc ();
63
65
}
64
- size_t old_size = size;
65
- size += HEADER_SIZE;
66
+
66
67
alloc_header *alloc = get_header (mem);
67
- assert (alloc->magic == MAGIC);
68
- alloc->size = old_size;
68
+ size_t size = orig_size + HEADER_SIZE;
69
69
alloc_header *newMem = (alloc_header *)_srv->realloc (alloc, size);
70
- #ifdef TRACK_ALLOCATIONS
70
+
71
+ # if RUSTRT_TRACK_ALLOCATIONS >= 1
72
+ assert (alloc->magic == MAGIC);
73
+ newMem->size = orig_size;
74
+ # endif
75
+
76
+ # if RUSTRT_TRACK_ALLOCATIONS >= 2
71
77
if (_allocation_list[newMem->index ] != alloc) {
72
78
printf (" at index %d, found %p, expected %p\n " ,
73
79
alloc->index , _allocation_list[alloc->index ], alloc);
@@ -80,7 +86,8 @@ memory_region::realloc(void *mem, size_t size) {
80
86
// printf("realloc: stored %p at index %d, replacing %p\n",
81
87
// newMem, index, mem);
82
88
}
83
- #endif
89
+ # endif
90
+
84
91
if (_synchronized) { _lock.unlock (); }
85
92
return get_data (newMem);
86
93
}
@@ -90,10 +97,13 @@ memory_region::malloc(size_t size, const char *tag, bool zero) {
90
97
size_t old_size = size;
91
98
size += HEADER_SIZE;
92
99
alloc_header *mem = (alloc_header *)_srv->malloc (size);
100
+
101
+ # if RUSTRT_TRACK_ALLOCATIONS >= 1
93
102
mem->magic = MAGIC;
94
103
mem->tag = tag;
95
104
mem->index = -1 ;
96
105
mem->size = old_size;
106
+ # endif
97
107
98
108
void *data = get_data (mem);
99
109
claim_alloc (data);
@@ -122,7 +132,8 @@ memory_region::~memory_region() {
122
132
" leaked memory in rust main loop (%d objects)" ,
123
133
_live_allocations);
124
134
}
125
- #ifdef TRACK_ALLOCATIONS
135
+
136
+ # if RUSTRT_TRACK_ALLOCATIONS >= 2
126
137
if (_detailed_leaks) {
127
138
int leak_count = 0 ;
128
139
for (size_t i = 0 ; i < _allocation_list.size (); i++) {
@@ -136,7 +147,8 @@ memory_region::~memory_region() {
136
147
}
137
148
assert (leak_count == _live_allocations);
138
149
}
139
- #endif
150
+ # endif
151
+
140
152
if (_live_allocations > 0 ) {
141
153
_srv->fatal (msg, __FILE__, __LINE__,
142
154
" %d objects" , _live_allocations);
@@ -146,10 +158,12 @@ memory_region::~memory_region() {
146
158
147
159
void
148
160
memory_region::release_alloc (void *mem) {
161
+ # if RUSTRT_TRACK_ALLOCATIONS >= 1
149
162
alloc_header *alloc = get_header (mem);
150
163
assert (alloc->magic == MAGIC);
164
+ # endif
151
165
152
- #ifdef TRACK_ALLOCATIONS
166
+ # if RUSTRT_TRACK_ALLOCATIONS >= 2
153
167
if (_synchronized) { _lock.lock (); }
154
168
if (_allocation_list[alloc->index ] != alloc) {
155
169
printf (" free: ptr 0x%" PRIxPTR " (%s) is not in allocation_list\n " ,
@@ -162,19 +176,24 @@ memory_region::release_alloc(void *mem) {
162
176
alloc->index = -1 ;
163
177
}
164
178
if (_synchronized) { _lock.unlock (); }
165
- #endif
179
+ # endif
180
+
166
181
dec_alloc ();
167
182
}
168
183
169
184
void
170
185
memory_region::claim_alloc (void *mem) {
186
+ # if RUSTRT_TRACK_ALLOCATIONS >= 1
171
187
alloc_header *alloc = get_header (mem);
172
188
assert (alloc->magic == MAGIC);
173
- #ifdef TRACK_ALLOCATIONS
189
+ # endif
190
+
191
+ # if RUSTRT_TRACK_ALLOCATIONS >= 2
174
192
if (_synchronized) { _lock.lock (); }
175
193
alloc->index = _allocation_list.append (alloc);
176
194
if (_synchronized) { _lock.unlock (); }
177
- #endif
195
+ # endif
196
+
178
197
add_alloc ();
179
198
}
180
199
@@ -190,8 +209,10 @@ memory_region::maybe_poison(void *mem) {
190
209
if (!poison)
191
210
return ;
192
211
212
+ # if RUSTRT_TRACK_ALLOCATIONS >= 1
193
213
alloc_header *alloc = get_header (mem);
194
214
memset (mem, ' \xcd ' , alloc->size );
215
+ # endif
195
216
}
196
217
197
218
//
0 commit comments