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