2 Copyright © 1995-2008, 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/config.h>
15 #include <aros/arossupportbase.h>
17 #include <exec/memory.h>
18 #include <exec/memheaderext.h>
19 #include <exec/nodes.h>
21 #include <dos/dosextens.h>
22 #include <proto/exec.h>
26 #include "exec_debug.h"
27 #ifndef DEBUG_AllocMem
28 # define DEBUG_AllocMem 0
35 # include <aros/debug.h>
37 struct checkMemHandlersState
39 struct Node
*cmhs_CurNode
;
40 struct MemHandlerData cmhs_Data
;
43 static APTR
stdAlloc(struct MemHeader
*mh
, ULONG byteSize
, ULONG requiements
);
44 static ULONG
checkMemHandlers(struct checkMemHandlersState
*cmhs
);
46 /*****************************************************************************
50 AROS_LH2(APTR
, AllocMem
,
53 AROS_LHA(ULONG
, byteSize
, D0
),
54 AROS_LHA(ULONG
, requirements
, D1
),
57 struct ExecBase
*, SysBase
, 33, Exec
)
60 Allocate some memory from the sytem memory pool with the given
64 byteSize - Number of bytes you want to get
65 requirements - Type of memory
68 A pointer to the number of bytes you wanted or NULL if the memory
72 The memory is aligned to sizeof(struct MemChunk). All requests
73 are rounded up to a multiple of that size.
76 mytask=(struct Task *)AllocMem(sizeof(struct Task),MEMF_PUBLIC|MEMF_CLEAR);
85 ******************************************************************************/
90 struct checkMemHandlersState cmhs
;
92 #if ENABLE_RT || AROS_MUNGWALL_DEBUG
93 ULONG origSize
= byteSize
;
94 ULONG origRequirements
= requirements
;
97 D(if (SysBase
->DebugAROSBase
))
98 D(bug("Call AllocMem (%d, %08x)\n", byteSize
, requirements
));
100 /* Zero bytes requested? May return everything ;-). */
104 #if AROS_MUNGWALL_DEBUG
105 /* Make room for safety walls around allocated block and an some more extra space
106 for other interesting things, actually --> the size.
108 This all will look like this:
110 [MEMCHUNK_FOR_EXTRA_STUFF][BEFORE-MUNGWALL][<alloced-memory-for-user>][AFTER_MUNGWALL]
112 The first ULONG in MEMCHUNK_FOR_EXTRA_STUFF is used to save the original alloc
113 size (byteSize) param. So it is possible in FreeMem to check, if freemem size
114 matches allocmem size or not.
117 byteSize
+= MUNGWALL_SIZE
* 2 + MUNGWALLHEADER_SIZE
;
118 #endif /* AROS_MUNGWALL_DEBUG */
120 /* First round byteSize to a multiple of MEMCHUNK_TOTAL */
121 byteSize
= AROS_ROUNDUP2(byteSize
, MEMCHUNK_TOTAL
);
123 cmhs
.cmhs_CurNode
= (struct Node
*)SysBase
->ex_MemHandlers
.mlh_Head
;
124 cmhs
.cmhs_Data
.memh_RequestSize
= byteSize
;
125 cmhs
.cmhs_Data
.memh_RequestFlags
= requirements
;
126 cmhs
.cmhs_Data
.memh_Flags
= 0;
128 /* Protect memory list against other tasks */
133 struct MemHeader
*mh
;
135 /* Loop over MemHeader structures */
136 ForeachNode(&SysBase
->MemList
, mh
)
139 Check for the right requirements and enough free memory.
140 The requirements are OK if there's no bit in the
141 'attributes' that isn't set in the 'mh->mh_Attributes'.
142 MEMF_CLEAR, MEMF_REVERSE and MEMF_NO_EXPUNGE are treated
143 as if they were always set in the memheader.
145 if((requirements
& ~(MEMF_CLEAR
|MEMF_REVERSE
|
146 MEMF_NO_EXPUNGE
|mh
->mh_Attributes
))
147 || mh
->mh_Free
< byteSize
)
150 if (mh
->mh_Attributes
& MEMF_MANAGED
)
152 struct MemHeaderExt
*mhe
= (struct MemHeaderExt
*)mh
;
154 res
= mhe
->mhe_Alloc(mhe
, byteSize
, &requirements
);
158 res
= stdAlloc(mh
, byteSize
, requirements
);
163 } while (res
== NULL
&& checkMemHandlers(&cmhs
) == MEM_TRY_AGAIN
);
167 if(res
&& (requirements
& MEMF_CLEAR
))
168 memset(res
, 0, byteSize
);
171 RT_Add (RTT_MEMORY
, res
, origSize
);
174 #if AROS_MUNGWALL_DEBUG
175 requirements
= origRequirements
;
179 struct MungwallHeader
*header
;
180 struct List
*allocmemlist
;
182 /* Save orig byteSize before wall (there is one room of MUNGWALLHEADER_SIZE
183 bytes before wall for such stuff (see above).
186 header
= (struct MungwallHeader
*)res
;
188 header
->mwh_magicid
= MUNGWALL_HEADER_ID
;
189 header
->mwh_allocsize
= origSize
;
191 /* Check whether list exists. AllocMem() might have been
192 called before PrepareAROSSupportBase(), which is responsible for
193 initialization of AllocMemList */
195 if (SysBase
->DebugAROSBase
)
197 allocmemlist
= (struct List
*)&((struct AROSSupportBase
*)SysBase
->DebugAROSBase
)->AllocMemList
;
199 AddHead(allocmemlist
, (struct Node
*)&header
->mwh_node
);
204 header
->mwh_node
.mln_Pred
= (struct MinNode
*)0x44332211;
205 header
->mwh_node
.mln_Succ
= (struct MinNode
*)0xCCBBAA99;
208 /* Skip to the start of the pre-wall */
209 res
+= MUNGWALLHEADER_SIZE
;
211 /* Initialize pre-wall */
212 BUILD_WALL(res
, 0xDB, MUNGWALL_SIZE
);
214 /* move over the block between the walls */
215 res
+= MUNGWALL_SIZE
;
217 /* Fill the block with weird stuff to exploit bugs in applications */
218 if (!(requirements
& MEMF_CLEAR
))
219 MUNGE_BLOCK(res
, MEMFILL_ALLOC
, byteSize
- MUNGWALL_SIZE
* 2 - MEMCHUNK_TOTAL
);
221 /* Initialize post-wall */
222 BUILD_WALL(res
+ origSize
, 0xDB, MUNGWALL_SIZE
+ AROS_ROUNDUP2(origSize
, MEMCHUNK_TOTAL
) - origSize
);
224 #endif /* AROS_MUNGWALL_DEBUG */
226 /* Set DOS error if called from a process */
229 struct Process
*process
= (struct Process
*)FindTask(NULL
);
230 if (process
->pr_Task
.tc_Node
.ln_Type
== NT_PROCESS
)
231 process
->pr_Result2
= ERROR_NO_FREE_STORE
;
235 if (SysBase
->DebugAROSBase
)
236 ReturnPtr ("AllocMem", APTR
, res
)
240 ReturnPtr ("AllocMem", APTR
, res
);
248 static APTR
stdAlloc(struct MemHeader
*mh
, ULONG byteSize
, ULONG requirements
)
250 struct MemChunk
*mc
=NULL
, *p1
, *p2
;
253 The free memory list is only single linked, i.e. to remove
254 elements from the list I need node's predessor. For the
255 first element I can use mh->mh_First instead of a real predessor.
257 p1
= (struct MemChunk
*)&mh
->mh_First
;
260 /* Is there anything in the list? */
266 #if !defined(NO_CONSISTENCY_CHECKS)
267 /* Consistency check: Check alignment restrictions */
268 if( ((IPTR
)p2
|(ULONG
)p2
->mc_Bytes
)
269 & (MEMCHUNK_TOTAL
-1) )
270 Alert(AN_MemCorrupt
|AT_DeadEnd
);
273 /* Check if the current block is large enough */
274 if(p2
->mc_Bytes
>=byteSize
)
278 /* Use this one if MEMF_REVERSE is not set.*/
279 if(!(requirements
&MEMF_REVERSE
))
281 /* Else continue - there may be more to come. */
284 /* Go to next block */
288 /* Check if this was the end */
291 #if !defined(NO_CONSISTENCY_CHECKS)
294 If the end of the last block+1 is bigger or equal to
295 the start of the current block something must be wrong.
297 if((UBYTE
*)p2
<=(UBYTE
*)p1
+p1
->mc_Bytes
)
298 Alert(AN_MemCorrupt
|AT_DeadEnd
);
302 /* Something found? */
306 Remember: if MEMF_REVERSE is set
307 p1 and p2 are now invalid.
312 /* Remove the block from the list and return it. */
313 if(p2
->mc_Bytes
== byteSize
)
315 /* Fits exactly. Just relink the list. */
316 p1
->mc_Next
= p2
->mc_Next
;
321 if(requirements
& MEMF_REVERSE
)
323 /* Return the last bytes. */
325 mc
=(struct MemChunk
*)((UBYTE
*)p2
+p2
->mc_Bytes
-byteSize
);
329 /* Return the first bytes. */
330 p1
->mc_Next
=(struct MemChunk
*)((UBYTE
*)p2
+byteSize
);
335 p1
->mc_Next
= p2
->mc_Next
;
336 p1
->mc_Bytes
= p2
->mc_Bytes
-byteSize
;
339 mh
->mh_Free
-= byteSize
;
346 ULONG
checkMemHandlers(struct checkMemHandlersState
*cmhs
)
349 struct Interrupt
*lmh
;
351 if (cmhs
->cmhs_Data
.memh_RequestFlags
& MEMF_NO_EXPUNGE
)
352 return MEM_DID_NOTHING
;
354 /* Loop over low memory handlers. Handlers can remove
355 themselves from the list while being invoked, thus
356 we need to be careful! */
359 lmh
= (struct Interrupt
*)cmhs
->cmhs_CurNode
;
360 (tmp
= lmh
->is_Node
.ln_Succ
);
361 lmh
= (struct Interrupt
*)(cmhs
->cmhs_CurNode
= tmp
)
366 ret
= AROS_UFC3 (LONG
, lmh
->is_Code
,
367 AROS_UFCA(struct MemHandlerData
*, &cmhs
->cmhs_Data
, A0
),
368 AROS_UFCA(APTR
, lmh
->is_Data
, A1
),
369 AROS_UFCA(struct ExecBase
*, SysBase
, A6
)
372 if (ret
== MEM_TRY_AGAIN
)
374 /* MemHandler said he did something. Try again. */
375 /* Is there any program that depends on this flag??? */
376 cmhs
->cmhs_Data
.memh_Flags
|= MEMHF_RECYCLE
;
377 return MEM_TRY_AGAIN
;
381 /* Nothing more to expect from this handler. */
382 cmhs
->cmhs_Data
.memh_Flags
&= ~MEMHF_RECYCLE
;
386 return MEM_DID_NOTHING
;