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 FIoMemoryMappedFile.cpp
19 * @brief This is the implementation file for MemoryMappedFile class.
23 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
27 #include <FIoMemoryMappedFile.h>
29 #include "FIo_MemoryMappedFileImpl.h"
31 namespace Tizen { namespace Io
34 MemoryMappedFile::MemoryMappedFile(void)
35 : __pMemoryMappedFileImpl(null)
39 MemoryMappedFile::~MemoryMappedFile(void)
41 delete __pMemoryMappedFileImpl;
45 MemoryMappedFile::Construct(const File& file)
49 SysAssertf(__pMemoryMappedFileImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
51 std::unique_ptr<_MemoryMappedFileImpl> pMemoryMappedFileImpl(new (std::nothrow) _MemoryMappedFileImpl);
52 SysTryReturnResult(NID_IO, pMemoryMappedFileImpl != null, E_OUT_OF_MEMORY,
53 "The memory is insufficient.");
55 r = pMemoryMappedFileImpl->Construct(file);
56 SysTryReturn(NID_IO, !(IsFailed(r)), r, r, "[%s] Propagated.", GetErrorMessage(r));
58 __pMemoryMappedFileImpl = pMemoryMappedFileImpl.release();
64 MemoryMappedFile::Map(void* address, long long length, unsigned long protection, unsigned long flag, long long offset)
66 SysAssertf(__pMemoryMappedFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
67 SysTryReturn(NID_IO, length > 0, null, E_INVALID_ARG,
68 "[E_INVALID_ARG] The specified length parameter is not positive.");
69 SysTryReturn(NID_IO, flag != (MEMORY_MAPPED_FILE_FLAG_PRIVATE | MEMORY_MAPPED_FILE_FLAG_SHARED), null, E_INVALID_ARG,
70 "[E_INVALID_ARG] The specified flag parameter is invalid.");
72 if ((address == null) && (flag & MEMORY_MAPPED_FILE_FLAG_FIXED) == MEMORY_MAPPED_FILE_FLAG_FIXED)
74 SysLogException(NID_IO, E_INVALID_ARG, "[E_INVALID_ARG] The specified address or flag parameter is invalid.");
77 else if ((address != null) && (flag & MEMORY_MAPPED_FILE_FLAG_FIXED) == 0x0)
79 SysLogException(NID_IO, E_INVALID_ARG, "[E_INVALID_ARG] The specified address or flag parameter is invalid.");
83 return __pMemoryMappedFileImpl->Map(address, length, protection, flag, offset);
87 MemoryMappedFile::Unmap(void* address, long long length)
89 SysAssertf(__pMemoryMappedFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
90 SysTryReturnResult(NID_IO, length > 0, E_INVALID_ARG,
91 "The specified length parameter is not positive.");
92 return __pMemoryMappedFileImpl->Unmap(address, length);
96 MemoryMappedFile::Sync(void* address, long long length, MemoryMappedFileSyncFlag syncFlag)
98 SysAssertf(__pMemoryMappedFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
99 SysTryReturnResult(NID_IO, length > 0, E_INVALID_ARG,
100 "The specified length parameter is not positive.");
101 SysTryReturnResult(NID_IO, (syncFlag == MEMORY_MAPPED_FILE_SYNC_FLAG_SYNC || syncFlag == MEMORY_MAPPED_FILE_SYNC_FLAG_ASYNC),
102 E_INVALID_ARG, "The specified syncFlag parameter is invalid.");
104 return __pMemoryMappedFileImpl->Sync(address, length, syncFlag);