11 MemHash
* Purify_LastNode
;
13 #define CalcHash(addr) \
17 ((x & 0xFF) ^ (x >> 8)) & 0xFF; \
20 MemHash
* Purify_AddMemory (void * memory
, int size
, int flag
, int type
)
22 MemHash
* node
= xmalloc (sizeof (MemHash
));
26 node
->flags
= xmalloc (size
);
32 Purify_SetMemoryFlags (node
, 0, size
, flag
);
34 hashcode
= CalcHash (memory
);
36 node
->next
= memHash
[hashcode
];
37 memHash
[hashcode
] = node
;
42 void Purify_RemMemory (const void * mem
)
44 int hashcode
= CalcHash (mem
);
45 MemHash
* node
= (MemHash
*)&memHash
[hashcode
], * next
;
48 printf ("Purify_FindMemory (mem=%p)\n", mem
);
51 for ( ; (next
=node
->next
); node
=next
)
54 printf (" Checking against %p:%d (%p)\n",
55 node
->mem
, node
->size
, node
->mem
+node
->size
);
57 if (next
->mem
<= mem
&& next
->mem
+next
->size
> mem
)
60 printf (" Node found\n");
62 node
->next
= next
->next
;
64 if (Purify_LastNode
== next
)
65 Purify_LastNode
= NULL
;
71 printf (" Nothing found\n");
76 void Purify_SetMemoryFlags (MemHash
* mem
, int offset
, int size
, int flag
)
81 printf ("SetMemoryFlags (hash=%p, offset=%d, size=%d, flag=%d)\n",
82 mem
, offset
, size
, flag
86 passert (offset
+size
<= mem
->size
);
88 ptr
= mem
->flags
+ offset
;
94 void Purify_ModifyMemoryFlags (MemHash
* mem
, int offset
, int size
, int flag
,
99 passert (offset
+size
<= mem
->size
);
101 ptr
= mem
->flags
+ offset
;
108 *ptr
= (*ptr
& mask
) | flag
;
116 MemHash
* Purify_FindMemory (const void * mem
)
118 int hashcode
= CalcHash (mem
);
119 MemHash
* node
= memHash
[hashcode
];
122 printf ("Purify_FindMemory (mem=%p)\n", mem
);
125 for ( ; node
; node
=node
->next
)
128 printf (" Checking against %p:%d (%p)\n",
129 node
->mem
, node
->size
, node
->mem
+node
->size
);
131 if (node
->mem
<= mem
&& node
->mem
+node
->size
> mem
)
133 Purify_LastNode
= node
;
135 printf (" Node found\n");
141 Purify_LastNode
= NULL
;
142 Purify_Error
= NotOwnMemory
;
145 printf (" Nothing found\n");
150 int Purify_CheckMemoryAccess (const void * mem
, int size
, int access
)
152 MemHash
* node
= Purify_FindMemory (mem
);
159 offset
= (long)mem
- (long)node
->mem
;
161 if (offset
+size
> node
->size
)
163 Purify_Error
= BorderAccess
;
167 ptr
= node
->flags
+ offset
;
169 if (access
== PURIFY_MemAccess_Read
)
174 if (!(*ptr
& PURIFY_MemFlag_Readable
) )
176 if (*ptr
& PURIFY_MemFlag_Free
)
178 Purify_Error
= (node
->type
== PURIFY_MemType_Stack
)
179 ? FreeStackRead
: FreeRead
;
182 else if (*ptr
& PURIFY_MemFlag_Empty
)
184 Purify_Error
= UndefRead
;
189 Purify_Error
= IllRead
;
199 if (node
->type
== PURIFY_MemType_Code
)
201 Purify_Error
= CodeWrite
;
208 if (!(*ptr
& PURIFY_MemFlag_Writable
) )
210 if (*ptr
& PURIFY_MemFlag_Free
)
213 (node
->type
== PURIFY_MemType_Stack
)
220 Purify_Error
= IllWrite
;
228 Purify_ModifyMemoryFlags (node
, offset
, size
,
229 PURIFY_MemFlag_Readable
,
230 PURIFY_MemFlag_Readable
|PURIFY_MemFlag_Empty
237 void Purify_PrintMemory (void)
245 for (i
=0; i
<256; i
++)
247 if ((node
= memHash
[i
]))
249 printf ("Hashline %3d:\n", i
);
256 printf (" Node %p: Memory=%p Size=%d Type=",
264 case PURIFY_MemType_Heap
: printf ("heap "); break;
265 case PURIFY_MemType_Stack
: printf ("stack"); break;
266 case PURIFY_MemType_Code
: printf ("code "); break;
267 case PURIFY_MemType_Data
: printf ("data "); break;
274 case PURIFY_MemType_Stack
:
275 case PURIFY_MemType_Code
:
276 case PURIFY_MemType_Data
:
277 printf (" name=\"%s\"", (char *)node
->data
);
280 case PURIFY_MemType_Heap
:
282 PMemoryNode
* mem
= (PMemoryNode
*)(node
->data
);
284 printf (" %s()", mem
->alloc
.current
.functionname
);
295 for (cnt
=0,t
=0; t
<node
->size
; t
++,cnt
++)
298 printf ("\t%4d: ", t
);
300 printf ("%x", node
->flags
[t
]);
307 else if ((cnt
& 7) == 7)
311 if (((t
-1) & WIDTH
) != WIDTH
-1)
320 # define ABS(x) (((x) < 0) ? -(x) : (x))
323 MemHash
* Purify_FindNextMemory (const void * mem
, int * offsetptr
)
325 MemHash
* node
, * next
;
331 for (i
=0; i
<256; i
++)
333 for (node
= memHash
[i
];
338 o
= (long)mem
- (long)node
->mem
;
353 if (ABS(o
) < ABS(offset
)
354 || (ABS(o
) == ABS(offset
) && o
> 0)