msync.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* msync -- Synchronize mapped memory to external storage. Mach version.
  2. Copyright (C) 2002-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <sys/types.h>
  16. #include <sys/mman.h>
  17. #include <errno.h>
  18. #include <mach.h>
  19. #include <sysdep-cancel.h>
  20. /* Some Mach variants have vm_msync and some don't. Those that have it
  21. define the VM_SYNC_* bits when we include <mach/mach_types.h>. */
  22. #ifndef VM_SYNC_SYNCHRONOUS
  23. # include <misc/msync.c>
  24. #else
  25. /* Synchronize the region starting at ADDR and extending LEN bytes with the
  26. file it maps. Filesystem operations on a file being mapped are
  27. unpredictable before this is done. */
  28. int
  29. msync (void *addr, size_t len, int flags)
  30. {
  31. vm_sync_t sync_flags = 0;
  32. kern_return_t err;
  33. int cancel_oldtype;
  34. if (flags & MS_SYNC)
  35. sync_flags |= VM_SYNC_SYNCHRONOUS;
  36. if (flags & MS_ASYNC)
  37. sync_flags |= VM_SYNC_ASYNCHRONOUS;
  38. if (flags & MS_INVALIDATE)
  39. sync_flags |= VM_SYNC_INVALIDATE;
  40. cancel_oldtype = LIBC_CANCEL_ASYNC();
  41. err = __vm_msync (__mach_task_self (),
  42. (vm_address_t) addr, (vm_size_t) len, sync_flags);
  43. LIBC_CANCEL_RESET (cancel_oldtype);
  44. if (err)
  45. {
  46. errno = err;
  47. return -1;
  48. }
  49. return 0;
  50. }
  51. #endif