Minor fixes to comments.
[AROS.git] / rom / exec / allocmem.c
blob7f951b00ceb60db0c742f1ba7a43efc63b4fc1ca
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Allocate some memory
6 Lang: english
7 */
9 #include <exec/alerts.h>
10 #include <exec/execbase.h>
11 #include <aros/libcall.h>
12 #include <aros/asmcall.h>
13 #include <aros/rt.h>
14 #include <aros/macros.h>
15 #include <aros/arossupportbase.h>
16 #include <exec/memory.h>
17 #include <exec/memheaderext.h>
18 #include <exec/nodes.h>
19 #include <dos/dos.h>
20 #include <dos/dosextens.h>
21 #include <proto/exec.h>
23 #include "exec_debug.h"
25 #ifndef DEBUG_AllocMem
26 # define DEBUG_AllocMem 0
27 #endif
28 #undef DEBUG
29 #if DEBUG_AllocMem
30 # define DEBUG 1
31 #endif
33 #include "exec_intern.h"
34 #include "exec_util.h"
35 #include "memory.h"
36 #include "mungwall.h"
38 /*****************************************************************************
40 NAME */
42 AROS_LH2(APTR, AllocMem,
44 /* SYNOPSIS */
45 AROS_LHA(ULONG, byteSize, D0),
46 AROS_LHA(ULONG, requirements, D1),
48 /* LOCATION */
49 struct ExecBase *, SysBase, 33, Exec)
51 /* FUNCTION
52 Allocate some memory from the sytem memory pool with the given
53 requirements.
55 INPUTS
56 byteSize - Number of bytes you want to get
57 requirements - Type of memory
59 RESULT
60 A pointer to the number of bytes you wanted or NULL if the memory
61 couldn't be allocated
63 NOTES
64 The memory is aligned to sizeof(struct MemChunk). All requests
65 are rounded up to a multiple of that size.
67 EXAMPLE
68 mytask=(struct Task *)AllocMem(sizeof(struct Task),MEMF_PUBLIC|MEMF_CLEAR);
70 BUGS
72 SEE ALSO
73 FreeMem()
75 INTERNALS
77 ******************************************************************************/
79 AROS_LIBFUNC_INIT
81 APTR res = NULL;
82 struct checkMemHandlersState cmhs;
83 ULONG origSize = byteSize;
84 struct TraceLocation loc = CURRENT_LOCATION("AllocMem");
86 D(if (SysBase->DebugAROSBase))
87 D(bug("Call AllocMem (%d, %08x)\n", byteSize, requirements));
89 /* Zero bytes requested? May return everything ;-). */
90 if(!byteSize)
91 return NULL;
93 /* Make room for safety walls around allocated block and an some more extra space
94 for other interesting things, actually --> the size.
96 This all will look like this:
98 [MEMCHUNK_FOR_EXTRA_STUFF][BEFORE-MUNGWALL][<alloced-memory-for-user>][AFTER_MUNGWALL]
100 MEMCHUNK_FOR_EXTRA_STUFF is used (amongst other things) to save the original alloc
101 size (byteSize) param. So it is possible in FreeMem to check, if freemem size
102 matches allocmem size or not.
104 if (PrivExecBase(SysBase)->IntFlags & EXECF_MungWall)
105 byteSize += MUNGWALL_TOTAL_SIZE;
107 cmhs.cmhs_CurNode = (struct Node *)SysBase->ex_MemHandlers.mlh_Head;
108 cmhs.cmhs_Data.memh_RequestSize = byteSize;
109 cmhs.cmhs_Data.memh_RequestFlags = requirements;
110 cmhs.cmhs_Data.memh_Flags = 0;
114 res = nommu_AllocMem(byteSize, requirements, &loc, SysBase);
115 } while (res == NULL && checkMemHandlers(&cmhs, SysBase) == MEM_TRY_AGAIN);
117 #if ENABLE_RT
118 RT_Add (RTT_MEMORY, res, origSize);
119 #endif
121 res = MungWall_Build(res, NULL, origSize, requirements, &loc, SysBase);
123 /* Set DOS error if called from a process */
124 if (res == NULL)
126 struct Process *process = (struct Process *)FindTask(NULL);
127 if (process->pr_Task.tc_Node.ln_Type == NT_PROCESS)
128 process->pr_Result2 = ERROR_NO_FREE_STORE;
131 #if DEBUG
132 if (SysBase->DebugAROSBase)
133 bug("AllocMem result: 0x%p\n", res);
134 #endif
135 return res;
137 AROS_LIBFUNC_EXIT
139 } /* AllocMem */