2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
8 #include "exec_intern.h"
9 #include <proto/exec.h>
11 /*****************************************************************************
15 AROS_LH1(LONG
, AllocTrap
,
18 AROS_LHA(long, trapNum
, D0
),
21 struct ExecBase
*, SysBase
, 57, Exec
)
38 Very similar to AllocSignal()
40 *****************************************************************************/
44 struct Task
*ThisTask
;
48 ThisTask
= FindTask(NULL
);
49 mask
= GetTrapAlloc(ThisTask
);
51 /* Will any trap do? */
55 * To get the last nonzero bit in a number I use a&~a+1:
56 * Given a number that ends with a row of zeros xxxx1000
57 * I first toggle all bits in that number XXXX0111
58 * then add 1 to toggle all but the last 0 again XXXX1000
59 * and AND this with the original number 00001000
61 * And since ~a+1=-a I can use a&-a instead.
63 * And to get the last zero bit I finally use ~a&-~a.
65 mask1
= ~mask
& - ~mask
;
67 /* Is the bit already allocated? */
71 /* And get the bit number */
72 trapNum
= (mask1
& 0xff00 ? 8 : 0) + (mask1
& 0xf0f0 ? 4 : 0) +
73 (mask1
& 0xcccc ? 2 : 0) + (mask1
& 0xaaaa ? 1 : 0);
79 /* If trap bit is already allocated, return. */
84 if (ThisTask
->tc_Flags
& TF_ETASK
) {
85 struct ETask
*et
= ThisTask
->tc_UnionETask
.tc_ETask
;
87 et
->et_TrapAlloc
|= mask1
;
89 ThisTask
->tc_TrapAlloc
|= mask1
;