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
29 #define BT_LOG_TAG "COMPAT-MMAN"
39 #include <babeltrace/compat/mman-internal.h>
42 /* The duplicated handle. */
44 /* Handle returned by CreateFileMapping. */
49 GHashTable
*mmap_mappings
= NULL
;
52 * This mutex protects the hashtable of memory mappings.
54 static pthread_mutex_t mmap_mutex
= PTHREAD_MUTEX_INITIALIZER
;
57 struct mmap_mapping
*mapping_create(void)
59 struct mmap_mapping
*mapping
;
61 mapping
= malloc(sizeof(struct mmap_mapping
));
62 if (mapping
!= NULL
) {
63 mapping
->file_handle
= NULL
;
64 mapping
->map_handle
= NULL
;
71 void mapping_clean(struct mmap_mapping
*mapping
)
74 if (!CloseHandle(mapping
->map_handle
)) {
75 BT_LOGF_STR("Failed to close mmap map_handle.");
78 if (!CloseHandle(mapping
->file_handle
)) {
79 BT_LOGF_STR("Failed to close mmap file_handle.");
88 void addr_clean(void *addr
)
90 /* Cleanup of handles should never fail. */
91 if (!UnmapViewOfFile(addr
)) {
92 BT_LOGF_STR("Failed to unmap mmap mapping.");
100 if (pthread_mutex_lock(&mmap_mutex
)) {
101 BT_LOGF_STR("Failed to acquire mmap_mutex.");
107 void mmap_unlock(void)
109 if (pthread_mutex_unlock(&mmap_mutex
)) {
110 BT_LOGF_STR("Failed to release mmap_mutex.");
116 * Convert mmap memory protection flags to CreateFileMapping page protection
117 * flag and MapViewOfFile desired access flag.
120 DWORD
map_prot_flags(int prot
, DWORD
*dwDesiredAccess
)
122 if (prot
& PROT_READ
) {
123 if (prot
& PROT_WRITE
) {
124 *dwDesiredAccess
= FILE_MAP_WRITE
;
125 if (prot
& PROT_EXEC
) {
126 return PAGE_EXECUTE_READWRITE
;
128 return PAGE_READWRITE
;
130 if (prot
& PROT_EXEC
) {
131 *dwDesiredAccess
= FILE_MAP_EXECUTE
;
132 return PAGE_EXECUTE_READ
;
134 *dwDesiredAccess
= FILE_MAP_READ
;
135 return PAGE_READONLY
;
137 if (prot
& PROT_WRITE
) {
138 *dwDesiredAccess
= FILE_MAP_COPY
;
139 return PAGE_WRITECOPY
;
141 if (prot
& PROT_EXEC
) {
142 *dwDesiredAccess
= FILE_MAP_EXECUTE
;
143 return PAGE_EXECUTE_READ
;
146 /* Mapping failed. */
147 *dwDesiredAccess
= 0;
152 void *bt_mmap(void *addr
, size_t length
, int prot
, int flags
, int fd
,
155 struct mmap_mapping
*mapping
= NULL
;
157 DWORD dwDesiredAccess
;
161 /* Check for a valid fd. */
167 /* We don't support this at the moment. */
168 if (flags
== MAP_FIXED
) {
173 /* Map mmap flags to those of the Windows API. */
174 flProtect
= map_prot_flags(prot
, &dwDesiredAccess
);
175 if (flProtect
== 0) {
180 /* Allocate the mapping struct. */
181 mapping
= mapping_create();
183 BT_LOGE_STR("Failed to allocate mmap mapping.");
188 /* Get a handle from the fd. */
189 handle
= (HANDLE
) _get_osfhandle(fd
);
191 /* Duplicate the handle and store it in 'mapping.file_handle'. */
192 if (!DuplicateHandle(GetCurrentProcess(), handle
, GetCurrentProcess(),
193 &mapping
->file_handle
, 0, FALSE
,
194 DUPLICATE_SAME_ACCESS
)) {
200 * Create a file mapping object with a maximum size
201 * of 'offset' + 'length'.
203 mapping
->map_handle
= CreateFileMapping(mapping
->file_handle
, NULL
,
204 flProtect
, 0, offset
+ length
, NULL
);
205 if (mapping
->map_handle
== 0) {
210 /* Map the requested block starting at 'offset' for 'length' bytes. */
211 mapping_addr
= MapViewOfFile(mapping
->map_handle
, dwDesiredAccess
, 0,
213 if (mapping_addr
== 0) {
214 DWORD dwLastErr
= GetLastError();
215 if (dwLastErr
== ERROR_MAPPED_ALIGNMENT
) {
225 /* If we have never done any mappings, allocate the hashtable. */
226 if (!mmap_mappings
) {
227 mmap_mappings
= g_hash_table_new_full(g_direct_hash
,
228 g_direct_equal
, (GDestroyNotify
) addr_clean
,
229 (GDestroyNotify
) mapping_clean
);
230 if (!mmap_mappings
) {
231 BT_LOGE_STR("Failed to allocate mmap hashtable.");
233 goto error_mutex_unlock
;
237 /* Add the new mapping to the hashtable. */
238 if (!g_hash_table_insert(mmap_mappings
, mapping_addr
, mapping
)) {
239 BT_LOGF_STR("Failed to insert mapping in the hashtable.");
250 mapping_clean(mapping
);
255 int bt_munmap(void *addr
, size_t length
)
261 /* Check if the mapping exists in the hashtable. */
262 if (g_hash_table_lookup(mmap_mappings
, addr
) == NULL
) {
269 if (!g_hash_table_remove(mmap_mappings
, addr
)) {
270 BT_LOGF_STR("Failed to remove mapping from hashtable.");