2 Copyright © 1995-2007, 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
)
128 rt
->Size
= va_arg (args
, ULONG
);
129 rt
->Flags
= va_arg (args
, ULONG
);
131 rt
->Memory
= AllocMem (rt
->Size
, rt
->Flags
);
136 return (IPTR
)(rt
->Memory
);
139 static IPTR
RT_FreeMem (RTData
* rtd
, MemoryResource
* rt
)
141 FreeMem (rt
->Memory
, rt
->Size
);
146 static IPTR
RT_SearchMem (RTData
* rtd
, int rtt
, MemoryResource
** rtptr
,
153 memory
= va_arg (args
, APTR
);
154 size
= va_arg (args
, ULONG
);
156 ForeachNode (&rtd
->rtd_ResHash
[rtt
][CALCHASH(memory
)], rt
)
158 if (rt
->Memory
== memory
)
162 if (rt
->Size
!= size
)
163 return RT_SEARCH_SIZE_MISMATCH
;
165 return RT_SEARCH_FOUND
;
169 return RT_SEARCH_NOT_FOUND
;
172 static IPTR
RT_ShowErrorMem (RTData
* rtd
, int rtt
, MemoryResource
* rt
,
173 IPTR ret
, int mode
, const char * file
, ULONG line
, va_list args
)
175 const char * modestr
= (mode
== RT_FREE
) ? "Free" : "Check";
181 memory
= va_arg (args
, APTR
);
182 size
= va_arg (args
, ULONG
);
186 case RT_SEARCH_FOUND
:
187 if (rt
->Node
.Flags
& RTNF_DONT_FREE
)
189 kprintf ("RT%s: Try to free read-only resource: Memory\n"
196 , rt
->Node
.File
, rt
->Node
.Line
202 case RT_SEARCH_NOT_FOUND
:
203 kprintf ("RT%s: Memory not found\n"
205 " MemPtr=%p Size=%ld\n"
213 case RT_SEARCH_SIZE_MISMATCH
:
214 kprintf ("RT%s: Size mismatch (Allocated=%ld, Check=%ld)\n"
216 " AllocMem()'d at %s:%d\n"
217 " MemPtr=%p Size=%ld Flags=%08lx\n"
222 , rt
->Node
.File
, rt
->Node
.Line
223 , rt
->Memory
, rt
->Size
, rt
->Flags
231 kprintf ("RTExit: Memory was not freed\n"
232 " AllocMem()'d at %s:%d\n"
233 " MemPtr=%p Size=%ld Flags=%08lx\n"
234 , rt
->Node
.File
, rt
->Node
.Line
235 , rt
->Memory
, rt
->Size
, rt
->Flags
240 } /* RT_ShowErrorMem */
242 static IPTR
RT_AllocVec (RTData
* rtd
, MemoryResource
* rt
, va_list args
, BOOL
* success
)
244 rt
->Size
= va_arg (args
, ULONG
);
245 rt
->Flags
= va_arg (args
, ULONG
);
247 rt
->Memory
= AllocVec (rt
->Size
, rt
->Flags
);
252 return (IPTR
)(rt
->Memory
);
255 static IPTR
RT_FreeVec (RTData
* rtd
, MemoryResource
* rt
)
258 FreeVec (rt
->Memory
);
263 static IPTR
RT_SearchVec (RTData
* rtd
, int rtt
, MemoryResource
** rtptr
,
269 memory
= va_arg (args
, APTR
);
274 return RT_SEARCH_FOUND
;
277 ForeachNode (&rtd
->rtd_ResHash
[rtt
][CALCHASH(memory
)], rt
)
279 if (rt
->Memory
== memory
)
283 return RT_SEARCH_FOUND
;
287 return RT_SEARCH_NOT_FOUND
;
290 static IPTR
RT_ShowErrorVec (RTData
* rtd
, int rtt
, MemoryResource
* rt
,
291 IPTR ret
, int mode
, const char * file
, ULONG line
, va_list args
)
293 const char * modestr
= (mode
== RT_FREE
) ? "Free" : "Check";
299 memory
= va_arg (args
, APTR
);
300 size
= va_arg (args
, ULONG
);
304 case RT_SEARCH_FOUND
:
305 if (rt
&& rt
->Node
.Flags
& RTNF_DONT_FREE
)
307 kprintf ("RT%s: Try to free read-only resource: Vec-Memory\n"
314 , rt
->Node
.File
, rt
->Node
.Line
321 case RT_SEARCH_NOT_FOUND
:
322 kprintf ("RT%s: Memory not found\n"
324 " MemPtr=%p Size=%ld\n"
336 kprintf ("RTExit: Memory was not freed\n"
337 " AllocVec()'d at %s:%d\n"
338 " MemPtr=%p Size=%ld Flags=%08lx\n"
339 , rt
->Node
.File
, rt
->Node
.Line
340 , rt
->Memory
, rt
->Size
, rt
->Flags
345 } /* RT_ShowErrorVec */
348 /**************************************
350 **************************************/
352 static IPTR
RT_CreatePort (RTData
* rtd
, PortResource
* rt
, va_list args
, BOOL
* success
)
357 name
= va_arg (args
, STRPTR
);
358 pri
= va_arg (args
, LONG
);
360 if (!CheckPtr (name
, NULL_PTR
))
362 kprintf ("CreatePort(): Illegal name pointer\n"
363 " name=%p at %s:%d\n"
365 , rt
->Node
.File
, rt
->Node
.Line
370 rt
->Port
= CreatePort (name
, pri
);
375 return (IPTR
)(rt
->Port
);
376 } /* RT_CreatePort */
378 static IPTR
RT_DeletePort (RTData
* rtd
, PortResource
* rt
)
380 DeletePort (rt
->Port
);
385 static IPTR
RT_ShowErrorPort (RTData
* rtd
, int rtt
, PortResource
* rt
,
386 IPTR ret
, int mode
, const char * file
, ULONG line
, va_list args
)
390 const char * modestr
= (mode
== RT_FREE
) ? "Close" : "Check";
391 struct MsgPort
* port
;
393 port
= va_arg (args
, struct MsgPort
*);
397 case RT_SEARCH_FOUND
:
398 if (rt
->Node
.Flags
& RTNF_DONT_FREE
)
400 kprintf ("RT%s: Try to free read-only resource: MsgPort\n"
403 " Port=%p (Name=%s Pri=%d)\n"
407 , rt
->Node
.File
, rt
->Node
.Line
409 , rt
->Port
->mp_Node
.ln_Name
410 ? rt
->Port
->mp_Node
.ln_Name
412 , rt
->Port
->mp_Node
.ln_Pri
417 case RT_SEARCH_NOT_FOUND
:
418 kprintf ("RT%s: Port not found\n"
432 kprintf ("RTExit: Port was not closed\n"
434 " Port=%p (Name=%s Pri=%d)\n"
435 , rt
->Node
.File
, rt
->Node
.Line
437 , rt
->Port
->mp_Node
.ln_Name
438 ? rt
->Port
->mp_Node
.ln_Name
440 , rt
->Port
->mp_Node
.ln_Pri
445 } /* RT_ShowErrorPort */
447 static IPTR
RT_CheckPort (RTData
* rtd
, int rtt
,
448 const char * file
, ULONG line
,
449 ULONG op
, va_list args
)
454 if (RT_Search (rtd
, rtt
, (RTNode
**)prt
, args
) != RT_SEARCH_FOUND
)
461 struct MsgPort
* port
;
462 struct Message
* message
;
464 port
= va_arg (args
, struct MsgPort
*);
465 message
= va_arg (args
, struct Message
*);
469 kprintf ("PutMsg(): Illegal port pointer\n"
470 " Port=%p Message=%p at %s:%d\n"
477 else if (CheckPtr (message
, 0))
479 kprintf ("PutMsg(): Illegal message pointer\n"
480 " Port=%p Message=%p at %s:%d\n"
488 PutMsg (port
, message
);
495 struct MsgPort
* port
;
497 port
= va_arg (args
, struct MsgPort
*);
501 kprintf ("GetMsg(): Illegal port pointer\n"
502 " Port=%p at %s:%d\n"
510 return (IPTR
) GetMsg (port
);
519 /**************************************
521 **************************************/
523 static IPTR
RT_OpenLibrary (RTData
* rtd
, LibraryResource
* rt
, va_list args
, BOOL
* success
)
525 rt
->Name
= va_arg (args
, STRPTR
);
526 rt
->Version
= va_arg (args
, ULONG
);
528 rt
->Lib
= OpenLibrary (rt
->Name
, rt
->Version
);
533 return (IPTR
)(rt
->Lib
);
534 } /* RT_OpenLibrary */
536 static IPTR
RT_CloseLibrary (RTData
* rtd
, LibraryResource
* rt
)
538 CloseLibrary (rt
->Lib
);
541 } /* RT_CloseLibrary */
543 static IPTR
RT_ShowErrorLib (RTData
* rtd
, int rtt
, LibraryResource
* rt
,
544 IPTR ret
, int mode
, const char * file
, ULONG line
, va_list args
)
549 const char * modestr
= (mode
== RT_FREE
) ? "Free" : "Check";
550 struct Library
* base
;
552 base
= va_arg (args
, struct Library
*);
556 case RT_SEARCH_FOUND
:
557 if (rt
->Node
.Flags
& RTNF_DONT_FREE
)
559 kprintf ("RT%s: Try to free read-only resource: Library\n"
566 , rt
->Node
.File
, rt
->Node
.Line
572 case RT_SEARCH_NOT_FOUND
:
573 kprintf ("RT%s: Library not found\n"
587 kprintf ("RTExit: Library was not closed\n"
589 " Base=%p Name=%s Version=%ld\n"
590 , rt
->Node
.File
, rt
->Node
.Line
591 , rt
->Lib
, rt
->Name
, rt
->Version
596 } /* RT_ShowErrorLib */