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.
29 #include <sys/types.h>
33 #include "mmap_anon.h"
35 typedef struct os2_mmap_s
40 struct os2_mmap_s
*prev
;
41 struct os2_mmap_s
*next
;
43 static os2_mmap
*m_mmap
= NULL
;
45 void *mmap( void *addr
, size_t len
, int prot
, int flags
, int fildes
, off_t off
)
54 if( prot
& PROT_WRITE
)
56 if( flags
& MAP_SHARED
)
59 if( !( flags
& MAP_PRIVATE
))
63 if( flags
& MAP_FIXED
)
68 rc
= DosQueryMem( addr
, &cb
, &fl
);
69 if( rc
|| ( cb
< len
))
72 rc
= DosSetMem( addr
, len
, fPERM
);
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
);
87 new_mmap
= malloc( sizeof( os2_mmap
));
90 new_mmap
->flags
= flags
;
91 new_mmap
->prev
= m_mmap
;
92 new_mmap
->next
= NULL
;
95 m_mmap
->next
= new_mmap
;
98 if( !( flags
& MAP_ANON
))
102 /* Now read in the file */
103 if(( pos
= lseek( fildes
, off
, SEEK_SET
)) == -1)
110 read( fildes
, ret
, len
);
111 lseek( fildes
, pos
, SEEK_SET
); /* Restore the file pointer */
116 if( prot
& PROT_READ
)
119 if( prot
& PROT_WRITE
)
122 if( prot
& PROT_EXEC
)
125 if( prot
& PROT_NONE
)
128 rc
= DosSetMem( ret
, len
, fl
);
139 int munmap( void *addr
, size_t len
)
143 for( mm
= m_mmap
; mm
; mm
= mm
->prev
)
145 if( mm
->addr
== addr
)
152 if( !( mm
->flags
& MAP_FIXED
))
156 mm
->next
->prev
= mm
->prev
;
159 mm
->prev
->next
= mm
->next
;
172 int mprotect( void *addr
, size_t len
, int prot
)
176 for( mm
= m_mmap
; mm
; mm
= mm
->prev
)
178 if( mm
->addr
== addr
)
188 if( prot
& PROT_READ
)
191 if( prot
& PROT_WRITE
)
194 if( prot
& PROT_EXEC
)
197 if( prot
& PROT_NONE
)
200 if( DosSetMem( addr
, len
, fl
) == 0 )
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
);