revert between 56095 -> 55830 in arch
[AROS.git] / rom / exec / allocmem.c
blobe5b6fc26674d2538df377c2d9187bb61e547f724
1 /*
2 Copyright © 1995-2015, 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(IPTR, 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 IPTR origSize = byteSize;
84 struct TraceLocation loc = CURRENT_LOCATION("AllocMem");
87 if (SysBase->DebugAROSBase)
89 bug("Call AllocMem (%d, %08x)\n", byteSize, requirements);
93 /* 0-sized allocation results in returning NULL (API guarantee) */
94 if(!byteSize)
95 return NULL;
97 /* Make room for safety walls around allocated block and an some more extra space
98 for other interesting things, actually --> the size.
100 This all will look like this:
102 [MEMCHUNK_FOR_EXTRA_STUFF][BEFORE-MUNGWALL][<alloced-memory-for-user>][AFTER_MUNGWALL]
104 MEMCHUNK_FOR_EXTRA_STUFF is used (amongst other things) to save the original alloc
105 size (byteSize) param. So it is possible in FreeMem to check, if freemem size
106 matches allocmem size or not.
108 if (PrivExecBase(SysBase)->IntFlags & EXECF_MungWall)
109 byteSize += MUNGWALL_TOTAL_SIZE;
111 cmhs.cmhs_CurNode = (struct Node *)SysBase->ex_MemHandlers.mlh_Head;
112 cmhs.cmhs_Data.memh_RequestSize = byteSize;
113 cmhs.cmhs_Data.memh_RequestFlags = requirements;
114 cmhs.cmhs_Data.memh_Flags = 0;
118 res = nommu_AllocMem(byteSize, requirements, &loc, SysBase);
119 } while (res == NULL && checkMemHandlers(&cmhs, SysBase) == MEM_TRY_AGAIN);
121 #if ENABLE_RT
122 RT_Add (RTT_MEMORY, res, origSize);
123 #endif
125 res = MungWall_Build(res, NULL, origSize, requirements, &loc, SysBase);
127 /* Set DOS error if called from a process */
128 if (res == NULL)
130 struct Process *process = (struct Process *)GET_THIS_TASK;
131 if (process && process->pr_Task.tc_Node.ln_Type == NT_PROCESS)
132 process->pr_Result2 = ERROR_NO_FREE_STORE;
136 if (SysBase->DebugAROSBase)
138 bug("AllocMem result: 0x%p\n", res);
141 return res;
143 AROS_LIBFUNC_EXIT
145 } /* AllocMem */