imgtools/imglib/memmap/source/memmaputils.cpp
changeset 0 044383f39525
child 590 360bd6b35136
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 /*
       
     2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "memmaputils.h"
       
    20 
       
    21 /**
       
    22 Constructor: MemmapUtils class
       
    23 Initilize the parameters to data members.
       
    24 
       
    25 @internalComponent
       
    26 @released
       
    27 */
       
    28 MemmapUtils::MemmapUtils( )
       
    29 : iHFile(0)
       
    30 {
       
    31 #ifdef WIN32
       
    32 	iHMMFile = 0;
       
    33 #endif
       
    34 }
       
    35 
       
    36 
       
    37 /**
       
    38 Destructor: MemmapUtils class
       
    39 Deallocates the memory for data members
       
    40 
       
    41 @internalComponent
       
    42 @released
       
    43 */
       
    44 MemmapUtils::~MemmapUtils( )
       
    45 {
       
    46 	CloseMapFile();
       
    47 
       
    48 	DeleteMapFile();
       
    49 }
       
    50 
       
    51 /**
       
    52 OpenMemMapPointer: Opens the memory map pointer
       
    53 
       
    54 @internalComponent
       
    55 @released
       
    56 
       
    57 @param aOffset - Starting offset of the memory map
       
    58 @param aSize - Size of the memory map
       
    59 */
       
    60 void* MemmapUtils::OpenMemMapPointer(unsigned long aOffset, unsigned long aSize)
       
    61 {
       
    62 #ifdef WIN32
       
    63 	// Create map view pointer
       
    64 	return (void*)MapViewOfFile(iHMMFile, FILE_MAP_ALL_ACCESS, 0, aOffset, aSize);
       
    65 #else
       
    66 	return KStatFalse;
       
    67 #endif
       
    68 }
       
    69 
       
    70 /**
       
    71 CloseMemMapPointer: Closes the memory map pointer
       
    72 
       
    73 @internalComponent
       
    74 @released
       
    75 
       
    76 @param aData - Memory map pointer
       
    77 @param aSize - Size of the memory map
       
    78 */
       
    79 int MemmapUtils::CloseMemMapPointer(void* aData, unsigned long aSize)
       
    80 {
       
    81 	unsigned long statusFlg = KStatFalse;
       
    82 
       
    83 	if(aData && iHFile)
       
    84 	{
       
    85 #ifdef WIN32
       
    86 		statusFlg = FlushViewOfFile(aData, aSize);
       
    87 		statusFlg &= FlushFileBuffers((HANDLE)_get_osfhandle(iHFile));
       
    88 		statusFlg &= UnmapViewOfFile(aData);
       
    89 #endif
       
    90 	}
       
    91 
       
    92 	if(statusFlg == (unsigned long)KStatFalse)
       
    93 	{
       
    94 		return KStatFalse;
       
    95 	}
       
    96 
       
    97 	return KStatTrue;
       
    98 }
       
    99 
       
   100 /**
       
   101 OpenMapFile: Opens the file for memory mapping
       
   102 
       
   103 @internalComponent
       
   104 @released
       
   105 */
       
   106 int MemmapUtils::OpenMapFile()
       
   107 {
       
   108 	GetMapFileName(iMapFileName);
       
   109 
       
   110 	if(iMapFileName.empty())
       
   111 	{
       
   112 		return KStatFalse;
       
   113 	}
       
   114 
       
   115 #ifdef WIN32
       
   116 	iHFile = open((const char*)iMapFileName.data(), (_O_CREAT | _O_BINARY | _O_RDWR));
       
   117 #endif
       
   118 
       
   119 	if((iHFile == (-1)) || (!iHFile))
       
   120 	{
       
   121 		Print(EAlways, "Cannot open the memory map file %s", (char*)iMapFileName.data());
       
   122 		iHFile = 0;
       
   123 
       
   124 		return KStatFalse;
       
   125 	}
       
   126 
       
   127 	return KStatTrue;
       
   128 }
       
   129 
       
   130 /**
       
   131 IsMapFileOpen: Returns the open status of the memory map file
       
   132 
       
   133 @internalComponent
       
   134 @released
       
   135 */
       
   136 int MemmapUtils::IsMapFileOpen()
       
   137 {
       
   138 	return (iHFile) ? KStatTrue : KStatFalse;
       
   139 }
       
   140 
       
   141 /**
       
   142 CloseMapFile: Closes the file for memory mapping
       
   143 
       
   144 @internalComponent
       
   145 @released
       
   146 */
       
   147 void MemmapUtils::CloseMapFile()
       
   148 {
       
   149 	if(iHFile)
       
   150 	{
       
   151 #ifdef WIN32
       
   152 		close(iHFile);
       
   153 #endif
       
   154 		iHFile = 0;
       
   155 	}
       
   156 }
       
   157 
       
   158 /**
       
   159 DeleteMapFile: Deletes the file for memory mapping
       
   160 
       
   161 @internalComponent
       
   162 @released
       
   163 */
       
   164 void MemmapUtils::DeleteMapFile()
       
   165 {
       
   166 #ifdef WIN32
       
   167 	unlink((char*)iMapFileName.data());
       
   168 #endif
       
   169 }
       
   170 
       
   171 /**
       
   172 CreateFileMapObject: Creates the map file object
       
   173 
       
   174 @internalComponent
       
   175 @released
       
   176 
       
   177 @param aSize - Size of the memory map
       
   178 */
       
   179 int MemmapUtils::CreateFileMapObject(unsigned long aSize)
       
   180 {
       
   181 #ifdef WIN32
       
   182 	// Create memory map object for the given size of the file
       
   183 	iHMMFile = CreateFileMapping((HANDLE)_get_osfhandle(iHFile), NULL, 
       
   184 				PAGE_READWRITE, 0, aSize, NULL);
       
   185 
       
   186 	if(!iHMMFile || (iHMMFile == INVALID_HANDLE_VALUE))
       
   187 	{
       
   188 		return KStatFalse;
       
   189 	}
       
   190 #endif
       
   191 
       
   192 	return KStatTrue;
       
   193 }
       
   194 
       
   195 /**
       
   196 CloseFileMapObject: Closes the map file object
       
   197 
       
   198 @internalComponent
       
   199 @released
       
   200 */
       
   201 void MemmapUtils::CloseFileMapObject()
       
   202 {
       
   203 #ifdef WIN32
       
   204 	if(iHMMFile)
       
   205 	{
       
   206 		CloseHandle(iHMMFile);
       
   207 		iHMMFile = 0;
       
   208 	}
       
   209 #endif
       
   210 }
       
   211 
       
   212 /**
       
   213 GetMapFileName: Generates a temporary file name
       
   214 
       
   215 @internalComponent
       
   216 @released
       
   217 
       
   218 @param aFile - Returns the name of the temporary file
       
   219 */
       
   220 void MemmapUtils::GetMapFileName(String& aFile)
       
   221 {
       
   222 	char *fileName = 0;
       
   223 
       
   224 #ifdef WIN32
       
   225 	fileName = new char[MAX_PATH];
       
   226 
       
   227 	if(fileName)
       
   228 		GetTempFileName(".", "MMAP", 0, fileName);
       
   229 
       
   230 	aFile.assign(fileName);
       
   231 #endif
       
   232 
       
   233 	if(fileName)
       
   234 		delete[] fileName;
       
   235 
       
   236 }
       
   237