3 * Copyright © 2001 Novell, Inc. All Rights Reserved.
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.
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.
16 * Date : Januray 2001.
22 #ifndef ___NWVMEM_H_INC___
23 #define ___NWVMEM_H_INC___
26 #include "win32ish.h" // For "BOOL", "TRUE" and "FALSE"
27 #include <nwhashcls.h> // CW changes
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);
44 BOOL m_dontTouchHashLists;
45 // WCValHashTable<void*>* m_allocList;
46 NWPerlHashList *m_allocList; // CW changes
52 /*============================================================================================
54 Function : fnAllocListHash
56 Description : Hashing function for hash table of memory allocations.
58 Parameters : invalue (IN).
62 ==============================================================================================*/
64 unsigned fnAllocListHash(void* const& invalue)
66 return (((unsigned) invalue & 0x0000ff00) >> 8);
71 /*============================================================================================
73 Function : fnFreeMemEntry
75 Description : Called for each outstanding memory allocation at the end of a script run.
76 Frees the outstanding allocations
78 Parameters : ptr (IN).
83 ==============================================================================================*/
85 void fnFreeMemEntry(void* ptr, void* context)
87 VMem* pVMem = (VMem*) context;
100 /*============================================================================================
102 Function : VMem Constructor
110 ==============================================================================================*/
115 m_dontTouchHashLists = FALSE;
117 // m_allocList = new WCValHashTable<void*> (fnAllocListHash, 256);
118 m_allocList = new NWPerlHashList(); // CW changes
123 /*============================================================================================
125 Function : VMem Destructor
133 ==============================================================================================*/
138 m_dontTouchHashLists = TRUE;
141 m_allocList->forAll(fnFreeMemEntry, (void*) this);
146 m_dontTouchHashLists = FALSE;
151 /*============================================================================================
153 Function : VMem::Malloc
155 Description : Allocates memory.
157 Parameters : size (IN) - Size of memory to be allocated.
159 Returns : Pointer to the allocated memory block.
161 ==============================================================================================*/
163 void* VMem::Malloc(size_t size)
174 m_allocList->insert(ptr);
178 m_dontTouchHashLists = TRUE;
181 m_allocList->forAll(fnFreeMemEntry, (void*) this);
185 m_dontTouchHashLists = FALSE;
187 // Serious error since memory allocation falied. So, exiting...
188 ExitThread(TSR_THREAD, 1);
196 /*============================================================================================
198 Function : VMem::Realloc
200 Description : Reallocates block of memory.
202 Parameters : block (IN) - Points to a previously allocated memory block.
203 size (IN) - Size of memory to be allocated.
205 Returns : Pointer to the allocated memory block.
207 ==============================================================================================*/
209 void* VMem::Realloc(void* block, size_t size)
216 ptr = realloc(block, size);
222 m_allocList->remove(block);
225 m_allocList->insert(ptr);
229 m_dontTouchHashLists = TRUE;
232 m_allocList->forAll(fnFreeMemEntry, (void*) this);
236 m_dontTouchHashLists = FALSE;
238 // Serious error since memory allocation falied. So, exiting...
239 ExitThread(TSR_THREAD, 1);
247 /*============================================================================================
249 Function : VMem::Calloc
251 Description : Allocates and clears memory space for an array of objects.
253 Parameters : num (IN) - Specifies the number of objects.
254 size (IN) - Size of each object.
256 Returns : Pointer to the allocated memory block.
258 ==============================================================================================*/
260 void* VMem::Calloc(size_t num, size_t size)
267 ptr = calloc(num, size);
271 m_allocList->insert(ptr);
275 m_dontTouchHashLists = TRUE;
278 m_allocList->forAll(fnFreeMemEntry, (void*) this);
282 m_dontTouchHashLists = FALSE;
284 // Serious error since memory allocation falied. So, exiting...
285 ExitThread(TSR_THREAD, 1);
293 /*============================================================================================
295 Function : VMem::Free
297 Description : Frees allocated memory.
299 Parameters : p (IN) - Points to the allocated memory.
303 ==============================================================================================*/
305 void VMem::Free(void* p)
307 // Final clean up, free all the nodes from the hash list
308 if (m_dontTouchHashLists)
320 if (m_allocList->remove(p))
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.
339 #endif //___NWVMEM_H_INC___