2 Copyright © 1995-2015, 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");
87 if (SysBase
->DebugAROSBase
)
89 bug("Call AllocMem (%d, %08x)\n", byteSize
, requirements
);
93 /* 0-sized allocation results in returning NULL (API guarantee) */
97 /* Make room for safety walls around allocated block and an some more extra space
98 for other interesting things, actually --> the size.
100 This all will look like this:
102 [MEMCHUNK_FOR_EXTRA_STUFF][BEFORE-MUNGWALL][<alloced-memory-for-user>][AFTER_MUNGWALL]
104 MEMCHUNK_FOR_EXTRA_STUFF is used (amongst other things) to save the original alloc
105 size (byteSize) param. So it is possible in FreeMem to check, if freemem size
106 matches allocmem size or not.
108 if (PrivExecBase(SysBase
)->IntFlags
& EXECF_MungWall
)
109 byteSize
+= MUNGWALL_TOTAL_SIZE
;
111 cmhs
.cmhs_CurNode
= (struct Node
*)SysBase
->ex_MemHandlers
.mlh_Head
;
112 cmhs
.cmhs_Data
.memh_RequestSize
= byteSize
;
113 cmhs
.cmhs_Data
.memh_RequestFlags
= requirements
;
114 cmhs
.cmhs_Data
.memh_Flags
= 0;
118 res
= nommu_AllocMem(byteSize
, requirements
, &loc
, SysBase
);
119 } while (res
== NULL
&& checkMemHandlers(&cmhs
, SysBase
) == MEM_TRY_AGAIN
);
122 RT_Add (RTT_MEMORY
, res
, origSize
);
125 res
= MungWall_Build(res
, NULL
, origSize
, requirements
, &loc
, SysBase
);
127 /* Set DOS error if called from a process */
130 struct Process
*process
= (struct Process
*)GET_THIS_TASK
;
131 if (process
&& process
->pr_Task
.tc_Node
.ln_Type
== NT_PROCESS
)
132 process
->pr_Result2
= ERROR_NO_FREE_STORE
;
136 if (SysBase
->DebugAROSBase
)
138 bug("AllocMem result: 0x%p\n", res
);