Skip to content

Commit 33981bd

Browse files
committed
Move os_mmap_aligned() to the common part
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent d53938f commit 33981bd

File tree

3 files changed

+70
-66
lines changed

3 files changed

+70
-66
lines changed

src/provider/provider_os_memory.c

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,69 @@ static void print_numa_nodes(os_memory_provider_t *os_provider, void *addr,
328328
hwloc_bitmap_free(nodeset);
329329
}
330330

331+
static inline void assert_is_page_aligned(uintptr_t ptr, size_t page_size) {
332+
assert((ptr & (page_size - 1)) == 0);
333+
(void)ptr; // unused in Release build
334+
(void)page_size; // unused in Release build
335+
}
336+
337+
static int os_mmap_aligned(void *hint_addr, size_t length, size_t alignment,
338+
size_t page_size, int prot, void **out_addr) {
339+
assert(out_addr);
340+
341+
size_t extended_length = length;
342+
343+
if (alignment > page_size) {
344+
// We have to increase length by alignment to be able to "cut out"
345+
// the correctly aligned part of the memory from the mapped region
346+
// by unmapping the rest: unaligned beginning and unaligned end
347+
// of this region.
348+
extended_length += alignment;
349+
}
350+
351+
void *ptr = os_mmap(hint_addr, extended_length, prot);
352+
if (ptr == NULL) {
353+
return -1;
354+
}
355+
356+
if (alignment > page_size) {
357+
uintptr_t addr = (uintptr_t)ptr;
358+
uintptr_t aligned_addr = addr;
359+
uintptr_t rest_of_div = aligned_addr % alignment;
360+
361+
if (rest_of_div) {
362+
aligned_addr += alignment - rest_of_div;
363+
}
364+
365+
assert_is_page_aligned(aligned_addr, page_size);
366+
367+
size_t head_len = aligned_addr - addr;
368+
if (head_len > 0) {
369+
os_munmap(ptr, head_len);
370+
}
371+
372+
// tail address has to page-aligned
373+
uintptr_t tail = aligned_addr + length;
374+
if (tail & (page_size - 1)) {
375+
tail = (tail + page_size) & ~(page_size - 1);
376+
}
377+
378+
assert_is_page_aligned(tail, page_size);
379+
assert(tail >= aligned_addr + length);
380+
381+
size_t tail_len = (addr + extended_length) - tail;
382+
if (tail_len > 0) {
383+
os_munmap((void *)tail, tail_len);
384+
}
385+
386+
*out_addr = (void *)aligned_addr;
387+
return 0;
388+
}
389+
390+
*out_addr = ptr;
391+
return 0;
392+
}
393+
331394
static umf_result_t os_alloc(void *provider, size_t size, size_t alignment,
332395
void **resultPtr) {
333396
int ret;
@@ -358,8 +421,7 @@ static umf_result_t os_alloc(void *provider, size_t size, size_t alignment,
358421

359422
void *addr = NULL;
360423
errno = 0;
361-
ret = os_mmap_aligned(NULL, size, alignment, page_size, protection, -1, 0,
362-
&addr);
424+
ret = os_mmap_aligned(NULL, size, alignment, page_size, protection, &addr);
363425
if (ret) {
364426
os_store_last_native_error(UMF_OS_RESULT_ERROR_ALLOC_FAILED, errno);
365427
if (os_provider->traces) {

src/provider/provider_os_memory_internal.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ int os_translate_flags(unsigned in_flags, unsigned max,
2525

2626
int os_translate_mem_protection_flags(unsigned protection);
2727

28-
int os_mmap_aligned(void *hint_addr, size_t length, size_t alignment,
29-
size_t page_size, int prot, int fd, long offset,
30-
void **out_addr);
28+
void *os_mmap(void *hint_addr, size_t length, int prot);
3129

3230
int os_munmap(void *addr, size_t length);
3331

src/provider/provider_os_memory_linux.c

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -45,70 +45,14 @@ static int os_translate_purge_advise(umf_purge_advise_t advise) {
4545
return -1;
4646
}
4747

48-
static inline void assert_is_page_aligned(uintptr_t ptr, size_t page_size) {
49-
assert((ptr & (page_size - 1)) == 0);
50-
(void)ptr; // unused in Release build
51-
(void)page_size; // unused in Release build
52-
}
53-
54-
int os_mmap_aligned(void *hint_addr, size_t length, size_t alignment,
55-
size_t page_size, int prot, int fd, long offset,
56-
void **out_addr) {
57-
assert(out_addr);
58-
59-
size_t extended_length = length;
60-
61-
if (alignment > page_size) {
62-
// We have to increase length by alignment to be able to "cut out"
63-
// the correctly aligned part of the memory from the mapped region
64-
// by unmapping the rest: unaligned beginning and unaligned end
65-
// of this region.
66-
extended_length += alignment;
67-
}
68-
48+
void *os_mmap(void *hint_addr, size_t length, int prot) {
6949
// MAP_ANONYMOUS - the mapping is not backed by any file
70-
void *ptr = mmap(hint_addr, extended_length, prot,
71-
MAP_ANONYMOUS | MAP_PRIVATE, fd, offset);
50+
void *ptr =
51+
mmap(hint_addr, length, prot, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
7252
if (ptr == MAP_FAILED) {
73-
return -1;
74-
}
75-
76-
if (alignment > page_size) {
77-
uintptr_t addr = (uintptr_t)ptr;
78-
uintptr_t aligned_addr = addr;
79-
uintptr_t rest_of_div = aligned_addr % alignment;
80-
81-
if (rest_of_div) {
82-
aligned_addr += alignment - rest_of_div;
83-
}
84-
85-
assert_is_page_aligned(aligned_addr, page_size);
86-
87-
size_t head_len = aligned_addr - addr;
88-
if (head_len > 0) {
89-
munmap(ptr, head_len);
90-
}
91-
92-
// tail address has to page-aligned
93-
uintptr_t tail = aligned_addr + length;
94-
if (tail & (page_size - 1)) {
95-
tail = (tail + page_size) & ~(page_size - 1);
96-
}
97-
98-
assert_is_page_aligned(tail, page_size);
99-
assert(tail >= aligned_addr + length);
100-
101-
size_t tail_len = (addr + extended_length) - tail;
102-
if (tail_len > 0) {
103-
munmap((void *)tail, tail_len);
104-
}
105-
106-
*out_addr = (void *)aligned_addr;
107-
return 0;
53+
return NULL;
10854
}
109-
110-
*out_addr = ptr;
111-
return 0;
55+
return ptr;
11256
}
11357

11458
int os_munmap(void *addr, size_t length) { return munmap(addr, length); }

0 commit comments

Comments
 (0)