Česky
Kamil Dudka

Share Library (C++)

File detail

Name:DownloadSegmentHeader.h [Download]
Location: sharelib > src > sharelib
Size:2.9 KB
Last modification:2007-08-27 01:16

Source code

#ifndef SEGMENT_HEADER_H
#define SEGMENT_HEADER_H
 
/**
 * @file SegmentHeader.h
 * @brief Object placed at begin of each shared segment
 * @author Kamil Dudka, xdudka00@gmail.com
 * @date 2007-05-15
 * @ingroup backend
 */
 
#include "DefaultAllocator.h"
#include "DefaultRefCounter.h"
#include "MutexLock.h"
 
namespace Share {
  typedef DefaultAllocator TSegmentAllocator;      ///< Change this typedef to use another allocator
  typedef DefaultRefCounter TSegmentRefCounter;    ///< Change this typedef to use another reference counter
 
  /**
   * It takes care about allocating blocks inside shared segment and
   * counting references to shared segment.
   * @brief Object placed at begin of each shared segment
   * @ingroup backend
   * @note This class use Mutex to acces its members.
   */
  class SegmentHeader {
    public:
 
      /**
       * @param name Name of shared segment.
       * @param dataSize Desired size of memory for data inside shared segment.
       * @note Usable memory inside shared segment could be smaller due to free space fragmentation.
       */
      SHARE_EXPORT SegmentHeader (const char *name, size_t dataSize);
 
      /**
       * @copydoc SegmentHeader()
       * @return Return computed size of shred segment to allocate.
       */
      SHARE_EXPORT static size_t sizeNeeded (const char *name, size_t dataSize);
 
      /**
       * @copydoc DefaultAllocator::alloc(size_t)
       */
      SHARE_EXPORT void *alloc (size_t size) throw (ShareException);
 
      /**
       * @copydoc DefaultAllocator::free(void*)
       */
      SHARE_EXPORT void free (void *addr) throw ();
 
      /**
       * @copydoc DefaultAllocator::size()
       */
      SHARE_EXPORT size_t size();
 
      /**
       * @copydoc DefaultAllocator::available()
       */
      SHARE_EXPORT size_t available();
 
      /**
       * @copydoc DefaultRefCounter::notifyAttach()
       */
      SHARE_EXPORT void notifyAttach();
 
      /**
       * @copydoc DefaultRefCounter::notifyDetach()
       */
      SHARE_EXPORT void notifyDetach();
 
      /**
       * @copydoc DefaultRefCounter::refCount()
       */
      SHARE_EXPORT int refCount();
 
      /**
       * @copydoc DefaultRefCounter::unlink()
       */
      SHARE_EXPORT void unlink();
 
      /**
       * @copydoc DefaultRefCounter::isUnlinked()
       */
      SHARE_EXPORT bool isUnlinked();
 
      /**
       * @copydoc DefaultRefCounter::shouldBeDestroyed()
       */
      SHARE_EXPORT bool shouldBeDestroyed();
 
      /**
       * @brief Pointer to zero-ended string
       */
      typedef RelocPtr<char> TStringZ;
 
    protected:
      /**
       * @brief allocator
       */
      TSegmentAllocator allocator_;
 
      /**
       * @brief reference counter
       */
      TSegmentRefCounter refCounter_;
 
      /**
       * @brief Pointer to mutex name (stored inside shared segment)
       */
      TStringZ mutexName_;
 
      /**
       * @brief Mutex used to access members
       */
      Mutex mutex_;
  };
}
 
#endif // SEGMENT_HEADER_H