| 1 | /* $Id: heapcode.cpp,v 1.4 2002-07-15 14:28:51 sandervl Exp $ */ | 
|---|
| 2 | /* | 
|---|
| 3 | * Code heap functions for OS/2 | 
|---|
| 4 | * | 
|---|
| 5 | * Initially commit 4 kb, add more when required | 
|---|
| 6 | * | 
|---|
| 7 | * NOTE: If high memory is ever used for this heap, then you must use VirtualAlloc! | 
|---|
| 8 | * | 
|---|
| 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 |  | 
|---|
| 23 | #define DBG_LOCALLOG    DBG_heapcode | 
|---|
| 24 | #include "dbglocal.h" | 
|---|
| 25 |  | 
|---|
| 26 | Heap_t          codeHeap = 0; | 
|---|
| 27 | static PVOID    pCodeMem = NULL; | 
|---|
| 28 |  | 
|---|
| 29 | void * _LNK_CONV getmoreCodeMem(Heap_t pHeap, size_t *size, int *clean); | 
|---|
| 30 | void _LNK_CONV   releaseCodeMem(Heap_t pHeap, void *block, size_t size); | 
|---|
| 31 |  | 
|---|
| 32 | //****************************************************************************** | 
|---|
| 33 | //****************************************************************************** | 
|---|
| 34 | BOOL InitializeCodeHeap() | 
|---|
| 35 | { | 
|---|
| 36 | APIRET rc; | 
|---|
| 37 |  | 
|---|
| 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); | 
|---|
| 50 | pCodeMem = NULL; | 
|---|
| 51 | dprintf(("InitializeSharedHeap: _ucreate failed!")); | 
|---|
| 52 | return FALSE; | 
|---|
| 53 | } | 
|---|
| 54 | return TRUE; | 
|---|
| 55 | } | 
|---|
| 56 | //****************************************************************************** | 
|---|
| 57 | //****************************************************************************** | 
|---|
| 58 | void DestroyCodeHeap() | 
|---|
| 59 | { | 
|---|
| 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 | } | 
|---|
| 70 | } | 
|---|
| 71 | //****************************************************************************** | 
|---|
| 72 | //****************************************************************************** | 
|---|
| 73 | void * _LNK_CONV getmoreCodeMem(Heap_t pHeap, size_t *size, int *clean) | 
|---|
| 74 | { | 
|---|
| 75 | APIRET rc; | 
|---|
| 76 | PVOID newblock; | 
|---|
| 77 |  | 
|---|
| 78 | dprintf(("KERNEL32: getmoreCodeMem(%08xh, %08xh, %08xh)\n", pHeap, *size, *clean)); | 
|---|
| 79 |  | 
|---|
| 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; | 
|---|
| 84 |  | 
|---|
| 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; | 
|---|
| 93 | } | 
|---|
| 94 | //****************************************************************************** | 
|---|
| 95 | //****************************************************************************** | 
|---|
| 96 | void _LNK_CONV releaseCodeMem(Heap_t pHeap, void *block, size_t size) | 
|---|
| 97 | { | 
|---|
| 98 | dprintf(("KERNEL32: releaseCodeMem %x %d", block, size)); | 
|---|
| 99 | DosFreeMem(block); | 
|---|
| 100 | } | 
|---|
| 101 | //****************************************************************************** | 
|---|
| 102 | //****************************************************************************** | 
|---|