Skip to content

Commit 63590ab

Browse files
author
Eric Holk
committed
---
yaml --- r: 3707 b: refs/heads/master c: f6e37f6 h: refs/heads/master i: 3705: 80205fc 3703: 42affa0 v: v3
1 parent 4474a90 commit 63590ab

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 5d9a5b7d556f6573508c83242a0e7b7795d2c2dd
2+
refs/heads/master: f6e37f659cd3a262c727f79792fc85df648fbe35

trunk/src/rt/memory_region.cpp

+48-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ void memory_region::free(void *mem) {
3434
if (!mem) { return; }
3535
if (_synchronized) { _lock.lock(); }
3636
#ifdef TRACK_ALLOCATIONS
37-
if (_allocation_list.replace(mem, NULL) == false) {
37+
int index = ((int *)mem)[-1];
38+
if (_allocation_list[index] != (uint8_t *)mem - sizeof(int)) {
3839
printf("free: ptr 0x%" PRIxPTR " is not in allocation_list\n",
39-
(uintptr_t) mem);
40+
(uintptr_t) mem);
4041
_srv->fatal("not in allocation_list", __FILE__, __LINE__, "");
4142
}
43+
else {
44+
// printf("freed index %d\n", index);
45+
_allocation_list[index] = NULL;
46+
}
47+
mem = (void*)((uint8_t*)mem - sizeof(int));
4248
#endif
4349
if (_live_allocations < 1) {
4450
_srv->fatal("live_allocs < 1", __FILE__, __LINE__, "");
@@ -54,47 +60,83 @@ memory_region::realloc(void *mem, size_t size) {
5460
if (!mem) {
5561
add_alloc();
5662
}
63+
#ifdef TRACK_ALLOCATIONS
64+
size += sizeof(int);
65+
mem = (void*)((uint8_t*)mem - sizeof(int));
66+
int index = *(int *)mem;
67+
#endif
5768
void *newMem = _srv->realloc(mem, size);
5869
#ifdef TRACK_ALLOCATIONS
59-
if (_allocation_list.replace(mem, newMem) == false) {
70+
if (_allocation_list[index] != mem) {
71+
printf("at index %d, found %p, expected %p\n",
72+
index, _allocation_list[index], mem);
6073
printf("realloc: ptr 0x%" PRIxPTR " is not in allocation_list\n",
6174
(uintptr_t) mem);
6275
_srv->fatal("not in allocation_list", __FILE__, __LINE__, "");
6376
}
77+
else {
78+
_allocation_list[index] = newMem;
79+
(*(int*)newMem) = index;
80+
// printf("realloc: stored %p at index %d, replacing %p\n",
81+
// newMem, index, mem);
82+
}
6483
#endif
6584
if (_synchronized) { _lock.unlock(); }
85+
#ifdef TRACK_ALLOCATIONS
86+
newMem = (void *)((uint8_t*)newMem + sizeof(int));
87+
#endif
6688
return newMem;
6789
}
6890

6991
void *
7092
memory_region::malloc(size_t size) {
7193
if (_synchronized) { _lock.lock(); }
7294
add_alloc();
95+
#ifdef TRACK_ALLOCATIONS
96+
size += sizeof(int);
97+
#endif
7398
void *mem = _srv->malloc(size);
7499
#ifdef TRACK_ALLOCATIONS
75-
_allocation_list.append(mem);
100+
int index = _allocation_list.append(mem);
101+
int *p = (int *)mem;
102+
*p = index;
103+
// printf("malloc: stored %p at index %d\n", mem, index);
76104
#endif
77105
// printf("malloc: ptr 0x%" PRIxPTR " region=%p\n",
78106
// (uintptr_t) mem, this);
79107
if (_synchronized) { _lock.unlock(); }
108+
#ifdef TRACK_ALLOCATIONS
109+
mem = (void*)((uint8_t*)mem + sizeof(int));
110+
#endif
80111
return mem;
81112
}
82113

83114
void *
84115
memory_region::calloc(size_t size) {
85116
if (_synchronized) { _lock.lock(); }
86117
add_alloc();
118+
#ifdef TRACK_ALLOCATIONS
119+
size += sizeof(int);
120+
#endif
87121
void *mem = _srv->malloc(size);
88122
memset(mem, 0, size);
89123
#ifdef TRACK_ALLOCATIONS
90-
_allocation_list.append(mem);
124+
int index = _allocation_list.append(mem);
125+
int *p = (int *)mem;
126+
*p = index;
127+
// printf("calloc: stored %p at index %d\n", mem, index);
91128
#endif
92129
if (_synchronized) { _lock.unlock(); }
130+
#ifdef TRACK_ALLOCATIONS
131+
mem = (void*)((uint8_t*)mem + sizeof(int));
132+
#endif
93133
return mem;
94134
}
95135

96136
memory_region::~memory_region() {
137+
if (_synchronized) { _lock.lock(); }
97138
if (_live_allocations == 0) {
139+
if (_synchronized) { _lock.unlock(); }
98140
return;
99141
}
100142
char msg[128];
@@ -112,6 +154,7 @@ memory_region::~memory_region() {
112154
}
113155
#endif
114156
_srv->fatal(msg, __FILE__, __LINE__, "%d objects", _live_allocations);
157+
if (_synchronized) { _lock.unlock(); }
115158
}
116159

117160
//

0 commit comments

Comments
 (0)