annotate osdep/mmap-os2.c @ 29691:183ea012e25b

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