📄 mmgrt_00.cc
字号:
// long size: (input) size of memory in bytes//// return: memory pointer//// allocate a chunk of contiguous memory//void* MemoryManagerTrack::getBlock(long size_a) { // check the arguments // if (size_a < 1) { Error::handle(name(), L"getBlock", Error::ARG, __FILE__, __LINE__); return (void*)NULL; } // get the pointer to a block of at least size_a bytes suitably // aligned // void* ptr = isip_malloc(size_a); // if it is NULL then return error // if (ptr == (void*)NULL) { Error::handle(name(), L"getBlock", Error::NOMEM, __FILE__, __LINE__); return (void*)NULL; } // pull a node from the top of the free heap // block_allocated_d.insert(ptr); // return the pointer // return ptr;}// method: releaseBlock//// arguments:// void* ptr: (input) buffer of memory to release//// return: a boolean value indicating status//// allocate a chunk of contiguous memory//boolean MemoryManagerTrack::releaseBlock(void* ptr_a) { // check the arguments // if (ptr_a == (void*)NULL) { return Error::handle(name(), L"releaseBlock", Error::ARG, __FILE__, __LINE__); } // extract the pointer from block allocated heap // if (!block_allocated_d.extract(ptr_a)) { display(ptr_a, true); return Error::handle(name(), L"releaseBlock", ERR_NOTFND, __FILE__, __LINE__); } // delete the memory // isip_free(ptr_a); // exit gracefully // return true;}// method: reallocateBlock//// arguments:// void*** ptr: (input) pointer to pointer to array// long& current_size: (input/output) size of the array// long size_increment: (input) increment with which to grow the array//// return: a boolean value indicating status//// increase the size of this array of pointers//boolean MemoryManagerTrack::reallocateBlock(void*** ptr_a, long& current_size_a, long grow_size_a) { // check for memory errors. either the pointer should be null and // the size 0, or the pointer should not be null and the size > 0 // if ((*ptr_a == (void*)NULL) ^ (current_size_a == 0)) { return Error::handle(name(), L"reallocateBlock", Error::MEM, __FILE__, __LINE__); } // if the pointer is not null, unregister it // if (*ptr_a != (void*)NULL) { if (!block_allocated_d.extract(*ptr_a)) { display(*ptr_a, true); return Error::handle(name(), L"reallocateBlock", ERR_NOTFND, __FILE__, __LINE__); } } // reallocate the memory // reallocate(ptr_a, current_size_a, grow_size_a); // register the pointer // if (!block_allocated_d.insert(*ptr_a)) { return Error::handle(name(), L"reallocateBlock", Error::ARG, __FILE__, __LINE__); } // exit gracefully // return true;}// method: countNodes//// arguments:// long& num_used: (output) number of nodes currently used// long& num_free: (output) number of nodes currently available//// return: a boolean value indicating status//// count the nodes in the two linked lists. possibly useful for debugging.//boolean MemoryManagerTrack::countNodes(long& num_used_a, long& num_free_a) const { // initialize the number of nodes currently used // num_used_a = 0; // loop over nodes and increment the number of nodes currently used // for (const MemoryNode* node = &used_nodes_d; node->next_d != (MemoryNode*)NULL; node = node->next_d) { num_used_a++; } // initialize the number of nodes currently available // num_free_a = 0; // loop over nodes and increment the number of nodes currently // available // for (const MemoryNode* node = &free_d; node->next_d != (MemoryNode*)NULL; node = node->next_d) { num_free_a++; } // exit gracefully // return true;}// method: display//// arguments:// void* ptr: (input) address to report// boolean block_mode: (input) are we in block mode?//// return: a boolean value indicating status//// display status before the error handler is called//boolean MemoryManagerTrack::display(void* ptr_a, boolean block_mode_a) { // initialize with the pointer value // SysString output; output.assign((void*)ptr_a); output.insert(L"\taddress = 0x", 0); output.concat(L", source = "); // either print the name or unknown // if (name_d.length() > 0) { output.concat(name_d); } else { output.concat(L"unknown"); } // either print block or single // if (block_mode_a) { output.concat(L" (block)\n"); } else { output.concat(L" (single)\n"); } // print the string // Console::put(output); // exit gracefully // return true;}// method: display//// arguments:// boolean block_mode: (input) are we in block mode?//// return: a boolean value indicating status//// display status before the error handler is called//boolean MemoryManagerTrack::display(boolean block_mode_a) { // initialize with a label // SysString output(L"source = "); // either print the name or unknown // if (name_d.length() > 0) { output.concat(name_d); } else { output.concat(L"unknown"); } // either print block or single // if (block_mode_a) { output.concat(L" (block)"); } else { output.concat(L" (single)"); } // print the string // Console::put(output); // exit gracefully // return true;}// method: clear//// arguments:// Integral::CMODE cmode: (input) clear mode (ignored)//// return: logical error status//// before clearing and releasing memory, this method this method// checks the internal storage and ensures that no unreleased memory// exists. if any violation is found it is reported. Note that the// cmode flag is ignored.//boolean MemoryManagerTrack::clear(Integral::CMODE cmode_a) { if (debug_level_d > Integral::BRIEF) { SysString var(L"class"); SysString output; //output.debugStr(name(), L"clear", var, name_d); Console::put(output); } if ((!used_d.isEmpty()) || (!block_allocated_d.isEmpty())) { // only error if we are not already exiting // if (!Error::isExiting()) { // print out message that memory is still in use // static boolean shown_header = false; if (!shown_header) { shown_header = true; Console::put(L"\nError: Dynamic memory not deleted\n"); } // declare local variable // void* node; // check if used heap is empty or not // while (!used_d.isEmpty()) { if (!used_d.extractMax(node)) { display(); Error::handle(name(), L"clear", ERR_NOTFND, __FILE__, __LINE__); } display(node); } // declare local variable // void* ptr; // check if block allocated heap is empty or not // while (!block_allocated_d.isEmpty()) { if (!block_allocated_d.extractMax(ptr)) { display(true); Error::handle(name(), L"clear", ERR_NOTFND, __FILE__, __LINE__); } display(ptr, true); } } } // release memory // allocated_d.clear(Integral::FREE); used_d.clear(Integral::FREE); block_allocated_d.clear(Integral::FREE); // exit gracefully // return true;} // method: releaseMgrs//// arguments: none//// return: logical error status//// this method checks cleans up all registered MemoryManager objects// and checks for unreleased memory. it is called in the exit routines// of Integral and Exit so that errors can be reported before the// error reporting mechanisms are deleted.//boolean MemoryManagerTrack::releaseMgrs() { // remove each registered manager and call its clear method // void* ptr = (void*)NULL; while ((allocated_mgrs_d != (SysHeap*)NULL) && (!allocated_mgrs_d->isEmpty())) { allocated_mgrs_d->extractMax(ptr); ((MemoryManagerTrack*)ptr)->clear(); } // exit gracefully // return true;}//-----------------------------------------------------------------------------//// we define non-integral constants in the default constructor// //-----------------------------------------------------------------------------// constants: class name//const SysString MemoryManagerTrack::CLASS_NAME(L"MemoryManagerTrack");// static instantiations: SysHeap//SysHeap* MemoryManagerTrack::allocated_mgrs_d = (SysHeap*)NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -