2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FIo_MemoryMappedFileImpl.cpp
19 * @brief This is the implementation file for _MemoryMappedFileImpl class.
28 #include <FBaseObject.h>
29 #include <FBaseColIEnumerator.h>
30 #include <FBaseRtMemoryManager.h>
31 #include <FBaseSysLog.h>
33 #include <FIoMemoryMappedFile.h>
35 #include <FIo_FileImpl.h>
36 #include <FIo_NormalFile.h>
37 #include <FIo_SecureFile.h>
39 #include "FIo_MemoryMappedFileImpl.h"
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Collection;
43 using namespace Tizen::Base::Runtime;
45 namespace Tizen { namespace Io
48 _MemoryMappedFileImpl::_MemoryMappedFileImpl(void)
53 _MemoryMappedFileImpl::~_MemoryMappedFileImpl(void)
58 _MemoryMappedFileImpl::Construct(const File& file)
62 const _FileImpl* pFileImpl = _FileImpl::GetInstance(file);
64 __fd = fileno(pFileImpl->GetFilePointer());
66 SysTryReturnResult(NID_IO, pFileImpl->IsReadable() == true, E_INVALID_ARG,
67 "The specified file parameter is not opened for reading.");
73 _MemoryMappedFileImpl::Map(void* address, long long length, unsigned long protection, unsigned long flag, long long offset)
79 if (protection & MEMORY_PROTECTION_MODE_READ)
83 if (protection & MEMORY_PROTECTION_MODE_WRITE)
87 if (protection & MEMORY_PROTECTION_MODE_EXEC)
92 if (flag & MEMORY_MAPPED_FILE_FLAG_SHARED)
96 if (flag & MEMORY_MAPPED_FILE_FLAG_PRIVATE)
100 if (flag & MEMORY_MAPPED_FILE_FLAG_FIXED)
105 void* pRetAddr = mmap(address, length, protection, flags, __fd, offset);
106 if (pRetAddr == MAP_FAILED)
111 r = E_ILLEGAL_ACCESS;
129 SysLog(NID_IO, "[%s] Map() fails. errno: %d (%s)", GetErrorMessage(r), errno, strerror(errno));
133 SetLastResult(E_SUCCESS);
142 _MemoryMappedFileImpl::Unmap(void* address, long long length)
144 result r = E_SUCCESS;
146 if (munmap(address, length) < 0)
157 SysLog(NID_IO, "[%s] Unmap() fails. errno: %d (%s)", GetErrorMessage(r), errno, strerror(errno));
168 _MemoryMappedFileImpl::Sync(void* address, long long length, MemoryMappedFileSyncFlag syncFlag)
171 result r = E_SUCCESS;
173 if (syncFlag == MEMORY_MAPPED_FILE_SYNC_FLAG_ASYNC)
177 else if (syncFlag == MEMORY_MAPPED_FILE_SYNC_FLAG_SYNC)
182 if (msync(address, length, flags) < 0)
201 SysLog(NID_IO, "[%s] Sync() fails. errno: %d (%s)", GetErrorMessage(r), errno, strerror(errno));