subs: options: add -sub-paths
[mplayer/greg.git] / osdep / mmap-os2.c
blob61fcc4f5dbc0cd44a97d7adbd03942d002e5276a
1 /*
2 * very simple implementation of mmap() for OS/2
4 * Copyright (c) 2008 KO Myung-Hun (komh@chollian.net)
6 * This file is part of MPlayer.
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.
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.
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.
23 #define INCL_DOS
24 #include <os2.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <io.h>
29 #include <unistd.h>
30 #include <sys/types.h>
32 #include "config.h"
33 #include "mmap.h"
34 #include "mmap_anon.h"
36 typedef struct os2_mmap_s
38 void *addr;
39 size_t len;
40 int flags;
41 struct os2_mmap_s *prev;
42 struct os2_mmap_s *next;
43 } os2_mmap;
44 static os2_mmap *m_mmap = NULL;
46 void *mmap( void *addr, size_t len, int prot, int flags, int fildes, off_t off )
48 os2_mmap *new_mmap;
50 ULONG fl;
51 ULONG rc;
53 void *ret;
55 if( prot & PROT_WRITE )
57 if( flags & MAP_SHARED )
58 return MAP_FAILED;
60 if( !( flags & MAP_PRIVATE ))
61 return MAP_FAILED;
64 if( flags & MAP_FIXED )
66 ULONG cb;
68 cb = len;
69 rc = DosQueryMem( addr, &cb, &fl );
70 if( rc || ( cb < len ))
71 return MAP_FAILED;
73 rc = DosSetMem( addr, len, fPERM );
74 if( rc )
75 return MAP_FAILED;
77 ret = addr;
79 else
81 // Allocate tiled memory compatible with 16-bit selectors
82 // 'fs_seg' in 'ldt_keeper.c' need this attribute
83 rc = DosAllocMem( &ret, len, fALLOC );
84 if( rc )
85 return MAP_FAILED;
88 new_mmap = malloc( sizeof( os2_mmap ));
89 new_mmap->addr = ret;
90 new_mmap->len = len;
91 new_mmap->flags = flags;
92 new_mmap->prev = m_mmap;
93 new_mmap->next = NULL;
95 if( m_mmap )
96 m_mmap->next = new_mmap;
97 m_mmap = new_mmap;
99 if( !( flags & MAP_ANON ))
101 int pos;
103 /* Now read in the file */
104 if(( pos = lseek( fildes, off, SEEK_SET )) == -1)
106 munmap( ret, len );
108 return MAP_FAILED;
111 read( fildes, ret, len );
112 lseek( fildes, pos, SEEK_SET ); /* Restore the file pointer */
115 fl = 0;
117 if( prot & PROT_READ )
118 fl |= PAG_READ;
120 if( prot & PROT_WRITE )
121 fl |= PAG_WRITE;
123 if( prot & PROT_EXEC )
124 fl |= PAG_EXECUTE;
126 if( prot & PROT_NONE )
127 fl |= PAG_GUARD;
129 rc = DosSetMem( ret, len, fl );
130 if( rc )
132 munmap( ret, len );
134 return MAP_FAILED;
137 return ret;
140 int munmap( void *addr, size_t len )
142 os2_mmap *mm;
144 for( mm = m_mmap; mm; mm = mm->prev )
146 if( mm->addr == addr )
147 break;
150 if( mm )
153 if( !( mm->flags & MAP_FIXED ))
154 DosFreeMem( addr );
156 if( mm->next )
157 mm->next->prev = mm->prev;
159 if( mm->prev )
160 mm->prev->next = mm->next;
162 if( m_mmap == mm )
163 m_mmap = mm->prev;
165 free( mm );
167 return 0;
170 return -1;
173 int mprotect( void *addr, size_t len, int prot )
175 os2_mmap *mm;
177 for( mm = m_mmap; mm; mm = mm->prev )
179 if( mm->addr == addr )
180 break;
183 if( mm )
185 ULONG fl;
187 fl = 0;
189 if( prot & PROT_READ )
190 fl |= PAG_READ;
192 if( prot & PROT_WRITE )
193 fl |= PAG_WRITE;
195 if( prot & PROT_EXEC )
196 fl |= PAG_EXECUTE;
198 if( prot & PROT_NONE )
199 fl |= PAG_GUARD;
201 if( DosSetMem( addr, len, fl ) == 0 )
202 return 0;
205 return -1;
208 void *mmap_anon( void *addr, size_t len, int prot, int flags, off_t off )
210 return mmap( addr, len, prot, flags | MAP_ANON, -1, off );