// =====================================================================
//  $Id: TObjectSharedMemory.cc,v 1.4 2004/03/07 10:30:32 goiwai Exp $
//  $Name: CLDAQ-1-14-03 $
//  $Log: TObjectSharedMemory.cc,v $
//  Revision 1.4  2004/03/07 10:30:32  goiwai
//  ROOTに組みこむためのおまじないマクロを埋めこみました。
//  全てにおいて完全に動作するわけではありません。
//
//  Revision 1.3  2003/10/06 17:02:42  goiwai
//  *** empty log message ***
//
//  Revision 1.2  2003/07/30 16:19:31  goiwai
//  ファイルにコミットログをつけることにしました.
//
// =====================================================================
#include "TObjectSharedMemory.hh"

TObjectSharedMemory* TObjectSharedMemory::theObjectSharedMemory = 0;

TObjectSharedMemory::TObjectSharedMemory( Tint memsize, Tint maxevt )
  : theSharedMemoryID( -1 ), theMemorySize( memsize ),
    theMaxEvent( maxevt ), theCurrentPositionOfMemorySpace( 0 ),
    theReadable( 0 ), theBaseAddress( 0 ), theAddress( 0 )
{
  Tstring head = "TObjectSharedMemory::TObjectSharedMemory: ";

  theSharedMemoryID =
    shmget( ftok( getenv( "HOME" ), Tkey ), theMemorySize, 0666 | IPC_CREAT );
  if ( theSharedMemoryID == -1 ) {
    Tcerr << head << "failed getting shared memory ID." << Tendl;
    exit( -errno );
  }
  Tvoid* voidbuf = Attach();
  if ( voidbuf == (Tvoid*)-1 ) {
    Tcerr << head << "failed attach." << Tendl;
    exit( -errno );
  }

  theReadable = (Tbool*)voidbuf;
  SetReadable( Tfalse );

  ( (Tbool*)voidbuf ) ++;
  theBaseAddress = voidbuf;
  theAddress = theBaseAddress;
  theObjectSharedMemory = this;
}

TObjectSharedMemory::~TObjectSharedMemory()
// When output object destruct, invoked Dettach(), Remove() functions.
{
  theObjectSharedMemory = 0;
}

#ifdef __CLDAQ_ROOT_DLL
    ClassImp(TObjectSharedMemory)
#endif
