CMemorySegment.cpp 6.72 KB
//===========================================================
//
//  Class Name:	CMemorySegment
//
//===========================================================
//                   SOURCE CODE NOTES:
//==========================================================
//
//  No notes.
//
//
//
//===========================================================


//===========================================================
//                SOURCE INCLUDE FILE SECTION
//===========================================================
#include "CMemorySegment.h"
#include <memory.h>
#include <stdio.h>


//===========================================================
//                       TYPES
//===========================================================


//===========================================================
//	Public Functions
//===========================================================

bool CMemorySegment::PrintWorkingDataBytes( CMessageIO& message_interface )
{
	
	message_interface.PrintStringStdioLF( "----------- Start of Working Bytes Hex Dump -----------");
	message_interface.PrintStringStdioLF("");
	const UnSignedLong row_size = 16;
	
	for( UnSignedLong i=0; i<m_working_byte_size; i++ )
	{
		if( i % row_size == 0 )
		{
			char buffer[50];
			sprintf(&(buffer[0]),"[%10lX - %10lX] :", (unsigned long)(m_working_start_address+i), (unsigned long)(m_working_start_address+i+row_size) );
			message_interface.PrintStringStdioLF(&(buffer[0]));
		}
		else
		if( i % 4 == 0 )
		{
			message_interface.PrintStringStdio(", ");
		}

		volatile unsigned char *char_ptr = ( volatile unsigned char *)(m_working_start_address+i);
		unsigned char tmp = *char_ptr;
		char buffer[20];
		sprintf(&(buffer[0])," %2X", (unsigned int)tmp);
		message_interface.PrintStringStdio( &(buffer[0]) );
	}

	message_interface.PrintStringStdioLF("");
	message_interface.PrintStringStdioLF( "----------- End of Working Bytes Hex Dump -----------");

	return( true );
}

//===========================================================

bool CMemorySegment::SetWorkingAddressForAccessOnly( volatile AddressType access_only_address )
{
	if( m_working_start_address != 0 )
	{
		return( false ); //This segment has already been created.
	}

	m_working_start_address = ( volatile unsigned char *) access_only_address;

	return( true );
}

bool CMemorySegment::Create( const UnSignedLong working_data_bytes,  const UnSignedLong alignment_boundary,  const unsigned char init_value ) 
{
	m_working_byte_size = working_data_bytes;
	m_alignment_boundary = alignment_boundary;
	m_true_byte_size = working_data_bytes + (alignment_boundary*2);

	m_true_heap_start_address = (volatile unsigned char*) AllocMemory( m_true_byte_size );

	if( m_true_heap_start_address == 0 )
	{
		return( false );
	}

	m_working_start_address = ( volatile unsigned char *) ((m_true_heap_start_address +  ( m_alignment_boundary - ((UnSignedLong)m_true_heap_start_address%m_alignment_boundary))));


	for( UnSignedLong i=0; i<m_true_byte_size; i++ )
	{
		((volatile unsigned char*)m_true_heap_start_address)[i] = init_value;
	}
    
	return( true );
}

//===========================================================

volatile AddressType CMemorySegment::GetWorkingAddress() const
{
	return( (AddressType) m_working_start_address );
}

//===========================================================

//===========================================================
// Member functions default overrides
//===========================================================

CMemorySegment::CMemorySegment( ) // Default constructor
{
	//Set the default values

	 m_true_heap_start_address = 0;
	 m_working_start_address = 0;
	 m_working_byte_size = 0;
	 m_true_byte_size = 0;
	 m_alignment_boundary = 0;

}

//===========================================================

CMemorySegment::~CMemorySegment( ) // Destructor
{
	if( m_true_heap_start_address != 0 )
	{
		DeleteMemory( (AddressType)m_true_heap_start_address );
	}
}

//===========================================================

CMemorySegment::CMemorySegment( const CMemorySegment &cmemorysegment_item ) // Copy constructor
{
	CopyDataToThis( cmemorysegment_item );
}

//===========================================================


//===========================================================
// Operator Members
//===========================================================

CMemorySegment& CMemorySegment::operator = ( const CMemorySegment &cmemorysegment_item )
{
	CopyDataToThis( cmemorysegment_item );

	return( *this ); 
}

//===========================================================

bool CMemorySegment::operator == ( const CMemorySegment &cmemorysegment_item ) const
{
	bool return_value = false;

	if( m_true_heap_start_address == cmemorysegment_item.m_true_heap_start_address &&
		m_working_start_address == cmemorysegment_item.m_working_start_address &&
		m_working_byte_size == cmemorysegment_item.m_working_byte_size &&
		m_true_byte_size == cmemorysegment_item.m_true_byte_size &&
		m_alignment_boundary == cmemorysegment_item.m_alignment_boundary )
		return_value = true;

	return( return_value );
}

//===========================================================

//===========================================================
//	Private Functions
//===========================================================



//===========================================================
// GetCopy Member Functions
//===========================================================

UnSignedLong CMemorySegment::GetCopyWorkingByteSize( void ) const
{
	return( m_working_byte_size );
}


//===========================================================

UnSignedLong CMemorySegment::GetCopyTrueByteSize( void ) const
{
	return( m_true_byte_size );
}


//===========================================================


//===========================================================
// Set Member Functions
//===========================================================



//===========================================================
//	Protected Functions
//===========================================================

void CMemorySegment::CopyDataToThis( const CMemorySegment &cmemorysegment_item ) //Class copy internal data.
{
	//Copy all the data into this.
	 m_true_heap_start_address = cmemorysegment_item.m_true_heap_start_address;
	 m_working_start_address = cmemorysegment_item.m_working_start_address;
	 m_working_byte_size = cmemorysegment_item.m_working_byte_size;
	 m_true_byte_size = cmemorysegment_item.m_true_byte_size;
	 m_alignment_boundary = cmemorysegment_item.m_alignment_boundary;
}



//===========================================================
// Friend functions
//===========================================================





//===========================================================
//	END OF FILE
//===========================================================