2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
5 Desc: Allocate some memory
8 #include <exec/alerts.h>
9 #include <exec/execbase.h>
10 #include <aros/libcall.h>
11 #include <aros/asmcall.h>
13 #include <aros/macros.h>
14 #include <aros/arossupportbase.h>
15 #include <exec/memory.h>
16 #include <exec/memheaderext.h>
17 #include <exec/nodes.h>
19 #include <dos/dosextens.h>
20 #include <proto/exec.h>
22 #include "exec_debug.h"
24 #ifndef DEBUG_AllocMem
25 # define DEBUG_AllocMem 0
32 #include "exec_intern.h"
36 /*****************************************************************************
40 AROS_LH2(APTR
, AllocMem
,
43 AROS_LHA(ULONG
, byteSize
, D0
),
44 AROS_LHA(ULONG
, requirements
, D1
),
47 struct ExecBase
*, SysBase
, 33, Exec
)
50 Allocate some memory from the sytem memory pool with the given
54 byteSize - Number of bytes you want to get
55 requirements - Type of memory
58 A pointer to the number of bytes you wanted or NULL if the memory
62 The memory is aligned to sizeof(struct MemChunk). All requests
63 are rounded up to a multiple of that size.
66 mytask=(struct Task *)AllocMem(sizeof(struct Task),MEMF_PUBLIC|MEMF_CLEAR);
75 ******************************************************************************/
80 struct checkMemHandlersState cmhs
;
81 ULONG origSize
= byteSize
;
83 D(if (SysBase
->DebugAROSBase
))
84 D(bug("Call AllocMem (%d, %08x)\n", byteSize
, requirements
));
86 /* Zero bytes requested? May return everything ;-). */
90 /* Make room for safety walls around allocated block and an some more extra space
91 for other interesting things, actually --> the size.
93 This all will look like this:
95 [MEMCHUNK_FOR_EXTRA_STUFF][BEFORE-MUNGWALL][<alloced-memory-for-user>][AFTER_MUNGWALL]
97 MEMCHUNK_FOR_EXTRA_STUFF is used (amongst other things) to save the original alloc
98 size (byteSize) param. So it is possible in FreeMem to check, if freemem size
99 matches allocmem size or not.
101 if (PrivExecBase(SysBase
)->IntFlags
& EXECF_MungWall
)
102 byteSize
+= MUNGWALL_TOTAL_SIZE
;
104 cmhs
.cmhs_CurNode
= (struct Node
*)SysBase
->ex_MemHandlers
.mlh_Head
;
105 cmhs
.cmhs_Data
.memh_RequestSize
= byteSize
;
106 cmhs
.cmhs_Data
.memh_RequestFlags
= requirements
;
107 cmhs
.cmhs_Data
.memh_Flags
= 0;
111 res
= nommu_AllocMem(byteSize
, requirements
, SysBase
);
112 } while (res
== NULL
&& checkMemHandlers(&cmhs
, SysBase
) == MEM_TRY_AGAIN
);
115 RT_Add (RTT_MEMORY
, res
, origSize
);
118 res
= MungWall_Build(res
, NULL
, origSize
, requirements
, "AllocMem", __builtin_return_address(0), SysBase
);
120 /* Set DOS error if called from a process */
123 struct Process
*process
= (struct Process
*)FindTask(NULL
);
124 if (process
->pr_Task
.tc_Node
.ln_Type
== NT_PROCESS
)
125 process
->pr_Result2
= ERROR_NO_FREE_STORE
;
129 if (SysBase
->DebugAROSBase
)
130 bug("AllocMem result: 0x%p\n", res
);