-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfbmld.bi
345 lines (276 loc) · 7.57 KB
/
fbmld.bi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
''
'' fbmld (FB Memory Leak Detector) version 0.6
'' Copyright (C) 2006 Daniel R. Verkamp
'' Tree storage implemented by yetifoot
''
'' This software is provided 'as-is', without any express or implied warranty.
'' In no event will the authors be held liable for any damages arising from
'' the use of this software.
''
'' Permission is granted to anyone to use this software for any purpose,
'' including commercial applications, and to alter it and redistribute it
'' freely, subject to the following restrictions:
''
'' 1. The origin of this software must not be misrepresented; you must not claim
'' that you wrote the original software. If you use this software in a product,
'' an acknowledgment in the product documentation would be appreciated but is
'' not required.
''
'' 2. Altered source versions must be plainly marked as such, and must not be
'' misrepresented as being the original software.
''
'' 3. This notice may not be removed or altered from any source
'' distribution.
''
#ifndef __FBMLD__
#define __FBMLD__
#include "crt.bi"
#undef allocate
#undef callocate
#undef reallocate
#undef deallocate
#define allocate(bytes) fbmld_allocate((bytes), __FILE__, __LINE__)
#define callocate(bytes) fbmld_callocate((bytes), __FILE__, __LINE__)
#define reallocate(pt, bytes) fbmld_reallocate((pt), (bytes), __FILE__, __LINE__, #pt)
#define deallocate(pt) fbmld_deallocate((pt), __FILE__, __LINE__, #pt)
type fbmld_t
pt as any ptr
bytes as uinteger
file as string
linenum as integer
left as fbmld_t ptr
right as fbmld_t ptr
end type
common shared fbmld_tree as fbmld_t ptr
common shared fbmld_mutex as any ptr
common shared fbmld_instances as integer
private sub fbmld_print(byref s as string)
fprintf(stderr, "(FBMLD) " & s & chr(10))
end sub
private sub fbmld_mutexlock( )
#ifndef FBMLD_NO_MULTITHREADING
mutexlock(fbmld_mutex)
#endif
end sub
private sub fbmld_mutexunlock( )
#ifndef FBMLD_NO_MULTITHREADING
mutexunlock(fbmld_mutex)
#endif
end sub
private function new_node _
( _
byval pt as any ptr, _
byval bytes as uinteger, _
byref file as string, _
byval linenum as integer _
) as fbmld_t ptr
dim as fbmld_t ptr node = calloc(1, sizeof(fbmld_t))
node->pt = pt
node->bytes = bytes
node->file = file
node->linenum = linenum
node->left = NULL
node->right = NULL
function = node
end function
private sub free_node _
( _
byval node as fbmld_t ptr _
)
node->file = ""
free( node )
end sub
private function fbmld_search _
( _
byval root as fbmld_t ptr ptr, _
byval pt as any ptr _
) as fbmld_t ptr ptr
dim as fbmld_t ptr ptr node = root
dim as any ptr a = pt, b = any
asm
mov eax, dword ptr [a]
bswap eax
mov dword ptr [a], eax
end asm
while *node <> NULL
b = (*node)->pt
asm
mov eax, dword ptr [b]
bswap eax
mov dword ptr [b], eax
end asm
if a < b then
node = @(*node)->left
elseif a > b then
node = @(*node)->right
else
exit while
end if
wend
function = node
end function
private sub fbmld_insert _
( _
byval root as fbmld_t ptr ptr, _
byval pt as any ptr, _
byval bytes as uinteger, _
byref file as string, _
byval linenum as integer _
)
dim as fbmld_t ptr ptr node = fbmld_search(root, pt)
if *node = NULL then
*node = new_node( pt, bytes, file, linenum )
end if
end sub
private sub fbmld_swap _
( _
byval node1 as fbmld_t ptr ptr, _
byval node2 as fbmld_t ptr ptr _
)
swap (*node1)->pt, (*node2)->pt
swap (*node1)->bytes, (*node2)->bytes
swap (*node1)->file, (*node2)->file
swap (*node1)->linenum, (*node2)->linenum
end sub
private sub fbmld_delete _
( _
byval node as fbmld_t ptr ptr _
)
dim as fbmld_t ptr old_node = *node
dim as fbmld_t ptr ptr pred
if (*node)->left = NULL then
*node = (*node)->right
free_node( old_node )
elseif (*node)->right = NULL then
*node = (*node)->left
free_node( old_node )
else
pred = @(*node)->left
while (*pred)->right <> NULL
pred = @(*pred)->right
wend
fbmld_swap( node, pred )
fbmld_delete( pred )
end if
end sub
private sub fbmld_init _
( _
) constructor 101
if fbmld_instances = 0 then
#ifndef FBMLD_NO_MULTITHREADING
fbmld_mutex = mutexcreate()
#endif
end if
fbmld_instances += 1
end sub
private sub fbmld_tree_clean _
( _
byval node as fbmld_t ptr ptr _
)
if *node <> NULL then
fbmld_tree_clean( @((*node)->left) )
fbmld_tree_clean( @((*node)->right) )
fbmld_print( "error: " & (*node)->bytes & " bytes allocated at " & (*node)->file & ":" & (*node)->linenum & " [&H" & hex( (*node)->pt, 8 ) & "] not deallocated" )
(*node)->file = ""
free( (*node)->pt )
free( *node )
*node = NULL
end if
end sub
private sub fbmld_exit _
( _
) destructor 101
fbmld_instances -= 1
if fbmld_instances = 0 then
if fbmld_tree <> NULL then
fbmld_print("---- memory leaks ----")
fbmld_tree_clean(@fbmld_tree)
else
fbmld_print("all memory deallocated")
end if
#ifndef FBMLD_NO_MULTITHREADING
if fbmld_mutex <> 0 then
mutexdestroy(fbmld_mutex)
fbmld_mutex = 0
end if
#endif
end if
end sub
private function fbmld_allocate(byval bytes as uinteger, byref file as string, byval linenum as integer) as any ptr
dim ret as any ptr = any
fbmld_mutexlock()
if bytes = 0 then
fbmld_print("warning: allocate(0) called at " & file & ":" & linenum & "; returning NULL")
ret = 0
else
ret = malloc(bytes)
fbmld_insert(@fbmld_tree, ret, bytes, file, linenum)
end if
fbmld_mutexunlock()
return ret
end function
private function fbmld_callocate(byval bytes as uinteger, byref file as string, byval linenum as integer) as any ptr
dim ret as any ptr = any
fbmld_mutexlock()
if bytes = 0 then
fbmld_print("warning: callocate(0) called at " & file & ":" & linenum & "; returning NULL")
ret = 0
else
ret = calloc(1, bytes)
fbmld_insert(@fbmld_tree, ret, bytes, file, linenum)
end if
fbmld_mutexunlock()
return ret
end function
private function fbmld_reallocate(byval pt as any ptr, byval bytes as uinteger, byref file as string, byval linenum as integer, byref varname as string) as any ptr
dim ret as any ptr = any
dim node as fbmld_t ptr ptr = any
fbmld_mutexlock()
node = fbmld_search(@fbmld_tree, pt)
if pt = NULL then
if bytes = 0 then
fbmld_print("error: reallocate(" & varname & " [NULL] , 0) called at " & file & ":" & linenum)
ret = NULL
else
ret = malloc(bytes)
fbmld_insert(@fbmld_tree, ret, bytes, file, linenum)
end if
elseif *node = NULL then
fbmld_print("error: invalid reallocate(" & varname & " [&H" & hex(pt, 8) & "] ) at " & file & ":" & linenum)
ret = NULL
elseif bytes = 0 then
fbmld_print("warning: reallocate(" & varname & " [&H" & hex(pt, 8) & "] , 0) called at " & file & ":" & linenum & "; deallocating")
free(pt)
if *node <> NULL then fbmld_delete(node)
ret = NULL
else
ret = realloc(pt, bytes)
if ret = pt then
(*node)->bytes = bytes
(*node)->file = file
(*node)->linenum = linenum
else
fbmld_delete(node)
fbmld_insert(@fbmld_tree, ret, bytes, file, linenum)
end if
end if
fbmld_mutexunlock()
return ret
end function
private sub fbmld_deallocate(byval pt as any ptr, byref file as string, byval linenum as integer, byref varname as string)
dim node as fbmld_t ptr ptr
fbmld_mutexlock()
if pt = NULL then
fbmld_print("warning: deallocate(" & varname & " [NULL] ) at " & file & ":" & linenum)
else
node = fbmld_search(@fbmld_tree, pt)
if *node = NULL then
fbmld_print("error: invalid deallocate(" & varname & " [&H" & hex(pt, 8) & "] ) at " & file & ":" & linenum)
else
fbmld_delete(node)
free(pt)
end if
end if
fbmld_mutexunlock()
end sub
#endif '' __FBMLD__