/* 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_B_ALLOCATOR_HPP
#define NMM_B_ALLOCATOR_HPP NMM_B_ALLOCATOR_HPP

#include "nmm/nmm_prefix.hpp"

#include "BufferManager.hpp"

#include "nmm/utils/NamedObject.hpp"

#include <map>

#include "visibility.hpp"

namespace NMM {
  
  /**
   * This is a simple BufferManager implementation using STL maps.
   */
  class DLLEXPORT B_Allocator : public BufferManager , public NamedObject {

  public:
    
    /** Constructor.
     * 
     * @param size the size of the buffers belonging to this buffer manager
     * @param start_num the number of pre-allocated buffers
     * @param max_num the maximum number of buffer managed by this buffer manager
     * @param name NamedObject name
     */
    B_Allocator(size_t size = 0, size_t start_num = 0, size_t max_num = 0, const char* name = "B_Allocator");

    /** Requests buffer.
     * 
     * @param size the size of the requested buffer
     * @return pointer to the newly created buffer
     */
     virtual Buffer* requestBuffer (const size_t size) const;
    
     /** Releases buffer.
     * 
     * @param buffer pointer to buffer to release
     */
     virtual void releaseBuffer (Buffer* buffer) const;

    /** Sets the maximum number of buffers.
     *
     * Sets the maximum number of buffers which can be allocated by this manager.
     * 
     * @param max maximum number of buffers
     * @return SUCCESS
     */
    Result setMaxBufferCount(const size_t max);

    /** Gets the maximum number of buffers.
     *
     * Gets the maximum number of buffers which can be allocated by this manager.
     *
     * @return maximum number of buffers 
     */
    const size_t getMaxBufferCount() const;

  protected:
    /** Destructor
     */
    virtual ~B_Allocator();

    /** Cleanup and delete itself.
     */
    virtual void erase() const;

  private:
    typedef multimap<size_t, Buffer*> bufset_t;

    mutable bufset_t bufs;          ///< contains preallocated or unused buffer
    mutable int num_of_buffers;     ///<  how many buffers are given away 
    mutable bool erase_flag;        ///< true if erase has been called, false otherwise

    ThreadMutex mutex;   ///< mutex to protect member variables

    size_t max_buffer_count;  ///< defines maximum of buffers stored in bufs

  };

};

#include "nmm/nmm_suffix.hpp"

#endif
