[4164] | 1 | /* $Id: malloc.c,v 1.7 2000-09-02 21:08:14 bird Exp $
|
---|
[847] | 2 | *
|
---|
[2511] | 3 | * Common Heap - this forwards to the swappable heap!
|
---|
[847] | 4 | *
|
---|
| 5 | * Note: This heap does very little checking on input.
|
---|
| 6 | * Use with care! We're running at Ring-0!
|
---|
| 7 | *
|
---|
[2511] | 8 | * Copyright (c) 1999-2000 knut st. osmundsen
|
---|
[847] | 9 | *
|
---|
[1678] | 10 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
| 11 | *
|
---|
[847] | 12 | */
|
---|
[2511] | 13 | /*******************************************************************************
|
---|
| 14 | * Defined Constants And Macros *
|
---|
| 15 | *******************************************************************************/
|
---|
| 16 | #define INCL_NOAPI
|
---|
[847] | 17 |
|
---|
| 18 | /******************************************************************************
|
---|
| 19 | * Headerfiles
|
---|
| 20 | ******************************************************************************/
|
---|
| 21 | #include <os2.h>
|
---|
[4164] | 22 | #include "devSegDf.h" /* Win32k segment definitions. */
|
---|
[2511] | 23 | #include "smalloc.h"
|
---|
| 24 | #include "rmalloc.h"
|
---|
| 25 | #include "options.h"
|
---|
[847] | 26 |
|
---|
| 27 |
|
---|
| 28 | /******************************************************************************
|
---|
| 29 | * Global data
|
---|
| 30 | ******************************************************************************/
|
---|
[4164] | 31 | #if !defined(RING0) || defined(R3TST)
|
---|
[1271] | 32 | char fInited; /* init flag */
|
---|
| 33 | #endif
|
---|
[847] | 34 |
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
[2511] | 38 | * Initiate the heap "subsystems" - both the resident and the swappable heaps.
|
---|
[847] | 39 | * @returns 0 on success, not 0 on error.
|
---|
[2511] | 40 | * @param cbResInit Resident heap initial size.
|
---|
| 41 | * @param cbResMax Resident heap maximum size.
|
---|
| 42 | * @param cbSwpInit Swappable heap initial size.
|
---|
| 43 | * @param cbSwpMax Swappable heap maximum size.
|
---|
[847] | 44 | */
|
---|
[2511] | 45 | int heapInit(unsigned cbResInit, unsigned cbResMax,
|
---|
| 46 | unsigned cbSwpInit, unsigned cbSwpMax)
|
---|
[847] | 47 | {
|
---|
[2511] | 48 | int rc;
|
---|
[847] | 49 |
|
---|
[2511] | 50 | rc = resHeapInit(cbResInit, cbResMax);
|
---|
| 51 | if (rc != 0)
|
---|
| 52 | return rc;
|
---|
| 53 | rc = swpHeapInit(cbSwpInit, cbSwpMax);
|
---|
| 54 | if (rc != 0)
|
---|
| 55 | return rc;
|
---|
[4164] | 56 | #if !defined(RING0) || defined(R3TST)
|
---|
[1271] | 57 | fInited = TRUE;
|
---|
| 58 | #endif
|
---|
[847] | 59 | return 0;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * malloc - allocates a given amount of memory.
|
---|
| 65 | * @returns Pointer to allocated memory.
|
---|
| 66 | * NULL if out of memory. (Or memory to fragmented.)
|
---|
| 67 | * @param cbSize Bytes user requests us to allocate. This is aligned
|
---|
| 68 | * to four bytes.
|
---|
| 69 | */
|
---|
| 70 | void * malloc(unsigned cbSize)
|
---|
| 71 | {
|
---|
[4164] | 72 | #ifdef R3TST
|
---|
| 73 | if (!fInited)
|
---|
| 74 | {
|
---|
| 75 | PVOID pv;
|
---|
| 76 | if (!DosAllocMem(&pv, cbSize, PAG_WRITE | PAG_READ | PAG_COMMIT)) /* no SSToDS! */
|
---|
| 77 | return pv;
|
---|
| 78 | else
|
---|
| 79 | return NULL;
|
---|
| 80 | }
|
---|
| 81 | #endif
|
---|
[2511] | 82 | return smalloc(cbSize);
|
---|
[847] | 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * Reallocate a heapblock.
|
---|
| 88 | * @returns Pointer to new heapblock.
|
---|
| 89 | * @param pv Pointer to the block to realloc.
|
---|
| 90 | * @param cbNew The new block size.
|
---|
| 91 | */
|
---|
| 92 | void *realloc(void *pv, unsigned cbNew)
|
---|
| 93 | {
|
---|
[4164] | 94 | #ifdef R3TST
|
---|
| 95 | if (!fInited)
|
---|
| 96 | Int3();
|
---|
| 97 | #endif
|
---|
[2511] | 98 | return srealloc(pv, cbNew);
|
---|
[847] | 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * Frees a block.
|
---|
| 104 | * @param pv User pointer.
|
---|
| 105 | */
|
---|
| 106 | void free(void *pv)
|
---|
| 107 | {
|
---|
[4164] | 108 | #ifdef R3TST
|
---|
| 109 | if (!fInited) /* controlled leak! */
|
---|
| 110 | return;
|
---|
| 111 | #endif
|
---|
[2511] | 112 | sfree(pv);
|
---|
[847] | 113 | }
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 | /**
|
---|
| 117 | * Gets the size of a block.
|
---|
| 118 | * @returns Bytes in a block.
|
---|
| 119 | */
|
---|
| 120 | unsigned _msize(void *pv)
|
---|
| 121 | {
|
---|
[2511] | 122 | return _swp_msize(pv);
|
---|
[847] | 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
| 127 | * Checks if pv is a valid heappointer.
|
---|
| 128 | * @returns 1 if valid. 0 if invalid.
|
---|
| 129 | * @param pv User data pointer.
|
---|
| 130 | */
|
---|
| 131 | int _validptr(void *pv)
|
---|
| 132 | {
|
---|
[2511] | 133 | return _swp_validptr(pv);
|
---|
[847] | 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | /**
|
---|
| 138 | * Checks that the dataaera made up by pv and cbSize valid with in the heap.
|
---|
| 139 | * @returns 1 if valid. 0 if invalid.
|
---|
| 140 | * @param pv User data pointer.
|
---|
| 141 | * @param cbSize Size of data which has to be valid.
|
---|
| 142 | */
|
---|
| 143 | int _validptr2(void *pv, unsigned cbSize)
|
---|
| 144 | {
|
---|
[2511] | 145 | return _swp_validptr2(pv, cbSize);
|
---|
[847] | 146 | }
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
| 150 | * Get amount of free memory (in bytes)
|
---|
| 151 | * @returns Amount of free memory (in bytes).
|
---|
| 152 | * @remark Note that this amount is of all free memory blocks and
|
---|
| 153 | * that these blocks are fragmented.
|
---|
| 154 | * You'll probably not be able to allocate a single block
|
---|
| 155 | * of the returned size.
|
---|
| 156 | */
|
---|
| 157 | unsigned _memfree(void)
|
---|
| 158 | {
|
---|
[2511] | 159 | return _swp_memfree();
|
---|
[847] | 160 | }
|
---|
| 161 |
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * Checks heap integrety.
|
---|
| 165 | * @returns TRUE when ok.
|
---|
| 166 | * FALSE on error.
|
---|
| 167 | * NULL if out of memory. (Or memory to fragmented.)
|
---|
| 168 | */
|
---|
| 169 | int _heap_check(void)
|
---|
| 170 | {
|
---|
[2511] | 171 | return _swp_heap_check();
|
---|
[847] | 172 | }
|
---|
| 173 |
|
---|
| 174 |
|
---|
[4164] | 175 |
|
---|
[1271] | 176 | #if !defined(RING0) && defined(__IBMC__)
|
---|
| 177 |
|
---|
| 178 | /**
|
---|
| 179 | * Initialize Memory Functions
|
---|
| 180 | * Called from _exeentry.
|
---|
| 181 | */
|
---|
| 182 | int _rmem_init(void)
|
---|
| 183 | {
|
---|
[4164] | 184 | int rc;
|
---|
| 185 | rc = heapInit(CB_RES_INIT, CB_RES_MAX, CB_SWP_INIT, CB_SWP_MAX);
|
---|
[1271] | 186 | return rc;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
| 190 | * Initialize Memory Functions
|
---|
| 191 | * Called from _exeentry.
|
---|
| 192 | */
|
---|
| 193 | int _rmem_term(void)
|
---|
| 194 | {
|
---|
| 195 | return 0;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | #endif
|
---|