| 1 | #ifndef _BABELTRACE_MMAP_ALIGN_H |
| 2 | #define _BABELTRACE_MMAP_ALIGN_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include <babeltrace/align-internal.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <stdint.h> |
| 29 | #include <babeltrace/compat/mman-internal.h> |
| 30 | #include <babeltrace/common-internal.h> |
| 31 | |
| 32 | /* |
| 33 | * This header implements a wrapper over mmap (mmap_align) that memory |
| 34 | * maps a file region that is not necessarily multiple of the page size. |
| 35 | * It returns a structure (instead of a pointer) that contains the mmap |
| 36 | * pointer (page-aligned) and a pointer to the offset requested within |
| 37 | * that page. Note: in the current implementation, the "addr" parameter |
| 38 | * cannot be forced, so we allocate at an address chosen by the OS. |
| 39 | */ |
| 40 | |
| 41 | struct mmap_align { |
| 42 | void *page_aligned_addr; /* mmap address, aligned to floor */ |
| 43 | size_t page_aligned_length; /* mmap length, containing range */ |
| 44 | |
| 45 | void *addr; /* virtual mmap address */ |
| 46 | size_t length; /* virtual mmap length */ |
| 47 | }; |
| 48 | |
| 49 | #ifdef __WIN32__ |
| 50 | #include <windows.h> |
| 51 | |
| 52 | /* |
| 53 | * On windows the memory mapping offset must be aligned to the memory |
| 54 | * allocator allocation granularity and not the page size. |
| 55 | */ |
| 56 | static inline |
| 57 | off_t get_page_aligned_offset(off_t offset, size_t page_size) |
| 58 | { |
| 59 | SYSTEM_INFO sysinfo; |
| 60 | |
| 61 | GetNativeSystemInfo(&sysinfo); |
| 62 | |
| 63 | return ALIGN_FLOOR(offset, sysinfo.dwAllocationGranularity); |
| 64 | } |
| 65 | #else |
| 66 | static inline |
| 67 | off_t get_page_aligned_offset(off_t offset, size_t page_size) |
| 68 | { |
| 69 | return ALIGN_FLOOR(offset, page_size); |
| 70 | } |
| 71 | #endif |
| 72 | |
| 73 | static inline |
| 74 | struct mmap_align *mmap_align(size_t length, int prot, |
| 75 | int flags, int fd, off_t offset) |
| 76 | { |
| 77 | struct mmap_align *mma; |
| 78 | off_t page_aligned_offset; /* mmap offset, aligned to floor */ |
| 79 | size_t page_size; |
| 80 | |
| 81 | page_size = bt_common_get_page_size(); |
| 82 | |
| 83 | mma = malloc(sizeof(*mma)); |
| 84 | if (!mma) |
| 85 | return MAP_FAILED; |
| 86 | mma->length = length; |
| 87 | page_aligned_offset = get_page_aligned_offset(offset, page_size); |
| 88 | /* |
| 89 | * Page aligned length needs to contain the requested range. |
| 90 | * E.g., for a small range that fits within a single page, we might |
| 91 | * require a 2 pages page_aligned_length if the range crosses a page |
| 92 | * boundary. |
| 93 | */ |
| 94 | mma->page_aligned_length = ALIGN(length + offset - page_aligned_offset, page_size); |
| 95 | mma->page_aligned_addr = bt_mmap(NULL, mma->page_aligned_length, |
| 96 | prot, flags, fd, page_aligned_offset); |
| 97 | if (mma->page_aligned_addr == MAP_FAILED) { |
| 98 | free(mma); |
| 99 | return MAP_FAILED; |
| 100 | } |
| 101 | mma->addr = ((uint8_t *) mma->page_aligned_addr) + (offset - page_aligned_offset); |
| 102 | return mma; |
| 103 | } |
| 104 | |
| 105 | static inline |
| 106 | int munmap_align(struct mmap_align *mma) |
| 107 | { |
| 108 | void *page_aligned_addr; |
| 109 | size_t page_aligned_length; |
| 110 | |
| 111 | page_aligned_addr = mma->page_aligned_addr; |
| 112 | page_aligned_length = mma->page_aligned_length; |
| 113 | free(mma); |
| 114 | return bt_munmap(page_aligned_addr, page_aligned_length); |
| 115 | } |
| 116 | |
| 117 | static inline |
| 118 | void *mmap_align_addr(struct mmap_align *mma) |
| 119 | { |
| 120 | return mma->addr; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Helper for special-cases, normally unused. |
| 125 | */ |
| 126 | static inline |
| 127 | void mmap_align_set_addr(struct mmap_align *mma, void *addr) |
| 128 | { |
| 129 | mma->addr = addr; |
| 130 | } |
| 131 | |
| 132 | #endif /* _BABELTRACE_MMAP_ALIGN_H */ |