1 | #pragma once |
2 | |
3 | #include <cstddef> |
4 | #include <sys/types.h> |
5 | #if !defined(_MSC_VER) |
6 | #include <sys/mman.h> |
7 | #endif |
8 | |
9 | |
10 | #ifdef MREMAP_MAYMOVE |
11 | #define HAS_MREMAP 1 |
12 | #else |
13 | #define HAS_MREMAP 0 |
14 | #endif |
15 | |
16 | |
17 | /// You can forcely disable mremap by defining DISABLE_MREMAP to 1 before including this file. |
18 | #ifndef DISABLE_MREMAP |
19 | #if HAS_MREMAP |
20 | #define DISABLE_MREMAP 0 |
21 | #else |
22 | #define DISABLE_MREMAP 1 |
23 | #endif |
24 | #endif |
25 | |
26 | |
27 | /// Implement mremap with mmap/memcpy/munmap. |
28 | void * mremap_fallback( |
29 | void * old_address, |
30 | size_t old_size, |
31 | size_t new_size, |
32 | int flags, |
33 | int mmap_prot, |
34 | int mmap_flags, |
35 | int mmap_fd, |
36 | off_t mmap_offset); |
37 | |
38 | |
39 | #if !HAS_MREMAP |
40 | #define MREMAP_MAYMOVE 1 |
41 | |
42 | inline void * mremap( |
43 | void * old_address, |
44 | size_t old_size, |
45 | size_t new_size, |
46 | int flags = 0, |
47 | int mmap_prot = 0, |
48 | int mmap_flags = 0, |
49 | int mmap_fd = -1, |
50 | off_t mmap_offset = 0) |
51 | { |
52 | return mremap_fallback(old_address, old_size, new_size, flags, mmap_prot, mmap_flags, mmap_fd, mmap_offset); |
53 | } |
54 | #endif |
55 | |
56 | |
57 | inline void * clickhouse_mremap( |
58 | void * old_address, |
59 | size_t old_size, |
60 | size_t new_size, |
61 | int flags = 0, |
62 | [[maybe_unused]] int mmap_prot = 0, |
63 | [[maybe_unused]] int mmap_flags = 0, |
64 | [[maybe_unused]] int mmap_fd = -1, |
65 | [[maybe_unused]] off_t mmap_offset = 0) |
66 | { |
67 | #if DISABLE_MREMAP |
68 | return mremap_fallback(old_address, old_size, new_size, flags, mmap_prot, mmap_flags, mmap_fd, mmap_offset); |
69 | #else |
70 | |
71 | return mremap( |
72 | old_address, |
73 | old_size, |
74 | new_size, |
75 | flags |
76 | #if !defined(MREMAP_FIXED) |
77 | , |
78 | mmap_prot, |
79 | mmap_flags, |
80 | mmap_fd, |
81 | mmap_offset |
82 | #endif |
83 | ); |
84 | #endif |
85 | } |
86 | |