2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
5 Desc: Basic functions for ressource tracking
11 #define ENABLE_RT 0 /* no RT inside this file */
15 #include <exec/lists.h>
16 #include <aros/system.h>
17 #include <exec/execbase.h>
22 #include <exec/tasks.h>
23 #include <exec/ports.h>
24 #include <exec/memory.h>
26 #include <proto/exec.h>
27 #include <proto/arossupport.h>
28 #include <proto/alib.h>
42 struct MsgPort
* Port
;
55 static IPTR
RT_AllocMem (RTData
* rtd
, MemoryResource
* rt
, va_list args
, BOOL
* success
);
56 static IPTR
RT_FreeMem (RTData
* rtd
, MemoryResource
* rt
);
57 static IPTR
RT_SearchMem (RTData
* rtd
, int rtt
, MemoryResource
** rtptr
, va_list args
);
58 static IPTR
RT_ShowErrorMem (RTData
* rtd
, int rtt
, MemoryResource
*, IPTR
, int, const char * file
, ULONG line
, va_list);
60 static IPTR
RT_AllocVec (RTData
* rtd
, MemoryResource
* rt
, va_list args
, BOOL
* success
);
61 static IPTR
RT_FreeVec (RTData
* rtd
, MemoryResource
* rt
);
62 static IPTR
RT_SearchVec (RTData
* rtd
, int rtt
, MemoryResource
** rtptr
, va_list args
);
63 static IPTR
RT_ShowErrorVec (RTData
* rtd
, int, MemoryResource
*, IPTR
, int, const char * file
, ULONG line
, va_list);
65 static IPTR
RT_CreatePort (RTData
* rtd
, PortResource
* rt
, va_list args
, BOOL
* success
);
66 static IPTR
RT_DeletePort (RTData
* rtd
, PortResource
* rt
);
67 static IPTR
RT_ShowErrorPort (RTData
* rtd
, int, PortResource
*, IPTR
, int, const char * file
, ULONG line
, va_list);
68 static IPTR
RT_CheckPort (RTData
* rtd
, int desc
, const char * file
, ULONG line
, ULONG op
, va_list args
);
70 static IPTR
RT_OpenLibrary (RTData
* rtd
, LibraryResource
* rt
, va_list args
, BOOL
* success
);
71 static IPTR
RT_CloseLibrary (RTData
* rtd
, LibraryResource
* rt
);
72 static IPTR
RT_ShowErrorLib (RTData
* rtd
, int, LibraryResource
*, IPTR
, int, const char * file
, ULONG line
, va_list);
74 static const RTDesc RT_ExecResources
[] =
77 sizeof (MemoryResource
),
78 (RT_AllocFunc
) RT_AllocMem
,
79 (RT_FreeFunc
) RT_FreeMem
,
80 (RT_SearchFunc
)RT_SearchMem
,
81 (RT_ShowError
) RT_ShowErrorMem
,
85 sizeof (MemoryResource
),
86 (RT_AllocFunc
) RT_AllocVec
,
87 (RT_FreeFunc
) RT_FreeVec
,
88 (RT_SearchFunc
)RT_SearchVec
,
89 (RT_ShowError
) RT_ShowErrorVec
,
93 sizeof (PortResource
),
94 (RT_AllocFunc
) RT_CreatePort
,
95 (RT_FreeFunc
) RT_DeletePort
,
97 (RT_ShowError
) RT_ShowErrorPort
,
98 (RT_CheckFunc
) RT_CheckPort
,
101 sizeof (LibraryResource
),
102 (RT_AllocFunc
) RT_OpenLibrary
,
103 (RT_FreeFunc
) RT_CloseLibrary
,
105 (RT_ShowError
) RT_ShowErrorLib
,
110 void RT_InitExec (void)
112 RT_Resources
[RTT_ALLOCMEM
] = &RT_ExecResources
[0];
113 RT_Resources
[RTT_ALLOCVEC
] = &RT_ExecResources
[1];
114 RT_Resources
[RTT_PORT
] = &RT_ExecResources
[2];
115 RT_Resources
[RTT_LIBRARY
] = &RT_ExecResources
[3];
118 void RT_ExitExec (void)
122 /**************************************
124 **************************************/
126 static IPTR
RT_AllocMem (RTData
* rtd
, MemoryResource
* rt
, va_list args
, BOOL
* success
)
129 rt
->Size
= va_arg (args
, ULONG
);
130 rt
->Flags
= va_arg (args
, ULONG
);
132 rt
->Memory
= AllocMem (rt
->Size
, rt
->Flags
);
137 return (IPTR
)(rt
->Memory
);
140 static IPTR
RT_FreeMem (RTData
* rtd
, MemoryResource
* rt
)
143 FreeMem (rt
->Memory
, rt
->Size
);
148 static IPTR
RT_SearchMem (RTData
* rtd
, int rtt
, MemoryResource
** rtptr
,
155 memory
= va_arg (args
, APTR
);
156 size
= va_arg (args
, ULONG
);
158 ForeachNode (&rtd
->rtd_ResHash
[rtt
][CALCHASH(memory
)], rt
)
160 if (rt
->Memory
== memory
)
164 if (rt
->Size
!= size
)
165 return RT_SEARCH_SIZE_MISMATCH
;
167 return RT_SEARCH_FOUND
;
171 return RT_SEARCH_NOT_FOUND
;
174 static IPTR
RT_ShowErrorMem (RTData
* rtd
, int rtt
, MemoryResource
* rt
,
175 IPTR ret
, int mode
, const char * file
, ULONG line
, va_list args
)
177 const char * modestr
= (mode
== RT_FREE
) ? "Free" : "Check";
183 memory
= va_arg (args
, APTR
);
184 size
= va_arg (args
, ULONG
);
188 case RT_SEARCH_FOUND
:
189 if (rt
->Node
.Flags
& RTNF_DONT_FREE
)
191 kprintf ("RT%s: Try to free read-only resource: Memory\n"
198 , rt
->Node
.File
, rt
->Node
.Line
204 case RT_SEARCH_NOT_FOUND
:
205 kprintf ("RT%s: Memory not found\n"
207 " MemPtr=%p Size=%ld\n"
215 case RT_SEARCH_SIZE_MISMATCH
:
216 kprintf ("RT%s: Size mismatch (Allocated=%ld, Check=%ld)\n"
218 " AllocMem()'d at %s:%d\n"
219 " MemPtr=%p Size=%ld Flags=%08lx\n"
224 , rt
->Node
.File
, rt
->Node
.Line
225 , rt
->Memory
, rt
->Size
, rt
->Flags
233 kprintf ("RTExit: Memory was not freed\n"
234 " AllocMem()'d at %s:%d\n"
235 " MemPtr=%p Size=%ld Flags=%08lx\n"
236 , rt
->Node
.File
, rt
->Node
.Line
237 , rt
->Memory
, rt
->Size
, rt
->Flags
242 } /* RT_ShowErrorMem */
244 static IPTR
RT_AllocVec (RTData
* rtd
, MemoryResource
* rt
, va_list args
, BOOL
* success
)
247 rt
->Size
= va_arg (args
, ULONG
);
248 rt
->Flags
= va_arg (args
, ULONG
);
250 rt
->Memory
= AllocVec (rt
->Size
, rt
->Flags
);
255 return (IPTR
)(rt
->Memory
);
258 static IPTR
RT_FreeVec (RTData
* rtd
, MemoryResource
* rt
)
262 FreeVec (rt
->Memory
);
267 static IPTR
RT_SearchVec (RTData
* rtd
, int rtt
, MemoryResource
** rtptr
,
273 memory
= va_arg (args
, APTR
);
278 return RT_SEARCH_FOUND
;
281 ForeachNode (&rtd
->rtd_ResHash
[rtt
][CALCHASH(memory
)], rt
)
283 if (rt
->Memory
== memory
)
287 return RT_SEARCH_FOUND
;
291 return RT_SEARCH_NOT_FOUND
;
294 static IPTR
RT_ShowErrorVec (RTData
* rtd
, int rtt
, MemoryResource
* rt
,
295 IPTR ret
, int mode
, const char * file
, ULONG line
, va_list args
)
297 const char * modestr
= (mode
== RT_FREE
) ? "Free" : "Check";
303 memory
= va_arg (args
, APTR
);
304 size
= va_arg (args
, ULONG
);
308 case RT_SEARCH_FOUND
:
309 if (rt
&& rt
->Node
.Flags
& RTNF_DONT_FREE
)
311 kprintf ("RT%s: Try to free read-only resource: Vec-Memory\n"
318 , rt
->Node
.File
, rt
->Node
.Line
325 case RT_SEARCH_NOT_FOUND
:
326 kprintf ("RT%s: Memory not found\n"
328 " MemPtr=%p Size=%ld\n"
340 kprintf ("RTExit: Memory was not freed\n"
341 " AllocVec()'d at %s:%d\n"
342 " MemPtr=%p Size=%ld Flags=%08lx\n"
343 , rt
->Node
.File
, rt
->Node
.Line
344 , rt
->Memory
, rt
->Size
, rt
->Flags
349 } /* RT_ShowErrorVec */
352 /**************************************
354 **************************************/
356 static IPTR
RT_CreatePort (RTData
* rtd
, PortResource
* rt
, va_list args
, BOOL
* success
)
362 name
= va_arg (args
, STRPTR
);
363 pri
= va_arg (args
, LONG
);
365 if (!CheckPtr (name
, NULL_PTR
))
367 kprintf ("CreatePort(): Illegal name pointer\n"
368 " name=%p at %s:%d\n"
370 , rt
->Node
.File
, rt
->Node
.Line
375 rt
->Port
= CreatePort (name
, pri
);
380 return (IPTR
)(rt
->Port
);
381 } /* RT_CreatePort */
383 static IPTR
RT_DeletePort (RTData
* rtd
, PortResource
* rt
)
385 DeletePort (rt
->Port
);
390 static IPTR
RT_ShowErrorPort (RTData
* rtd
, int rtt
, PortResource
* rt
,
391 IPTR ret
, int mode
, const char * file
, ULONG line
, va_list args
)
395 const char * modestr
= (mode
== RT_FREE
) ? "Close" : "Check";
396 struct MsgPort
* port
;
398 port
= va_arg (args
, struct MsgPort
*);
402 case RT_SEARCH_FOUND
:
403 if (rt
->Node
.Flags
& RTNF_DONT_FREE
)
405 kprintf ("RT%s: Try to free read-only resource: MsgPort\n"
408 " Port=%p (Name=%s Pri=%d)\n"
412 , rt
->Node
.File
, rt
->Node
.Line
414 , rt
->Port
->mp_Node
.ln_Name
415 ? rt
->Port
->mp_Node
.ln_Name
417 , rt
->Port
->mp_Node
.ln_Pri
422 case RT_SEARCH_NOT_FOUND
:
423 kprintf ("RT%s: Port not found\n"
437 kprintf ("RTExit: Port was not closed\n"
439 " Port=%p (Name=%s Pri=%d)\n"
440 , rt
->Node
.File
, rt
->Node
.Line
442 , rt
->Port
->mp_Node
.ln_Name
443 ? rt
->Port
->mp_Node
.ln_Name
445 , rt
->Port
->mp_Node
.ln_Pri
450 } /* RT_ShowErrorPort */
452 static IPTR
RT_CheckPort (RTData
* rtd
, int rtt
,
453 const char * file
, ULONG line
,
454 ULONG op
, va_list args
)
459 if (RT_Search (rtd
, rtt
, (RTNode
**)prt
, args
) != RT_SEARCH_FOUND
)
466 struct MsgPort
* port
;
467 struct Message
* message
;
469 port
= va_arg (args
, struct MsgPort
*);
470 message
= va_arg (args
, struct Message
*);
474 kprintf ("PutMsg(): Illegal port pointer\n"
475 " Port=%p Message=%p at %s:%d\n"
482 else if (CheckPtr (message
, 0))
484 kprintf ("PutMsg(): Illegal message pointer\n"
485 " Port=%p Message=%p at %s:%d\n"
493 PutMsg (port
, message
);
500 struct MsgPort
* port
;
502 port
= va_arg (args
, struct MsgPort
*);
506 kprintf ("GetMsg(): Illegal port pointer\n"
507 " Port=%p at %s:%d\n"
515 return (IPTR
) GetMsg (port
);
524 /**************************************
526 **************************************/
528 static IPTR
RT_OpenLibrary (RTData
* rtd
, LibraryResource
* rt
, va_list args
, BOOL
* success
)
531 rt
->Name
= va_arg (args
, STRPTR
);
532 rt
->Version
= va_arg (args
, ULONG
);
534 rt
->Lib
= OpenLibrary (rt
->Name
, rt
->Version
);
539 return (IPTR
)(rt
->Lib
);
540 } /* RT_OpenLibrary */
542 static IPTR
RT_CloseLibrary (RTData
* rtd
, LibraryResource
* rt
)
545 CloseLibrary (rt
->Lib
);
548 } /* RT_CloseLibrary */
550 static IPTR
RT_ShowErrorLib (RTData
* rtd
, int rtt
, LibraryResource
* rt
,
551 IPTR ret
, int mode
, const char * file
, ULONG line
, va_list args
)
556 const char * modestr
= (mode
== RT_FREE
) ? "Free" : "Check";
557 struct Library
* base
;
559 base
= va_arg (args
, struct Library
*);
563 case RT_SEARCH_FOUND
:
564 if (rt
->Node
.Flags
& RTNF_DONT_FREE
)
566 kprintf ("RT%s: Try to free read-only resource: Library\n"
573 , rt
->Node
.File
, rt
->Node
.Line
579 case RT_SEARCH_NOT_FOUND
:
580 kprintf ("RT%s: Library not found\n"
594 kprintf ("RTExit: Library was not closed\n"
596 " Base=%p Name=%s Version=%ld\n"
597 , rt
->Node
.File
, rt
->Node
.Line
598 , rt
->Lib
, rt
->Name
, rt
->Version
603 } /* RT_ShowErrorLib */