26076
|
1 /*
|
|
2 * very simple implementation of mmap() for OS/2
|
|
3 *
|
|
4 * Copyright (c) 2008 KO Myung-Hun (komh@chollian.net)
|
|
5 *
|
|
6 * This file is part of MPlayer.
|
|
7 *
|
|
8 * MPlayer is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
11 * (at your option) any later version.
|
|
12 *
|
|
13 * MPlayer is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License along
|
|
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
21 */
|
|
22
|
|
23 #define INCL_DOS
|
|
24 #include <os2.h>
|
|
25
|
|
26 #include <stdio.h>
|
|
27 #include <stdlib.h>
|
|
28 #include <io.h>
|
|
29 #include <sys/types.h>
|
|
30
|
|
31 #include "config.h"
|
|
32 #include "mmap.h"
|
|
33 #include "mmap_anon.h"
|
|
34
|
|
35 typedef struct os2_mmap_s
|
|
36 {
|
|
37 void *addr;
|
|
38 size_t len;
|
|
39 int flags;
|
|
40 struct os2_mmap_s *prev;
|
|
41 struct os2_mmap_s *next;
|
|
42 } os2_mmap;
|
|
43 static os2_mmap *m_mmap = NULL;
|
|
44
|
|
45 void *mmap( void *addr, size_t len, int prot, int flags, int fildes, off_t off )
|
|
46 {
|
|
47 os2_mmap *new_mmap;
|
|
48
|
|
49 ULONG fl;
|
|
50 ULONG rc;
|
|
51
|
|
52 void *ret;
|
|
53
|
|
54 if( prot & PROT_WRITE )
|
|
55 {
|
|
56 if( flags & MAP_SHARED )
|
|
57 return MAP_FAILED;
|
|
58
|
|
59 if( !( flags & MAP_PRIVATE ))
|
|
60 return MAP_FAILED;
|
|
61 }
|
|
62
|
|
63 if( flags & MAP_FIXED )
|
|
64 {
|
|
65 ULONG cb;
|
|
66
|
|
67 cb = len;
|
|
68 rc = DosQueryMem( addr, &cb, &fl );
|
|
69 if( rc || ( cb < len ))
|
|
70 return MAP_FAILED;
|
|
71
|
|
72 rc = DosSetMem( addr, len, fPERM );
|
|
73 if( rc )
|
|
74 return MAP_FAILED;
|
|
75
|
|
76 ret = addr;
|
|
77 }
|
|
78 else
|
|
79 {
|
|
80 // Allocate tiled memory compatible with 16-bit selectors
|
|
81 // 'fs_seg' in 'ldt_keeper.c' need this attribute
|
|
82 rc = DosAllocMem( &ret, len, fALLOC );
|
|
83 if( rc )
|
|
84 return MAP_FAILED;
|
|
85 }
|
|
86
|
30702
|
87 new_mmap = malloc( sizeof( os2_mmap ));
|
26076
|
88 new_mmap->addr = ret;
|
|
89 new_mmap->len = len;
|
|
90 new_mmap->flags = flags;
|
|
91 new_mmap->prev = m_mmap;
|
|
92 new_mmap->next = NULL;
|
|
93
|
|
94 if( m_mmap )
|
|
95 m_mmap->next = new_mmap;
|
|
96 m_mmap = new_mmap;
|
|
97
|
|
98 if( !( flags & MAP_ANON ))
|
|
99 {
|
|
100 int pos;
|
|
101
|
|
102 /* Now read in the file */
|
|
103 if(( pos = lseek( fildes, off, SEEK_SET )) == -1)
|
|
104 {
|
|
105 munmap( ret, len );
|
|
106
|
|
107 return MAP_FAILED;
|
|
108 }
|
|
109
|
|
110 read( fildes, ret, len );
|
|
111 lseek( fildes, pos, SEEK_SET ); /* Restore the file pointer */
|
|
112 }
|
|
113
|
|
114 fl = 0;
|
|
115
|
|
116 if( prot & PROT_READ )
|
|
117 fl |= PAG_READ;
|
|
118
|
|
119 if( prot & PROT_WRITE )
|
|
120 fl |= PAG_WRITE;
|
|
121
|
|
122 if( prot & PROT_EXEC )
|
|
123 fl |= PAG_EXECUTE;
|
|
124
|
|
125 if( prot & PROT_NONE )
|
|
126 fl |= PAG_GUARD;
|
|
127
|
|
128 rc = DosSetMem( ret, len, fl );
|
|
129 if( rc )
|
|
130 {
|
|
131 munmap( ret, len );
|
|
132
|
|
133 return MAP_FAILED;
|
|
134 }
|
|
135
|
|
136 return ret;
|
|
137 }
|
|
138
|
|
139 int munmap( void *addr, size_t len )
|
|
140 {
|
|
141 os2_mmap *mm;
|
|
142
|
|
143 for( mm = m_mmap; mm; mm = mm->prev )
|
|
144 {
|
|
145 if( mm->addr == addr )
|
|
146 break;
|
|
147 }
|
|
148
|
|
149 if( mm )
|
|
150 {
|
|
151
|
|
152 if( !( mm->flags & MAP_FIXED ))
|
|
153 DosFreeMem( addr );
|
|
154
|
|
155 if( mm->next )
|
|
156 mm->next->prev = mm->prev;
|
|
157
|
|
158 if( mm->prev )
|
|
159 mm->prev->next = mm->next;
|
|
160
|
|
161 if( m_mmap == mm )
|
|
162 m_mmap = mm->prev;
|
|
163
|
|
164 free( mm );
|
|
165
|
|
166 return 0;
|
|
167 }
|
|
168
|
|
169 return -1;
|
|
170 }
|
|
171
|
|
172 int mprotect( void *addr, size_t len, int prot )
|
|
173 {
|
|
174 os2_mmap *mm;
|
|
175
|
|
176 for( mm = m_mmap; mm; mm = mm->prev )
|
|
177 {
|
|
178 if( mm->addr == addr )
|
|
179 break;
|
|
180 }
|
|
181
|
|
182 if( mm )
|
|
183 {
|
|
184 ULONG fl;
|
|
185
|
|
186 fl = 0;
|
|
187
|
|
188 if( prot & PROT_READ )
|
|
189 fl |= PAG_READ;
|
|
190
|
|
191 if( prot & PROT_WRITE )
|
|
192 fl |= PAG_WRITE;
|
|
193
|
|
194 if( prot & PROT_EXEC )
|
|
195 fl |= PAG_EXECUTE;
|
|
196
|
|
197 if( prot & PROT_NONE )
|
|
198 fl |= PAG_GUARD;
|
|
199
|
|
200 if( DosSetMem( addr, len, fl ) == 0 )
|
|
201 return 0;
|
|
202 }
|
|
203
|
|
204 return -1;
|
|
205 }
|
|
206
|
|
207 void *mmap_anon( void *addr, size_t len, int prot, int flags, off_t off )
|
|
208 {
|
|
209 return mmap( addr, len, prot, flags | MAP_ANON, -1, off );
|
|
210 }
|