lib: iterator auto-seeking: handle intersecting discarded items messages
[babeltrace.git] / include / babeltrace / mmap-align-internal.h
index ccb0e1f44039a2d7dd21375e584b45a083aa7070..bef6378762bef0fc0a59ee2b38324eae8f25df15 100644 (file)
@@ -2,8 +2,6 @@
 #define _BABELTRACE_MMAP_ALIGN_H
 
 /*
- * BabelTrace mmap-align.h - mmap alignment header
- *
  * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -27,7 +25,9 @@
 
 #include <babeltrace/align-internal.h>
 #include <stdlib.h>
-#include <sys/mman.h>
+#include <stdint.h>
+#include <babeltrace/compat/mman-internal.h>
+#include <babeltrace/common-internal.h>
 
 /*
  * This header implements a wrapper over mmap (mmap_align) that memory
@@ -46,32 +46,59 @@ 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)
 {
        struct mmap_align *mma;
        off_t page_aligned_offset;      /* mmap offset, aligned to floor */
+       size_t page_size;
+
+       page_size = bt_common_get_page_size();
 
        mma = malloc(sizeof(*mma));
        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
         * require a 2 pages page_aligned_length if the range crosses a page
         * boundary.
         */
-       mma->page_aligned_length = ALIGN(length + offset - page_aligned_offset, PAGE_SIZE);
-       mma->page_aligned_addr = mmap(NULL, mma->page_aligned_length,
+       mma->page_aligned_length = ALIGN(length + offset - page_aligned_offset, page_size);
+       mma->page_aligned_addr = bt_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;
        }
-       mma->addr = mma->page_aligned_addr + (offset - page_aligned_offset);
+       mma->addr = ((uint8_t *) mma->page_aligned_addr) + (offset - page_aligned_offset);
        return mma;
 }
 
@@ -84,7 +111,7 @@ int munmap_align(struct mmap_align *mma)
        page_aligned_addr = mma->page_aligned_addr;
        page_aligned_length = mma->page_aligned_length;
        free(mma);
-       return munmap(page_aligned_addr, page_aligned_length);
+       return bt_munmap(page_aligned_addr, page_aligned_length);
 }
 
 static inline
This page took 0.02555 seconds and 4 git commands to generate.