source: trunk/src/kernel32/heapcode.cpp

Last change on this file was 8877, checked in by sandervl, 23 years ago

Rewrote algorithm for 64kb alignment in VirtualAlloc'ed memory; allocation changes for heap (in 64kb chunks) & PE image (align at 64kb)

File size: 3.5 KB
RevLine 
[8877]1/* $Id: heapcode.cpp,v 1.4 2002-07-15 14:28:51 sandervl Exp $ */
[2032]2/*
3 * Code heap functions for OS/2
4 *
5 * Initially commit 4 kb, add more when required
6 *
[8877]7 * NOTE: If high memory is ever used for this heap, then you must use VirtualAlloc!
8 *
[2032]9 * TODO: Not process/thread safe (initializing/destroying heap)
10 *
11 * ASSUMPTION: Rtl library takes care of protection of heap increase/decrease
12 * (from multiple threads/processes)
13 *
14 * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl)
15 *
16 */
17#define INCL_BASE
18#define INCL_DOSMEMMGR
19#include <os2wrap.h>
20#include <misc.h>
21#include <heapcode.h>
22
[2802]23#define DBG_LOCALLOG DBG_heapcode
24#include "dbglocal.h"
25
[8877]26Heap_t codeHeap = 0;
27static PVOID pCodeMem = NULL;
[2032]28
29void * _LNK_CONV getmoreCodeMem(Heap_t pHeap, size_t *size, int *clean);
[8877]30void _LNK_CONV releaseCodeMem(Heap_t pHeap, void *block, size_t size);
[2032]31
32//******************************************************************************
33//******************************************************************************
34BOOL InitializeCodeHeap()
35{
[8877]36 APIRET rc;
[2032]37
[8877]38 dprintf(("KERNEL32: InitializeCodeHeap"));
39
40 //NOTE: MUST use 64kb here or else we are at risk of running out of virtual
41 // memory space. (when allocating 4kb we actually get 4kb + 60k uncommited)
42 rc = DosAllocMem(&pCodeMem, 64*1024, PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
43 if(rc != 0) {
44 dprintf(("InitializeSharedHeap: DosAllocSharedMem failed with %d", rc));
45 return FALSE;
46 }
47 codeHeap = _ucreate(pCodeMem, PAGE_SIZE, _BLOCK_CLEAN, _HEAP_REGULAR, getmoreCodeMem, releaseCodeMem);
48 if(codeHeap == NULL) {
49 DosFreeMem(pCodeMem);
[2032]50 pCodeMem = NULL;
[8877]51 dprintf(("InitializeSharedHeap: _ucreate failed!"));
52 return FALSE;
53 }
54 return TRUE;
[2032]55}
56//******************************************************************************
57//******************************************************************************
58void DestroyCodeHeap()
59{
[8877]60 dprintf(("KERNEL32: DestroyCodeHeap"));
61 if(codeHeap) {
62 _uclose(codeHeap);
63 _udestroy(codeHeap, _FORCE);
64 codeHeap = NULL;
65 }
66 if(pCodeMem) {
67 DosFreeMem(pCodeMem);
68 pCodeMem = NULL;
69 }
[2032]70}
71//******************************************************************************
72//******************************************************************************
73void * _LNK_CONV getmoreCodeMem(Heap_t pHeap, size_t *size, int *clean)
74{
[8877]75 APIRET rc;
76 PVOID newblock;
[2032]77
[8877]78 dprintf(("KERNEL32: getmoreCodeMem(%08xh, %08xh, %08xh)\n", pHeap, *size, *clean));
[2032]79
[8877]80 /* round the size up to a multiple of 64K */
81 //NOTE: MUST use 64kb here or else we are at risk of running out of virtual
82 // memory space. (when allocating 4kb we actually get 4kb + 60k uncommited)
83 *size = ( (*size / 65536) + 1) * 65536;
[2032]84
[8877]85 rc = DosAllocMem(&newblock, *size, PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
86 if(rc != 0) {
87 dprintf(("getmoreCodeMem: DosAllocMem failed with %d", rc));
88 return FALSE;
89 }
90 *clean = _BLOCK_CLEAN;
91 dprintf(("KERNEL32: getmoreCodeMem %x %d", newblock, *size));
92 return newblock;
[2032]93}
94//******************************************************************************
95//******************************************************************************
96void _LNK_CONV releaseCodeMem(Heap_t pHeap, void *block, size_t size)
97{
[8877]98 dprintf(("KERNEL32: releaseCodeMem %x %d", block, size));
99 DosFreeMem(block);
[2032]100}
101//******************************************************************************
102//******************************************************************************
Note: See TracBrowser for help on using the repository browser.