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