1 | /* $Id: malloc.c,v 1.7 2000-09-02 21:08:14 bird Exp $
|
---|
2 | *
|
---|
3 | * Common Heap - this forwards to the swappable heap!
|
---|
4 | *
|
---|
5 | * Note: This heap does very little checking on input.
|
---|
6 | * Use with care! We're running at Ring-0!
|
---|
7 | *
|
---|
8 | * Copyright (c) 1999-2000 knut st. osmundsen
|
---|
9 | *
|
---|
10 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
11 | *
|
---|
12 | */
|
---|
13 | /*******************************************************************************
|
---|
14 | * Defined Constants And Macros *
|
---|
15 | *******************************************************************************/
|
---|
16 | #define INCL_NOAPI
|
---|
17 |
|
---|
18 | /******************************************************************************
|
---|
19 | * Headerfiles
|
---|
20 | ******************************************************************************/
|
---|
21 | #include <os2.h>
|
---|
22 | #include "devSegDf.h" /* Win32k segment definitions. */
|
---|
23 | #include "smalloc.h"
|
---|
24 | #include "rmalloc.h"
|
---|
25 | #include "options.h"
|
---|
26 |
|
---|
27 |
|
---|
28 | /******************************************************************************
|
---|
29 | * Global data
|
---|
30 | ******************************************************************************/
|
---|
31 | #if !defined(RING0) || defined(R3TST)
|
---|
32 | char fInited; /* init flag */
|
---|
33 | #endif
|
---|
34 |
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Initiate the heap "subsystems" - both the resident and the swappable heaps.
|
---|
39 | * @returns 0 on success, not 0 on error.
|
---|
40 | * @param cbResInit Resident heap initial size.
|
---|
41 | * @param cbResMax Resident heap maximum size.
|
---|
42 | * @param cbSwpInit Swappable heap initial size.
|
---|
43 | * @param cbSwpMax Swappable heap maximum size.
|
---|
44 | */
|
---|
45 | int heapInit(unsigned cbResInit, unsigned cbResMax,
|
---|
46 | unsigned cbSwpInit, unsigned cbSwpMax)
|
---|
47 | {
|
---|
48 | int rc;
|
---|
49 |
|
---|
50 | rc = resHeapInit(cbResInit, cbResMax);
|
---|
51 | if (rc != 0)
|
---|
52 | return rc;
|
---|
53 | rc = swpHeapInit(cbSwpInit, cbSwpMax);
|
---|
54 | if (rc != 0)
|
---|
55 | return rc;
|
---|
56 | #if !defined(RING0) || defined(R3TST)
|
---|
57 | fInited = TRUE;
|
---|
58 | #endif
|
---|
59 | return 0;
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * malloc - allocates a given amount of memory.
|
---|
65 | * @returns Pointer to allocated memory.
|
---|
66 | * NULL if out of memory. (Or memory to fragmented.)
|
---|
67 | * @param cbSize Bytes user requests us to allocate. This is aligned
|
---|
68 | * to four bytes.
|
---|
69 | */
|
---|
70 | void * malloc(unsigned cbSize)
|
---|
71 | {
|
---|
72 | #ifdef R3TST
|
---|
73 | if (!fInited)
|
---|
74 | {
|
---|
75 | PVOID pv;
|
---|
76 | if (!DosAllocMem(&pv, cbSize, PAG_WRITE | PAG_READ | PAG_COMMIT)) /* no SSToDS! */
|
---|
77 | return pv;
|
---|
78 | else
|
---|
79 | return NULL;
|
---|
80 | }
|
---|
81 | #endif
|
---|
82 | return smalloc(cbSize);
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Reallocate a heapblock.
|
---|
88 | * @returns Pointer to new heapblock.
|
---|
89 | * @param pv Pointer to the block to realloc.
|
---|
90 | * @param cbNew The new block size.
|
---|
91 | */
|
---|
92 | void *realloc(void *pv, unsigned cbNew)
|
---|
93 | {
|
---|
94 | #ifdef R3TST
|
---|
95 | if (!fInited)
|
---|
96 | Int3();
|
---|
97 | #endif
|
---|
98 | return srealloc(pv, cbNew);
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Frees a block.
|
---|
104 | * @param pv User pointer.
|
---|
105 | */
|
---|
106 | void free(void *pv)
|
---|
107 | {
|
---|
108 | #ifdef R3TST
|
---|
109 | if (!fInited) /* controlled leak! */
|
---|
110 | return;
|
---|
111 | #endif
|
---|
112 | sfree(pv);
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Gets the size of a block.
|
---|
118 | * @returns Bytes in a block.
|
---|
119 | */
|
---|
120 | unsigned _msize(void *pv)
|
---|
121 | {
|
---|
122 | return _swp_msize(pv);
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Checks if pv is a valid heappointer.
|
---|
128 | * @returns 1 if valid. 0 if invalid.
|
---|
129 | * @param pv User data pointer.
|
---|
130 | */
|
---|
131 | int _validptr(void *pv)
|
---|
132 | {
|
---|
133 | return _swp_validptr(pv);
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Checks that the dataaera made up by pv and cbSize valid with in the heap.
|
---|
139 | * @returns 1 if valid. 0 if invalid.
|
---|
140 | * @param pv User data pointer.
|
---|
141 | * @param cbSize Size of data which has to be valid.
|
---|
142 | */
|
---|
143 | int _validptr2(void *pv, unsigned cbSize)
|
---|
144 | {
|
---|
145 | return _swp_validptr2(pv, cbSize);
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Get amount of free memory (in bytes)
|
---|
151 | * @returns Amount of free memory (in bytes).
|
---|
152 | * @remark Note that this amount is of all free memory blocks and
|
---|
153 | * that these blocks are fragmented.
|
---|
154 | * You'll probably not be able to allocate a single block
|
---|
155 | * of the returned size.
|
---|
156 | */
|
---|
157 | unsigned _memfree(void)
|
---|
158 | {
|
---|
159 | return _swp_memfree();
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Checks heap integrety.
|
---|
165 | * @returns TRUE when ok.
|
---|
166 | * FALSE on error.
|
---|
167 | * NULL if out of memory. (Or memory to fragmented.)
|
---|
168 | */
|
---|
169 | int _heap_check(void)
|
---|
170 | {
|
---|
171 | return _swp_heap_check();
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 |
|
---|
176 | #if !defined(RING0) && defined(__IBMC__)
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Initialize Memory Functions
|
---|
180 | * Called from _exeentry.
|
---|
181 | */
|
---|
182 | int _rmem_init(void)
|
---|
183 | {
|
---|
184 | int rc;
|
---|
185 | rc = heapInit(CB_RES_INIT, CB_RES_MAX, CB_SWP_INIT, CB_SWP_MAX);
|
---|
186 | return rc;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Initialize Memory Functions
|
---|
191 | * Called from _exeentry.
|
---|
192 | */
|
---|
193 | int _rmem_term(void)
|
---|
194 | {
|
---|
195 | return 0;
|
---|
196 | }
|
---|
197 |
|
---|
198 | #endif
|
---|