New bitmap method SetRGBConversionFunction which can be used to
[tangerine.git] / compiler / arossupport / rt_exec.c
blobca0b9038deefea5a3a3a9cedb714de1c98495ab7
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Basic functions for ressource tracking
6 Lang: english
7 */
9 #include "rt.h"
10 #if 0
11 #define ENABLE_RT 0 /* no RT inside this file */
12 #define RT_INTERNAL 1
13 #include <aros/rt.h>
15 #include <exec/lists.h>
16 #include <aros/system.h>
17 #include <exec/execbase.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include "etask.h"
21 #endif
22 #include <exec/tasks.h>
23 #include <exec/ports.h>
24 #include <exec/memory.h>
25 #include <stdarg.h>
26 #include <proto/exec.h>
27 #include <proto/arossupport.h>
28 #include <proto/alib.h>
30 typedef struct
32 RTNode Node;
33 APTR Memory;
34 ULONG Size;
35 ULONG Flags;
37 MemoryResource;
39 typedef struct
41 RTNode Node;
42 struct MsgPort * Port;
44 PortResource;
46 typedef struct
48 RTNode Node;
49 struct Library * Lib;
50 STRPTR Name;
51 ULONG Version;
53 LibraryResource;
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[] =
76 { /* RTT_ALLOCMEM */
77 sizeof (MemoryResource),
78 (RT_AllocFunc) RT_AllocMem,
79 (RT_FreeFunc) RT_FreeMem,
80 (RT_SearchFunc)RT_SearchMem,
81 (RT_ShowError) RT_ShowErrorMem,
82 NULL, /* Check */
84 { /* RTT_ALLOCVEC */
85 sizeof (MemoryResource),
86 (RT_AllocFunc) RT_AllocVec,
87 (RT_FreeFunc) RT_FreeVec,
88 (RT_SearchFunc)RT_SearchVec,
89 (RT_ShowError) RT_ShowErrorVec,
90 NULL, /* Check */
92 { /* RTT_PORT */
93 sizeof (PortResource),
94 (RT_AllocFunc) RT_CreatePort,
95 (RT_FreeFunc) RT_DeletePort,
96 RT_Search,
97 (RT_ShowError) RT_ShowErrorPort,
98 (RT_CheckFunc) RT_CheckPort,
100 { /* RTT_LIBRARY */
101 sizeof (LibraryResource),
102 (RT_AllocFunc) RT_OpenLibrary,
103 (RT_FreeFunc) RT_CloseLibrary,
104 RT_Search,
105 (RT_ShowError) RT_ShowErrorLib,
106 NULL, /* Check */
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 /**************************************
123 RT Memory
124 **************************************/
126 static IPTR RT_AllocMem (RTData * rtd, MemoryResource * rt, va_list args, BOOL * success)
128 AROS_GET_SYSBASE_OK
129 rt->Size = va_arg (args, ULONG);
130 rt->Flags = va_arg (args, ULONG);
132 rt->Memory = AllocMem (rt->Size, rt->Flags);
134 if (rt->Memory)
135 *success = TRUE;
137 return (IPTR)(rt->Memory);
138 } /* RT_AllocMem */
140 static IPTR RT_FreeMem (RTData * rtd, MemoryResource * rt)
142 AROS_GET_SYSBASE_OK
143 FreeMem (rt->Memory, rt->Size);
145 return TRUE;
146 } /* RT_FreeMem */
148 static IPTR RT_SearchMem (RTData * rtd, int rtt, MemoryResource ** rtptr,
149 va_list args)
151 MemoryResource * rt;
152 APTR memory;
153 ULONG size;
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)
162 *rtptr = rt;
164 if (rt->Size != size)
165 return RT_SEARCH_SIZE_MISMATCH;
167 return RT_SEARCH_FOUND;
171 return RT_SEARCH_NOT_FOUND;
172 } /* RT_SearchMem */
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";
178 APTR memory;
179 ULONG size;
181 if (mode != RT_EXIT)
183 memory = va_arg (args, APTR);
184 size = va_arg (args, ULONG);
186 switch (ret)
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"
192 " %s at %s:%d\n"
193 " Added at %s:%d\n"
194 " MemPtr=%p\n"
195 , modestr
196 , modestr
197 , file, line
198 , rt->Node.File, rt->Node.Line
199 , rt->Memory
202 break;
204 case RT_SEARCH_NOT_FOUND:
205 kprintf ("RT%s: Memory not found\n"
206 " %s at %s:%d\n"
207 " MemPtr=%p Size=%ld\n"
208 , modestr
209 , modestr
210 , file, line
211 , memory, size
213 break;
215 case RT_SEARCH_SIZE_MISMATCH:
216 kprintf ("RT%s: Size mismatch (Allocated=%ld, Check=%ld)\n"
217 " %s at %s:%d\n"
218 " AllocMem()'d at %s:%d\n"
219 " MemPtr=%p Size=%ld Flags=%08lx\n"
220 , modestr
221 , rt->Size, size
222 , modestr
223 , file, line
224 , rt->Node.File, rt->Node.Line
225 , rt->Memory, rt->Size, rt->Flags
227 break;
229 } /* switch */
231 else
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
241 return ret;
242 } /* RT_ShowErrorMem */
244 static IPTR RT_AllocVec (RTData * rtd, MemoryResource * rt, va_list args, BOOL * success)
246 AROS_GET_SYSBASE_OK
247 rt->Size = va_arg (args, ULONG);
248 rt->Flags = va_arg (args, ULONG);
250 rt->Memory = AllocVec (rt->Size, rt->Flags);
252 if (rt->Memory)
253 *success = TRUE;
255 return (IPTR)(rt->Memory);
256 } /* RT_AllocVec */
258 static IPTR RT_FreeVec (RTData * rtd, MemoryResource * rt)
260 AROS_GET_SYSBASE_OK
261 if (rt)
262 FreeVec (rt->Memory);
264 return TRUE;
265 } /* RT_FreeVec */
267 static IPTR RT_SearchVec (RTData * rtd, int rtt, MemoryResource ** rtptr,
268 va_list args)
270 MemoryResource * rt;
271 APTR memory;
273 memory = va_arg (args, APTR);
275 if (!memory)
277 *rtptr = NULL;
278 return RT_SEARCH_FOUND;
281 ForeachNode (&rtd->rtd_ResHash[rtt][CALCHASH(memory)], rt)
283 if (rt->Memory == memory)
285 *rtptr = rt;
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";
298 APTR memory;
299 ULONG size;
301 if (mode != RT_EXIT)
303 memory = va_arg (args, APTR);
304 size = va_arg (args, ULONG);
306 switch (ret)
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"
312 " %s at %s:%d\n"
313 " Added at %s:%d\n"
314 " MemPtr=%p\n"
315 , modestr
316 , modestr
317 , file, line
318 , rt->Node.File, rt->Node.Line
319 , rt->Memory
322 break;
325 case RT_SEARCH_NOT_FOUND:
326 kprintf ("RT%s: Memory not found\n"
327 " %s at %s:%d\n"
328 " MemPtr=%p Size=%ld\n"
329 , modestr
330 , modestr
331 , file, line
332 , memory, size
334 break;
336 } /* switch */
338 else
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
348 return ret;
349 } /* RT_ShowErrorVec */
352 /**************************************
353 RT Ports
354 **************************************/
356 static IPTR RT_CreatePort (RTData * rtd, PortResource * rt, va_list args, BOOL * success)
358 AROS_GET_SYSBASE_OK
359 STRPTR name;
360 LONG pri;
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"
369 , name
370 , rt->Node.File, rt->Node.Line
372 return 0ul;
375 rt->Port = CreatePort (name, pri);
377 if (rt->Port)
378 *success = TRUE;
380 return (IPTR)(rt->Port);
381 } /* RT_CreatePort */
383 static IPTR RT_DeletePort (RTData * rtd, PortResource * rt)
385 DeletePort (rt->Port);
387 return TRUE;
388 } /* RT_ClosePort */
390 static IPTR RT_ShowErrorPort (RTData * rtd, int rtt, PortResource * rt,
391 IPTR ret, int mode, const char * file, ULONG line, va_list args)
393 if (mode != RT_EXIT)
395 const char * modestr = (mode == RT_FREE) ? "Close" : "Check";
396 struct MsgPort * port;
398 port = va_arg (args, struct MsgPort *);
400 switch (ret)
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"
406 " %s at %s:%d\n"
407 " Added at %s:%d\n"
408 " Port=%p (Name=%s Pri=%d)\n"
409 , modestr
410 , modestr
411 , file, line
412 , rt->Node.File, rt->Node.Line
413 , rt->Port
414 , rt->Port->mp_Node.ln_Name
415 ? rt->Port->mp_Node.ln_Name
416 : NULL
417 , rt->Port->mp_Node.ln_Pri
420 break;
422 case RT_SEARCH_NOT_FOUND:
423 kprintf ("RT%s: Port not found\n"
424 " %s at %s:%d\n"
425 " Port=%p\n"
426 , modestr
427 , modestr
428 , file, line
429 , port
431 break;
433 } /* switch */
435 else
437 kprintf ("RTExit: Port was not closed\n"
438 " Opened at %s:%d\n"
439 " Port=%p (Name=%s Pri=%d)\n"
440 , rt->Node.File, rt->Node.Line
441 , rt->Port
442 , rt->Port->mp_Node.ln_Name
443 ? rt->Port->mp_Node.ln_Name
444 : NULL
445 , rt->Port->mp_Node.ln_Pri
449 return ret;
450 } /* RT_ShowErrorPort */
452 static IPTR RT_CheckPort (RTData * rtd, int rtt,
453 const char * file, ULONG line,
454 ULONG op, va_list args)
456 PortResource * rt;
457 APTR prt = &rt;
459 if (RT_Search (rtd, rtt, (RTNode **)prt, args) != RT_SEARCH_FOUND)
460 rt = NULL;
462 switch (op)
464 case RTTO_PutMsg:
466 struct MsgPort * port;
467 struct Message * message;
469 port = va_arg (args, struct MsgPort *);
470 message = va_arg (args, struct Message *);
472 if (!rt)
474 kprintf ("PutMsg(): Illegal port pointer\n"
475 " Port=%p Message=%p at %s:%d\n"
476 , port, message
477 , file, line
480 return -1;
482 else if (CheckPtr (message, 0))
484 kprintf ("PutMsg(): Illegal message pointer\n"
485 " Port=%p Message=%p at %s:%d\n"
486 , port, message
487 , file, line
490 return -1;
493 PutMsg (port, message);
495 return 0;
498 case RTTO_GetMsg:
500 struct MsgPort * port;
502 port = va_arg (args, struct MsgPort *);
504 if (!rt)
506 kprintf ("GetMsg(): Illegal port pointer\n"
507 " Port=%p at %s:%d\n"
508 , port
509 , file, line
512 return 0L;
515 return (IPTR) GetMsg (port);
518 } /* switch (op) */
520 return 0L;
521 } /* RT_CheckPort */
524 /**************************************
525 RT Libraries
526 **************************************/
528 static IPTR RT_OpenLibrary (RTData * rtd, LibraryResource * rt, va_list args, BOOL * success)
530 AROS_GET_SYSBASE_OK
531 rt->Name = va_arg (args, STRPTR);
532 rt->Version = va_arg (args, ULONG);
534 rt->Lib = OpenLibrary (rt->Name, rt->Version);
536 if (rt->Lib)
537 *success = TRUE;
539 return (IPTR)(rt->Lib);
540 } /* RT_OpenLibrary */
542 static IPTR RT_CloseLibrary (RTData * rtd, LibraryResource * rt)
544 AROS_GET_SYSBASE_OK
545 CloseLibrary (rt->Lib);
547 return TRUE;
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)
554 if (mode != RT_EXIT)
556 const char * modestr = (mode == RT_FREE) ? "Free" : "Check";
557 struct Library * base;
559 base = va_arg (args, struct Library *);
561 switch (ret)
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"
567 " %s at %s:%d\n"
568 " Added at %s:%d\n"
569 " LibBase=%p\n"
570 , modestr
571 , modestr
572 , file, line
573 , rt->Node.File, rt->Node.Line
574 , rt->Lib
577 break;
579 case RT_SEARCH_NOT_FOUND:
580 kprintf ("RT%s: Library not found\n"
581 " %s at %s:%d\n"
582 " Base=%p\n"
583 , modestr
584 , modestr
585 , file, line
586 , base
588 break;
590 } /* switch */
592 else
594 kprintf ("RTExit: Library was not closed\n"
595 " Opened at %s:%d\n"
596 " Base=%p Name=%s Version=%ld\n"
597 , rt->Node.File, rt->Node.Line
598 , rt->Lib, rt->Name, rt->Version
602 return ret;
603 } /* RT_ShowErrorLib */