2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
5 Desc: Allocate some memory
9 #include <exec/alerts.h>
10 #include <exec/execbase.h>
11 #include <aros/libcall.h>
12 #include <aros/asmcall.h>
14 #include <aros/macros.h>
15 #include <aros/arossupportbase.h>
16 #include <exec/memory.h>
17 #include <exec/memheaderext.h>
18 #include <exec/nodes.h>
20 #include <dos/dosextens.h>
21 #include <proto/exec.h>
23 #include "exec_debug.h"
25 #ifndef DEBUG_AllocMem
26 # define DEBUG_AllocMem 0
33 #include "exec_intern.h"
34 #include "exec_util.h"
38 /*****************************************************************************
42 AROS_LH2(APTR
, AllocMem
,
45 AROS_LHA(IPTR
, byteSize
, D0
),
46 AROS_LHA(ULONG
, requirements
, D1
),
49 struct ExecBase
*, SysBase
, 33, Exec
)
52 Allocate some memory from the sytem memory pool with the given
56 byteSize - Number of bytes you want to get
57 requirements - Type of memory
60 A pointer to the number of bytes you wanted or NULL if the memory
64 The memory is aligned to sizeof(struct MemChunk). All requests
65 are rounded up to a multiple of that size.
68 mytask=(struct Task *)AllocMem(sizeof(struct Task),MEMF_PUBLIC|MEMF_CLEAR);
77 ******************************************************************************/
82 struct checkMemHandlersState cmhs
;
83 IPTR origSize
= byteSize
;
84 struct TraceLocation loc
= CURRENT_LOCATION("AllocMem");
86 D(if (SysBase
->DebugAROSBase
))
87 D(bug("Call AllocMem (%d, %08x)\n", byteSize
, requirements
));
89 /* 0-sized allocation results in returning NULL (API guarantee) */
93 /* Make room for safety walls around allocated block and an some more extra space
94 for other interesting things, actually --> the size.
96 This all will look like this:
98 [MEMCHUNK_FOR_EXTRA_STUFF][BEFORE-MUNGWALL][<alloced-memory-for-user>][AFTER_MUNGWALL]
100 MEMCHUNK_FOR_EXTRA_STUFF is used (amongst other things) to save the original alloc
101 size (byteSize) param. So it is possible in FreeMem to check, if freemem size
102 matches allocmem size or not.
104 if (PrivExecBase(SysBase
)->IntFlags
& EXECF_MungWall
)
105 byteSize
+= MUNGWALL_TOTAL_SIZE
;
107 cmhs
.cmhs_CurNode
= (struct Node
*)SysBase
->ex_MemHandlers
.mlh_Head
;
108 cmhs
.cmhs_Data
.memh_RequestSize
= byteSize
;
109 cmhs
.cmhs_Data
.memh_RequestFlags
= requirements
;
110 cmhs
.cmhs_Data
.memh_Flags
= 0;
114 res
= nommu_AllocMem(byteSize
, requirements
, &loc
, SysBase
);
115 } while (res
== NULL
&& checkMemHandlers(&cmhs
, SysBase
) == MEM_TRY_AGAIN
);
118 RT_Add (RTT_MEMORY
, res
, origSize
);
121 res
= MungWall_Build(res
, NULL
, origSize
, requirements
, &loc
, SysBase
);
123 /* Set DOS error if called from a process */
126 struct Process
*process
= (struct Process
*)FindTask(NULL
);
127 if (process
->pr_Task
.tc_Node
.ln_Type
== NT_PROCESS
)
128 process
->pr_Result2
= ERROR_NO_FREE_STORE
;
132 if (SysBase
->DebugAROSBase
)
133 bug("AllocMem result: 0x%p\n", res
);