Merge "Fixed Klocworks issues" into devel_3.0_main
[platform/framework/native/appfw.git] / src / io / FIoMemoryMappedFile.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file        FIoMemoryMappedFile.cpp
19  * @brief       This is the implementation file for MemoryMappedFile class.
20  */
21
22 #include <new>
23 #include <unique_ptr.h>
24
25 #include <FBaseSysLog.h>
26 #include <FIoFile.h>
27 #include <FIoMemoryMappedFile.h>
28
29 #include "FIo_MemoryMappedFileImpl.h"
30
31 namespace Tizen { namespace Io
32 {
33
34 MemoryMappedFile::MemoryMappedFile(void)
35         : __pMemoryMappedFileImpl(null)
36 {
37 }
38
39 MemoryMappedFile::~MemoryMappedFile(void)
40 {
41         delete __pMemoryMappedFileImpl;
42 }
43
44 result
45 MemoryMappedFile::Construct(const File& file)
46 {
47         result r = E_SUCCESS;
48
49         SysAssertf(__pMemoryMappedFileImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
50
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.");
54
55         r = pMemoryMappedFileImpl->Construct(file);
56         SysTryReturn(NID_IO, !(IsFailed(r)), r, r, "[%s] Propagated.", GetErrorMessage(r));
57
58         __pMemoryMappedFileImpl = pMemoryMappedFileImpl.release();
59
60         return E_SUCCESS;
61 }
62
63 void*
64 MemoryMappedFile::Map(void* address, long long length, unsigned long protection, unsigned long flag, long long offset)
65 {
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.");
71
72         if ((address == null) && (flag & MEMORY_MAPPED_FILE_FLAG_FIXED) == MEMORY_MAPPED_FILE_FLAG_FIXED)
73         {
74                 SysLogException(NID_IO, E_INVALID_ARG, "[E_INVALID_ARG] The specified address or flag parameter is invalid.");
75                 return null;
76         }
77         else if ((address != null) && (flag & MEMORY_MAPPED_FILE_FLAG_FIXED) == 0x0)
78         {
79                 SysLogException(NID_IO, E_INVALID_ARG, "[E_INVALID_ARG] The specified address or flag parameter is invalid.");
80                 return null;
81         }
82
83         return __pMemoryMappedFileImpl->Map(address, length, protection, flag, offset);
84 }
85
86 result
87 MemoryMappedFile::Unmap(void* address, long long length)
88 {
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);
93 }
94
95 result
96 MemoryMappedFile::Sync(void* address, long long length, MemoryMappedFileSyncFlag syncFlag)
97 {
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.");
103
104         return __pMemoryMappedFileImpl->Sync(address, length, syncFlag);
105 }
106
107 }} // Tizen::Io
108