Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

pana_memory_manager.h

00001 /* BEGIN_COPYRIGHT                                                        */
00002 /*                                                                        */
00003 /* Open Diameter: Open-source software for the Diameter and               */
00004 /*                Diameter related protocols                              */
00005 /*                                                                        */
00006 /* Copyright (C) 2002-2004 Open Diameter Project                          */
00007 /*                                                                        */
00008 /* This library is free software; you can redistribute it and/or modify   */
00009 /* it under the terms of the GNU Lesser General Public License as         */
00010 /* published by the Free Software Foundation; either version 2.1 of the   */
00011 /* License, or (at your option) any later version.                        */
00012 /*                                                                        */
00013 /* This library is distributed in the hope that it will be useful,        */
00014 /* but WITHOUT ANY WARRANTY; without even the implied warranty of         */
00015 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      */
00016 /* Lesser General Public License for more details.                        */
00017 /*                                                                        */
00018 /* You should have received a copy of the GNU Lesser General Public       */
00019 /* License along with this library; if not, write to the Free Software    */
00020 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307    */
00021 /* USA.                                                                   */
00022 /*                                                                        */
00023 /* In addition, when you copy and redistribute some or the entire part of */
00024 /* the source code of this software with or without modification, you     */
00025 /* MUST include this copyright notice in each copy.                       */
00026 /*                                                                        */
00027 /* If you make any changes that are appeared to be useful, please send    */
00028 /* sources that include the changed part to                               */
00029 /* diameter-developers@lists.sourceforge.net so that we can reflect your  */
00030 /* changes to one unified version of this software.                       */
00031 /*                                                                        */
00032 /* END_COPYRIGHT                                                          */
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                         // reset read/write ptr
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 /* __PANA_MEMORY_MANAGER_H__ */

Generated on Fri Jun 25 19:18:29 2004 for PANA by doxygen 1.3.5