2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
16 MemHash
* Purify_LastNode
;
18 #define CalcHash(addr) \
22 ((x & 0xFF) ^ (x >> 8)) & 0xFF; \
25 MemHash
* Purify_AddMemory (void * memory
, int size
, int flag
, int type
)
27 MemHash
* node
= xmalloc (sizeof (MemHash
));
31 node
->flags
= xmalloc (size
);
37 Purify_SetMemoryFlags (node
, 0, size
, flag
);
39 hashcode
= CalcHash (memory
);
41 node
->next
= memHash
[hashcode
];
42 memHash
[hashcode
] = node
;
47 void Purify_RemMemory (const void * mem
)
49 int hashcode
= CalcHash (mem
);
50 MemHash
* node
= (MemHash
*)&memHash
[hashcode
], * next
;
53 printf ("Purify_FindMemory (mem=%p)\n", mem
);
56 for ( ; (next
=node
->next
); node
=next
)
59 printf (" Checking against %p:%d (%p)\n",
60 node
->mem
, node
->size
, node
->mem
+node
->size
);
62 if (next
->mem
<= mem
&& next
->mem
+next
->size
> mem
)
65 printf (" Node found\n");
67 node
->next
= next
->next
;
69 if (Purify_LastNode
== next
)
70 Purify_LastNode
= NULL
;
76 printf (" Nothing found\n");
81 void Purify_SetMemoryFlags (MemHash
* mem
, int offset
, int size
, int flag
)
86 printf ("SetMemoryFlags (hash=%p, offset=%d, size=%d, flag=%d)\n",
87 mem
, offset
, size
, flag
91 passert (offset
+size
<= mem
->size
);
93 ptr
= mem
->flags
+ offset
;
99 void Purify_ModifyMemoryFlags (MemHash
* mem
, int offset
, int size
, int flag
,
104 passert (offset
+size
<= mem
->size
);
106 ptr
= mem
->flags
+ offset
;
113 *ptr
= (*ptr
& mask
) | flag
;
121 MemHash
* Purify_FindMemory (const void * mem
)
123 int hashcode
= CalcHash (mem
);
124 MemHash
* node
= memHash
[hashcode
];
127 printf ("Purify_FindMemory (mem=%p)\n", mem
);
130 for ( ; node
; node
=node
->next
)
133 printf (" Checking against %p:%d (%p)\n",
134 node
->mem
, node
->size
, node
->mem
+node
->size
);
136 if (node
->mem
<= mem
&& node
->mem
+node
->size
> mem
)
138 Purify_LastNode
= node
;
140 printf (" Node found\n");
146 Purify_LastNode
= NULL
;
147 Purify_Error
= NotOwnMemory
;
150 printf (" Nothing found\n");
155 int Purify_CheckMemoryAccess (const void * mem
, int size
, int access
)
157 MemHash
* node
= Purify_FindMemory (mem
);
164 offset
= (long)mem
- (long)node
->mem
;
166 if (offset
+size
> node
->size
)
168 Purify_Error
= BorderAccess
;
172 ptr
= node
->flags
+ offset
;
174 if (access
== PURIFY_MemAccess_Read
)
179 if (!(*ptr
& PURIFY_MemFlag_Readable
) )
181 if (*ptr
& PURIFY_MemFlag_Free
)
183 Purify_Error
= (node
->type
== PURIFY_MemType_Stack
)
184 ? FreeStackRead
: FreeRead
;
187 else if (*ptr
& PURIFY_MemFlag_Empty
)
189 Purify_Error
= UndefRead
;
194 Purify_Error
= IllRead
;
204 if (node
->type
== PURIFY_MemType_Code
)
206 Purify_Error
= CodeWrite
;
213 if (!(*ptr
& PURIFY_MemFlag_Writable
) )
215 if (*ptr
& PURIFY_MemFlag_Free
)
218 (node
->type
== PURIFY_MemType_Stack
)
225 Purify_Error
= IllWrite
;
233 Purify_ModifyMemoryFlags (node
, offset
, size
,
234 PURIFY_MemFlag_Readable
,
235 PURIFY_MemFlag_Readable
|PURIFY_MemFlag_Empty
242 void Purify_PrintMemory (void)
250 for (i
=0; i
<256; i
++)
252 if ((node
= memHash
[i
]))
254 printf ("Hashline %3d:\n", i
);
261 printf (" Node %p: Memory=%p Size=%d Type=",
269 case PURIFY_MemType_Heap
: printf ("heap "); break;
270 case PURIFY_MemType_Stack
: printf ("stack"); break;
271 case PURIFY_MemType_Code
: printf ("code "); break;
272 case PURIFY_MemType_Data
: printf ("data "); break;
279 case PURIFY_MemType_Stack
:
280 case PURIFY_MemType_Code
:
281 case PURIFY_MemType_Data
:
282 printf (" name=\"%s\"", (char *)node
->data
);
285 case PURIFY_MemType_Heap
:
287 PMemoryNode
* mem
= (PMemoryNode
*)(node
->data
);
289 printf (" %s()", mem
->alloc
.current
.functionname
);
300 for (cnt
=0,t
=0; t
<node
->size
; t
++,cnt
++)
303 printf ("\t%4d: ", t
);
305 printf ("%x", node
->flags
[t
]);
312 else if ((cnt
& 7) == 7)
316 if (((t
-1) & WIDTH
) != WIDTH
-1)
325 # define ABS(x) (((x) < 0) ? -(x) : (x))
328 MemHash
* Purify_FindNextMemory (const void * mem
, int * offsetptr
)
330 MemHash
* node
, * next
;
336 for (i
=0; i
<256; i
++)
338 for (node
= memHash
[i
];
343 o
= (long)mem
- (long)node
->mem
;
358 if (ABS(o
) < ABS(offset
)
359 || (ABS(o
) == ABS(offset
) && o
> 0)