⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmmemmappedfile.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  {
    m_dwSysReturnCode = ::GetLastError();
  }

  if ( 0L == m_dwSysReturnCode )
  {
    return( true );
  }
  else
  {
    return( false );
  }
}
/*	end of function "Open" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	GetBufferFromMemMappedFile

       DESCRIPTION:	returns a raw data buffer to the caller that is a mapping
                    of the mmf        

             INPUT: dwOffsetLow - the starting offset in the mmf that will be
                                  occupy the zero'th position in the buffer
                    dwNumToGet - the number of bytes from the mmf to get, ie
                                 the size of the buffer	
           RETURNS:	a pointer to the requested buffer or NULL if the
                    request fails.
*/
LPVOID VMMemMappedFile::GetBufferFromMemMappedFile( DWORD dwOffsetLow, 
                                                    DWORD dwNumToGet )
{
  void* pvAView;

  if ( Open() )
  {
    DWORD dwLowPage;

    dwLowPage  = min( m_dwSizeLow, dwOffsetLow ); 

    if ( !dwNumToGet )
    {
      dwNumToGet = m_dwSizeLow;
    }

    // come in here if we got the handle to the semaphores
    //
    pvAView = ::MapViewOfFile( m_hFileMapReader,
                               FILE_MAP_READ | FILE_MAP_WRITE, 
                               0, 
                               dwLowPage, 
                               dwNumToGet );

    if ( (BYTE*)pvAView == NULL )
    {
      m_dwSysReturnCode = GetLastError(); 
      return( NULL );
    }
    else
    {
      return( pvAView );
    }
  }
  else
  {
    return( NULL );
  }
}
/*	end of function "GetBufferFromMemMappedFile" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	LockBuffer

       DESCRIPTION:	Attempts to lock the buffer associated with the mmf. Does
                    so in the order that will prevent deadlocks. If the buffer
                    is already locked, simply returns true to prevent a single
                    user from deadlocking themselves. 

                    IMPORTANT NOTE: This class IS NOT THREADSAFE in this imp-
                    lementation. Need context management or a data monitor 
                    implemented for that.

             INPUT: void	
           RETURNS:	true if the buffer is/was locked. false otherwise
*/
bool  VMMemMappedFile::LockBuffer( void )
{   
  if ( m_iFlags & BUFFER_LOCKED )
  {
    return( true );
  }

  if ( LockForWrites() )
  {
    if ( LockForReads() )
    {
      m_iFlags |= BUFFER_LOCKED;
      return( true );
    }
    else
    {
      UnlockWrites();
      return( false );
    }
  }
  else
  {
    return( false );
  }
}
/*	end of function "LockBuffer" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	ReleaseBuffer

       DESCRIPTION:	Performs the unlocking operation on the buffer associated
                    with the mmf in the proper order to prevent deadlocks

             INPUT: void	
           RETURNS:	void
*/
void  VMMemMappedFile::ReleaseBuffer( void )
{ 
  UnlockReads(); 
  UnlockWrites(); 
  m_iFlags &= ~BUFFER_LOCKED;
}
/*	end of function "ReleaseBuffer" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	Lock

       DESCRIPTION:	given a semaphore name, attempts to get a lock on the 
                    semaphore. 

             INPUT:    hSem - the semaphore handle 
                    pchName - the name of the semaphore to attempt to lock	
           RETURNS:	true if successful. false otherwise. The contents of 
                    hSem are modified on a true return. 
*/
bool VMMemMappedFile::Lock( HANDLE hSem, char* pchName )
{
  m_dwSysReturnCode = 0L;

  hSem = ::OpenSemaphore( SEMAPHORE_ALL_ACCESS | SEMAPHORE_MODIFY_STATE | SYNCHRONIZE,
                          FALSE,
                          pchName );
  DWORD dwWaitError = ::WaitForSingleObject( hSem, 0 );
  if ( WAIT_FAILED == dwWaitError )
  {  
    m_dwSysReturnCode = ::GetLastError();
    return( false );
  }
  else
  if ( WAIT_TIMEOUT == dwWaitError )
  {  
    return( false );
  }
  else
  {  
    return( true );
  }
}
/*	end of function "Lock" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	Read

       DESCRIPTION:	Performs a read operation against the mmf

             INPUT: lpDataOut - a pointer to put the results of the read
                    dwNumToGet - the number of bytes to read from the mmf
                    dwOffsetLow - the offset into the mmf to start the read
                                  from	
           RETURNS:	-1 on errors. or returns the results of the implementation
                    of the virtual function PostProcessReadData
*/
unsigned long VMMemMappedFile::Read( LPVOID lpDataOut,
                                     DWORD dwNumToGet, 
                                     DWORD dwOffsetLow )
{
  unsigned long lReturn = 0;

  if ( Open() )
  {
    if ( LockBuffer() )
    {
      DWORD dwLowPage;
 
      dwLowPage  = min( m_dwSizeLow, dwOffsetLow ); 

      m_pvView = ::MapViewOfFile( m_hFileMapReader,
                                  FILE_MAP_READ | FILE_MAP_WRITE, 
                                  0,
                                  dwLowPage, 
                                  dwNumToGet );

      if ( (BYTE *) m_pvView == NULL )
      {
        m_dwSysReturnCode = GetLastError(); 
      }

      lReturn = PostProcessReadData( m_pvView, lpDataOut );

      ::UnmapViewOfFile( m_pvView );
      ::CloseHandle( m_hFileMapReader );

      ReleaseBuffer();
      return( lReturn );
    }
    else
    {
      return( (unsigned long) -1L );
    }
  }
  else
  {
    return( (unsigned long) -1L );
  }
}
/*	end of function "Read" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:	Write

       DESCRIPTION:	Performs a write operation to the memory mapped file

             INPUT: lpData - the data to write
                    dwInBufSize - the number of bytes to write
                    dwOffsetLow - the offset in the mmf to start the write at	
           RETURNS:	-1 on errors. or returns the results of the implementation
                    of the virtual function PreProcessWriteData
*/
unsigned long VMMemMappedFile::Write( LPVOID lpData, 
                                      DWORD  dwInBufSize, 
                                      DWORD  dwOffsetLow )
{
  unsigned long ulReturn = (unsigned long) -1L;

  if ( LockBuffer() )
  {
    DWORD dwLowPage;

    dwLowPage  = min( m_dwSizeLow, dwOffsetLow ); 

    m_pvView = MapViewOfFile( m_hFileMap,
                              FILE_MAP_READ | FILE_MAP_WRITE, 
                              0,  //dwHighPage, 
                              dwLowPage, 
                              dwInBufSize );

    if ( (BYTE*) m_pvView != NULL )
    {
      ulReturn = PreProcessWriteData( m_pvView, lpData, dwInBufSize );
    }
    else 
    {
      m_dwSysReturnCode = GetLastError();
    }

    ::UnmapViewOfFile( m_pvView );
    ReleaseBuffer();
    return( ulReturn );
  }
  else
  {
    return( (unsigned long) -1 );
  }
}
/*	end of function "Write" */
/*****************************************************************************/



/*****************************************************************************/
/* Check-in history */
/*
*$Log:   $
*/
/*****************************************************************************/




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -