2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FIo_MemoryMappedFileImpl.cpp
20 * @brief This is the implementation file for _MemoryMappedFileImpl class.
29 #include <FBaseObject.h>
30 #include <FBaseColIEnumerator.h>
31 #include <FBaseRtMemoryManager.h>
32 #include <FBaseSysLog.h>
34 #include <FIoMemoryMappedFile.h>
36 #include <FIo_FileImpl.h>
37 #include <FIo_NormalFile.h>
38 #include <FIo_SecureFile.h>
40 #include "FIo_MemoryMappedFileImpl.h"
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Collection;
44 using namespace Tizen::Base::Runtime;
46 namespace Tizen { namespace Io
49 _MemoryMappedFileImpl::_MemoryMappedFileImpl(void)
54 _MemoryMappedFileImpl::~_MemoryMappedFileImpl(void)
59 _MemoryMappedFileImpl::Construct(const File& file)
63 const _FileImpl* pFileImpl = _FileImpl::GetInstance(file);
65 __fd = fileno(pFileImpl->GetFilePointer());
67 SysTryReturnResult(NID_IO, pFileImpl->IsReadable() == true, E_INVALID_ARG,
68 "The specified file parameter is not opened for reading.");
74 _MemoryMappedFileImpl::Map(void* address, long long length, unsigned long protection, unsigned long flag, long long offset)
80 if (protection & MEMORY_PROTECTION_MODE_READ)
84 if (protection & MEMORY_PROTECTION_MODE_WRITE)
88 if (protection & MEMORY_PROTECTION_MODE_EXEC)
93 if (flag & MEMORY_MAPPED_FILE_FLAG_SHARED)
97 if (flag & MEMORY_MAPPED_FILE_FLAG_PRIVATE)
101 if (flag & MEMORY_MAPPED_FILE_FLAG_FIXED)
106 void* pRetAddr = mmap(address, length, protection, flags, __fd, offset);
107 if (pRetAddr == MAP_FAILED)
112 r = E_ILLEGAL_ACCESS;
130 SysLog(NID_IO, "[%s] Map() fails. errno: %d (%s)", GetErrorMessage(r), errno, strerror(errno));
134 SetLastResult(E_SUCCESS);
143 _MemoryMappedFileImpl::Unmap(void* address, long long length)
145 result r = E_SUCCESS;
147 if (munmap(address, length) < 0)
158 SysLog(NID_IO, "[%s] Unmap() fails. errno: %d (%s)", GetErrorMessage(r), errno, strerror(errno));
169 _MemoryMappedFileImpl::Sync(void* address, long long length, MemoryMappedFileSyncFlag syncFlag)
172 result r = E_SUCCESS;
174 if (syncFlag == MEMORY_MAPPED_FILE_SYNC_FLAG_ASYNC)
178 else if (syncFlag == MEMORY_MAPPED_FILE_SYNC_FLAG_SYNC)
183 if (msync(address, length, flags) < 0)
202 SysLog(NID_IO, "[%s] Sync() fails. errno: %d (%s)", GetErrorMessage(r), errno, strerror(errno));