📄 mmgrt_00.cc
字号:
// file: $isip/class/system/MemoryManager/mmgrt_00.cc// version: $Id: mmgrt_00.cc,v 1.16 2002/08/23 19:32:45 jelinek Exp $//// isip include files//#include <SysString.h>#include <Console.h>// special isip include files//#define ISIP_INTERNAL_USE_ONLY#include "MemoryManagerTrack.h" // method: destructor//// arguments: none//// return: none//// this is the destructor for the MemoryManagerTrack class//MemoryManagerTrack::~MemoryManagerTrack() { // if the registered list is not empty (ie, releaseMgrs has not been // called), then clear and unregister this manager // if ((allocated_mgrs_d != (SysHeap*)NULL) && !allocated_mgrs_d->isEmpty()) { // release all internal data // clear(); unRegisterMgr(); } // exit gracefully //}// method: constructor//// arguments:// long elem_size: (input) size of each entry// long grow_size: (input) size of each new chunk of memory//// return: none//// this is one of the most commonly constructors for the// MemoryManagerTrack class.//MemoryManagerTrack::MemoryManagerTrack(long elem_size_a, long grow_size_a) { // initialize the data // free_d.next_d = (MemoryNode*)NULL; free_d.ptr_d = (void*)NULL; used_nodes_d.next_d = (MemoryNode*)NULL; used_nodes_d.ptr_d = (void*)NULL; size_d = -1; grow_size_d = DEF_GROW_SIZE; // check the arguments // if (grow_size_a < 1) { Error::handle(name(), L"constructor", Error::ARG, __FILE__, __LINE__); } if (elem_size_a < 1) { Error::handle(name(), L"constructor", Error::ARG, __FILE__, __LINE__); } // set the parameters from arguments // size_d = elem_size_a; grow_size_d = grow_size_a; // register the memory manager // registerMgr(); // exit gracefully //}// method: constructor//// arguments:// long elem_size: (input) size of each entry// const SysString& name: (input) name to associate with manager// long grow_size: (input) size of each new chunk of memory//// return: none//// this is oneof the most commonly used constructors for the// MemoryManagerTrack class//MemoryManagerTrack::MemoryManagerTrack(long elem_size_a, const SysString& name_a, long grow_size_a) { // initialize the data // free_d.next_d = (MemoryNode*)NULL; free_d.ptr_d = (void*)NULL; used_nodes_d.next_d = (MemoryNode*)NULL; used_nodes_d.ptr_d = (void*)NULL; size_d = -1; grow_size_d = DEF_GROW_SIZE; // check the arguments // if (grow_size_a < 1) { Error::handle(name(), L"constructor", Error::ARG, __FILE__, __LINE__); } if (elem_size_a < 1) { Error::handle(name(), L"constructor", Error::ARG, __FILE__, __LINE__); } // set the parameters from arguments // size_d = elem_size_a; grow_size_d = grow_size_a; // name_d.assign(name_a); // register the memory manager // registerMgr(); // exit gracefully //}// method: debug//// arguments:// const unichar* msg: (input) debugging message// // return: a boolean value indicating status//boolean MemoryManagerTrack::debug(const unichar* msg_a) const { // dump the data // SysString output; SysString value; // get the size // value.assign(size_d); output.debugStr(name(), msg_a, L"size_d", value); Console::put(output); // get the grow size // value.assign(grow_size_d); output.debugStr(name(), msg_a, L"grow_size_d", value); Console::put(output); // get the allocated heap // allocated_d.debug(L"allocated_d"); block_allocated_d.debug(L"block_allocated_d"); used_d.debug(L"used_d"); // get the number of free and used lists // long num_free; long num_used; countNodes(num_used, num_free); value.assign(num_used); output.debugStr(name(), msg_a, L"num_used_d", value); Console::put(output); value.assign(num_free); output.debugStr(name(), msg_a, L"num_free_d", value); Console::put(output); // exit gracefully // return true;}// method: get//// arguments: none//// return: pointer to new memory//// give the user a new chunk of memory//void* MemoryManagerTrack::get() { // without purify running, go from the pool of objects //#ifndef PURIFY // possibly allocate more nodes // if (free_d.next_d == (MemoryNode*)NULL) { if (!grow()) { Error::handle(name(), L"get", Error::NOMEM, __FILE__, __LINE__); return (void*)NULL; } } // declare local variables // MemoryNode* node; // pull a node from the top of the free list // node = free_d.next_d; free_d.next_d = free_d.next_d->next_d; // push the node to the top of the used list // node->next_d = used_nodes_d.next_d; used_nodes_d.next_d = node; // set the nodes // void* ptr = node->ptr_d; node->ptr_d = (MemoryNode*)NULL; // insert this node to used heap // used_d.insert(ptr); // return the new node's memory // return ptr; // purify is running, just call malloc //#else return isip_malloc(size_d);#endif}// method: release//// arguments:// void* ptr: (input/output) pointer to free//// return: a boolean value indicating status//// release a chunk of memory//boolean MemoryManagerTrack::release(void* ptr_a) { // without purify, go from the pool of objects //#ifndef PURIFY // check the arguments // if (ptr_a == (void*)NULL) { Error::handle(name(), L"release", Error::ARG, __FILE__, __LINE__); } // extract the pointer from used heap // if (!used_d.extract(ptr_a)) { display(ptr_a); return Error::handle(name(), L"release", ERR_NOTFND, __FILE__, __LINE__); } // declare local variables // MemoryNode* node; // grab first node from used_nodes_d // node = used_nodes_d.next_d; // error off if the node is NULL // if (node == (MemoryNode*)NULL) { return Error::handle(name(), L"release", Error::MEM, __FILE__, __LINE__); } // reset the nodes // used_nodes_d.next_d = used_nodes_d.next_d->next_d; node->ptr_d = ptr_a; // push the node onto the top of the free list // node->next_d = free_d.next_d; free_d.next_d = node; // exit gracefully // return true; // purify is running, just call free //#else return isip_free(ptr_a);#endif}// method: grow//// arguments: none//// return: a boolean value indicating status//// add to the array of free pointers//boolean MemoryManagerTrack::grow() { // free list must be out of nodes before this is called // if (free_d.next_d != (MemoryNode*)NULL) { return Error::handle(name(), L"grow", ERR_NOTEMP, __FILE__, __LINE__); } // allocate new MemoryNodes for the new data // void* buffer = isip_malloc(grow_size_d * size_d); // register the new pointer onto the allocated heap // allocated_d.insert(buffer); // create nodes to hold the pointers // MemoryNode* nodes = (MemoryNode*)isip_malloc(sizeof(MemoryNode) * grow_size_d); // insert nodes on allocated heap // allocated_d.insert(nodes); // configure the first node // free_d.next_d = &nodes[0]; MemoryNode* node = free_d.next_d; // initialize the newly created memory // for (long i = 0; i < grow_size_d - 1; i++) { node->ptr_d = (void*)((long)buffer + (long)(i * size_d)); node->next_d = &nodes[i + 1]; node = node->next_d; } // configure the last node // node->ptr_d = (void*)((long)buffer + (grow_size_d-1) * size_d); node->next_d = (MemoryNode*)NULL; // exit gracefully // return true;}// method: getBlock//// arguments:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -