Skip to content

Commit df7ef79

Browse files
committed
Extract common sources for Linux and MacOSX
Signed-off-by: Lukasz Dorau <lukasz.dorau@intel.com>
1 parent d526d7f commit df7ef79

File tree

4 files changed

+186
-148
lines changed

4 files changed

+186
-148
lines changed

src/CMakeLists.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,36 @@ set(UMF_SOURCES
6666

6767
set(UMF_SOURCES_LINUX libumf_linux.c)
6868

69+
set(UMF_SOURCES_MACOSX libumf_linux.c)
70+
6971
set(UMF_SOURCES_WINDOWS libumf_windows.c)
7072

7173
# Compile definitions for UMF library.
7274
#
7375
# TODO: Cleanup the compile definitions across all the CMake files
7476
set(UMF_PRIVATE_COMPILE_DEFINITIONS "")
7577

76-
set(UMF_SOURCES_LINUX
77-
${UMF_SOURCES_LINUX}
78+
set(UMF_SOURCES_COMMON_LINUX_MACOSX
7879
provider/provider_os_memory.c
79-
provider/provider_os_memory_linux.c
80+
provider/provider_os_memory_posix.c
8081
memory_targets/memory_target_numa.c
8182
memspaces/memspace_numa.c
8283
memspaces/memspace_host_all.c
8384
memspaces/memspace_highest_capacity.c)
8485

86+
set(UMF_SOURCES_LINUX ${UMF_SOURCES_LINUX} ${UMF_SOURCES_COMMON_LINUX_MACOSX}
87+
provider/provider_os_memory_linux.c)
88+
89+
set(UMF_SOURCES_MACOSX ${UMF_SOURCES_MACOSX} ${UMF_SOURCES_COMMON_LINUX_MACOSX}
90+
provider/provider_os_memory_macosx.c)
91+
8592
set(UMF_SOURCES_WINDOWS ${UMF_SOURCES_WINDOWS} provider/provider_os_memory.c
8693
provider/provider_os_memory_windows.c)
8794

8895
set(UMF_LIBS ${UMF_LIBS} ${LIBHWLOC_LIBRARIES})
8996
set(UMF_PRIVATE_LIBRARY_DIRS ${UMF_PRIVATE_LIBRARY_DIRS}
9097
${LIBHWLOC_LIBRARY_DIRS})
9198

92-
set(UMF_SOURCES_MACOSX ${UMF_SOURCES_MACOSX} ${UMF_SOURCES_LINUX})
93-
9499
if(LINUX)
95100
set(UMF_SOURCES ${UMF_SOURCES} ${UMF_SOURCES_LINUX})
96101
elseif(WINDOWS)
Lines changed: 2 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
/*
2-
* Copyright (C) 2023 Intel Corporation
2+
* Copyright (C) 2023-2024 Intel Corporation
33
*
44
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
*/
77

8-
#include <assert.h>
98
#include <errno.h>
10-
#include <limits.h>
11-
#include <string.h>
129
#include <sys/mman.h>
1310
#include <sys/syscall.h>
1411
#include <unistd.h>
@@ -18,49 +15,19 @@
1815
#include "provider_os_memory_internal.h"
1916
#include "utils_log.h"
2017

21-
// maximum value of the off_t type
22-
#define OFF_T_MAX \
23-
(sizeof(off_t) == sizeof(long long) \
24-
? LLONG_MAX \
25-
: (sizeof(off_t) == sizeof(long) ? LONG_MAX : INT_MAX))
26-
27-
umf_result_t os_translate_mem_protection_one_flag(unsigned in_protection,
28-
unsigned *out_protection) {
29-
switch (in_protection) {
30-
case UMF_PROTECTION_NONE:
31-
*out_protection = PROT_NONE;
32-
return UMF_RESULT_SUCCESS;
33-
case UMF_PROTECTION_READ:
34-
*out_protection = PROT_READ;
35-
return UMF_RESULT_SUCCESS;
36-
case UMF_PROTECTION_WRITE:
37-
*out_protection = PROT_WRITE;
38-
return UMF_RESULT_SUCCESS;
39-
case UMF_PROTECTION_EXEC:
40-
*out_protection = PROT_EXEC;
41-
return UMF_RESULT_SUCCESS;
42-
}
43-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
44-
}
45-
4618
umf_result_t os_translate_mem_visibility_flag(umf_memory_visibility_t in_flag,
4719
unsigned *out_flag) {
4820
switch (in_flag) {
4921
case UMF_MEM_MAP_PRIVATE:
5022
*out_flag = MAP_PRIVATE;
5123
return UMF_RESULT_SUCCESS;
5224
case UMF_MEM_MAP_SHARED:
53-
#ifdef __APPLE__
54-
return UMF_RESULT_ERROR_NOT_SUPPORTED; // not supported on MacOSX
55-
#else
5625
*out_flag = MAP_SHARED;
5726
return UMF_RESULT_SUCCESS;
58-
#endif
5927
}
6028
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
6129
}
6230

63-
#ifndef __APPLE__
6431
static int syscall_memfd_secret(void) {
6532
int fd = -1;
6633
#ifdef __NR_memfd_secret
@@ -90,14 +57,9 @@ static int syscall_memfd_create(void) {
9057
#endif /* __NR_memfd_create */
9158
return fd;
9259
}
93-
#endif /* __APPLE__ */
9460

9561
// create an anonymous file descriptor
9662
int os_create_anonymous_fd(unsigned translated_memory_flag) {
97-
#ifdef __APPLE__
98-
(void)translated_memory_flag; // unused
99-
return 0; // ignored on MacOSX
100-
#else /* !__APPLE__ */
10163
// fd is created only for MAP_SHARED
10264
if (translated_memory_flag != MAP_SHARED) {
10365
return 0;
@@ -118,123 +80,20 @@ int os_create_anonymous_fd(unsigned translated_memory_flag) {
11880
fd = syscall_memfd_create();
11981

12082
#if !(defined __NR_memfd_secret) && !(defined __NR_memfd_create)
121-
if (fd == = 1) {
83+
if (fd == -1) {
12284
LOG_ERR("cannot create an anonymous file descriptor - neither "
12385
"memfd_secret() nor memfd_create() are defined");
12486
}
12587
#endif /* !(defined __NR_memfd_secret) && !(defined __NR_memfd_create) */
12688

12789
return fd;
128-
129-
#endif /* !__APPLE__ */
13090
}
13191

132-
size_t get_max_file_size(void) { return OFF_T_MAX; }
133-
13492
int os_set_file_size(int fd, size_t size) {
135-
#ifdef __APPLE__
136-
(void)fd; // unused
137-
(void)size; // unused
138-
return 0; // ignored on MacOSX
139-
#else
14093
errno = 0;
14194
int ret = ftruncate(fd, size);
14295
if (ret) {
14396
LOG_PERR("ftruncate(%i, %zu) failed", fd, size);
14497
}
14598
return ret;
146-
#endif /* __APPLE__ */
147-
}
148-
149-
umf_result_t os_translate_mem_protection_flags(unsigned in_protection,
150-
unsigned *out_protection) {
151-
// translate protection - combination of 'umf_mem_protection_flags_t' flags
152-
return os_translate_flags(in_protection, UMF_PROTECTION_MAX,
153-
os_translate_mem_protection_one_flag,
154-
out_protection);
155-
}
156-
157-
static int os_translate_purge_advise(umf_purge_advise_t advise) {
158-
switch (advise) {
159-
case UMF_PURGE_LAZY:
160-
return MADV_FREE;
161-
case UMF_PURGE_FORCE:
162-
return MADV_DONTNEED;
163-
}
164-
assert(0);
165-
return -1;
166-
}
167-
168-
void *os_mmap(void *hint_addr, size_t length, int prot, int flag, int fd,
169-
size_t fd_offset) {
170-
fd = (fd == 0) ? -1 : fd;
171-
if (fd == -1) {
172-
// MAP_ANONYMOUS - the mapping is not backed by any file
173-
flag |= MAP_ANONYMOUS;
174-
}
175-
176-
void *ptr = mmap(hint_addr, length, prot, flag, fd, fd_offset);
177-
if (ptr == MAP_FAILED) {
178-
return NULL;
179-
}
180-
181-
return ptr;
182-
}
183-
184-
int os_munmap(void *addr, size_t length) { return munmap(addr, length); }
185-
186-
size_t os_get_page_size(void) { return sysconf(_SC_PAGE_SIZE); }
187-
188-
int os_purge(void *addr, size_t length, int advice) {
189-
return madvise(addr, length, os_translate_purge_advise(advice));
190-
}
191-
192-
void os_strerror(int errnum, char *buf, size_t buflen) {
193-
strerror_r(errnum, buf, buflen);
194-
}
195-
196-
int os_getpid(void) { return getpid(); }
197-
198-
umf_result_t os_duplicate_fd(int pid, int fd_in, int *fd_out) {
199-
// pidfd_getfd(2) is used to obtain a duplicate of another process's file descriptor.
200-
// Permission to duplicate another process's file descriptor
201-
// is governed by a ptrace access mode PTRACE_MODE_ATTACH_REALCREDS check (see ptrace(2))
202-
// that can be changed using the /proc/sys/kernel/yama/ptrace_scope interface.
203-
// pidfd_getfd(2) is supported since Linux 5.6
204-
// pidfd_open(2) is supported since Linux 5.3
205-
#if defined(__NR_pidfd_open) && defined(__NR_pidfd_getfd)
206-
errno = 0;
207-
int pid_fd = syscall(SYS_pidfd_open, pid, 0);
208-
if (pid_fd == -1) {
209-
LOG_PDEBUG("SYS_pidfd_open");
210-
return UMF_RESULT_ERROR_UNKNOWN;
211-
}
212-
213-
int fd_dup = syscall(SYS_pidfd_getfd, pid_fd, fd_in, 0);
214-
close(pid_fd);
215-
if (fd_dup == -1) {
216-
LOG_PDEBUG("SYS_pidfd_getfd");
217-
return UMF_RESULT_ERROR_UNKNOWN;
218-
}
219-
220-
*fd_out = fd_dup;
221-
222-
return UMF_RESULT_SUCCESS;
223-
#else
224-
// TODO: find another way to obtain a duplicate of another process's file descriptor
225-
(void)pid; // unused
226-
(void)fd_in; // unused
227-
(void)fd_out; // unused
228-
errno = ENOTSUP;
229-
return UMF_RESULT_ERROR_NOT_SUPPORTED; // unsupported
230-
#endif /* defined(__NR_pidfd_open) && defined(__NR_pidfd_getfd) */
231-
}
232-
233-
umf_result_t os_close_fd(int fd) {
234-
if (close(fd)) {
235-
LOG_PERR("close() failed");
236-
return UMF_RESULT_ERROR_UNKNOWN;
237-
}
238-
239-
return UMF_RESULT_SUCCESS;
24099
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2023-2024 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#include <sys/mman.h>
9+
10+
#include <umf/providers/provider_os_memory.h>
11+
12+
#include "provider_os_memory_internal.h"
13+
#include "utils_log.h"
14+
15+
umf_result_t os_translate_mem_visibility_flag(umf_memory_visibility_t in_flag,
16+
unsigned *out_flag) {
17+
switch (in_flag) {
18+
case UMF_MEM_MAP_PRIVATE:
19+
*out_flag = MAP_PRIVATE;
20+
return UMF_RESULT_SUCCESS;
21+
case UMF_MEM_MAP_SHARED:
22+
return UMF_RESULT_ERROR_NOT_SUPPORTED; // not supported on MacOSX
23+
}
24+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
25+
}
26+
27+
// create an anonymous file descriptor
28+
int os_create_anonymous_fd(unsigned translated_memory_flag) {
29+
(void)translated_memory_flag; // unused
30+
return 0; // ignored on MacOSX
31+
}
32+
33+
int os_set_file_size(int fd, size_t size) {
34+
(void)fd; // unused
35+
(void)size; // unused
36+
return 0; // ignored on MacOSX
37+
}

0 commit comments

Comments
 (0)