2 Copyright © 1995-2010, The AROS Development Team. All rights reserved.
5 Allocate memory from a specific MemHeader.
10 #include <aros/debug.h>
11 #include "exec_intern.h"
13 #include <exec/alerts.h>
14 #include <aros/libcall.h>
15 #include <aros/macros.h>
16 #include <exec/memory.h>
17 #include <proto/exec.h>
19 /*****************************************************************************
23 AROS_LH2(APTR
, Allocate
,
26 AROS_LHA(struct MemHeader
*, freeList
, A0
),
27 AROS_LHA(ULONG
, byteSize
, D0
),
30 struct ExecBase
*, SysBase
, 31, Exec
)
33 Allocate memory out of a private region handled by the MemHeader
37 freeList - Pointer to the MemHeader structure which holds the memory
38 byteSize - Number of bytes you want to get
41 A pointer to the number of bytes you wanted or NULL if the memory
45 The memory is aligned to sizeof(struct MemChunk). All requests
46 are rounded up to a multiple of that size.
50 \* Get a MemHeader structure and some private memory *\
51 mh=(struct MemHeader *)
52 AllocMem(sizeof(struct MemHeader)+POOLSIZE,MEMF_ANY);
55 \* Build a private pool *\
56 mh->mh_First=(struct MemChunk *)(mh+1);
57 mh->mh_First->mc_Next=NULL;
58 mh->mh_First->mc_Bytes=POOLSIZE;
63 mem1=Allocate(mh,1000);
64 mem2=Allocate(mh,2000);
65 \* Do something with memory... *\
67 \* Free everything at once *\
68 FreeMem(mh,sizeof(struct MemHeader)+POOLSIZE);
72 Does not work with managed memory blocks because of backwards
80 ******************************************************************************/
86 D(bug("[exec] Allocate(0x%p, %u)\n", freeList
, byteSize
));
87 ASSERT_VALID_PTR(freeList
);
89 /* Zero bytes requested? May return everything ;-). */
93 /* Is there enough free memory in the list? */
94 if(freeList
->mh_Free
<byteSize
)
97 res
= stdAlloc(freeList
, byteSize
, 0, SysBase
);
99 if ((PrivExecBase(SysBase
)->IntFlags
& EXECF_MungWall
) && res
) {
100 MUNGE_BLOCK(res
, MEMFILL_ALLOC
, byteSize
);