summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2022-03-29 10:16:17 +0200
committerSimon Ser <contact@emersion.fr>2022-04-11 09:21:53 +0000
commitff972f85b245d6564ec30c739a73437bcf7fac0e (patch)
tree45d825c22e861540d304746e9e4c8190068e3b11 /src
parent6c424e9d4c92087958928492e2b5635d4d5f2b36 (diff)
downloadwayland-ff972f85b245d6564ec30c739a73437bcf7fac0e.tar.gz
os: drop unnecessary memcpy in wl_os_mremap_maymove
FreeBSD doesn't support mremap [1], so we have a fallback implementation based on munmap+mmap. We memcpy from the old memory region to the new one, however this is unnecessary because the new mapping references the same file as the old one. Use msync to make sure any pending write is flushed to the underlying file before we map the new region. [1]: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=59912 Signed-off-by: Simon Ser <contact@emersion.fr>
Diffstat (limited to 'src')
-rw-r--r--src/wayland-os.c25
1 files changed, 13 insertions, 12 deletions
diff --git a/src/wayland-os.c b/src/wayland-os.c
index 27c6035..a9066ca 100644
--- a/src/wayland-os.c
+++ b/src/wayland-os.c
@@ -231,21 +231,22 @@ wl_os_mremap_maymove(int fd, void *old_data, ssize_t *old_size,
ssize_t new_size, int prot, int flags)
{
void *result;
- /*
- * We could try mapping a new block immediately after the current one
+
+ /* Make sure any pending write is flushed. */
+ if (msync(old_data, *old_size, MS_SYNC) != 0)
+ return MAP_FAILED;
+
+ /* We could try mapping a new block immediately after the current one
* with MAP_FIXED, however that is not guaranteed to work and breaks
* on CHERI-enabled architectures since the data pointer will still
- * have the bounds of the previous allocation. As this is not a
- * performance-critical path, we always map a new region and copy the
- * old data to the new region.
+ * have the bounds of the previous allocation.
*/
result = mmap(NULL, new_size, prot, flags, fd, 0);
- if (result != MAP_FAILED) {
- /* Copy the data over and unmap the old mapping. */
- memcpy(result, old_data, *old_size);
- if (munmap(old_data, *old_size) == 0) {
- *old_size = 0; /* successfully unmapped old data. */
- }
- }
+ if (result == MAP_FAILED)
+ return MAP_FAILED;
+
+ if (munmap(old_data, *old_size) == 0)
+ *old_size = 0;
+
return result;
}