2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
5 Allocate memory from a specific MemHeader.
10 #include <aros/debug.h>
11 #include <exec/alerts.h>
12 #include <aros/libcall.h>
13 #include <aros/macros.h>
14 #include <exec/memory.h>
15 #include <exec/memheaderext.h>
16 #include <proto/exec.h>
18 #include "exec_intern.h"
19 #include "exec_util.h"
22 /*****************************************************************************
26 AROS_LH2(APTR
, Allocate
,
29 AROS_LHA(struct MemHeader
*, freeList
, A0
),
30 AROS_LHA(IPTR
, byteSize
, D0
),
33 struct ExecBase
*, SysBase
, 31, Exec
)
36 Allocate memory out of a private region handled by the MemHeader
40 freeList - Pointer to the MemHeader structure which holds the memory
41 byteSize - Number of bytes you want to get
44 A pointer to the number of bytes you wanted or NULL if the memory
48 The memory is aligned to sizeof(struct MemChunk). All requests
49 are rounded up to a multiple of that size.
53 \* Get a MemHeader structure and some private memory *\
54 mh=(struct MemHeader *)
55 AllocMem(sizeof(struct MemHeader)+POOLSIZE,MEMF_ANY);
58 \* Build a private pool *\
59 mh->mh_First=(struct MemChunk *)(mh+1);
60 mh->mh_First->mc_Next=NULL;
61 mh->mh_First->mc_Bytes=POOLSIZE;
66 mem1=Allocate(mh,1000);
67 mem2=Allocate(mh,2000);
68 \* Do something with memory... *\
70 \* Free everything at once *\
71 FreeMem(mh,sizeof(struct MemHeader)+POOLSIZE);
75 Does not work with managed memory blocks because of backwards
83 ******************************************************************************/
87 if ((freeList
->mh_Node
.ln_Type
== NT_MEMORY
) &&
88 (freeList
->mh_Attributes
& MEMF_MANAGED
) &&
89 (((struct MemHeaderExt
*)freeList
)->mhe_Magic
== MEMHEADER_EXT_MAGIC
)
92 struct MemHeaderExt
*mhe
= (struct MemHeaderExt
*)freeList
;
95 return mhe
->mhe_Alloc(mhe
, byteSize
, NULL
);
101 struct TraceLocation tp
= CURRENT_LOCATION("Allocate");
104 D(bug("[exec] Allocate(0x%p, %u)\n", freeList
, byteSize
));
105 ASSERT(freeList
!= NULL
);
107 /* There are some programs (ie users of an older libSDL.a)
108 * that use Allocate() on their own managed pool, and did
109 * NOT set freeList->mh_Node.ln_Type. We fix it up for them
112 if (freeList
->mh_Node
.ln_Type
== 0)
113 freeList
->mh_Node
.ln_Type
= NT_MEMORY
;
114 /* Fix also possibly uninitialized mh_Attributes */
115 freeList
->mh_Attributes
&= ~MEMF_MANAGED
;
117 ASSERT(freeList
->mh_Node
.ln_Type
== NT_MEMORY
);
118 ASSERT(freeList
->mh_Lower
<= freeList
->mh_Upper
);
120 /* Zero bytes requested? May return everything ;-). */
124 /* Is there enough free memory in the list? */
125 if(freeList
->mh_Free
<byteSize
)
128 res
= stdAlloc(freeList
, NULL
/* by design */, byteSize
, 0, &tp
, SysBase
);
130 if ((PrivExecBase(SysBase
)->IntFlags
& EXECF_MungWall
) && res
) {
131 MUNGE_BLOCK(res
, MEMFILL_ALLOC
, byteSize
);