| 1 |
|
|---|
| 2 | /*
|
|---|
| 3 | * Copyright © 2001 Novell, Inc. All Rights Reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * You may distribute under the terms of either the GNU General Public
|
|---|
| 6 | * License or the Artistic License, as specified in the README file.
|
|---|
| 7 | *
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | /*
|
|---|
| 11 | * FILENAME : NWVMem.h
|
|---|
| 12 | * DESCRIPTION : Memory management for Perl Interpreter on NetWare.
|
|---|
| 13 | * Watcom's hash table is used to store memory pointers.
|
|---|
| 14 | * All malloc's, realloc's, free's go through this.
|
|---|
| 15 | * Author : HYAK, SGP
|
|---|
| 16 | * Date : Januray 2001.
|
|---|
| 17 | *
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | #ifndef ___NWVMEM_H_INC___
|
|---|
| 23 | #define ___NWVMEM_H_INC___
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | #include "win32ish.h" // For "BOOL", "TRUE" and "FALSE"
|
|---|
| 27 | #include <nwhashcls.h> // CW changes
|
|---|
| 28 | #include <nwmalloc.h>
|
|---|
| 29 | #include "string.h"
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | class VMem
|
|---|
| 34 | {
|
|---|
| 35 | public:
|
|---|
| 36 | VMem();
|
|---|
| 37 | virtual ~VMem();
|
|---|
| 38 | virtual void* Malloc(size_t size);
|
|---|
| 39 | virtual void* Realloc(void* pMem, size_t size);
|
|---|
| 40 | virtual void Free(void* pMem);
|
|---|
| 41 | virtual void* Calloc(size_t num, size_t size);
|
|---|
| 42 |
|
|---|
| 43 | protected:
|
|---|
| 44 | BOOL m_dontTouchHashLists;
|
|---|
| 45 | // WCValHashTable<void*>* m_allocList;
|
|---|
| 46 | NWPerlHashList *m_allocList; // CW changes
|
|---|
| 47 | };
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 | /*============================================================================================
|
|---|
| 53 |
|
|---|
| 54 | Function : fnAllocListHash
|
|---|
| 55 |
|
|---|
| 56 | Description : Hashing function for hash table of memory allocations.
|
|---|
| 57 |
|
|---|
| 58 | Parameters : invalue (IN).
|
|---|
| 59 |
|
|---|
| 60 | Returns : unsigned.
|
|---|
| 61 |
|
|---|
| 62 | ==============================================================================================*/
|
|---|
| 63 |
|
|---|
| 64 | unsigned fnAllocListHash(void* const& invalue)
|
|---|
| 65 | {
|
|---|
| 66 | return (((unsigned) invalue & 0x0000ff00) >> 8);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 | /*============================================================================================
|
|---|
| 72 |
|
|---|
| 73 | Function : fnFreeMemEntry
|
|---|
| 74 |
|
|---|
| 75 | Description : Called for each outstanding memory allocation at the end of a script run.
|
|---|
| 76 | Frees the outstanding allocations
|
|---|
| 77 |
|
|---|
| 78 | Parameters : ptr (IN).
|
|---|
| 79 | context (IN)
|
|---|
| 80 |
|
|---|
| 81 | Returns : Nothing.
|
|---|
| 82 |
|
|---|
| 83 | ==============================================================================================*/
|
|---|
| 84 |
|
|---|
| 85 | void fnFreeMemEntry(void* ptr, void* context)
|
|---|
| 86 | {
|
|---|
| 87 | VMem* pVMem = (VMem*) context;
|
|---|
| 88 |
|
|---|
| 89 | if(ptr && pVMem)
|
|---|
| 90 | {
|
|---|
| 91 | pVMem->Free(ptr);
|
|---|
| 92 | ptr=NULL;
|
|---|
| 93 | pVMem = NULL;
|
|---|
| 94 | context = NULL;
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 | /*============================================================================================
|
|---|
| 101 |
|
|---|
| 102 | Function : VMem Constructor
|
|---|
| 103 |
|
|---|
| 104 | Description :
|
|---|
| 105 |
|
|---|
| 106 | Parameters :
|
|---|
| 107 |
|
|---|
| 108 | Returns :
|
|---|
| 109 |
|
|---|
| 110 | ==============================================================================================*/
|
|---|
| 111 |
|
|---|
| 112 | VMem::VMem()
|
|---|
| 113 | {
|
|---|
| 114 | //Constructor
|
|---|
| 115 | m_dontTouchHashLists = FALSE;
|
|---|
| 116 | m_allocList = NULL;
|
|---|
| 117 | // m_allocList = new WCValHashTable<void*> (fnAllocListHash, 256);
|
|---|
| 118 | m_allocList = new NWPerlHashList(); // CW changes
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 | /*============================================================================================
|
|---|
| 124 |
|
|---|
| 125 | Function : VMem Destructor
|
|---|
| 126 |
|
|---|
| 127 | Description :
|
|---|
| 128 |
|
|---|
| 129 | Parameters :
|
|---|
| 130 |
|
|---|
| 131 | Returns :
|
|---|
| 132 |
|
|---|
| 133 | ==============================================================================================*/
|
|---|
| 134 |
|
|---|
| 135 | VMem::~VMem(void)
|
|---|
| 136 | {
|
|---|
| 137 | //Destructor
|
|---|
| 138 | m_dontTouchHashLists = TRUE;
|
|---|
| 139 | if (m_allocList)
|
|---|
| 140 | {
|
|---|
| 141 | m_allocList->forAll(fnFreeMemEntry, (void*) this);
|
|---|
| 142 |
|
|---|
| 143 | delete m_allocList;
|
|---|
| 144 | m_allocList = NULL;
|
|---|
| 145 | }
|
|---|
| 146 | m_dontTouchHashLists = FALSE;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | /*============================================================================================
|
|---|
| 152 |
|
|---|
| 153 | Function : VMem::Malloc
|
|---|
| 154 |
|
|---|
| 155 | Description : Allocates memory.
|
|---|
| 156 |
|
|---|
| 157 | Parameters : size (IN) - Size of memory to be allocated.
|
|---|
| 158 |
|
|---|
| 159 | Returns : Pointer to the allocated memory block.
|
|---|
| 160 |
|
|---|
| 161 | ==============================================================================================*/
|
|---|
| 162 |
|
|---|
| 163 | void* VMem::Malloc(size_t size)
|
|---|
| 164 | {
|
|---|
| 165 | void *ptr = NULL;
|
|---|
| 166 |
|
|---|
| 167 | if (size <= 0)
|
|---|
| 168 | return NULL;
|
|---|
| 169 |
|
|---|
| 170 | ptr = malloc(size);
|
|---|
| 171 | if (ptr)
|
|---|
| 172 | {
|
|---|
| 173 | if(m_allocList)
|
|---|
| 174 | m_allocList->insert(ptr);
|
|---|
| 175 | }
|
|---|
| 176 | else
|
|---|
| 177 | {
|
|---|
| 178 | m_dontTouchHashLists = TRUE;
|
|---|
| 179 | if (m_allocList)
|
|---|
| 180 | {
|
|---|
| 181 | m_allocList->forAll(fnFreeMemEntry, (void*) this);
|
|---|
| 182 | delete m_allocList;
|
|---|
| 183 | m_allocList = NULL;
|
|---|
| 184 | }
|
|---|
| 185 | m_dontTouchHashLists = FALSE;
|
|---|
| 186 |
|
|---|
| 187 | // Serious error since memory allocation falied. So, exiting...
|
|---|
| 188 | ExitThread(TSR_THREAD, 1);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | return(ptr);
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 | /*============================================================================================
|
|---|
| 197 |
|
|---|
| 198 | Function : VMem::Realloc
|
|---|
| 199 |
|
|---|
| 200 | Description : Reallocates block of memory.
|
|---|
| 201 |
|
|---|
| 202 | Parameters : block (IN) - Points to a previously allocated memory block.
|
|---|
| 203 | size (IN) - Size of memory to be allocated.
|
|---|
| 204 |
|
|---|
| 205 | Returns : Pointer to the allocated memory block.
|
|---|
| 206 |
|
|---|
| 207 | ==============================================================================================*/
|
|---|
| 208 |
|
|---|
| 209 | void* VMem::Realloc(void* block, size_t size)
|
|---|
| 210 | {
|
|---|
| 211 | void *ptr = NULL;
|
|---|
| 212 |
|
|---|
| 213 | if (size <= 0)
|
|---|
| 214 | return NULL;
|
|---|
| 215 |
|
|---|
| 216 | ptr = realloc(block, size);
|
|---|
| 217 | if (ptr)
|
|---|
| 218 | {
|
|---|
| 219 | if (block)
|
|---|
| 220 | {
|
|---|
| 221 | if (m_allocList)
|
|---|
| 222 | m_allocList->remove(block);
|
|---|
| 223 | }
|
|---|
| 224 | if (m_allocList)
|
|---|
| 225 | m_allocList->insert(ptr);
|
|---|
| 226 | }
|
|---|
| 227 | else
|
|---|
| 228 | {
|
|---|
| 229 | m_dontTouchHashLists = TRUE;
|
|---|
| 230 | if (m_allocList)
|
|---|
| 231 | {
|
|---|
| 232 | m_allocList->forAll(fnFreeMemEntry, (void*) this);
|
|---|
| 233 | delete m_allocList;
|
|---|
| 234 | m_allocList = NULL;
|
|---|
| 235 | }
|
|---|
| 236 | m_dontTouchHashLists = FALSE;
|
|---|
| 237 |
|
|---|
| 238 | // Serious error since memory allocation falied. So, exiting...
|
|---|
| 239 | ExitThread(TSR_THREAD, 1);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | return(ptr);
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 | /*============================================================================================
|
|---|
| 248 |
|
|---|
| 249 | Function : VMem::Calloc
|
|---|
| 250 |
|
|---|
| 251 | Description : Allocates and clears memory space for an array of objects.
|
|---|
| 252 |
|
|---|
| 253 | Parameters : num (IN) - Specifies the number of objects.
|
|---|
| 254 | size (IN) - Size of each object.
|
|---|
| 255 |
|
|---|
| 256 | Returns : Pointer to the allocated memory block.
|
|---|
| 257 |
|
|---|
| 258 | ==============================================================================================*/
|
|---|
| 259 |
|
|---|
| 260 | void* VMem::Calloc(size_t num, size_t size)
|
|---|
| 261 | {
|
|---|
| 262 | void *ptr = NULL;
|
|---|
| 263 |
|
|---|
| 264 | if (size <= 0)
|
|---|
| 265 | return NULL;
|
|---|
| 266 |
|
|---|
| 267 | ptr = calloc(num, size);
|
|---|
| 268 | if (ptr)
|
|---|
| 269 | {
|
|---|
| 270 | if(m_allocList)
|
|---|
| 271 | m_allocList->insert(ptr);
|
|---|
| 272 | }
|
|---|
| 273 | else
|
|---|
| 274 | {
|
|---|
| 275 | m_dontTouchHashLists = TRUE;
|
|---|
| 276 | if (m_allocList)
|
|---|
| 277 | {
|
|---|
| 278 | m_allocList->forAll(fnFreeMemEntry, (void*) this);
|
|---|
| 279 | delete m_allocList;
|
|---|
| 280 | m_allocList = NULL;
|
|---|
| 281 | }
|
|---|
| 282 | m_dontTouchHashLists = FALSE;
|
|---|
| 283 |
|
|---|
| 284 | // Serious error since memory allocation falied. So, exiting...
|
|---|
| 285 | ExitThread(TSR_THREAD, 1);
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | return(ptr);
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 | /*============================================================================================
|
|---|
| 294 |
|
|---|
| 295 | Function : VMem::Free
|
|---|
| 296 |
|
|---|
| 297 | Description : Frees allocated memory.
|
|---|
| 298 |
|
|---|
| 299 | Parameters : p (IN) - Points to the allocated memory.
|
|---|
| 300 |
|
|---|
| 301 | Returns : Nothing.
|
|---|
| 302 |
|
|---|
| 303 | ==============================================================================================*/
|
|---|
| 304 |
|
|---|
| 305 | void VMem::Free(void* p)
|
|---|
| 306 | {
|
|---|
| 307 | // Final clean up, free all the nodes from the hash list
|
|---|
| 308 | if (m_dontTouchHashLists)
|
|---|
| 309 | {
|
|---|
| 310 | if(p)
|
|---|
| 311 | {
|
|---|
| 312 | free(p);
|
|---|
| 313 | p = NULL;
|
|---|
| 314 | }
|
|---|
| 315 | }
|
|---|
| 316 | else
|
|---|
| 317 | {
|
|---|
| 318 | if(p && m_allocList)
|
|---|
| 319 | {
|
|---|
| 320 | if (m_allocList->remove(p))
|
|---|
| 321 | {
|
|---|
| 322 | free(p);
|
|---|
| 323 | p = NULL;
|
|---|
| 324 | }
|
|---|
| 325 | else
|
|---|
| 326 | {
|
|---|
| 327 | // If it comes here, that means that the memory pointer is not contained in the hash list.
|
|---|
| 328 | // But no need to free now, since if is deleted here, it will result in an abend!!
|
|---|
| 329 | // If the memory is still there, it will be cleaned during final cleanup anyway.
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 | return;
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 | #endif //___NWVMEM_H_INC___
|
|---|
| 340 |
|
|---|