4 * Copyright (C) 2013 JP Ikaheimonen <jp_ikaheimonen@mentor.com>
5 * 2016 Michael Jeanson <mjeanson@efficios.com>
7 * These sources are based on ftp://g.oswego.edu/pub/misc/malloc.c
8 * file by Doug Lea, released to the public domain.
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 #include <babeltrace/compat/mman-internal.h>
40 HANDLE file_handle
; /* the duplicated handle */
41 HANDLE map_handle
; /* handle returned by CreateFileMapping */
42 void *start
; /* ptr returned by MapViewOfFile */
46 * This mutex protects the array of memory mappings and its associated
47 * counters. (mmap_infos, mmap_infos_cur mmap_infos_max)
49 static pthread_mutex_t mmap_mutex
= PTHREAD_MUTEX_INITIALIZER
;
51 static int mmap_infos_cur
= 0;
52 static int mmap_infos_max
= 0;
53 static struct mmap_info
*mmap_infos
= NULL
;
55 #define NEW_MMAP_STRUCT_CNT 10
60 if (pthread_mutex_lock(&mmap_mutex
)) {
66 void mmap_unlock(void)
68 if (pthread_mutex_unlock(&mmap_mutex
)) {
74 * Convert mmap memory protection flags to CreateFileMapping page protection
75 * flag and MapViewOfFile desired access flag.
78 DWORD
map_prot_flags(int prot
, DWORD
*dwDesiredAccess
)
80 if (prot
& PROT_READ
) {
81 if (prot
& PROT_WRITE
) {
82 *dwDesiredAccess
= FILE_MAP_WRITE
;
83 if (prot
& PROT_EXEC
) {
84 return PAGE_EXECUTE_READWRITE
;
86 return PAGE_READWRITE
;
88 if (prot
& PROT_EXEC
) {
89 *dwDesiredAccess
= FILE_MAP_EXECUTE
;
90 return PAGE_EXECUTE_READ
;
92 *dwDesiredAccess
= FILE_MAP_READ
;
95 if (prot
& PROT_WRITE
) {
96 *dwDesiredAccess
= FILE_MAP_COPY
;
97 return PAGE_WRITECOPY
;
99 if (prot
& PROT_EXEC
) {
100 *dwDesiredAccess
= FILE_MAP_EXECUTE
;
101 return PAGE_EXECUTE_READ
;
105 *dwDesiredAccess
= 0;
109 void *mmap(void *start
, size_t length
, int prot
, int flags
, int fd
,
112 struct mmap_info mapping
;
113 DWORD dwDesiredAccess
;
117 /* Check for a valid fd */
123 /* we don't support this atm */
124 if (flags
== MAP_FIXED
) {
129 /* Map mmap flags to windows API */
130 flProtect
= map_prot_flags(prot
, &dwDesiredAccess
);
131 if (flProtect
== 0) {
136 /* Get a handle from the fd */
137 handle
= (HANDLE
) _get_osfhandle(fd
);
139 /* Duplicate the handle and store it in 'mapping.file_handle' */
140 if (!DuplicateHandle(GetCurrentProcess(), handle
, GetCurrentProcess(),
141 &mapping
.file_handle
, 0, FALSE
,
142 DUPLICATE_SAME_ACCESS
)) {
147 * Create a file mapping object with a maximum size
148 * of 'offset' + 'length'
150 mapping
.map_handle
= CreateFileMapping(mapping
.file_handle
, NULL
,
151 flProtect
, 0, offset
+ length
, NULL
);
152 if (mapping
.map_handle
== 0) {
153 if (!CloseHandle(mapping
.file_handle
)) {
160 /* Map the requested block starting at 'offset' for 'length' bytes */
161 mapping
.start
= MapViewOfFile(mapping
.map_handle
, dwDesiredAccess
, 0,
163 if (mapping
.start
== 0) {
164 DWORD dwLastErr
= GetLastError();
165 if (!CloseHandle(mapping
.map_handle
)) {
168 if (!CloseHandle(mapping
.file_handle
)) {
172 if (dwLastErr
== ERROR_MAPPED_ALIGNMENT
) {
183 /* If we have never done any mappings, allocate the array */
184 if (mmap_infos
== NULL
) {
185 mmap_infos_max
= NEW_MMAP_STRUCT_CNT
;
186 mmap_infos
= (struct mmap_info
*) calloc(mmap_infos_max
,
187 sizeof(struct mmap_info
));
188 if (mmap_infos
== NULL
) {
190 goto error_mutex_unlock
;
194 /* if we have reached our current mapping limit, expand it */
195 if (mmap_infos_cur
== mmap_infos_max
) {
196 struct mmap_info
*realloc_mmap_infos
= NULL
;
198 mmap_infos_max
+= NEW_MMAP_STRUCT_CNT
;
199 realloc_mmap_infos
= (struct mmap_info
*) realloc(mmap_infos
,
200 mmap_infos_max
* sizeof(struct mmap_info
));
201 if (realloc_mmap_infos
== NULL
) {
202 mmap_infos_max
-= NEW_MMAP_STRUCT_CNT
;
203 goto error_mutex_unlock
;
205 mmap_infos
= realloc_mmap_infos
;
208 /* Add the new mapping to the array */
209 memcpy(&mmap_infos
[mmap_infos_cur
], &mapping
,
210 sizeof(struct mmap_info
));
215 return mapping
.start
;
220 if (!CloseHandle(mapping
.map_handle
)) {
223 if (!CloseHandle(mapping
.file_handle
)) {
231 int munmap(void *start
, size_t length
)
235 /* Find the mapping to unmap */
236 for (i
= 0; i
< mmap_infos_cur
; i
++) {
237 if (mmap_infos
[i
].start
== start
)
241 /* Mapping was not found */
242 if (i
== mmap_infos_cur
) {
247 /* Cleanup of handles should never fail */
248 if (!UnmapViewOfFile(mmap_infos
[i
].start
)) {
251 if (!CloseHandle(mmap_infos
[i
].map_handle
)) {
254 if (!CloseHandle(mmap_infos
[i
].file_handle
)) {
260 /* Clean the mapping list */
261 for (j
= i
+ 1; j
< mmap_infos_cur
; j
++) {
262 memcpy(&mmap_infos
[j
- 1], &mmap_infos
[j
],
263 sizeof(struct mmap_info
));
267 /* If the mapping list is now empty, free it */
268 if (mmap_infos_cur
== 0) {