2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
15 #define ALIGN(x) (char *)((((long)(x))+15) & -8)
17 void * Purify_malloc (size_t size
)
19 PMemoryNode
* node
= malloc (sizeof (PMemoryNode
) + size
);
25 Purify_RememberCallers (&node
->alloc
);
27 node
->free
.nstack
= -1; /* Not freed yet */
29 node
->memptr
= ALIGN(node
->mem
);
31 hash
= Purify_AddMemory (node
->memptr
, size
,
32 PURIFY_MemFlag_Writable
33 | PURIFY_MemFlag_Empty
42 void * Purify_calloc (size_t nemb
, size_t size
)
44 PMemoryNode
* node
= calloc (1, sizeof (PMemoryNode
) + nemb
*size
);
50 Purify_RememberCallers (&node
->alloc
);
52 node
->free
.nstack
= -1; /* Not freed yet */
54 node
->memptr
= ALIGN(node
->mem
);
56 hash
= Purify_AddMemory (node
->memptr
, nemb
*size
,
57 PURIFY_MemFlag_Readable
58 | PURIFY_MemFlag_Writable
67 void * Purify_realloc (void * mem
, size_t size
)
69 PMemoryNode
* oldnode
, * newnode
;
70 MemHash
* oldmem
, * newmem
;
74 newnode
= malloc (sizeof (PMemoryNode
) + size
);
79 Purify_RememberCallers (&newnode
->alloc
);
81 newnode
->free
.nstack
= -1; /* Not freed yet */
83 newnode
->memptr
= ALIGN(newnode
->mem
);
85 newmem
= Purify_AddMemory (newnode
->memptr
, size
,
86 PURIFY_MemFlag_Writable
87 | PURIFY_MemFlag_Empty
91 newmem
->data
= newnode
;
98 memcpy (newnode
->memptr
, mem
, size
);
103 oldmem
= Purify_FindMemory (mem
);
107 Purify_Error
= IllPointer
;
108 Purify_PrintError ("realloc(addr=%p, size=%ld)", mem
, size
);
112 oldnode
= (PMemoryNode
*)(oldmem
->data
);
116 if (size
> oldmem
->size
)
119 memcpy (newnode
->memptr
, oldnode
->memptr
, size
);
121 Purify_SetMemoryFlags (newmem
, 0, size
,
122 PURIFY_MemFlag_Readable
| PURIFY_MemFlag_Writable
126 Purify_SetMemoryFlags (oldmem
, 0, oldmem
->size
, PURIFY_MemFlag_Free
);
127 Purify_RememberCallers (&oldnode
->free
);
131 return newnode
? newnode
->memptr
: NULL
;
134 void Purify_free (void * mem
)
136 MemHash
* hash
= Purify_FindMemory (mem
);
141 Purify_Error
= IllPointer
;
142 Purify_PrintError ("free(addr=%p)", mem
);
146 node
= (PMemoryNode
*)(hash
->data
);
148 Purify_SetMemoryFlags (hash
, 0, hash
->size
, PURIFY_MemFlag_Free
);
149 Purify_RememberCallers (&node
->free
);
153 void Purify_MemoryExit (void)
155 extern MemHashTable memHash
;
157 PMemoryNode
* memnode
;
160 Purify_Error
= MemLeak
;
162 for (i
=0; i
<256; i
++)
164 for (node
=memHash
[i
];
169 if (node
->type
== PURIFY_MemType_Heap
)
171 memnode
= (PMemoryNode
*)(node
->data
);
173 if (memnode
->free
.nstack
== -1)
175 Purify_PrePrintError ();
177 "The memory block at %p with the size %d was allocated at\n",
178 node
->mem
, node
->size
180 Purify_PrintCallers (&memnode
->alloc
);
181 Purify_PostPrintError ();
188 void * Purify_memmove (void *dest
, const void *src
, size_t n
)
190 if (!Purify_CheckMemoryAccess (src
, n
, PURIFY_MemAccess_Read
))
191 Purify_PrintAccessError ("memmove (src)", src
, n
);
192 if (!Purify_CheckMemoryAccess (dest
, n
, PURIFY_MemAccess_Write
))
193 Purify_PrintAccessError ("memmove (dest)", dest
, n
);
195 return memmove (dest
, src
, n
);
198 void * Purify_memset (void *dest
, int c
, size_t n
)
200 if (!Purify_CheckMemoryAccess (dest
, n
, PURIFY_MemAccess_Write
))
201 Purify_PrintAccessError ("memmove (dest)", dest
, n
);
203 return memset (dest
, c
, n
);
206 void * Purify_memcpy (void *dest
, const void *src
, size_t n
)
208 if (!Purify_CheckMemoryAccess (src
, n
, PURIFY_MemAccess_Read
))
209 Purify_PrintAccessError ("memcpy (src)", src
, n
);
211 if (!Purify_CheckMemoryAccess (dest
, n
, PURIFY_MemAccess_Write
))
212 Purify_PrintAccessError ("memcpy (dest)", dest
, n
);
214 return memcpy (dest
, src
, n
);
217 char * Purify_strcpy (char *dest
, const char *src
)
221 if (!Purify_CheckMemoryAccess (src
, 1, PURIFY_MemAccess_Read
))
222 Purify_PrintAccessError ("strcpy (src)", src
, 1);
224 len
= strlen (src
)+1;
226 if (!Purify_CheckMemoryAccess (src
, len
, PURIFY_MemAccess_Read
))
227 Purify_PrintAccessError ("strcpy (src)", src
, len
);
229 if (!Purify_CheckMemoryAccess (dest
, len
, PURIFY_MemAccess_Write
))
230 Purify_PrintAccessError ("strcpy (dest)", dest
, len
);
232 return strcpy (dest
, src
);
235 char * Purify_strcat (char *dest
, const char *src
)
239 if (!Purify_CheckMemoryAccess (src
, 1, PURIFY_MemAccess_Read
))
240 Purify_PrintAccessError ("strcat (src)", src
, 1);
242 slen
= strlen (src
)+1;
244 if (!Purify_CheckMemoryAccess (src
, slen
, PURIFY_MemAccess_Read
))
245 Purify_PrintAccessError ("strcat (src)", src
, slen
);
247 if (!Purify_CheckMemoryAccess (src
, 1, PURIFY_MemAccess_Read
))
248 Purify_PrintAccessError ("strcat (dest)", dest
, 1);
250 dlen
= strlen (dest
);
252 if (!Purify_CheckMemoryAccess (dest
+dlen
, slen
, PURIFY_MemAccess_Write
))
253 Purify_PrintAccessError ("strcat (dest)", dest
+dlen
, slen
);
255 return strcpy (dest
+dlen
, src
);
258 char * Purify_strncpy (char *dest
, const char *src
, size_t n
)
260 if (!Purify_CheckMemoryAccess (src
, n
, PURIFY_MemAccess_Read
))
261 Purify_PrintAccessError ("strncpy (src)", src
, n
);
263 if (!Purify_CheckMemoryAccess (dest
, n
, PURIFY_MemAccess_Write
))
264 Purify_PrintAccessError ("strncpy (dest)", dest
, n
);
266 return strncpy (dest
, src
, n
);
269 char * Purify_strdup (char * src
)
275 if (!Purify_CheckMemoryAccess (src
, 1, PURIFY_MemAccess_Read
))
276 Purify_PrintAccessError ("strdup (src)", src
, 1);
278 size
= strlen (src
) + 1;
280 node
= malloc (sizeof (PMemoryNode
) + size
);
285 Purify_RememberCallers (&node
->alloc
);
287 node
->free
.nstack
= -1; /* Not freed yet */
289 node
->memptr
= ALIGN(node
->mem
);
291 hash
= Purify_AddMemory (node
->memptr
, size
,
292 PURIFY_MemFlag_Writable
293 | PURIFY_MemFlag_Readable
294 , PURIFY_MemType_Heap
299 strcpy (node
->memptr
, src
);