Port: Add mman.h compat for mingw
authorMichael Jeanson <mjeanson@efficios.com>
Thu, 15 Sep 2016 18:51:45 +0000 (14:51 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 9 Jun 2017 20:58:15 +0000 (16:58 -0400)
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
cli/babeltrace-log.c
compat/Makefile.am
compat/compat_mman.c [new file with mode: 0644]
include/babeltrace/compat/mman-internal.h
include/babeltrace/ctf-writer/serialize-internal.h
include/babeltrace/mmap-align-internal.h
plugins/ctf/fs-src/data-stream-file.c
plugins/ctf/lttng-live/data-stream.c

index 27070fc5e90d441d4c62e1f04360028f04a85eb4..a2d4884c825bc64919456285ef621173b23ce668 100644 (file)
@@ -29,7 +29,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-#include <sys/mman.h>
+#include <babeltrace/compat/mman-internal.h>
 #include <babeltrace/compat/dirent-internal.h>
 #include <stdio.h>
 #include <stdlib.h>
index 33d49241600d7af4c21bc1e90db9749aaa457e8b..bf79838a8a786d6dbf7fd96f2ebda2479f42a74e 100644 (file)
@@ -2,7 +2,12 @@ AM_CFLAGS = $(PACKAGE_CFLAGS) -I$(top_srcdir)/include
 
 noinst_LTLIBRARIES = libcompat.la
 
-libcompat_la_SOURCES = compat_uuid.c
+libcompat_la_SOURCES = compat_uuid.c \
+                       compat_mman.c
 
 libcompat_la_LDFLAGS = \
        $(LD_NO_AS_NEEDED)
+
+if BABELTRACE_BUILD_WITH_MINGW
+libcompat_la_LDFLAGS += -lrpcrt4
+endif
diff --git a/compat/compat_mman.c b/compat/compat_mman.c
new file mode 100644 (file)
index 0000000..813fac4
--- /dev/null
@@ -0,0 +1,279 @@
+/*
+ * compat/compat_mman.h
+ *
+ * Copyright (C) 2013   JP Ikaheimonen <jp_ikaheimonen@mentor.com>
+ *               2016   Michael Jeanson <mjeanson@efficios.com>
+ *
+ * These sources are based on ftp://g.oswego.edu/pub/misc/malloc.c
+ * file by Doug Lea, released to the public domain.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifdef __MINGW32__
+
+#include <assert.h>
+#include <errno.h>
+#include <io.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <windows.h>
+#include <babeltrace/compat/mman-internal.h>
+
+struct mmap_info {
+       HANDLE file_handle; /* the duplicated handle */
+       HANDLE map_handle;  /* handle returned by CreateFileMapping */
+       void *start;        /* ptr returned by MapViewOfFile */
+};
+
+/*
+ * This mutex protects the array of memory mappings and its associated
+ * counters. (mmap_infos, mmap_infos_cur mmap_infos_max)
+ */
+static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static int mmap_infos_cur = 0;
+static int mmap_infos_max = 0;
+static struct mmap_info *mmap_infos = NULL;
+
+#define NEW_MMAP_STRUCT_CNT 10
+
+static
+void mmap_lock(void)
+{
+       if (pthread_mutex_lock(&mmap_mutex)) {
+               assert(0);
+       }
+}
+
+static
+void mmap_unlock(void)
+{
+       if (pthread_mutex_unlock(&mmap_mutex)) {
+               assert(0);
+       }
+}
+
+/*
+ * Convert mmap memory protection flags to CreateFileMapping page protection
+ * flag and MapViewOfFile desired access flag.
+ */
+static
+DWORD map_prot_flags(int prot, DWORD *dwDesiredAccess)
+{
+       if (prot & PROT_READ) {
+               if (prot & PROT_WRITE) {
+                       *dwDesiredAccess = FILE_MAP_WRITE;
+                       if (prot & PROT_EXEC) {
+                               return PAGE_EXECUTE_READWRITE;
+                       }
+                       return PAGE_READWRITE;
+               }
+               if (prot & PROT_EXEC) {
+                       *dwDesiredAccess = FILE_MAP_EXECUTE;
+                       return PAGE_EXECUTE_READ;
+               }
+               *dwDesiredAccess = FILE_MAP_READ;
+               return PAGE_READONLY;
+       }
+       if (prot & PROT_WRITE) {
+               *dwDesiredAccess = FILE_MAP_COPY;
+               return PAGE_WRITECOPY;
+       }
+       if (prot & PROT_EXEC) {
+               *dwDesiredAccess = FILE_MAP_EXECUTE;
+               return PAGE_EXECUTE_READ;
+       }
+
+       /* Mapping failed */
+       *dwDesiredAccess = 0;
+       return 0;
+}
+
+void *mmap(void *start, size_t length, int prot, int flags, int fd,
+               off_t offset)
+{
+       struct mmap_info mapping;
+       DWORD dwDesiredAccess;
+       DWORD flProtect;
+       HANDLE handle;
+
+       /* Check for a valid fd */
+       if (fd == -1) {
+               _set_errno(EBADF);
+               return MAP_FAILED;
+       }
+
+       /* we don't support this atm */
+       if (flags == MAP_FIXED) {
+               _set_errno(ENOTSUP);
+               return MAP_FAILED;
+       }
+
+       /* Map mmap flags to windows API */
+       flProtect = map_prot_flags(prot, &dwDesiredAccess);
+       if (flProtect == 0) {
+               _set_errno(EINVAL);
+               return MAP_FAILED;
+       }
+
+       /* Get a handle from the fd */
+       handle = (HANDLE) _get_osfhandle(fd);
+
+       /* Duplicate the handle and store it in 'mapping.file_handle' */
+       if (!DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(),
+                       &mapping.file_handle, 0, FALSE,
+                       DUPLICATE_SAME_ACCESS)) {
+               return MAP_FAILED;
+       }
+
+       /*
+        * Create a file mapping object with a maximum size
+        * of 'offset' + 'length'
+        */
+       mapping.map_handle = CreateFileMapping(mapping.file_handle, NULL,
+                       flProtect, 0, offset + length, NULL);
+       if (mapping.map_handle == 0) {
+               if (!CloseHandle(mapping.file_handle)) {
+                       assert(0);
+               }
+               _set_errno(EACCES);
+               return MAP_FAILED;
+       }
+
+       /* Map the requested block starting at 'offset' for 'length' bytes */
+       mapping.start = MapViewOfFile(mapping.map_handle, dwDesiredAccess, 0,
+                       offset, length);
+       if (mapping.start == 0) {
+               DWORD dwLastErr = GetLastError();
+               if (!CloseHandle(mapping.map_handle)) {
+                       assert(0);
+               }
+               if (!CloseHandle(mapping.file_handle)) {
+                       assert(0);
+               }
+
+               if (dwLastErr == ERROR_MAPPED_ALIGNMENT) {
+                       _set_errno(EINVAL);
+               } else {
+                       _set_errno(EACCES);
+               }
+
+               return MAP_FAILED;
+       }
+
+       mmap_lock();
+
+       /* If we have never done any mappings, allocate the array */
+       if (mmap_infos == NULL) {
+               mmap_infos_max = NEW_MMAP_STRUCT_CNT;
+               mmap_infos = (struct mmap_info *) calloc(mmap_infos_max,
+                               sizeof(struct mmap_info));
+               if (mmap_infos == NULL) {
+                       mmap_infos_max = 0;
+                       goto error_mutex_unlock;
+               }
+       }
+
+       /* if we have reached our current mapping limit, expand it */
+       if (mmap_infos_cur == mmap_infos_max) {
+               struct mmap_info *realloc_mmap_infos = NULL;
+
+               mmap_infos_max += NEW_MMAP_STRUCT_CNT;
+               realloc_mmap_infos = (struct mmap_info *) realloc(mmap_infos,
+                               mmap_infos_max * sizeof(struct mmap_info));
+               if (realloc_mmap_infos == NULL) {
+                       mmap_infos_max -= NEW_MMAP_STRUCT_CNT;
+                       goto error_mutex_unlock;
+               }
+               mmap_infos = realloc_mmap_infos;
+       }
+
+       /* Add the new mapping to the array */
+       memcpy(&mmap_infos[mmap_infos_cur], &mapping,
+                       sizeof(struct mmap_info));
+       mmap_infos_cur++;
+
+       mmap_unlock();
+
+       return mapping.start;
+
+error_mutex_unlock:
+       mmap_unlock();
+
+       if (!CloseHandle(mapping.map_handle)) {
+               assert(0);
+       }
+       if (!CloseHandle(mapping.file_handle)) {
+               assert(0);
+       }
+
+       _set_errno(ENOMEM);
+       return MAP_FAILED;
+}
+
+int munmap(void *start, size_t length)
+{
+       int i, j;
+
+       /* Find the mapping to unmap */
+       for (i = 0; i < mmap_infos_cur; i++) {
+               if (mmap_infos[i].start == start)
+                       break;
+       }
+
+       /* Mapping was not found */
+       if (i == mmap_infos_cur) {
+               _set_errno(EINVAL);
+               return -1;
+       }
+
+       /* Cleanup of handles should never fail */
+       if (!UnmapViewOfFile(mmap_infos[i].start)) {
+               assert(0);
+       }
+       if (!CloseHandle(mmap_infos[i].map_handle)) {
+               assert(0);
+       }
+       if (!CloseHandle(mmap_infos[i].file_handle)) {
+               assert(0);
+       }
+
+       mmap_lock();
+
+       /* Clean the mapping list */
+       for (j = i + 1; j < mmap_infos_cur; j++) {
+               memcpy(&mmap_infos[j - 1], &mmap_infos[j],
+                               sizeof(struct mmap_info));
+       }
+       mmap_infos_cur--;
+
+       /* If the mapping list is now empty, free it */
+       if (mmap_infos_cur == 0) {
+               free(mmap_infos);
+               mmap_infos = NULL;
+               mmap_infos_max = 0;
+       }
+
+       mmap_unlock();
+
+       return 0;
+}
+
+#endif
index 061e244ba3c1daef1fe720389896088294800f34..6d9fa9260f3ada1bdf0bd33fb3c75bb3e0e40946 100644 (file)
@@ -4,7 +4,7 @@
 /*
  * babeltrace/compat/mman.h
  *
- * Copyright (C) 2015  Michael Jeanson <mjeanson@efficios.com>
+ * Copyright (C) 2015-2016  Michael Jeanson <mjeanson@efficios.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * SOFTWARE.
  */
 
+#ifndef __MINGW32__
+
 #include <sys/mman.h>
 
+#else /* __MINGW32__ */
+
+#include <sys/types.h>
+
+#define PROT_NONE      0x0
+#define PROT_READ      0x1
+#define PROT_WRITE     0x2
+#define PROT_EXEC      0x4
+
+#define MAP_FILE       0
+#define MAP_SHARED     1
+#define MAP_PRIVATE    2
+#define MAP_TYPE       0xF
+#define MAP_FIXED      0x10
+#define MAP_ANONYMOUS  0x20
+#define MAP_ANON       MAP_ANONYMOUS
+#define MAP_FAILED     ((void *) -1)
+
+void *mmap(void *addr, size_t length, int prot, int flags, int fd,
+       off_t offset);
+int munmap(void *addr, size_t length);
+
+#endif /* __MINGW32__ */
+
 #ifndef MAP_ANONYMOUS
 # ifdef MAP_ANON
 #   define MAP_ANONYMOUS MAP_ANON
index e029764e53b2c917b0e664bac31a16f2fd06a426..b0ef6cb9ffa13689aded75c679ac651840b801cf 100644 (file)
@@ -29,7 +29,7 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <limits.h>
-#include <sys/mman.h>
+#include <babeltrace/compat/mman-internal.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
index 0667ab0c8933ad7a8db91c659fd0977c3294ae9e..e87f4a98efc297110700314914654aa3568d128f 100644 (file)
@@ -28,7 +28,7 @@
 #include <babeltrace/align-internal.h>
 #include <stdlib.h>
 #include <stdint.h>
-#include <sys/mman.h>
+#include <babeltrace/compat/mman-internal.h>
 #include <babeltrace/common-internal.h>
 
 /*
@@ -48,6 +48,30 @@ struct mmap_align {
        size_t length;                  /* virtual mmap length */
 };
 
+#ifdef __WIN32__
+#include <windows.h>
+
+/*
+ * On windows the memory mapping offset must be aligned to the memory
+ * allocator allocation granularity and not the page size.
+ */
+static inline
+off_t get_page_aligned_offset(off_t offset, size_t page_size)
+{
+       SYSTEM_INFO sysinfo;
+
+       GetNativeSystemInfo(&sysinfo);
+
+       return ALIGN_FLOOR(offset, sysinfo.dwAllocationGranularity);
+}
+#else
+static inline
+off_t get_page_aligned_offset(off_t offset, size_t page_size)
+{
+       return ALIGN_FLOOR(offset, page_size);
+}
+#endif
+
 static inline
 struct mmap_align *mmap_align(size_t length, int prot,
                int flags, int fd, off_t offset)
@@ -62,7 +86,7 @@ struct mmap_align *mmap_align(size_t length, int prot,
        if (!mma)
                return MAP_FAILED;
        mma->length = length;
-       page_aligned_offset = ALIGN_FLOOR(offset, page_size);
+       page_aligned_offset = get_page_aligned_offset(offset, page_size);
        /*
         * Page aligned length needs to contain the requested range.
         * E.g., for a small range that fits within a single page, we might
@@ -72,7 +96,7 @@ struct mmap_align *mmap_align(size_t length, int prot,
        mma->page_aligned_length = ALIGN(length + offset - page_aligned_offset, page_size);
        mma->page_aligned_addr = mmap(NULL, mma->page_aligned_length,
                prot, flags, fd, page_aligned_offset);
-       if (mma->page_aligned_addr == (void *) -1UL) {
+       if (mma->page_aligned_addr == MAP_FAILED) {
                free(mma);
                return MAP_FAILED;
        }
index 94f2b9b3edfb1c563eaeb4d35801a71b5cd2cf89..4d264203aee2178b5448023546310975d84fed93 100644 (file)
@@ -29,7 +29,7 @@
 #include <stdbool.h>
 #include <glib.h>
 #include <inttypes.h>
-#include <sys/mman.h>
+#include <babeltrace/compat/mman-internal.h>
 #include <babeltrace/ctf-ir/stream.h>
 #include <babeltrace/graph/notification-iterator.h>
 #include <babeltrace/graph/notification-stream.h>
index 5669b614e923847a9866d6a93e01e430a6cabbc8..810c93aac20bd5b41af638d49a94af5a89674989 100644 (file)
@@ -31,7 +31,7 @@
 #include <stdbool.h>
 #include <glib.h>
 #include <inttypes.h>
-#include <sys/mman.h>
+#include <babeltrace/compat/mman-internal.h>
 #include <babeltrace/ctf-ir/stream.h>
 #include "../common/notif-iter/notif-iter.h"
 #include <assert.h>
This page took 0.031411 seconds and 4 git commands to generate.