/*****************************************************************************/
/* SOURCE FILE */
/*****************************************************************************/
/*
$Archive: $
$Revision: $
$Date: $
$Author: $
Description:
TOOL And XML FORMS License
==========================
Except where otherwise noted, all of the documentation
and software included in the TOOL package is
copyrighted by Michael Swartzendruber.
Copyright (C) 2005 Michael John Swartzendruber.
All rights reserved.
Access to this code, whether intentional or accidental,
does NOT IMPLY any transfer of rights.
This software is provided "as-is," without any express
or implied warranty. In no event shall the author be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for
any purpose, including commercial applications, and to
alter and redistribute it, provided that the following
conditions are met:
1. All redistributions of source code files must retain
all copyright notices that are currently in place,
and this list of conditions without modification.
2. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
3. If you use this software in another product, an acknowledgment
in the product documentation would be appreciated but is
not required.
4. Modified versions in source or binary form must be plainly
marked as such, and must not be misrepresented as being
the original software.
*/
static char OBJECT_ID[] = "$Revision: 1 $ : $Date: 9/12/97 3:30p $";
/*****************************************************************************/
#include "../../../stdafx.h"
#include <assert.h>
#include <string.h>
#include "VMMemMappedFile.h"
/*****************************************************************************/
/*
FUNCTION NAME: VMMemMappedFile
DESCRIPTION: ctor. Sets up all variables.
INPUT: pchName - The name of the MMF to attach to this class
bCreate - Create the MMF during construction
dwSizeLow - the size of the mmf to create
RETURNS: void
*/
VMMemMappedFile::VMMemMappedFile( char* pchName, bool bCreate, DWORD dwSizeLow )
{
assert ( NULL != pchName );
strcpy( m_achName, pchName );
char achTemp[ 32 ];
strcpy( achTemp, m_achName );
strcat( achTemp, "SemForWrite" );
strcpy( m_achSemNameWrite, achTemp );
strcpy( achTemp, m_achName );
strcat( achTemp, "SemForRead" );
strcpy( m_achSemNameRead, achTemp );
assert( 0 < dwSizeLow );
m_dwSizeHigh = 0;
m_dwSizeLow = dwSizeLow;
m_hFileMap = NULL;
m_hFileMapReader = NULL;
m_bICreatedMapping = false;
m_dwSysReturnCode = 0L;
m_pvView = NULL;
m_hWriteSemaphore = NULL;
m_hReadSemaphore = NULL;
m_iFlags = 0;
if ( true == bCreate )
{
Create();
}
else
{
Open();
}
}
/* end of function "VMMemMappedFile" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: ~VMMemMappedFile
DESCRIPTION: dtor. closes all object handles
INPUT: void
RETURNS: void
*/
VMMemMappedFile::~VMMemMappedFile()
{
if ( NULL != m_hFileMap )
{
::CloseHandle( m_hFileMap );
}
if ( NULL != m_hFileMapReader )
{
::CloseHandle( m_hFileMapReader );
}
if ( NULL != m_hReadSemaphore )
{
::CloseHandle( m_hReadSemaphore );
}
if ( NULL != m_hWriteSemaphore )
{
::CloseHandle( m_hWriteSemaphore );
}
}
/* end of function "~VMMemMappedFile" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetLastErrorText
DESCRIPTION: support function that allows the caller to get a text
message based on the last error encountered in the class
INPUT: pchOutBuffer - a buffer to put the message into
iBufferSize - the size of the output buffer
RETURNS:
*/
void VMMemMappedFile::GetLastErrorText( char* pchOutBuffer, int iBufferSize )
{
void* pvBuffer = NULL;
if ( 0 != m_dwSysReturnCode )
{
::FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
m_dwSysReturnCode,
MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
(LPTSTR) &pvBuffer,
0,
NULL );
int iMsgLen;
if ( 0 != ( iMsgLen = strlen( (const char*)pvBuffer ) ) )
{
if ( iMsgLen < iBufferSize )
{
strcpy( pchOutBuffer, (char*)pvBuffer );
}
else
{
strncpy( pchOutBuffer, (char*)pvBuffer, iBufferSize );
}
}
::LocalFree( pvBuffer );
}
else
{
strcpy( pchOutBuffer, "No Error Text" );
}
}
/* end of function "GetLastErrorText" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: Create
DESCRIPTION: creates the mmf and all of the access control objects (single
owner semaphores) for the MMF.
INPUT: void
RETURNS: true if no errors, false otherwise
*/
bool VMMemMappedFile::Create( void )
{
m_dwSysReturnCode = 0L;
SECURITY_ATTRIBUTES xsa;
SECURITY_DESCRIPTOR xsd;
::InitializeSecurityDescriptor( &xsd, SECURITY_DESCRIPTOR_REVISION );
::SetSecurityDescriptorDacl( &xsd,
TRUE,
NULL,
FALSE );
xsa.nLength = sizeof( SECURITY_ATTRIBUTES );
xsa.bInheritHandle = true;
xsa.lpSecurityDescriptor = &xsd;
m_hFileMap = ::CreateFileMapping( (HANDLE) 0xFFFFFFFF,
&xsa,
PAGE_READWRITE,
m_dwSizeHigh,
m_dwSizeLow,
m_achName );
if ( m_hFileMap != NULL )
{
if ( ERROR_ALREADY_EXISTS == ( m_dwSysReturnCode = ::GetLastError() ) )
{
// this is a non critical error
//
::SetLastError( 0 );
m_dwSysReturnCode = 0L;
m_bICreatedMapping = false;
}
else
{
m_bICreatedMapping = true;
}
m_hReadSemaphore = ::CreateSemaphore ( &xsa,
(LONG) 1,
(LONG) 2,
m_achSemNameRead );
DWORD dwError = ::GetLastError();
if ( ERROR_ALREADY_EXISTS == dwError )
{
::SetLastError( 0 );
m_dwSysReturnCode = 0L;
}
else
{
m_dwSysReturnCode = dwError;
}
m_hWriteSemaphore = ::CreateSemaphore ( &xsa,
(LONG) 1,
(LONG) 2,
m_achSemNameWrite );
dwError = ::GetLastError();
if ( ERROR_ALREADY_EXISTS == dwError )
{
::SetLastError( 0 );
m_dwSysReturnCode = 0L;
}
else
{
m_dwSysReturnCode = dwError;
}
}
else
{
m_dwSysReturnCode = ::GetLastError();
m_bICreatedMapping = false;
}
if ( 0 == m_dwSysReturnCode )
{
return( true );
}
else
{
return( false );
}
}
/* end of function "Create" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: Open
DESCRIPTION: Opens the file mapping belonging to this class
INPUT: void
RETURNS: true if the operation succeeded, false otherwise
*/
bool VMMemMappedFile::Open()
{
m_dwSysReturnCode = 0L;
m_hFileMapReader = ::OpenFileMapping( FILE_MAP_READ | FILE_MAP_WRITE,
FALSE,
m_achName );
if ( NULL == m_hFileMapReader )
{
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: $
*/
/*****************************************************************************/