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.
30 #include <sys/types.h>
34 #include "mmap_anon.h"
36 typedef struct os2_mmap_s
41 struct os2_mmap_s
*prev
;
42 struct os2_mmap_s
*next
;
44 static os2_mmap
*m_mmap
= NULL
;
46 void *mmap( void *addr
, size_t len
, int prot
, int flags
, int fildes
, off_t off
)
55 if( prot
& PROT_WRITE
)
57 if( flags
& MAP_SHARED
)
60 if( !( flags
& MAP_PRIVATE
))
64 if( flags
& MAP_FIXED
)
69 rc
= DosQueryMem( addr
, &cb
, &fl
);
70 if( rc
|| ( cb
< len
))
73 rc
= DosSetMem( addr
, len
, fPERM
);
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
);
88 new_mmap
= malloc( sizeof( os2_mmap
));
91 new_mmap
->flags
= flags
;
92 new_mmap
->prev
= m_mmap
;
93 new_mmap
->next
= NULL
;
96 m_mmap
->next
= new_mmap
;
99 if( !( flags
& MAP_ANON
))
103 /* Now read in the file */
104 if(( pos
= lseek( fildes
, off
, SEEK_SET
)) == -1)
111 read( fildes
, ret
, len
);
112 lseek( fildes
, pos
, SEEK_SET
); /* Restore the file pointer */
117 if( prot
& PROT_READ
)
120 if( prot
& PROT_WRITE
)
123 if( prot
& PROT_EXEC
)
126 if( prot
& PROT_NONE
)
129 rc
= DosSetMem( ret
, len
, fl
);
140 int munmap( void *addr
, size_t len
)
144 for( mm
= m_mmap
; mm
; mm
= mm
->prev
)
146 if( mm
->addr
== addr
)
153 if( !( mm
->flags
& MAP_FIXED
))
157 mm
->next
->prev
= mm
->prev
;
160 mm
->prev
->next
= mm
->next
;
173 int mprotect( void *addr
, size_t len
, int prot
)
177 for( mm
= m_mmap
; mm
; mm
= mm
->prev
)
179 if( mm
->addr
== addr
)
189 if( prot
& PROT_READ
)
192 if( prot
& PROT_WRITE
)
195 if( prot
& PROT_EXEC
)
198 if( prot
& PROT_NONE
)
201 if( DosSetMem( addr
, len
, fl
) == 0 )
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
);