資源分配管理器
STL容器
我們將使用下面的容器來(lái)實(shí)現(xiàn)本文的例子:
queue |
隊(duì)列容器支持添加一個(gè)元素,并且從中刪除一個(gè)元素,也可以變成一個(gè)雙端隊(duì)列 |
priority_queue |
向隊(duì)尾添加一個(gè)新元素,如果該優(yōu)先權(quán)大于前面的元素,將放在前面,元素的優(yōu)先權(quán)由用戶指定的函數(shù)傳入。 |
stack |
有后進(jìn)先出的特點(diǎn),可以選擇選擇vetctor或其他線性列表放入容器。 |
1.消息隊(duì)列
消息隊(duì)列是項(xiàng)目中經(jīng)常需要使用的模式,下面使用STL的queue容器實(shí)現(xiàn):
#include // STL header file for queue #include using namespace std; // Specify that we are using the std namespace class Message; class Message_Queue { typedef queue > MsgQueType; MsgQueType m_msgQueue; public: void Add(Message *pMsg) { // Insert the element at the end of the queue m_msgQueue.push(pMsg); } Message *Remove() { Message *pMsg = NULL; // Check if the message queue is not empty if (!m_msgQueue.empty()) { // Queue is not empty so get a pointer to the // first message in the queue pMsg = m_msgQueue.front(); // Now remove the pointer from the message queue m_msgQueue.pop(); } return pMsg; } int GetLength() const { return m_msgQueue.size(); } };
2.優(yōu)先級(jí)消息隊(duì)列
上面的消息隊(duì)列類只支持在隊(duì)列的尾部添加一個(gè)元素,在許多的應(yīng)用程序中,需要根據(jù)消息的優(yōu)先級(jí)將消息添加到隊(duì)列中,當(dāng)一個(gè)高優(yōu)先級(jí)的消息來(lái)了,需要將該消息添加到所有比它優(yōu)先級(jí)低的消息前面,下面將使用priority_queue來(lái)實(shí)現(xiàn)優(yōu)先級(jí)消息隊(duì)列類。
函數(shù)對(duì)象(Functors)
優(yōu)先級(jí)消息隊(duì)列的實(shí)現(xiàn)和上面消息隊(duì)列實(shí)現(xiàn)相似,唯一不同的是這里使用了函數(shù)對(duì)象來(lái)決定優(yōu)先權(quán)CompareMessages,該結(jié)構(gòu)重載了操作符"(,)",該機(jī)制可以實(shí)現(xiàn)可以將一個(gè)函數(shù)作為一個(gè)參數(shù),和函數(shù)指針對(duì)比有如下好處:
1.函數(shù)對(duì)象消息更高,可以是內(nèi)聯(lián)函數(shù),函數(shù)指針總是有函數(shù)調(diào)用的開銷。
2.函數(shù)對(duì)象提供了一個(gè)安全的實(shí)現(xiàn)方法,這樣的實(shí)現(xiàn)不會(huì)出現(xiàn)空指針訪問。
#include // STL header file for queue #include using namespace std; // Specify that we are using the std namespace class Message; class Priority_Message_Queue { struct Entry { Message *pMsg; int priority; }; struct Compare_Messages { bool operator () (const Entry& left , const Entry& right) { return (left.priority < right.priority); } }; typedef priority_queue, Compare_Messages > Message_Queue_Type; Message_Queue_Type m_message_Queue; public: void Add(Message *pMsg, int priority) { // Make an entry Entry entry; entry.pMsg = pMsg; entry.priority = priority; // Insert the element according to its priority m_message_Queue.push(entry); } Message *Remove() { Message *pMsg = NULL; // Check if the message queue is not empty if (!m_message_Queue.empty()) { // Queue is not empty so get a pointer to the // first message in the queue pMsg = (m_message_Queue.top()).pMsg; // Now remove the pointer from the message queue m_message_Queue.pop(); } return (pMsg); } size_t Get_Length() const { return m_message_Queue.size(); } };
3.資源分配管理器
這里使用queue 和 stack兩個(gè)容器來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的資源分配器,用該容器來(lái)保存空閑的資源列表。
該資源分配器支持如下接口:
1.Construction:當(dāng)這個(gè)資源分配器構(gòu)造出來(lái),需要給定空閑的資源列表,這些資源將添加到這個(gè)空閑資源列表。
2.Allocate:當(dāng)需要一個(gè)資源時(shí),需要從空閑資源隊(duì)列中移除一個(gè)資源,并返回給調(diào)用者。
3.Free:當(dāng)一個(gè)資源被釋放,需要將該資源加入空閑資源列表。
4.GetFreeResourceCount:返回當(dāng)前可用的資源數(shù)目。
Coldest First(使用queue)
#include // STL header file for queue #include using namespace std; // Specify that we are using the std namespace class Resource; class Cold_Resource_Allocator { typedef queue > Free_Queue_Type; Free_Queue_Type m_free_Resource_Queue; public: Cold_Resource_Allocator(int resource_Count, Resource *resource_Array[]) { for (int i = 0; i < resource_Count; i++) { m_free_Resource_Queue.push(resource_Array[i]); } } Resource * Allocate() { Resource *pResource = NULL; // Check if any free resources are available. if (!m_free_Resource_Queue.empty()) { // Queue is not empty so get a pointer to the // first resource in the queue pResource = m_free_Resource_Queue.front(); // Now remove the pointer from the free resource queue m_free_Resource_Queue.pop(); } return pResource; } void Free(Resource *pResource) { // Insert the resource at the end of the free queue m_free_Resource_Queue.push(pResource); } size_t GetFreeResourceCount() { return m_free_Resource_Queue.size(); } };
Hottest First(使用statck)
#include // STL header file for stack #include using namespace std; // Specify that we are using the std namespace class Resource; class Hot_Resource_Allocator { typedef stack > Free_Stack_Type; Free_Stack_Type m_free_Resource_Stack; public: Hot_Resource_Allocator(int resource_Count, Resource *resource_Array[]) { for (int i = 0; i < resource_Count; i++) { m_free_Resource_Stack.push(resource_Array[i]); } } Resource * Allocate() { Resource *pResource = NULL; // Check if any free resources are available. if (!m_free_Resource_Stack.empty()) { // Queue is not empty so get a pointer to the // first resource in the stack pResource = m_free_Resource_Stack.top(); // Now remove the pointer from the free resource stack m_free_Resource_Stack.pop(); } return pResource; } void Free(Resource *pResource) { // Insert the resource at the end of the free stack m_free_Resource_Stack.push(pResource); } size_t GetFreeResourceCount() { return m_free_Resource_Stack.size(); } };