2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
5 Desc: Exec utility functions.
8 #include <exec/lists.h>
9 #include <exec/tasks.h>
10 #include <exec/memory.h>
11 #include <exec/execbase.h>
13 #include <proto/exec.h>
16 #include "exec_util.h"
18 /*****************************************************************************
21 #include "exec_util.h"
23 APTR
Exec_AllocTaskMem (
29 struct ExecBase
*SysBase
)
32 Allocate memory which will be freed when the task is removed.
35 task - The memory will be freed when this task is removed.
36 size - How much memory.
37 req - What memory. See AllocMem() for details.
40 Adress to a memory block or NULL if no memory was available.
49 AllocMem(), FreeTaskMem()
52 The memory is allocated and queued in the tc_MemList. This
53 list is freed in RemTask().
55 ******************************************************************************/
60 ml
= AllocMem (sizeof (struct MemList
), MEMF_ANY
);
61 mem
= AllocMem (size
, req
);
66 FreeMem (ml
, sizeof (struct MemList
));
74 ml
->ml_NumEntries
= 1;
75 ml
->ml_ME
[0].me_Addr
= mem
;
76 ml
->ml_ME
[0].me_Length
= size
;
79 AddHead (&task
->tc_MemEntry
, &ml
->ml_Node
);
86 /*****************************************************************************
89 #include "exec_util.h"
91 void Exec_FreeTaskMem (
96 struct ExecBase
*SysBase
)
99 Freeate memory which will be freed when the task is removed.
102 task - The memory will be freed when this task is removed.
103 size - How much memory.
104 req - What memory. See FreeMem() for details.
107 Adress to a memory block or NULL if no memory was available.
116 FreeMem(), FreeTaskMem()
119 The memory is allocated and queued in the tc_MemList. This
120 list is freed in RemTask().
122 ******************************************************************************/
124 struct MemList
* ml
, * next
;
128 ForeachNodeSafe (&task
->tc_MemEntry
, ml
, next
)
131 Quick check: If the node was allocated by AllocTaskMem(),
132 then it has only one entry.
134 if (ml
->ml_NumEntries
== 1
135 && ml
->ml_ME
[0].me_Addr
== mem
138 Remove (&ml
->ml_Node
);
141 FreeMem (ml
->ml_ME
[0].me_Addr
, ml
->ml_ME
[0].me_Length
);
142 FreeMem (ml
, sizeof (struct MemList
));
152 /*****************************************************************************
155 #include "exec_util.h"
157 struct Task
* Exec_FindTaskByID(
161 struct ExecBase
*SysBase
)
164 Scan through the task lists searching for the task whose
165 et_UniqueID field matches.
168 id - The task ID to match.
171 Address of the Task control structure that matches, or
184 ******************************************************************************/
190 First up, check ThisTask. It could be NULL because of exec_init.c
192 if (SysBase
->ThisTask
!= NULL
)
194 et
= GetETask(SysBase
->ThisTask
);
195 if (et
!= NULL
&& et
->et_UniqueID
== id
)
196 return SysBase
->ThisTask
;
199 /* Next, go through the ready list */
200 ForeachNode(&SysBase
->TaskReady
, t
)
203 if (et
!= NULL
&& et
->et_UniqueID
== id
)
207 /* Finally, go through the wait list */
208 ForeachNode(&SysBase
->TaskWait
, t
)
211 if (et
!= NULL
&& et
->et_UniqueID
== id
)
218 /*****************************************************************************
221 #include "exec_util.h"
223 struct ETask
* Exec_FindChild(
227 struct ExecBase
*SysBase
)
230 Scan through the current tasks children list searching for the task
231 whose et_UniqueID field matches.
234 id - The task ID to match.
237 Address of the ETask structure that matches, or
250 ******************************************************************************/
253 struct ETask
*thisET
;
255 thisET
= GetETask(FindTask(NULL
));
258 ForeachNode (&thisET
->et_Children
, et
)
260 if (et
->et_UniqueID
== id
)
268 Exec_InitETask(struct Task
*task
, struct ETask
*et
, struct ExecBase
*SysBase
)
270 et
->et_Parent
= FindTask(NULL
);
271 NEWLIST(&et
->et_Children
);
273 /* Initialise the message list */
274 NEWLIST(&et
->et_TaskMsgPort
.mp_MsgList
);
275 et
->et_TaskMsgPort
.mp_Flags
= PA_SIGNAL
;
276 et
->et_TaskMsgPort
.mp_Node
.ln_Type
= NT_MSGPORT
;
277 et
->et_TaskMsgPort
.mp_SigTask
= task
;
278 et
->et_TaskMsgPort
.mp_SigBit
= SIGB_CHILD
;
280 /* Initialise the trap fields */
281 et
->et_TrapAlloc
= SysBase
->TaskTrapAlloc
;
286 int len
= strlen(task
->tc_Node
.ln_Name
) + 1;
287 IntETask(et
)->iet_Me
= AllocVec(len
, MEMF_CLEAR
|MEMF_PUBLIC
);
288 if (IntETask(et
)->iet_Me
!= NULL
)
289 CopyMem(task
->tc_Node
.ln_Name
, IntETask(et
)->iet_Me
, len
);
295 while(et
->et_UniqueID
== 0)
298 * Add some fuzz on wrapping. Its likely that the early numbers
299 * where taken by somebody else.
301 if(++SysBase
->ex_TaskID
== 0)
302 SysBase
->ex_TaskID
= 1024;
305 if(Exec_FindTaskByID(SysBase
->ex_TaskID
, SysBase
) == NULL
)
306 et
->et_UniqueID
= SysBase
->ex_TaskID
;