Move to kernel style SPDX license identifiers
[babeltrace.git] / src / common / mmap-align.h
... / ...
CommitLineData
1/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
7#ifndef _BABELTRACE_MMAP_ALIGN_H
8#define _BABELTRACE_MMAP_ALIGN_H
9
10#include "common/align.h"
11#include <stdlib.h>
12#include <stdint.h>
13#include "compat/mman.h"
14#include "common/common.h"
15
16/*
17 * This header implements a wrapper over mmap (mmap_align) that memory
18 * maps a file region that is not necessarily multiple of the page size.
19 * It returns a structure (instead of a pointer) that contains the mmap
20 * pointer (page-aligned) and a pointer to the offset requested within
21 * that page. Note: in the current implementation, the "addr" parameter
22 * cannot be forced, so we allocate at an address chosen by the OS.
23 */
24
25struct mmap_align {
26 void *page_aligned_addr; /* mmap address, aligned to floor */
27 size_t page_aligned_length; /* mmap length, containing range */
28
29 void *addr; /* virtual mmap address */
30 size_t length; /* virtual mmap length */
31};
32
33static inline
34off_t get_page_aligned_offset(off_t offset, int log_level)
35{
36 return ALIGN_FLOOR(offset, bt_mmap_get_offset_align_size(log_level));
37}
38
39static inline
40struct mmap_align *mmap_align(size_t length, int prot,
41 int flags, int fd, off_t offset, int log_level)
42{
43 struct mmap_align *mma;
44 off_t page_aligned_offset; /* mmap offset, aligned to floor */
45 size_t page_size;
46
47 page_size = bt_common_get_page_size(log_level);
48
49 mma = malloc(sizeof(*mma));
50 if (!mma)
51 return MAP_FAILED;
52 mma->length = length;
53 page_aligned_offset = get_page_aligned_offset(offset, log_level);
54 /*
55 * Page aligned length needs to contain the requested range.
56 * E.g., for a small range that fits within a single page, we might
57 * require a 2 pages page_aligned_length if the range crosses a page
58 * boundary.
59 */
60 mma->page_aligned_length = ALIGN(length + offset - page_aligned_offset, page_size);
61 mma->page_aligned_addr = bt_mmap(NULL, mma->page_aligned_length,
62 prot, flags, fd, page_aligned_offset, log_level);
63 if (mma->page_aligned_addr == MAP_FAILED) {
64 free(mma);
65 return MAP_FAILED;
66 }
67 mma->addr = ((uint8_t *) mma->page_aligned_addr) + (offset - page_aligned_offset);
68 return mma;
69}
70
71static inline
72int munmap_align(struct mmap_align *mma)
73{
74 void *page_aligned_addr;
75 size_t page_aligned_length;
76
77 page_aligned_addr = mma->page_aligned_addr;
78 page_aligned_length = mma->page_aligned_length;
79 free(mma);
80 return bt_munmap(page_aligned_addr, page_aligned_length);
81}
82
83static inline
84void *mmap_align_addr(struct mmap_align *mma)
85{
86 return mma->addr;
87}
88
89/*
90 * Helper for special-cases, normally unused.
91 */
92static inline
93void mmap_align_set_addr(struct mmap_align *mma, void *addr)
94{
95 mma->addr = addr;
96}
97
98#endif /* _BABELTRACE_MMAP_ALIGN_H */
This page took 0.022866 seconds and 4 git commands to generate.