Revert "fixed PLM issue. Assert occured when an application was closed using back...
[platform/framework/native/appfw.git] / src / io / FIo_MemoryMappedFileImpl.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        FIo_MemoryMappedFileImpl.cpp
19  * @brief       This is the implementation file for _MemoryMappedFileImpl class.
20  */
21
22 #include <new>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/mman.h>
26 #include <errno.h>
27
28 #include <FBaseObject.h>
29 #include <FBaseColIEnumerator.h>
30 #include <FBaseRtMemoryManager.h>
31 #include <FBaseSysLog.h>
32 #include <FIoFile.h>
33 #include <FIoMemoryMappedFile.h>
34
35 #include <FIo_FileImpl.h>
36 #include <FIo_NormalFile.h>
37 #include <FIo_SecureFile.h>
38
39 #include "FIo_MemoryMappedFileImpl.h"
40
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Collection;
43 using namespace Tizen::Base::Runtime;
44
45 namespace Tizen { namespace Io
46 {
47
48 _MemoryMappedFileImpl::_MemoryMappedFileImpl(void)
49         : __fd(-1)
50 {
51 }
52
53 _MemoryMappedFileImpl::~_MemoryMappedFileImpl(void)
54 {
55 }
56
57 result
58 _MemoryMappedFileImpl::Construct(const File& file)
59 {
60         result r = E_SUCCESS;
61
62         const _FileImpl* pFileImpl = _FileImpl::GetInstance(file);
63
64         __fd = fileno(pFileImpl->GetFilePointer());
65
66         SysTryReturnResult(NID_IO, pFileImpl->IsReadable() == true, E_INVALID_ARG,
67                         "The specified file parameter is not opened for reading.");
68
69         return r;
70 }
71
72 void*
73 _MemoryMappedFileImpl::Map(void* address, long long length, unsigned long protection, unsigned long flag, long long offset)
74 {
75         int prot = PROT_NONE;
76         int flags = 0x0;
77         result r = E_SUCCESS;
78
79         if (protection & MEMORY_PROTECTION_MODE_READ)
80         {
81                 prot |= PROT_READ;
82         }
83         if (protection & MEMORY_PROTECTION_MODE_WRITE)
84         {
85                 prot |= PROT_WRITE;
86         }
87         if (protection & MEMORY_PROTECTION_MODE_EXEC)
88         {
89                 prot |= PROT_EXEC;
90         }
91
92         if (flag & MEMORY_MAPPED_FILE_FLAG_SHARED)
93         {
94                 flags |= MAP_SHARED;
95         }
96         if (flag & MEMORY_MAPPED_FILE_FLAG_PRIVATE)
97         {
98                 flags |= MAP_PRIVATE;
99         }
100         if (flag & MEMORY_MAPPED_FILE_FLAG_FIXED)
101         {
102                 flags |= MAP_FIXED;
103         }
104
105         void* pRetAddr = mmap(address, length, protection, flags, __fd, offset);
106         if (pRetAddr == MAP_FAILED)
107         {
108                 switch (errno)
109                 {
110                 case EACCES:
111                         r = E_ILLEGAL_ACCESS;
112                         break;
113                 case EAGAIN:
114                         r = E_OBJECT_LOCKED;
115                         break;
116                 case EINVAL:
117                         r = E_INVALID_ARG;
118                         break;
119                 case ENFILE:
120                         r = E_MAX_EXCEEDED;
121                         break;
122                 case ENOMEM:
123                         r = E_OUT_OF_MEMORY;
124                         break;
125                 default:
126                         r = E_IO;
127                         break;
128                 }
129                 SysLog(NID_IO, "[%s] Map() fails. errno: %d (%s)", GetErrorMessage(r), errno, strerror(errno));
130                 goto CATCH;
131         }
132
133         SetLastResult(E_SUCCESS);
134         return pRetAddr;
135
136 CATCH:
137         SetLastResult(r);
138         return null;
139 }
140
141 result
142 _MemoryMappedFileImpl::Unmap(void* address, long long length)
143 {
144         result r = E_SUCCESS;
145
146         if (munmap(address, length) < 0)
147         {
148                 switch (errno)
149                 {
150                 case EINVAL:
151                         r = E_INVALID_ARG;
152                         break;
153                 default:
154                         r = E_IO;
155                         break;
156                 }
157                 SysLog(NID_IO, "[%s] Unmap() fails. errno: %d (%s)", GetErrorMessage(r), errno, strerror(errno));
158                 goto CATCH;
159         }
160
161         return E_SUCCESS;
162
163 CATCH:
164         return r;
165 }
166
167 result
168 _MemoryMappedFileImpl::Sync(void* address, long long length, MemoryMappedFileSyncFlag syncFlag)
169 {
170         int flags = 0x0;
171         result r = E_SUCCESS;
172
173         if (syncFlag == MEMORY_MAPPED_FILE_SYNC_FLAG_ASYNC)
174         {
175                 flags |= MS_ASYNC;
176         }
177         else if (syncFlag == MEMORY_MAPPED_FILE_SYNC_FLAG_SYNC)
178         {
179                 flags |= MS_SYNC;
180         }
181
182         if (msync(address, length, flags) < 0)
183         {
184                 switch (errno)
185                 {
186                 case EINVAL:
187                         // fall through
188                 case ENOMEM:
189                         r = E_INVALID_ARG;
190                         break;
191                 default:
192                         r = E_IO;
193                         break;
194                 }
195                 goto CATCH;
196         }
197
198         return E_SUCCESS;
199
200 CATCH:
201         SysLog(NID_IO, "[%s] Sync() fails. errno: %d (%s)", GetErrorMessage(r), errno, strerror(errno));
202         return r;
203 }
204
205 }} // Tizen::Io
206