Mercurial > mplayer.hg
annotate osdep/mmap-os2.c @ 36532:2398517e5bda
film: Fix memory leaks in error cases.
author | reimar |
---|---|
date | Sun, 19 Jan 2014 11:30:05 +0000 |
parents | c70109fc98b2 |
children |
rev | line source |
---|---|
26076 | 1 /* |
2 * very simple implementation of mmap() for OS/2 | |
3 * | |
4 * Copyright (c) 2008 KO Myung-Hun (komh@chollian.net) | |
5 * | |
6 * This file is part of MPlayer. | |
7 * | |
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. | |
12 * | |
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. | |
17 * | |
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. | |
21 */ | |
22 | |
23 #define INCL_DOS | |
24 #include <os2.h> | |
25 | |
26 #include <stdio.h> | |
27 #include <stdlib.h> | |
28 #include <io.h> | |
32675
c70109fc98b2
Add/remove a few standard header #includes in our libc function replacements.
diego
parents:
30702
diff
changeset
|
29 #include <unistd.h> |
26076 | 30 #include <sys/types.h> |
31 | |
32 #include "config.h" | |
33 #include "mmap.h" | |
34 #include "mmap_anon.h" | |
35 | |
36 typedef struct os2_mmap_s | |
37 { | |
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; | |
45 | |
46 void *mmap( void *addr, size_t len, int prot, int flags, int fildes, off_t off ) | |
47 { | |
48 os2_mmap *new_mmap; | |
49 | |
50 ULONG fl; | |
51 ULONG rc; | |
52 | |
53 void *ret; | |
54 | |
55 if( prot & PROT_WRITE ) | |
56 { | |
57 if( flags & MAP_SHARED ) | |
58 return MAP_FAILED; | |
59 | |
60 if( !( flags & MAP_PRIVATE )) | |
61 return MAP_FAILED; | |
62 } | |
63 | |
64 if( flags & MAP_FIXED ) | |
65 { | |
66 ULONG cb; | |
67 | |
68 cb = len; | |
69 rc = DosQueryMem( addr, &cb, &fl ); | |
70 if( rc || ( cb < len )) | |
71 return MAP_FAILED; | |
72 | |
73 rc = DosSetMem( addr, len, fPERM ); | |
74 if( rc ) | |
75 return MAP_FAILED; | |
76 | |
77 ret = addr; | |
78 } | |
79 else | |
80 { | |
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; | |
86 } | |
87 | |
30702 | 88 new_mmap = malloc( sizeof( os2_mmap )); |
26076 | 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; | |
94 | |
95 if( m_mmap ) | |
96 m_mmap->next = new_mmap; | |
97 m_mmap = new_mmap; | |
98 | |
99 if( !( flags & MAP_ANON )) | |
100 { | |
101 int pos; | |
102 | |
103 /* Now read in the file */ | |
104 if(( pos = lseek( fildes, off, SEEK_SET )) == -1) | |
105 { | |
106 munmap( ret, len ); | |
107 | |
108 return MAP_FAILED; | |
109 } | |
110 | |
111 read( fildes, ret, len ); | |
112 lseek( fildes, pos, SEEK_SET ); /* Restore the file pointer */ | |
113 } | |
114 | |
115 fl = 0; | |
116 | |
117 if( prot & PROT_READ ) | |
118 fl |= PAG_READ; | |
119 | |
120 if( prot & PROT_WRITE ) | |
121 fl |= PAG_WRITE; | |
122 | |
123 if( prot & PROT_EXEC ) | |
124 fl |= PAG_EXECUTE; | |
125 | |
126 if( prot & PROT_NONE ) | |
127 fl |= PAG_GUARD; | |
128 | |
129 rc = DosSetMem( ret, len, fl ); | |
130 if( rc ) | |
131 { | |
132 munmap( ret, len ); | |
133 | |
134 return MAP_FAILED; | |
135 } | |
136 | |
137 return ret; | |
138 } | |
139 | |
140 int munmap( void *addr, size_t len ) | |
141 { | |
142 os2_mmap *mm; | |
143 | |
144 for( mm = m_mmap; mm; mm = mm->prev ) | |
145 { | |
146 if( mm->addr == addr ) | |
147 break; | |
148 } | |
149 | |
150 if( mm ) | |
151 { | |
152 | |
153 if( !( mm->flags & MAP_FIXED )) | |
154 DosFreeMem( addr ); | |
155 | |
156 if( mm->next ) | |
157 mm->next->prev = mm->prev; | |
158 | |
159 if( mm->prev ) | |
160 mm->prev->next = mm->next; | |
161 | |
162 if( m_mmap == mm ) | |
163 m_mmap = mm->prev; | |
164 | |
165 free( mm ); | |
166 | |
167 return 0; | |
168 } | |
169 | |
170 return -1; | |
171 } | |
172 | |
173 int mprotect( void *addr, size_t len, int prot ) | |
174 { | |
175 os2_mmap *mm; | |
176 | |
177 for( mm = m_mmap; mm; mm = mm->prev ) | |
178 { | |
179 if( mm->addr == addr ) | |
180 break; | |
181 } | |
182 | |
183 if( mm ) | |
184 { | |
185 ULONG fl; | |
186 | |
187 fl = 0; | |
188 | |
189 if( prot & PROT_READ ) | |
190 fl |= PAG_READ; | |
191 | |
192 if( prot & PROT_WRITE ) | |
193 fl |= PAG_WRITE; | |
194 | |
195 if( prot & PROT_EXEC ) | |
196 fl |= PAG_EXECUTE; | |
197 | |
198 if( prot & PROT_NONE ) | |
199 fl |= PAG_GUARD; | |
200 | |
201 if( DosSetMem( addr, len, fl ) == 0 ) | |
202 return 0; | |
203 } | |
204 | |
205 return -1; | |
206 } | |
207 | |
208 void *mmap_anon( void *addr, size_t len, int prot, int flags, off_t off ) | |
209 { | |
210 return mmap( addr, len, prot, flags | MAP_ANON, -1, off ); | |
211 } |