1 | /* $Id: heapcode.cpp,v 1.2 2000-02-16 14:23:57 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 | #define DBG_LOCALLOG DBG_heapcode
|
---|
22 | #include "dbglocal.h"
|
---|
23 |
|
---|
24 | static Heap_t codeHeap = 0;
|
---|
25 | static PVOID pCodeMem = NULL;
|
---|
26 |
|
---|
27 | void * _LNK_CONV getmoreCodeMem(Heap_t pHeap, size_t *size, int *clean);
|
---|
28 | void _LNK_CONV releaseCodeMem(Heap_t pHeap, void *block, size_t size);
|
---|
29 |
|
---|
30 | //******************************************************************************
|
---|
31 | //******************************************************************************
|
---|
32 | BOOL InitializeCodeHeap()
|
---|
33 | {
|
---|
34 | APIRET rc;
|
---|
35 |
|
---|
36 | dprintf(("KERNEL32: InitializeCodeHeap"));
|
---|
37 | rc = DosAllocMem(&pCodeMem, PAGE_SIZE, PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
|
---|
38 | if(rc != 0) {
|
---|
39 | dprintf(("InitializeSharedHeap: DosAllocSharedMem failed with %d", rc));
|
---|
40 | return FALSE;
|
---|
41 | }
|
---|
42 | codeHeap = _ucreate(pCodeMem, PAGE_SIZE, _BLOCK_CLEAN,
|
---|
43 | _HEAP_REGULAR, getmoreCodeMem, releaseCodeMem);
|
---|
44 | if(codeHeap == NULL) {
|
---|
45 | DosFreeMem(pCodeMem);
|
---|
46 | pCodeMem = NULL;
|
---|
47 | dprintf(("InitializeSharedHeap: _ucreate failed!"));
|
---|
48 | return FALSE;
|
---|
49 | }
|
---|
50 | return TRUE;
|
---|
51 | }
|
---|
52 | //******************************************************************************
|
---|
53 | //******************************************************************************
|
---|
54 | void DestroyCodeHeap()
|
---|
55 | {
|
---|
56 | dprintf(("KERNEL32: DestroyCodeHeap"));
|
---|
57 | if(codeHeap) {
|
---|
58 | _uclose(codeHeap);
|
---|
59 | _udestroy(codeHeap, _FORCE);
|
---|
60 | codeHeap = NULL;
|
---|
61 | }
|
---|
62 | if(pCodeMem) {
|
---|
63 | DosFreeMem(pCodeMem);
|
---|
64 | pCodeMem = NULL;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | //******************************************************************************
|
---|
68 | //******************************************************************************
|
---|
69 | void *_cmalloc(size_t size)
|
---|
70 | {
|
---|
71 | return _umalloc(codeHeap, size);
|
---|
72 | }
|
---|
73 | //******************************************************************************
|
---|
74 | //******************************************************************************
|
---|
75 | void * _LNK_CONV getmoreCodeMem(Heap_t pHeap, size_t *size, int *clean)
|
---|
76 | {
|
---|
77 | APIRET rc;
|
---|
78 | PVOID newblock;
|
---|
79 |
|
---|
80 | dprintf(("KERNEL32: getmoreCodeMem(%08xh, %08xh, %08xh)\n",
|
---|
81 | pHeap,
|
---|
82 | *size,
|
---|
83 | *clean));
|
---|
84 |
|
---|
85 | /* round the size up to a multiple of 4K */
|
---|
86 | *size = (*size / 4096) * 4096 + 4096;
|
---|
87 |
|
---|
88 | rc = DosAllocMem(&newblock, *size, PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
|
---|
89 | if(rc != 0) {
|
---|
90 | dprintf(("getmoreCodeMem: DosAllocMem failed with %d", rc));
|
---|
91 | return FALSE;
|
---|
92 | }
|
---|
93 | *clean = _BLOCK_CLEAN;
|
---|
94 | dprintf(("KERNEL32: getmoreCodeMem %x %d", newblock, *size));
|
---|
95 | return newblock;
|
---|
96 | }
|
---|
97 | //******************************************************************************
|
---|
98 | //******************************************************************************
|
---|
99 | void _LNK_CONV releaseCodeMem(Heap_t pHeap, void *block, size_t size)
|
---|
100 | {
|
---|
101 | dprintf(("KERNEL32: releaseCodeMem %x %d", block, size));
|
---|
102 | DosFreeMem(block);
|
---|
103 | }
|
---|
104 | //******************************************************************************
|
---|
105 | //******************************************************************************
|
---|