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