00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef __PANA_MEMORY_MANAGER_H__
00035 #define __PANA_MEMORY_MANAGER_H__
00036
00037 #include "ace/Atomic_Op.h"
00038 #include "ace/Malloc_T.h"
00039 #include "ace/Synch.h"
00040 #include "ace/Message_Block.h"
00041 #include "pana_exports.h"
00042 #include "pana_defs.h"
00043
00044 class PANA_EXPORT PANA_MessageBuffer : public ACE_Message_Block
00045 {
00046 public:
00047 PANA_MessageBuffer() :
00048 ACE_Message_Block(PANA_MAX_MESSAGE_SIZE,
00049 MB_DATA, 0, 0,
00050 AAAMemoryManager_S::instance()),
00051 m_RefCount(0) {
00052 }
00053
00054 friend class PANA_MessagePoolManager;
00055
00056 protected:
00057 ACE_Atomic_Op<ACE_Thread_Mutex, long> m_RefCount;
00058 };
00059
00060 class PANA_EXPORT PANA_MessagePoolManager
00061 {
00062 public:
00063 PANA_MessagePoolManager(int n_blocks = PANA_MIN_MESSAGE_COUNT) :
00064 m_Pool(NULL), m_NumBlocks(n_blocks) {
00065 ACE_NEW_NORETURN(m_Pool, PANA_MessageBuffer[m_NumBlocks]);
00066 }
00067 PANA_MessageBuffer *malloc() {
00068 if (m_Pool) {
00069 PANA_MessageBuffer *buffer;
00070 for (int i = 0; i < m_NumBlocks; i++ ) {
00071 buffer = &(m_Pool[i]);
00072 if (buffer->m_RefCount.value() == 0) {
00073 buffer->m_RefCount ++;
00074
00075 buffer->rd_ptr(buffer->base());
00076 buffer->wr_ptr(buffer->base());
00077 return (buffer);
00078 }
00079 }
00080 }
00081 else {
00082 throw (PANA_Exception(PANA_Exception::NO_MEMORY,
00083 "Message pool not allocated"));
00084 }
00085 return (NULL);
00086 }
00087 void free(const PANA_MessageBuffer *buffer) {
00088 const_cast<PANA_MessageBuffer*>(buffer)->size(PANA_MAX_MESSAGE_SIZE);
00089 ((PANA_MessageBuffer*)buffer)->m_RefCount --;
00090 }
00091
00092 private:
00093 PANA_MessageBuffer *m_Pool;
00094 int m_NumBlocks;
00095 };
00096
00097 typedef ACE_Singleton<PANA_MessagePoolManager,
00098 ACE_Recursive_Thread_Mutex> PANA_MessagePoolManager_S;
00099 #define PANA_MESSAGE_POOL() PANA_MessagePoolManager_S::instance()
00100
00101 #endif