/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef NMM_SHM_BUFFER_MANAGER_HPP
#define NMM_SHM_BUFFER_MANAGER_HPP NMM_SHM_BUFFER_MANAGER_HPP

#include "nmm/nmm_prefix.hpp"

#include "nmm/comm/messaging/BufferManager.hpp"

#include "nmm/utils/NamedObject.hpp"

#include <list>

namespace NMM {
  
  /** Shared memory buffer manager.
   *
   * This is a %BufferManager implementation to handle shared memory It
   * does not allocate any memory, but manages memory segments that
   * could come from XShm shared memory areas for example.
   *
   * @see XShmImg XShmManager XDisplayNode
   */
  class ShmBufferManager 
    : public BufferManager, 
      public NamedObject
  {
  private:
    typedef list<Buffer*> bufferlist_t;
    
    //! list of all Shared Memory Buffers
    mutable bufferlist_t  buffers;
    //! list of the free Shared Memory Buffers
    mutable bufferlist_t  freebuffers;
    //! number of Buffers currently given out
    mutable int           num_of_buffers;
    //! flag to indicate, that the BufferManager should delete itself
    //  when all running Buffers have returned
    mutable bool          erase_flag;
    //! a pointer to the Shared Memory segment
    mutable char*         shm_segment;
    //! the size to the Shared Memory segment
    mutable size_t        shm_size;

    //! A Mutex to protect internal variables from concurent access
    ThreadMutex      mutex;

    //! The size of our Buffers
    mutable unsigned int buffer_size;

    //! A fallback BufferManager for the case, that no more Shared Memory Buffers are available
    mutable BufferManager* fallback_bm;
    
  public:
    /**
     * Constructor.
     * \param name The name of this object.
     */
    ShmBufferManager(const string & name = "ShmBufferManager");

    /**
     * This method sets the shared memory segmentof this BufferManager
     * \param memptr A pointer to begin of shared memory segment
     * \param size   The size of shared memory segment
     * \return true iff the memory could be added
     * note: once a memory segment is set, it cannot be unset or replaced
     *       by an other.
     */
    bool setSharedMemory(char* memptr, size_t size);

    /**
     * Implementation of pure virtual method
     * BufferManager::requestBuffer(const size_t)
     * \param size buffer size that is requested
     * \returns A Buffer-Ptr if a Buffer is available, otherwise NULL.
     * A Buffer with a larger size cannot be available.
     * If no shared memory pages were added before, no Buffer is
     * available.
     * If all Buffers are given out, this call sleeps until a Buffer
     * is available.
     * If the BufferManager was requested to erase itself, no Buffers
     * are available.
     */
    Buffer* requestBuffer(const size_t size) const;

    /**
     * Implementation of pure virtual method
     * BufferManager::releaseBuffer(Buffer*)
     * releases a buffer, here it means: make the buffer available again by
     * putting it into the freebuffers list
     * \param buffer The buffer to be released
     * If the last Buffer is released and the BufferManager was
     * requested to erase itself, it does it here!
     */
    void releaseBuffer(Buffer* buffer) const;

  protected:

    /**
     * implementation of pure virtual method BufferManager::erase().
     * Tells the BufferManager to delete itself if all given out
     * Buffers have returned.
     */
    void erase() const;

    /**
     * Destructor.
     * The destructor is protected, because a BufferManager deletes themself if the refernce counter is 0.
     */
    ~ShmBufferManager();
  };

}

#include "nmm/nmm_suffix.hpp"

#endif
