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