imgtools/imglib/memmap/source/memmap.cpp
changeset 629 541af5ee3ed9
parent 590 360bd6b35136
equal deleted inserted replaced
628:7c4a911dc066 629:541af5ee3ed9
    26 @released
    26 @released
    27 
    27 
    28 @param aFillFlg	- Flag to enable the initialisation of memory map
    28 @param aFillFlg	- Flag to enable the initialisation of memory map
    29 @param aOutputFile - Name of the output file
    29 @param aOutputFile - Name of the output file
    30 */
    30 */
    31 Memmap::Memmap( int aFillFlg, String aOutputFile )
    31 Memmap::Memmap( int aFillFlg, const string& aOutputFile )
    32 : iOutFileName(aOutputFile), iData(0), iMaxMapSize(0), iStartOffset(0), iFillFlg(aFillFlg)
    32 : iOutFileName(aOutputFile), iData(0), iMaxMapSize(0), iStartOffset(0), iFillFlg(aFillFlg) {
    33 {
       
    34 	iUtils = new MemmapUtils();
    33 	iUtils = new MemmapUtils();
    35 }
    34 }
    36 
    35 
    37 /**
    36 /**
    38 Constructor: Memmap class
    37 Constructor: Memmap class
    42 @released
    41 @released
    43 
    42 
    44 @param aFillFlg	- Flag to enable the initialisation of memory map
    43 @param aFillFlg	- Flag to enable the initialisation of memory map
    45 */
    44 */
    46 Memmap::Memmap( int aFillFlg )
    45 Memmap::Memmap( int aFillFlg )
    47 : iData(0), iMaxMapSize(0), iStartOffset(0), iFillFlg(aFillFlg)
    46 : iData(0), iMaxMapSize(0), iStartOffset(0), iFillFlg(aFillFlg) {
    48 {
       
    49 	iUtils = new MemmapUtils();
    47 	iUtils = new MemmapUtils();
    50 }
    48 }
    51 
    49 
    52 
    50 
    53 /**
    51 /**
    55 Deallocates the memory for data members
    53 Deallocates the memory for data members
    56 
    54 
    57 @internalComponent
    55 @internalComponent
    58 @released
    56 @released
    59 */
    57 */
    60 Memmap::~Memmap( )
    58 Memmap::~Memmap( ) {
    61 {
    59 	if(iData) {
    62 	if(iData)
       
    63 	{
       
    64 		CloseMemoryMap();
    60 		CloseMemoryMap();
    65 	}
    61 	}
    66 
    62 
    67 	if(iUtils)
    63 	if(iUtils) {
    68 	{
       
    69 		delete iUtils;
    64 		delete iUtils;
    70 	}
    65 	}
    71 }
    66 }
    72 
    67 
    73 /**
    68 /**
    76 @internalComponent
    71 @internalComponent
    77 @released
    72 @released
    78 
    73 
    79 @param aOutputFile  - Name of the output image file
    74 @param aOutputFile  - Name of the output image file
    80 */
    75 */
    81 void Memmap::SetOutputFile( String aOutputFile )
    76 void Memmap::SetOutputFile(const string& aOutputFile ) {
    82 {
       
    83 	iOutFileName = aOutputFile;
    77 	iOutFileName = aOutputFile;
    84 }
    78 }
    85 
    79 
    86 /**
    80 /**
    87 SetMaxMapSize: To set the maximum size of the memory map
    81 SetMaxMapSize: To set the maximum size of the memory map
   120 	return iData[aIndex]; 
   114 	return iData[aIndex]; 
   121 }
   115 }
   122 
   116 
   123 /**
   117 /**
   124 CreateMemoryMap: 
   118 CreateMemoryMap: 
   125  Opens the memory map file
   119 Opens the memory map file
   126  Initialises the map size member
   120 Initialises the map size member
   127  Create the memory map pointer
   121 Create the memory map pointer
   128  Fill the memory map with the specified value
   122 Fill the memory map with the specified value
   129 
   123 
   130 @internalComponent
   124 @internalComponent
   131 @released
   125 @released
   132 
   126 
   133 @param aStartOffset - Start offset of the memory map location
   127 @param aStartOffset - Start offset of the memory map location
   134 @param aFillVal - Value to be filled in the memory map
   128 @param aFillVal - Value to be filled in the memory map
   135 */
   129 */
   136 int Memmap::CreateMemoryMap( unsigned long aStartOffset, unsigned char aFillVal )
   130 int Memmap::CreateMemoryMap( unsigned long aStartOffset, unsigned char aFillVal ) {
   137 {
   131 	if((!iMaxMapSize) || (aStartOffset > iMaxMapSize)) {
   138 	if((!iMaxMapSize) || (aStartOffset > iMaxMapSize))
       
   139 	{
       
   140 		return KStatFalse;
   132 		return KStatFalse;
   141 	}
   133 	}
   142 	else if(iUtils->IsMapFileOpen() && iData)
   134 	else if(iUtils->IsMapFileOpen() && iData) {
   143 	{
       
   144 		iStartOffset = aStartOffset;
   135 		iStartOffset = aStartOffset;
   145 		return KStatTrue;
   136 		return KStatTrue;
   146 	}
   137 	}
   147 
   138 
   148 	if(iUtils->IsMapFileOpen() == KStatFalse)
   139 	if(iUtils->IsMapFileOpen() == KStatFalse) {
   149 	{
   140 		if(iUtils->OpenMapFile() == KStatFalse) {
   150 		if(iUtils->OpenMapFile() == KStatFalse)
       
   151 		{
       
   152 			return KStatFalse;
   141 			return KStatFalse;
   153 		}
   142 		}
   154 	}
   143 	}
   155 
   144 
   156 	if(iUtils->CreateFileMapObject(iMaxMapSize) == KStatFalse)
   145 	if(iUtils->CreateFileMapObject(iMaxMapSize) == KStatFalse) {
   157 	{
       
   158 		return KStatFalse;
   146 		return KStatFalse;
   159 	}
   147 	}
   160 
   148 
   161 	iData = (char*)(iUtils->OpenMemMapPointer(0,iMaxMapSize));
   149 	iData = (char*)(iUtils->OpenMemMapPointer(0,iMaxMapSize));
   162 	if( !iData )
   150 	if( !iData ) {
   163 	{
       
   164 		return KStatFalse;
   151 		return KStatFalse;
   165 	}
   152 	}
   166 
   153 
   167 	iStartOffset = aStartOffset;
   154 	iStartOffset = aStartOffset;
   168 	
   155 
   169 	if(iFillFlg)
   156 	if(iFillFlg) {
   170 	{
       
   171 		return FillMemMap( aFillVal );
   157 		return FillMemMap( aFillVal );
   172 	}
   158 	}
   173 
   159 
   174 	return KStatTrue;
   160 	return KStatTrue;
   175 }
   161 }
   180 @internalComponent
   166 @internalComponent
   181 @released
   167 @released
   182 
   168 
   183 @param aCloseFile - Flag to close the memory map file
   169 @param aCloseFile - Flag to close the memory map file
   184 */
   170 */
   185 void Memmap::CloseMemoryMap( int aCloseFile )
   171 void Memmap::CloseMemoryMap( int aCloseFile ) {
   186 {
       
   187 	// Close map view pointer
   172 	// Close map view pointer
   188 	if(!iUtils->CloseMemMapPointer((void*)iData, iMaxMapSize))
   173 	if(!iUtils->CloseMemMapPointer((void*)iData, iMaxMapSize)) {
   189 	{
       
   190 		Print(ELog, "Failed to unmap the memory map object");
   174 		Print(ELog, "Failed to unmap the memory map object");
   191 	}
   175 	}
   192 	iData = 0;
   176 	iData = 0;
   193 
   177 
   194 	iUtils->CloseFileMapObject();
   178 	iUtils->CloseFileMapObject();
   195 
   179 
   196 	// Close map file
   180 	// Close map file
   197 	if(aCloseFile)
   181 	if(aCloseFile) {
   198 	{
       
   199 		iUtils->CloseMapFile();
   182 		iUtils->CloseMapFile();
   200 	}
   183 	}
   201 }
   184 }
   202 
   185 
   203 /**
   186 /**
   204 GetMemoryMapPointer: Get the stating address of the memory map
   187 GetMemoryMapPointer: Get the stating address of the memory map
   205 
   188 
   206 @internalComponent
   189 @internalComponent
   207 @released
   190 @released
   208 */
   191 */
   209 char *Memmap::GetMemoryMapPointer( )
   192 char *Memmap::GetMemoryMapPointer( ) {
   210 {
       
   211 	if(iData)
   193 	if(iData)
   212 		return (iData + iStartOffset);
   194 		return (iData + iStartOffset);
   213 
   195 
   214 	return KStatFalse;
   196 	return KStatFalse;
   215 }
   197 }
   218 WriteToOutputFile: Writes the memory map contents to the output file
   200 WriteToOutputFile: Writes the memory map contents to the output file
   219 
   201 
   220 @internalComponent
   202 @internalComponent
   221 @released
   203 @released
   222 */
   204 */
   223 void Memmap::WriteToOutputFile( )
   205 void Memmap::WriteToOutputFile( ) {
   224 {
   206  
   225 	Ofstream ofs;
   207 
   226 
   208 	if(!iData) {
   227 	if(!iData)
       
   228 	{
       
   229 		Print(EAlways, "Memory map has not been created");
   209 		Print(EAlways, "Memory map has not been created");
   230 	}
   210 	}
   231 
   211 
   232 	if(iOutFileName.empty())
   212 	if(iOutFileName.empty()) {
   233 	{
       
   234 		Print(EAlways, "Output file has not been set");
   213 		Print(EAlways, "Output file has not been set");
   235 		return;
   214 		return;
   236 	}
   215 	}
   237 
   216 
   238 	ofs.open(((const char*)iOutFileName.data()), std::ios::binary);
   217 	ofstream ofs(iOutFileName.c_str(), ios_base::out + ios_base::binary );
   239 	if(!ofs.is_open())
   218 	if(!ofs.is_open()) {
   240 	{
       
   241 		Print(EAlways, "Cannot open output file %s", (char*)iOutFileName.data());
   219 		Print(EAlways, "Cannot open output file %s", (char*)iOutFileName.data());
   242 		return;
   220 		return;
   243 	}
   221 	}
   244 
   222 
   245 	ofs.write((const char*)(iData + iStartOffset), (iMaxMapSize - iStartOffset));
   223 	ofs.write((const char*)(iData + iStartOffset), (iMaxMapSize - iStartOffset));
   255 @internalComponent
   233 @internalComponent
   256 @released
   234 @released
   257 
   235 
   258 @param aFillVal - Value to be filled
   236 @param aFillVal - Value to be filled
   259 */
   237 */
   260 int Memmap::FillMemMap( unsigned char aFillVal )
   238 int Memmap::FillMemMap( unsigned char aFillVal ) {
   261 {
   239 	if(iData) {
   262 	if(iData)
       
   263 	{
       
   264 		// Fill the value
   240 		// Fill the value
   265 		memset(iData, aFillVal, iMaxMapSize);
   241 		memset(iData, aFillVal, iMaxMapSize);
   266 
   242 
   267 		// Unmap the file
   243 		// Unmap the file
   268 		if(iUtils->CloseMemMapPointer((void*)iData, iMaxMapSize) == KStatFalse)
   244 		if(iUtils->CloseMemMapPointer((void*)iData, iMaxMapSize) == KStatFalse) {
   269 		{
       
   270 			return KStatFalse;
   245 			return KStatFalse;
   271 		}
   246 		}
   272 
   247 
   273 		// Map it again
   248 		// Map it again
   274 		iData = (char*)(iUtils->OpenMemMapPointer(0,iMaxMapSize));
   249 		iData = (char*)(iUtils->OpenMemMapPointer(0,iMaxMapSize));
   275 		if(!iData)
   250 		if(!iData) {
   276 		{
       
   277 			return KStatFalse;
   251 			return KStatFalse;
   278 		}
   252 		}
   279 	}
   253 	}
   280 
   254 
   281 	return KStatTrue;
   255 	return KStatTrue;