@@ -34,11 +34,17 @@ void memory_region::free(void *mem) {
34
34
if (!mem) { return ; }
35
35
if (_synchronized) { _lock.lock (); }
36
36
#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 )) {
38
39
printf (" free: ptr 0x%" PRIxPTR " is not in allocation_list\n " ,
39
- (uintptr_t ) mem);
40
+ (uintptr_t ) mem);
40
41
_srv->fatal (" not in allocation_list" , __FILE__, __LINE__, " " );
41
42
}
43
+ else {
44
+ // printf("freed index %d\n", index);
45
+ _allocation_list[index ] = NULL ;
46
+ }
47
+ mem = (void *)((uint8_t *)mem - sizeof (int ));
42
48
#endif
43
49
if (_live_allocations < 1 ) {
44
50
_srv->fatal (" live_allocs < 1" , __FILE__, __LINE__, " " );
@@ -54,47 +60,83 @@ memory_region::realloc(void *mem, size_t size) {
54
60
if (!mem) {
55
61
add_alloc ();
56
62
}
63
+ #ifdef TRACK_ALLOCATIONS
64
+ size += sizeof (int );
65
+ mem = (void *)((uint8_t *)mem - sizeof (int ));
66
+ int index = *(int *)mem;
67
+ #endif
57
68
void *newMem = _srv->realloc (mem, size);
58
69
#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);
60
73
printf (" realloc: ptr 0x%" PRIxPTR " is not in allocation_list\n " ,
61
74
(uintptr_t ) mem);
62
75
_srv->fatal (" not in allocation_list" , __FILE__, __LINE__, " " );
63
76
}
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
+ }
64
83
#endif
65
84
if (_synchronized) { _lock.unlock (); }
85
+ #ifdef TRACK_ALLOCATIONS
86
+ newMem = (void *)((uint8_t *)newMem + sizeof (int ));
87
+ #endif
66
88
return newMem;
67
89
}
68
90
69
91
void *
70
92
memory_region::malloc (size_t size) {
71
93
if (_synchronized) { _lock.lock (); }
72
94
add_alloc ();
95
+ #ifdef TRACK_ALLOCATIONS
96
+ size += sizeof (int );
97
+ #endif
73
98
void *mem = _srv->malloc (size);
74
99
#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);
76
104
#endif
77
105
// printf("malloc: ptr 0x%" PRIxPTR " region=%p\n",
78
106
// (uintptr_t) mem, this);
79
107
if (_synchronized) { _lock.unlock (); }
108
+ #ifdef TRACK_ALLOCATIONS
109
+ mem = (void *)((uint8_t *)mem + sizeof (int ));
110
+ #endif
80
111
return mem;
81
112
}
82
113
83
114
void *
84
115
memory_region::calloc (size_t size) {
85
116
if (_synchronized) { _lock.lock (); }
86
117
add_alloc ();
118
+ #ifdef TRACK_ALLOCATIONS
119
+ size += sizeof (int );
120
+ #endif
87
121
void *mem = _srv->malloc (size);
88
122
memset (mem, 0 , size);
89
123
#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);
91
128
#endif
92
129
if (_synchronized) { _lock.unlock (); }
130
+ #ifdef TRACK_ALLOCATIONS
131
+ mem = (void *)((uint8_t *)mem + sizeof (int ));
132
+ #endif
93
133
return mem;
94
134
}
95
135
96
136
memory_region::~memory_region () {
137
+ if (_synchronized) { _lock.lock (); }
97
138
if (_live_allocations == 0 ) {
139
+ if (_synchronized) { _lock.unlock (); }
98
140
return ;
99
141
}
100
142
char msg[128 ];
@@ -112,6 +154,7 @@ memory_region::~memory_region() {
112
154
}
113
155
#endif
114
156
_srv->fatal (msg, __FILE__, __LINE__, " %d objects" , _live_allocations);
157
+ if (_synchronized) { _lock.unlock (); }
115
158
}
116
159
117
160
//
0 commit comments