Removed __startup attribute. It is already present in AROS_SHxx definition, and doubl...
[AROS.git] / rom / exec / allocate.c
blobbbd616ece4f0430367a2bcf232bfe9aee8b2d7d7
1 /*
2 Copyright © 1995-2011, 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/alerts.h>
12 #include <aros/libcall.h>
13 #include <aros/macros.h>
14 #include <exec/memory.h>
15 #include <proto/exec.h>
17 #include "exec_intern.h"
18 #include "exec_util.h"
19 #include "memory.h"
21 /*****************************************************************************
23 NAME */
25 AROS_LH2(APTR, Allocate,
27 /* SYNOPSIS */
28 AROS_LHA(struct MemHeader *, freeList, A0),
29 AROS_LHA(ULONG, byteSize, D0),
31 /* LOCATION */
32 struct ExecBase *, SysBase, 31, Exec)
34 /* FUNCTION
35 Allocate memory out of a private region handled by the MemHeader
36 structure.
38 INPUTS
39 freeList - Pointer to the MemHeader structure which holds the memory
40 byteSize - Number of bytes you want to get
42 RESULT
43 A pointer to the number of bytes you wanted or NULL if the memory
44 couldn't be allocated
46 NOTES
47 The memory is aligned to sizeof(struct MemChunk). All requests
48 are rounded up to a multiple of that size.
50 EXAMPLE
51 #define POOLSIZE 4096
52 \* Get a MemHeader structure and some private memory *\
53 mh=(struct MemHeader *)
54 AllocMem(sizeof(struct MemHeader)+POOLSIZE,MEMF_ANY);
55 if(mh!=NULL)
57 \* Build a private pool *\
58 mh->mh_First=(struct MemChunk *)(mh+1);
59 mh->mh_First->mc_Next=NULL;
60 mh->mh_First->mc_Bytes=POOLSIZE;
61 mh->mh_Free=POOLSIZE;
63 \* Use the pool *\
64 UBYTE *mem1,*mem2;
65 mem1=Allocate(mh,1000);
66 mem2=Allocate(mh,2000);
67 \* Do something with memory... *\
69 \* Free everything at once *\
70 FreeMem(mh,sizeof(struct MemHeader)+POOLSIZE);
73 BUGS
74 Does not work with managed memory blocks because of backwards
75 compatibility issues
77 SEE ALSO
78 Deallocate()
80 INTERNALS
82 ******************************************************************************/
84 AROS_LIBFUNC_INIT
86 struct TraceLocation tp = CURRENT_LOCATION("Allocate");
87 APTR res;
89 D(bug("[exec] Allocate(0x%p, %u)\n", freeList, byteSize));
90 ASSERT_VALID_PTR(freeList);
92 /* Zero bytes requested? May return everything ;-). */
93 if(!byteSize)
94 return NULL;
96 /* Is there enough free memory in the list? */
97 if(freeList->mh_Free<byteSize)
98 return NULL;
100 res = stdAlloc(freeList, byteSize, 0, &tp, SysBase);
102 if ((PrivExecBase(SysBase)->IntFlags & EXECF_MungWall) && res) {
103 MUNGE_BLOCK(res, MEMFILL_ALLOC, byteSize);
106 return res;
108 AROS_LIBFUNC_EXIT
109 } /* Allocate() */