| 1 | /* $Id: heapcode.cpp,v 1.1 1999-12-09 00:52:20 sandervl Exp $ */
|
|---|
| 2 | /*
|
|---|
| 3 | * Code heap functions for OS/2
|
|---|
| 4 | *
|
|---|
| 5 | * Initially commit 4 kb, add more when required
|
|---|
| 6 | *
|
|---|
| 7 | * TODO: Not process/thread safe (initializing/destroying heap)
|
|---|
| 8 | *
|
|---|
| 9 | * ASSUMPTION: Rtl library takes care of protection of heap increase/decrease
|
|---|
| 10 | * (from multiple threads/processes)
|
|---|
| 11 | *
|
|---|
| 12 | * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 13 | *
|
|---|
| 14 | */
|
|---|
| 15 | #define INCL_BASE
|
|---|
| 16 | #define INCL_DOSMEMMGR
|
|---|
| 17 | #include <os2wrap.h>
|
|---|
| 18 | #include <misc.h>
|
|---|
| 19 | #include <heapcode.h>
|
|---|
| 20 |
|
|---|
| 21 | static Heap_t codeHeap = 0;
|
|---|
| 22 | static PVOID pCodeMem = NULL;
|
|---|
| 23 |
|
|---|
| 24 | void * _LNK_CONV getmoreCodeMem(Heap_t pHeap, size_t *size, int *clean);
|
|---|
| 25 | void _LNK_CONV releaseCodeMem(Heap_t pHeap, void *block, size_t size);
|
|---|
| 26 |
|
|---|
| 27 | //******************************************************************************
|
|---|
| 28 | //******************************************************************************
|
|---|
| 29 | BOOL InitializeCodeHeap()
|
|---|
| 30 | {
|
|---|
| 31 | APIRET rc;
|
|---|
| 32 |
|
|---|
| 33 | dprintf(("KERNEL32: InitializeCodeHeap"));
|
|---|
| 34 | rc = DosAllocMem(&pCodeMem, PAGE_SIZE, PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
|
|---|
| 35 | if(rc != 0) {
|
|---|
| 36 | dprintf(("InitializeSharedHeap: DosAllocSharedMem failed with %d", rc));
|
|---|
| 37 | return FALSE;
|
|---|
| 38 | }
|
|---|
| 39 | codeHeap = _ucreate(pCodeMem, PAGE_SIZE, _BLOCK_CLEAN,
|
|---|
| 40 | _HEAP_REGULAR, getmoreCodeMem, releaseCodeMem);
|
|---|
| 41 | if(codeHeap == NULL) {
|
|---|
| 42 | DosFreeMem(pCodeMem);
|
|---|
| 43 | pCodeMem = NULL;
|
|---|
| 44 | dprintf(("InitializeSharedHeap: _ucreate failed!"));
|
|---|
| 45 | return FALSE;
|
|---|
| 46 | }
|
|---|
| 47 | return TRUE;
|
|---|
| 48 | }
|
|---|
| 49 | //******************************************************************************
|
|---|
| 50 | //******************************************************************************
|
|---|
| 51 | void DestroyCodeHeap()
|
|---|
| 52 | {
|
|---|
| 53 | dprintf(("KERNEL32: DestroyCodeHeap"));
|
|---|
| 54 | if(codeHeap) {
|
|---|
| 55 | _uclose(codeHeap);
|
|---|
| 56 | _udestroy(codeHeap, _FORCE);
|
|---|
| 57 | codeHeap = NULL;
|
|---|
| 58 | }
|
|---|
| 59 | if(pCodeMem) {
|
|---|
| 60 | DosFreeMem(pCodeMem);
|
|---|
| 61 | pCodeMem = NULL;
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 | //******************************************************************************
|
|---|
| 65 | //******************************************************************************
|
|---|
| 66 | void *_cmalloc(size_t size)
|
|---|
| 67 | {
|
|---|
| 68 | return _umalloc(codeHeap, size);
|
|---|
| 69 | }
|
|---|
| 70 | //******************************************************************************
|
|---|
| 71 | //******************************************************************************
|
|---|
| 72 | void * _LNK_CONV getmoreCodeMem(Heap_t pHeap, size_t *size, int *clean)
|
|---|
| 73 | {
|
|---|
| 74 | APIRET rc;
|
|---|
| 75 | PVOID newblock;
|
|---|
| 76 |
|
|---|
| 77 | dprintf(("KERNEL32: getmoreCodeMem(%08xh, %08xh, %08xh)\n",
|
|---|
| 78 | pHeap,
|
|---|
| 79 | *size,
|
|---|
| 80 | *clean));
|
|---|
| 81 |
|
|---|
| 82 | /* round the size up to a multiple of 4K */
|
|---|
| 83 | *size = (*size / 4096) * 4096 + 4096;
|
|---|
| 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 | //******************************************************************************
|
|---|