cgma
|
00001 //- Class: MemoryManager 00002 //- Owner: Jim Hipp 00003 //- Description: MemoryManager provides object information and stack 00004 //- storage pointer management used in association with 00005 //- MemoryBlock and MemoryAllocation classes. 00006 //- Checked By: 00007 //- Version: 00008 00009 #ifndef MEMORY_MANAGER_HPP 00010 #define MEMORY_MANAGER_HPP 00011 00012 #define USE_DYNAMIC_MEMORY_ALLOCATION 00013 //- comment out this #define to use global system level dynamic memory 00014 //- allocation ... Otherwise overloaded operators new and delete will 00015 //- be set to call operator_new and operator_delete defined in this module 00016 00017 const int DEFAULT_MEMORY_ALLOC_SIZE = 1024; 00018 //- Default MemoryBlock allocation size 00019 00020 const int STATIC_MEMORY_MANAGER = 1; 00021 //- Static Memory Manager definition argument 00022 00023 #include <cstdlib> 00024 #include <cassert> 00025 #include "CGMUtilConfigure.h" 00026 00027 class MemoryBlock; 00028 00029 class CUBIT_UTIL_EXPORT MemoryManager 00030 { 00031 private: 00032 00033 const bool useNew; 00034 //- Disable memory management (pass all calls to new/delete) 00035 //- Unused if compiled with -DNDEBUG 00036 00037 char* objectName; 00038 //- name of the class for which this memory manager is declared 00039 00040 MemoryBlock* memBlockStack; 00041 //- memBlockStack is the head of a stack of MemoryBlock objects 00042 //- that contain allocated memory chunks 00043 00044 char* headOfFreeList; 00045 //- headOfFreeList is the head of a stack that contains free 00046 //- elements that have not yet been constructed by the allocating 00047 //- object 00048 00049 std::size_t objectSize; 00050 //- objectSize is the size of the allocating object 00051 00052 int memAllocatnSize; 00053 //- memAllocatnSize is the size of the MemoryBlocks that will be 00054 //- allocated by the allocating object 00055 00056 MemoryManager* next; 00057 //- next object pointer is used to save memory managers on a 00058 //- static stack. 00059 00060 int staticManager; 00061 //- static flag to indicate that this memory manager is a static 00062 //- object. This parameter is used by the destructor when the 00063 //- program terminates and static object destructors are called. 00064 //- The destructor calls destroy_memory() which asserts if 00065 //- the used objects attached to this memory manager have not been 00066 //- deleted (generally true for static memory managers at program 00067 //- termination). Forcing an assert is desirable during normal 00068 //- program operation but not during a normal program termination 00069 //- sequence (Typing QUIT at the CUBIT command line prompt and 00070 //- observing an assert in MemoryManager is not comforting). 00071 00072 static MemoryManager* memoryManagerListHead; 00073 //- stack head containing all allocated memory managers. 00074 00075 MemoryManager(); 00076 MemoryManager(const MemoryManager&); 00077 //- do not allow default constructor (must assign objectSize) 00078 00079 static void process_mem_usage(unsigned long &vm_usage, unsigned long &resident_set); 00080 static void process_file_io(unsigned long &read, unsigned long &write ); 00081 00082 public: 00083 00084 MemoryManager(const char* name, std::size_t size, int mem_size = 0, 00085 int static_flag = 0); 00086 ~MemoryManager(); 00087 //- constructor / destructor 00088 00089 void set_memory_allocation_increment(int mem_size = 0); 00090 int get_memory_allocation_increment() const; 00091 //- set / get memory block allocation increment 00092 00093 std::size_t get_object_size() const; 00094 //- get object size 00095 00096 void destroy_memory(int static_flag = 0); 00097 //- destroy allocated memory 00098 00099 int compress_memory(); 00100 //- compresses memory for the object by removing unused MemoryBlocks 00101 00102 int get_allocated_objects(); 00103 //- returns number of object storage locations that have been 00104 //- allocated 00105 00106 int get_free_objects(); 00107 //- returns number of objects that are not in use but have been 00108 //- allocated 00109 00110 int get_used_objects(); 00111 //- returns number of objects that are currently in use from those 00112 //- that have been allocated 00113 00114 void* operator_new(std::size_t size); 00115 //- generic operator new 00116 00117 void operator_delete(void *deadObject, std::size_t size); 00118 //- generic operator delete 00119 00120 static void show_object_memory(const char* s); 00121 static void show_all_object_memory(); 00122 //- prints allocation information to the command line. The 00123 //- functions are declared static to enable their use throughout 00124 //- the code. The character string argument is interpreted as the 00125 //- class name provided as input when the memory manager was 00126 //- constructed. 00127 00128 static int compress_object_memory(const char* s); 00129 static int compress_all_object_memory(); 00130 //- recaptures unused memory blocks and returns them to free store 00131 //- and returns the amount of reclaimed memory. The functions are 00132 //- declared static to enable their use throughout the code. The 00133 //- character string argument is interpreted as the class name 00134 //- provided as input when the memory manager was constructed. 00135 }; 00136 00137 #ifdef USE_DYNAMIC_MEMORY_ALLOCATION 00138 00139 #define SetDynamicMemoryAllocation(memManager) \ 00140 \ 00141 void* operator new(std::size_t size) \ 00142 {return memManager.operator_new(size);} \ 00143 void operator delete(void *deadObject, std::size_t size) \ 00144 {memManager.operator_delete(deadObject, size);} \ 00145 /* overloaded new and delete operators */ \ 00146 \ 00147 00148 #else 00149 00150 #define SetDynamicMemoryAllocation(memManager) \ 00151 \ 00152 /* DO NOT overload new and delete operators ... use global allocation */ \ 00153 \ 00154 00155 #endif 00156 00157 #endif // MEMORY_MANAGER_HPP 00158