source: trunk/src/kernel32/os2heap.cpp@ 7029

Last change on this file since 7029 was 7029, checked in by phaller, 24 years ago

.

File size: 11.0 KB
Line 
1/* $Id: os2heap.cpp,v 1.28 2001-10-12 07:05:48 phaller Exp $ */
2
3/*
4 * Heap class for OS/2
5 *
6 * Copyright 1998 Sander van Leeuwen
7 *
8 *
9 * NOTE: ReAlloc allocates memory using Alloc if memory pointer == NULL
10 * WINE controls depend on this, even though it apparently
11 * doesn't work like this in Windows.
12 *
13 * Project Odin Software License can be found in LICENSE.TXT
14 *
15 */
16#define INCL_DOSMEMMGR
17#define INCL_DOSSEMAPHORES
18#define INCL_DOSERRORS
19#include <os2wrap.h> //Odin32 OS/2 api wrappers
20#include <stdlib.h>
21#include <string.h>
22#include <umalloc.h>
23
24#include "win32type.h"
25#include "os2heap.h"
26#include "misc.h"
27#include "vmutex.h"
28#include "initterm.h"
29#include <odin32validate.h>
30
31#define DBG_LOCALLOG DBG_os2heap
32#include "dbglocal.h"
33
34#ifndef HEAP_NO_SERIALIZE
35 #define HEAP_NO_SERIALIZE 1
36#endif
37
38#ifndef HEAP_ZERO_MEMORY
39 #define HEAP_ZERO_MEMORY 8
40#endif
41
42VMutex heaplistmutex; //protects linked lists of heaps
43
44void * _LNK_CONV getmoreHeapMem(Heap_t pHeap, size_t *size, int *clean);
45void _LNK_CONV releaseHeapMem(Heap_t pHeap, void *block, size_t size);
46
47
48//******************************************************************************
49// Fast Heap Handle Management
50//******************************************************************************
51
52static int fhhm_hit = 0;
53static int fhhm_miss = 0;
54static HANDLE fhhm_lastHandle = 0;
55static OS2Heap* fhhm_lastHeap = NULL;
56
57
58//******************************************************************************
59//******************************************************************************
60OS2Heap::OS2Heap(DWORD flOptions, DWORD dwInitialSize, DWORD dwMaximumSize)
61{
62 OS2Heap *curheap = OS2Heap::heap;
63
64 totalAlloc = 0;
65 fInitialized = 0;
66 nrHeaps = 0;
67 heapelem = NULL;
68
69 dwInitialSize = (dwInitialSize >= 0x4000) ? dwInitialSize : 0x4000;
70
71 this->dwMaximumSize = dwMaximumSize;
72 this->dwInitialSize = dwInitialSize;
73 this->flOptions = flOptions;
74
75 heaplistmutex.enter();
76 if(curheap != NULL) {
77 while(curheap->next != NULL) {
78 curheap = curheap->next;
79 }
80 curheap->next = this;
81 }
82 else heap = this;
83 next = NULL;
84
85 heaplistmutex.leave();
86
87 APIRET rc;
88
89 rc = DosAllocMem((PPVOID)&pInitialHeapMem, dwInitialSize, PAG_READ|PAG_WRITE|PAG_COMMIT);
90 if(rc != 0) {
91 dprintf(("OS2Heap::OS2Heap: DosAllocSharedMem failed with %d", rc));
92 DebugInt3();
93 }
94 uheap = _ucreate(pInitialHeapMem, dwInitialSize, _BLOCK_CLEAN,
95 _HEAP_REGULAR, getmoreHeapMem, releaseHeapMem);
96 if(uheap == NULL) {
97 DosFreeMem(pInitialHeapMem);
98 pInitialHeapMem = NULL;
99 dprintf(("OS2Heap::OS2Heap: _ucreate failed!"));
100 DebugInt3();
101 }
102 hPrimaryHeap = (HANDLE)uheap;
103 dprintf(("KERNEL32: HeapCreate: initial size %d, max size %d (flags %X) returned %X\n", dwInitialSize, dwMaximumSize, flOptions, hPrimaryHeap));
104}
105//******************************************************************************
106//******************************************************************************
107OS2Heap::~OS2Heap()
108{
109 OS2Heap *curheap = OS2Heap::heap;
110 int i;
111
112 // invalidate handle cache
113 fhhm_lastHandle = 0;
114 fhhm_lastHeap = NULL;
115
116
117 dprintf(("dtr OS2Heap, hPrimaryHeap = %X\n", hPrimaryHeap));
118
119 heaplistmutex.enter();
120 if(heap == this) {
121 heap = next;
122 }
123 else {
124 while(curheap->next != NULL) {
125 if(curheap->next == this) {
126 curheap->next = next;
127 break;
128 }
129 curheap = curheap->next;
130 }
131 }
132 heaplistmutex.leave();
133
134 if(uheap) {
135 _uclose(uheap);
136 _udestroy(uheap, _FORCE);
137 uheap = NULL;
138 }
139 if(pInitialHeapMem) {
140 DosFreeMem(pInitialHeapMem);
141 pInitialHeapMem = NULL;
142 }
143
144 dprintf(("dtr OS2Heap, hPrimaryHeap = %X done\n", hPrimaryHeap));
145}
146//******************************************************************************
147//******************************************************************************
148LPVOID OS2Heap::Alloc(DWORD dwFlags, DWORD dwBytes)
149{
150 HEAPELEM *lpHeapObj;
151 LPVOID lpMem;
152
153// dprintf(("OS2Heap::Alloc\n"));
154 lpMem = _umalloc(uheap, dwBytes + HEAP_OVERHEAD);
155 if(lpMem == NULL) {
156 dprintf(("OS2Heap::Alloc, lpMem == NULL"));
157 return(NULL);
158 }
159 if(dwFlags & HEAP_ZERO_MEMORY) {
160 memset(lpMem, 0, dwBytes+HEAP_OVERHEAD);
161 }
162 totalAlloc += dwBytes;
163
164 //align at 8 byte boundary
165 lpHeapObj = (HEAPELEM *)(((ULONG)lpMem+7) & ~7);
166 lpHeapObj->lpMem = lpMem;
167 lpHeapObj->magic = MAGIC_NR_HEAP;
168
169 return(LPVOID)(lpHeapObj+1);
170}
171//******************************************************************************
172//******************************************************************************
173DWORD OS2Heap::Size(DWORD dwFlags, PVOID lpMem)
174{
175 HEAPELEM *helem = GET_HEAPOBJ(lpMem);
176
177 if(lpMem == NULL) {
178 dprintf(("OS2Heap::Size lpMem == NULL\n"));
179 return -1;
180 }
181 /* verify lpMem address */
182 if (lpMem >= (LPVOID)ulMaxAddr || lpMem < (LPVOID)0x10000)
183 {
184 dprintf(("OS2Heap::Size ERROR BAD HEAP POINTER:%X\n", lpMem));
185 return -1;
186 }
187
188 if(helem->magic != MAGIC_NR_HEAP)
189 {
190 dprintf(("OS2Heap::Size ERROR BAD HEAP POINTER:%X\n", lpMem));
191 return -1;
192 }
193
194 return(_msize(helem->lpMem) - HEAP_OVERHEAD);
195}
196//******************************************************************************
197//******************************************************************************
198LPVOID OS2Heap::ReAlloc(DWORD dwFlags, LPVOID lpMem, DWORD dwBytes)
199{
200 HEAPELEM *helem = GET_HEAPOBJ(lpMem);
201 LPVOID lpNewMem;
202 int i, oldSize;
203
204 if (dwBytes == 0) return NULL; // intercept stupid parameters
205
206 //NOTE: Allocate memory using Alloc -> WINE controls depend on this, even
207 // though it apparently doesn't work in Windows.
208 if (lpMem == 0) return Alloc(dwFlags, dwBytes);
209// if (lpMem == 0) return NULL;
210
211 if (helem->magic != MAGIC_NR_HEAP)
212 {
213 dprintf(("OS2Heap::ReAlloc ERROR BAD HEAP POINTER:%X\n", lpMem));
214 return lpMem;
215 }
216
217 oldSize = Size(0,lpMem);
218 if (oldSize >= dwBytes) {
219 dprintf(("ReAlloc with smaller size than original (%d); return old pointer", oldSize));
220 return lpMem; // if reallocation with same size don't do anything
221 }
222 lpNewMem = Alloc(dwFlags, dwBytes);
223 memcpy(lpNewMem, lpMem, dwBytes < oldSize ? dwBytes : oldSize);
224 Free(0, lpMem);
225
226 if(lpNewMem == NULL)
227 {
228 dprintf(("OS2Heap::ReAlloc, no more memory left\n"));
229 }
230
231 return(lpNewMem);
232}
233//******************************************************************************
234//******************************************************************************
235BOOL OS2Heap::Free(DWORD dwFlags, LPVOID lpMem)
236{
237 HEAPELEM *helem = GET_HEAPOBJ(lpMem);
238
239 if(lpMem == NULL) {
240 dprintf(("OS2Heap::Free lpMem == NULL\n"));
241 return(FALSE);
242 }
243 /* verify lpMem address */
244 if (lpMem >= (LPVOID)ulMaxAddr || lpMem < (LPVOID)0x10000)
245 {
246 dprintf(("OS2Heap::Free ERROR BAD HEAP POINTER:%X\n", lpMem));
247 return FALSE;
248 }
249
250 if(helem->magic != MAGIC_NR_HEAP)
251 {
252 dprintf(("OS2Heap::Free ERROR BAD HEAP POINTER:%X\n", lpMem));
253 return FALSE;
254 }
255
256#ifdef DEBUG1
257 int size = Size(0, lpMem);
258 dprintf(("OS2Heap::Free lpMem = %X, size %d\n", lpMem, size));
259 totalAlloc -= size;
260#endif
261
262 free(helem->lpMem);
263 return(TRUE);
264}
265//******************************************************************************
266//******************************************************************************
267DWORD OS2Heap::Compact(DWORD dwFlags)
268{
269 dprintf(("OS2Heap::Compact, %X- stub\n", dwFlags));
270 return(0);
271}
272//******************************************************************************
273//******************************************************************************
274BOOL OS2Heap::Validate(DWORD dwFlags, LPCVOID lpMem)
275{
276 HEAPELEM *helem = GET_HEAPOBJ(lpMem);
277
278 dprintf(("OS2Heap::Validate, %X %X", dwFlags, lpMem));
279
280 if(lpMem == NULL) {
281 dprintf(("OS2Heap::Validate lpMem == NULL\n"));
282 return(FALSE);
283 }
284 /* verify lpMem address */
285 if (lpMem >= (LPVOID)ulMaxAddr || lpMem < (LPVOID)0x10000)
286 {
287 dprintf(("OS2Heap::Validate BAD HEAP POINTER:%X\n", lpMem));
288 return FALSE;
289 }
290
291 if(helem->magic != MAGIC_NR_HEAP)
292 {
293 dprintf(("OS2Heap::Validate BAD HEAP POINTER:%X\n", lpMem));
294 return FALSE;
295 }
296 return(TRUE);
297}
298//******************************************************************************
299//******************************************************************************
300BOOL OS2Heap::Walk(void *lpEntry)
301{
302 dprintf(("OS2Heap::Walk, %X - stub? (TRUE)\n", lpEntry));
303 return(TRUE);
304}
305//******************************************************************************
306//******************************************************************************
307OS2Heap *OS2Heap::find(HANDLE hHeap)
308{
309 // check against cache first
310 if (fhhm_lastHeap)
311 if (hHeap == fhhm_lastHandle)
312 {
313 fhhm_hit++;
314 return fhhm_lastHeap;
315 }
316
317 fhhm_miss++;
318
319 OS2Heap *curheap = OS2Heap::heap;
320
321 //@@@PH NT programs seem to assume heap 0 is always valid?!
322 if (hHeap == 0)
323 if (curheap != NULL)
324 {
325 fhhm_lastHandle = hHeap;
326 fhhm_lastHeap = curheap;
327 return curheap;
328 }
329
330 while(curheap != NULL)
331 {
332 if(curheap->hPrimaryHeap == hHeap)
333 {
334 fhhm_lastHandle = hHeap;
335 fhhm_lastHeap = curheap;
336 return(curheap);
337 }
338 curheap = curheap->next;
339 }
340 dprintf(("Heap %X not found!\n", hHeap));
341 fhhm_lastHandle = hHeap;
342 fhhm_lastHeap = NULL;
343 return(NULL);
344}
345//******************************************************************************
346//******************************************************************************
347OS2Heap *OS2Heap::heap = NULL;
348
349//******************************************************************************
350//******************************************************************************
351void * _LNK_CONV getmoreHeapMem(Heap_t pHeap, size_t *size, int *clean)
352{
353 APIRET rc;
354 PVOID newblock;
355
356 dprintf(("KERNEL32: getmoreHeapMem(%08xh, %08xh, %08xh)", pHeap, *size, *clean));
357
358 /* round the size up to a multiple of 64K */
359 //NOTE: MUST use 64kb here or else we are at risk of running out of virtual
360 // memory space. (when allocating 4kb we actually get 4kb + 60k uncommited)
361 *size = (*size / 65536) * 65536 + 65536;
362
363 rc = DosAllocMem(&newblock, *size, PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
364//// rc = DosAllocMem(&newblock, *size, flAllocMem|PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
365 if(rc != 0) {
366 dprintf(("getmoreHeapMem: DosAllocMem failed with %d", rc));
367 return FALSE;
368 }
369 *clean = _BLOCK_CLEAN;
370 dprintf(("KERNEL32: getmoreHeapMem %x %d", newblock, *size));
371 return newblock;
372}
373//******************************************************************************
374//******************************************************************************
375void _LNK_CONV releaseHeapMem(Heap_t pHeap, void *block, size_t size)
376{
377 dprintf(("KERNEL32: releaseHeapMem %x %x %d", pHeap, block, size));
378 DosFreeMem(block);
379}
380//******************************************************************************
381//******************************************************************************
Note: See TracBrowser for help on using the repository browser.