Port the SB128 code to AROS.
[AROS.git] / rom / exec / allocate.c
blobdaee1a267256233bd65ce58311b21cd85faa6ad2
1 /*
2 Copyright © 1995-2010, The AROS Development Team. All rights reserved.
3 $Id$
5 Allocate memory from a specific MemHeader.
6 */
8 #define MDEBUG 1
10 #include <aros/debug.h>
11 #include "exec_intern.h"
12 #include "memory.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 /*****************************************************************************
21 NAME */
23 AROS_LH2(APTR, Allocate,
25 /* SYNOPSIS */
26 AROS_LHA(struct MemHeader *, freeList, A0),
27 AROS_LHA(ULONG, byteSize, D0),
29 /* LOCATION */
30 struct ExecBase *, SysBase, 31, Exec)
32 /* FUNCTION
33 Allocate memory out of a private region handled by the MemHeader
34 structure.
36 INPUTS
37 freeList - Pointer to the MemHeader structure which holds the memory
38 byteSize - Number of bytes you want to get
40 RESULT
41 A pointer to the number of bytes you wanted or NULL if the memory
42 couldn't be allocated
44 NOTES
45 The memory is aligned to sizeof(struct MemChunk). All requests
46 are rounded up to a multiple of that size.
48 EXAMPLE
49 #define POOLSIZE 4096
50 \* Get a MemHeader structure and some private memory *\
51 mh=(struct MemHeader *)
52 AllocMem(sizeof(struct MemHeader)+POOLSIZE,MEMF_ANY);
53 if(mh!=NULL)
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;
59 mh->mh_Free=POOLSIZE;
61 \* Use the pool *\
62 UBYTE *mem1,*mem2;
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);
71 BUGS
72 Does not work with managed memory blocks because of backwards
73 compatibility issues
75 SEE ALSO
76 Deallocate()
78 INTERNALS
80 ******************************************************************************/
82 AROS_LIBFUNC_INIT
84 APTR res;
86 D(bug("[exec] Allocate(0x%p, %u)\n", freeList, byteSize));
87 ASSERT_VALID_PTR(freeList);
89 /* Zero bytes requested? May return everything ;-). */
90 if(!byteSize)
91 return NULL;
93 /* Is there enough free memory in the list? */
94 if(freeList->mh_Free<byteSize)
95 return NULL;
97 res = stdAlloc(freeList, byteSize, 0, SysBase);
99 if ((PrivExecBase(SysBase)->IntFlags & EXECF_MungWall) && res) {
100 MUNGE_BLOCK(res, MEMFILL_ALLOC, byteSize);
103 return res;
105 AROS_LIBFUNC_EXIT
106 } /* Allocate() */