vo_corevideo: indentation fixes
[mplayer/glamo.git] / osdep / mmap-os2.c
bloba1e0c29e3fb81f80966880bc9d21fa20a33ffd17
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 <sys/types.h>
31 #include "config.h"
32 #include "mmap.h"
33 #include "mmap_anon.h"
35 typedef struct os2_mmap_s
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;
45 void *mmap( void *addr, size_t len, int prot, int flags, int fildes, off_t off )
47 os2_mmap *new_mmap;
49 ULONG fl;
50 ULONG rc;
52 void *ret;
54 if( prot & PROT_WRITE )
56 if( flags & MAP_SHARED )
57 return MAP_FAILED;
59 if( !( flags & MAP_PRIVATE ))
60 return MAP_FAILED;
63 if( flags & MAP_FIXED )
65 ULONG cb;
67 cb = len;
68 rc = DosQueryMem( addr, &cb, &fl );
69 if( rc || ( cb < len ))
70 return MAP_FAILED;
72 rc = DosSetMem( addr, len, fPERM );
73 if( rc )
74 return MAP_FAILED;
76 ret = addr;
78 else
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;
87 new_mmap = malloc( sizeof( os2_mmap ));
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;
94 if( m_mmap )
95 m_mmap->next = new_mmap;
96 m_mmap = new_mmap;
98 if( !( flags & MAP_ANON ))
100 int pos;
102 /* Now read in the file */
103 if(( pos = lseek( fildes, off, SEEK_SET )) == -1)
105 munmap( ret, len );
107 return MAP_FAILED;
110 read( fildes, ret, len );
111 lseek( fildes, pos, SEEK_SET ); /* Restore the file pointer */
114 fl = 0;
116 if( prot & PROT_READ )
117 fl |= PAG_READ;
119 if( prot & PROT_WRITE )
120 fl |= PAG_WRITE;
122 if( prot & PROT_EXEC )
123 fl |= PAG_EXECUTE;
125 if( prot & PROT_NONE )
126 fl |= PAG_GUARD;
128 rc = DosSetMem( ret, len, fl );
129 if( rc )
131 munmap( ret, len );
133 return MAP_FAILED;
136 return ret;
139 int munmap( void *addr, size_t len )
141 os2_mmap *mm;
143 for( mm = m_mmap; mm; mm = mm->prev )
145 if( mm->addr == addr )
146 break;
149 if( mm )
152 if( !( mm->flags & MAP_FIXED ))
153 DosFreeMem( addr );
155 if( mm->next )
156 mm->next->prev = mm->prev;
158 if( mm->prev )
159 mm->prev->next = mm->next;
161 if( m_mmap == mm )
162 m_mmap = mm->prev;
164 free( mm );
166 return 0;
169 return -1;
172 int mprotect( void *addr, size_t len, int prot )
174 os2_mmap *mm;
176 for( mm = m_mmap; mm; mm = mm->prev )
178 if( mm->addr == addr )
179 break;
182 if( mm )
184 ULONG fl;
186 fl = 0;
188 if( prot & PROT_READ )
189 fl |= PAG_READ;
191 if( prot & PROT_WRITE )
192 fl |= PAG_WRITE;
194 if( prot & PROT_EXEC )
195 fl |= PAG_EXECUTE;
197 if( prot & PROT_NONE )
198 fl |= PAG_GUARD;
200 if( DosSetMem( addr, len, fl ) == 0 )
201 return 0;
204 return -1;
207 void *mmap_anon( void *addr, size_t len, int prot, int flags, off_t off )
209 return mmap( addr, len, prot, flags | MAP_ANON, -1, off );