2 Copyright © 1995-2001, 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 <proto/exec.h>
23 #include "exec_debug.h"
24 #ifndef DEBUG_AllocMem
25 # define DEBUG_AllocMem 0
32 # include <aros/debug.h>
34 struct checkMemHandlersState
36 struct Node
*cmhs_CurNode
;
37 struct MemHandlerData cmhs_Data
;
40 static APTR
stdAlloc(struct MemHeader
*mh
, ULONG byteSize
, ULONG requiements
);
41 static ULONG
checkMemHandlers(struct checkMemHandlersState
*cmhs
);
43 /*****************************************************************************
47 AROS_LH2(APTR
, AllocMem
,
50 AROS_LHA(ULONG
, byteSize
, D0
),
51 AROS_LHA(ULONG
, requirements
, D1
),
54 struct ExecBase
*, SysBase
, 33, Exec
)
57 Allocate some memory from the sytem memory pool with the given
61 byteSize - Number of bytes you want to get
62 requirements - Type of memory
65 A pointer to the number of bytes you wanted or NULL if the memory
69 The memory is aligned to sizeof(struct MemChunk). All requests
70 are rounded up to a multiple of that size.
73 mytask=(struct Task *)AllocMem(sizeof(struct Task),MEMF_PUBLIC|MEMF_CLEAR);
82 ******************************************************************************/
87 struct checkMemHandlersState cmhs
;
89 #if ENABLE_RT || AROS_MUNGWALL_DEBUG
90 ULONG origSize
= byteSize
;
91 ULONG origRequirements
= requirements
;
94 D(bug("Call AllocMem (%d, %08lx)\n", byteSize
, requirements
));
96 /* Zero bytes requested? May return everything ;-). */
100 #if AROS_MUNGWALL_DEBUG
101 /* Make room for safety walls around allocated block and an some more extra space
102 for other interesting things, actually --> the size.
104 This all will look like this:
106 [MEMCHUNK_FOR_EXTRA_STUFF][BEFORE-MUNGWALL][<alloced-memory-for-user>][AFTER_MUNGWALL]
108 The first ULONG in MEMCHUNK_FOR_EXTRA_STUFF is used to save the original alloc
109 size (byteSize) param. So it is possible in FreeMem to check, if freemem size
110 matches allocmem size or not.
113 byteSize
+= MUNGWALL_SIZE
* 2 + MUNGWALLHEADER_SIZE
;
114 #endif /* AROS_MUNGWALL_DEBUG */
116 /* First round byteSize to a multiple of MEMCHUNK_TOTAL */
117 byteSize
= AROS_ROUNDUP2(byteSize
, MEMCHUNK_TOTAL
);
119 cmhs
.cmhs_CurNode
= (struct Node
*)SysBase
->ex_MemHandlers
.mlh_Head
;
120 cmhs
.cmhs_Data
.memh_RequestSize
= byteSize
;
121 cmhs
.cmhs_Data
.memh_RequestFlags
= requirements
;
122 cmhs
.cmhs_Data
.memh_Flags
= 0;
124 /* Protect memory list against other tasks */
129 struct MemHeader
*mh
;
131 /* Loop over MemHeader structures */
132 ForeachNode(&SysBase
->MemList
, mh
)
135 Check for the right requirements and enough free memory.
136 The requirements are OK if there's no bit in the
137 'attributes' that isn't set in the 'mh->mh_Attributes'.
138 MEMF_CLEAR, MEMF_REVERSE and MEMF_NO_EXPUNGE are treated
139 as if they were always set in the memheader.
141 if((requirements
& ~(MEMF_CLEAR
|MEMF_REVERSE
|
142 MEMF_NO_EXPUNGE
|mh
->mh_Attributes
))
143 || mh
->mh_Free
< byteSize
)
146 if (mh
->mh_Attributes
& MEMF_MANAGED
)
148 struct MemHeaderExt
*mhe
= (struct MemHeaderExt
*)mh
;
150 res
= mhe
->mhe_Alloc(mhe
, byteSize
, &requirements
);
154 res
= stdAlloc(mh
, byteSize
, requirements
);
159 } while (res
== NULL
&& checkMemHandlers(&cmhs
) == MEM_TRY_AGAIN
);
164 if(res
&& (requirements
& MEMF_CLEAR
))
165 memset(res
, 0, byteSize
);
168 RT_Add (RTT_MEMORY
, res
, origSize
);
171 #if AROS_MUNGWALL_DEBUG
172 requirements
= origRequirements
;
176 struct MungwallHeader
*header
;
177 struct List
*allocmemlist
;
179 /* Save orig byteSize before wall (there is one room of MUNGWALLHEADER_SIZE
180 bytes before wall for such stuff (see above).
183 header
= (struct MungwallHeader
*)res
;
185 header
->mwh_magicid
= MUNGWALL_HEADER_ID
;
186 header
->mwh_allocsize
= origSize
;
188 /* Check whether list does exist. AllocMem() might have been
189 called before PrepareAROSSupportBase() which is responsible for
190 initialization of AllocMemList */
192 if (SysBase
->DebugAROSBase
)
194 allocmemlist
= (struct List
*)&((struct AROSSupportBase
*)SysBase
->DebugAROSBase
)->AllocMemList
;
196 AddHead(allocmemlist
, (struct Node
*)&header
->mwh_node
);
201 header
->mwh_node
.mln_Pred
= (struct MinNode
*)0x44332211;
202 header
->mwh_node
.mln_Succ
= (struct MinNode
*)0xCCBBAA99;
205 /* Skip to the start of the pre-wall */
206 res
+= MUNGWALLHEADER_SIZE
;
208 /* Initialize pre-wall */
209 BUILD_WALL(res
, 0xDB, MUNGWALL_SIZE
);
211 /* move over the block between the walls */
212 res
+= MUNGWALL_SIZE
;
214 /* Fill the block with weird stuff to exploit bugs in applications */
215 if (!(requirements
& MEMF_CLEAR
))
216 MUNGE_BLOCK(res
, MEMFILL_ALLOC
, byteSize
- MUNGWALL_SIZE
* 2 - MEMCHUNK_TOTAL
);
218 /* Initialize post-wall */
219 BUILD_WALL(res
+ origSize
, 0xDB, MUNGWALL_SIZE
+ AROS_ROUNDUP2(origSize
, MEMCHUNK_TOTAL
) - origSize
);
221 #endif /* AROS_MUNGWALL_DEBUG */
223 ReturnPtr ("AllocMem", APTR
, res
);
230 static APTR
stdAlloc(struct MemHeader
*mh
, ULONG byteSize
, ULONG requirements
)
232 struct MemChunk
*mc
=NULL
, *p1
, *p2
;
235 The free memory list is only single linked, i.e. to remove
236 elements from the list I need node's predessor. For the
237 first element I can use mh->mh_First instead of a real predessor.
239 p1
= (struct MemChunk
*)&mh
->mh_First
;
242 /* Is there anything in the list? */
248 #if !defined(NO_CONSISTENCY_CHECKS)
249 /* Consistency check: Check alignment restrictions */
250 if( ((IPTR
)p2
|(ULONG
)p2
->mc_Bytes
)
251 & (MEMCHUNK_TOTAL
-1) )
252 Alert(AN_MemCorrupt
|AT_DeadEnd
);
255 /* Check if the current block is large enough */
256 if(p2
->mc_Bytes
>=byteSize
)
260 /* Use this one if MEMF_REVERSE is not set.*/
261 if(!(requirements
&MEMF_REVERSE
))
263 /* Else continue - there may be more to come. */
266 /* Go to next block */
270 /* Check if this was the end */
273 #if !defined(NO_CONSISTENCY_CHECKS)
276 If the end of the last block+1 is bigger or equal to
277 the start of the current block something must be wrong.
279 if((UBYTE
*)p2
<=(UBYTE
*)p1
+p1
->mc_Bytes
)
280 Alert(AN_MemCorrupt
|AT_DeadEnd
);
284 /* Something found? */
288 Remember: if MEMF_REVERSE is set
289 p1 and p2 are now invalid.
294 /* Remove the block from the list and return it. */
295 if(p2
->mc_Bytes
== byteSize
)
297 /* Fits exactly. Just relink the list. */
298 p1
->mc_Next
= p2
->mc_Next
;
303 if(requirements
& MEMF_REVERSE
)
305 /* Return the last bytes. */
307 mc
=(struct MemChunk
*)((UBYTE
*)p2
+p2
->mc_Bytes
-byteSize
);
311 /* Return the first bytes. */
312 p1
->mc_Next
=(struct MemChunk
*)((UBYTE
*)p2
+byteSize
);
317 p1
->mc_Next
= p2
->mc_Next
;
318 p1
->mc_Bytes
= p2
->mc_Bytes
-byteSize
;
321 mh
->mh_Free
-= byteSize
;
328 ULONG
checkMemHandlers(struct checkMemHandlersState
*cmhs
)
331 struct Interrupt
*lmh
;
333 if (cmhs
->cmhs_Data
.memh_RequestFlags
& MEMF_NO_EXPUNGE
)
334 return MEM_DID_NOTHING
;
336 /* Loop over low memory handlers. Handlers can remove
337 themselves from the list while being invoked, thus
338 we need to be careful! */
341 lmh
= (struct Interrupt
*)cmhs
->cmhs_CurNode
;
342 (tmp
= lmh
->is_Node
.ln_Succ
);
343 lmh
= (struct Interrupt
*)(cmhs
->cmhs_CurNode
= tmp
)
348 ret
= AROS_UFC3 (LONG
, lmh
->is_Code
,
349 AROS_UFCA(struct MemHandlerData
*, &cmhs
->cmhs_Data
, A0
),
350 AROS_UFCA(APTR
, lmh
->is_Data
, A1
),
351 AROS_UFCA(struct ExecBase
*, SysBase
, A6
)
354 if (ret
== MEM_TRY_AGAIN
)
356 /* MemHandler said he did something. Try again. */
357 /* Is there any program that depends on this flag??? */
358 cmhs
->cmhs_Data
.memh_Flags
|= MEMHF_RECYCLE
;
359 return MEM_TRY_AGAIN
;
363 /* Nothing more to expect from this handler. */
364 cmhs
->cmhs_Data
.memh_Flags
&= ~MEMHF_RECYCLE
;
368 return MEM_DID_NOTHING
;