1 /** 2 * D header file for POSIX. 3 * 4 * Copyright: Copyright Sean Kelly 2005 - 2009. 5 * License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). 6 * Authors: Sean Kelly, Alex Rønne Petersen 7 * Standards: The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition 8 */ 9 10 /* Copyright Sean Kelly 2005 - 2009. 11 * Distributed under the Boost Software License, Version 1.0. 12 * (See accompanying file LICENSE or copy at 13 * http://www.boost.org/LICENSE_1_0.txt) 14 */ 15 module core.sys.posix.sys.mman; 16 17 import core.sys.posix.config; 18 public import core.sys.posix.sys.types; // for off_t, mode_t 19 20 version (OSX) 21 version = Darwin; 22 else version (iOS) 23 version = Darwin; 24 else version (TVOS) 25 version = Darwin; 26 else version (WatchOS) 27 version = Darwin; 28 29 version (ARM) version = ARM_Any; 30 version (AArch64) version = ARM_Any; 31 version (HPPA) version = HPPA_Any; 32 version (HPPA64) version = HPPA_Any; 33 version (MIPS32) version = MIPS_Any; 34 version (MIPS64) version = MIPS_Any; 35 version (PPC) version = PPC_Any; 36 version (PPC64) version = PPC_Any; 37 version (RISCV32) version = RISCV_Any; 38 version (RISCV64) version = RISCV_Any; 39 version (S390) version = IBMZ_Any; 40 version (SPARC) version = SPARC_Any; 41 version (SPARC64) version = SPARC_Any; 42 version (SystemZ) version = IBMZ_Any; 43 version (X86) version = X86_Any; 44 version (X86_64) version = X86_Any; 45 46 version (Posix): 47 extern (C) nothrow @nogc: 48 49 // 50 // Advisory Information (ADV) 51 // 52 /* 53 int posix_madvise(void*, size_t, int); 54 */ 55 56 version (CRuntime_Glibc) 57 { 58 static if (_XOPEN_SOURCE >= 600) 59 { 60 int posix_madvise(void *__addr, size_t __len, int __advice); 61 } 62 } 63 else version (Darwin) 64 { 65 int posix_madvise(void *addr, size_t len, int advice); 66 } 67 else version (FreeBSD) 68 { 69 int posix_madvise(void *addr, size_t len, int advice); 70 } 71 else version (NetBSD) 72 { 73 int posix_madvise(void *addr, size_t len, int advice); 74 } 75 else version (OpenBSD) 76 { 77 int posix_madvise(void *addr, size_t len, int advice); 78 } 79 else version (DragonFlyBSD) 80 { 81 int posix_madvise(void *addr, size_t len, int advice); 82 } 83 else version (Solaris) 84 { 85 } 86 else version (CRuntime_Bionic) 87 { 88 } 89 else version (CRuntime_Musl) 90 { 91 int posix_madvise(void *, size_t, int); 92 } 93 else version (CRuntime_UClibc) 94 { 95 int posix_madvise(void *__addr, size_t __len, int __advice); 96 } 97 else 98 { 99 static assert(false, "Unsupported platform"); 100 } 101 102 103 // 104 // Advisory Information and either Memory Mapped Files or Shared Memory Objects (MC1) 105 // 106 /* 107 POSIX_MADV_NORMAL 108 POSIX_MADV_SEQUENTIAL 109 POSIX_MADV_RANDOM 110 POSIX_MADV_WILLNEED 111 POSIX_MADV_DONTNEED 112 */ 113 114 version (linux) 115 { 116 version (Alpha) 117 private enum __POSIX_MADV_DONTNEED = 6; 118 else 119 private enum __POSIX_MADV_DONTNEED = 4; 120 121 enum 122 { 123 POSIX_MADV_NORMAL = 0, 124 POSIX_MADV_RANDOM = 1, 125 POSIX_MADV_SEQUENTIAL = 2, 126 POSIX_MADV_WILLNEED = 3, 127 POSIX_MADV_DONTNEED = __POSIX_MADV_DONTNEED, 128 } 129 } 130 else version (Darwin) 131 { 132 enum POSIX_MADV_NORMAL = 0; 133 enum POSIX_MADV_RANDOM = 1; 134 enum POSIX_MADV_SEQUENTIAL = 2; 135 enum POSIX_MADV_WILLNEED = 3; 136 enum POSIX_MADV_DONTNEED = 4; 137 } 138 else version (FreeBSD) 139 { 140 enum POSIX_MADV_NORMAL = 0; 141 enum POSIX_MADV_RANDOM = 1; 142 enum POSIX_MADV_SEQUENTIAL = 2; 143 enum POSIX_MADV_WILLNEED = 3; 144 enum POSIX_MADV_DONTNEED = 4; 145 } 146 else version (NetBSD) 147 { 148 enum POSIX_MADV_NORMAL = 0; 149 enum POSIX_MADV_RANDOM = 1; 150 enum POSIX_MADV_SEQUENTIAL = 2; 151 enum POSIX_MADV_WILLNEED = 3; 152 enum POSIX_MADV_DONTNEED = 4; 153 } 154 else version (OpenBSD) 155 { 156 enum POSIX_MADV_NORMAL = 0; 157 enum POSIX_MADV_RANDOM = 1; 158 enum POSIX_MADV_SEQUENTIAL = 2; 159 enum POSIX_MADV_WILLNEED = 3; 160 enum POSIX_MADV_DONTNEED = 4; 161 } 162 else version (DragonFlyBSD) 163 { 164 enum POSIX_MADV_NORMAL = 0; 165 enum POSIX_MADV_RANDOM = 1; 166 enum POSIX_MADV_SEQUENTIAL = 2; 167 enum POSIX_MADV_WILLNEED = 3; 168 enum POSIX_MADV_DONTNEED = 4; 169 } 170 else version (Solaris) 171 { 172 } 173 else 174 { 175 static assert(false, "Unsupported platform"); 176 } 177 178 // 179 // Memory Mapped Files, Shared Memory Objects, or Memory Protection (MC2) 180 // 181 /* 182 PROT_READ 183 PROT_WRITE 184 PROT_EXEC 185 PROT_NONE 186 */ 187 188 version (linux) 189 { 190 enum PROT_NONE = 0x0; 191 enum PROT_READ = 0x1; 192 enum PROT_WRITE = 0x2; 193 enum PROT_EXEC = 0x4; 194 } 195 else version (Darwin) 196 { 197 enum PROT_NONE = 0x00; 198 enum PROT_READ = 0x01; 199 enum PROT_WRITE = 0x02; 200 enum PROT_EXEC = 0x04; 201 } 202 else version (FreeBSD) 203 { 204 enum PROT_NONE = 0x00; 205 enum PROT_READ = 0x01; 206 enum PROT_WRITE = 0x02; 207 enum PROT_EXEC = 0x04; 208 } 209 else version (NetBSD) 210 { 211 enum PROT_NONE = 0x00; 212 enum PROT_READ = 0x01; 213 enum PROT_WRITE = 0x02; 214 enum PROT_EXEC = 0x04; 215 } 216 else version (OpenBSD) 217 { 218 enum PROT_NONE = 0x00; 219 enum PROT_READ = 0x01; 220 enum PROT_WRITE = 0x02; 221 enum PROT_EXEC = 0x04; 222 } 223 else version (DragonFlyBSD) 224 { 225 enum PROT_NONE = 0x00; 226 enum PROT_READ = 0x01; 227 enum PROT_WRITE = 0x02; 228 enum PROT_EXEC = 0x04; 229 } 230 else version (Solaris) 231 { 232 enum PROT_NONE = 0x00; 233 enum PROT_READ = 0x01; 234 enum PROT_WRITE = 0x02; 235 enum PROT_EXEC = 0x04; 236 } 237 else 238 { 239 static assert(false, "Unsupported platform"); 240 } 241 242 // 243 // Memory Mapped Files, Shared Memory Objects, or Typed Memory Objects (MC3) 244 // 245 /* 246 void* mmap(void*, size_t, int, int, int, off_t); 247 int munmap(void*, size_t); 248 */ 249 250 version (CRuntime_Glibc) 251 { 252 static if (__USE_LARGEFILE64) void* mmap64(void*, size_t, int, int, int, off_t); 253 static if (__USE_FILE_OFFSET64) 254 alias mmap = mmap64; 255 else 256 void* mmap(void*, size_t, int, int, int, off_t); 257 int munmap(void*, size_t); 258 } 259 else version (Darwin) 260 { 261 void* mmap(void*, size_t, int, int, int, off_t); 262 int munmap(void*, size_t); 263 } 264 else version (FreeBSD) 265 { 266 void* mmap(void*, size_t, int, int, int, off_t); 267 int munmap(void*, size_t); 268 } 269 else version (NetBSD) 270 { 271 void* mmap(void*, size_t, int, int, int, off_t); 272 int munmap(void*, size_t); 273 } 274 else version (OpenBSD) 275 { 276 void* mmap(void*, size_t, int, int, int, off_t); 277 int munmap(void*, size_t); 278 } 279 else version (DragonFlyBSD) 280 { 281 void* mmap(void*, size_t, int, int, int, off_t); 282 int munmap(void*, size_t); 283 } 284 else version (Solaris) 285 { 286 void* mmap(void*, size_t, int, int, int, off_t); 287 int munmap(void*, size_t); 288 } 289 else version (CRuntime_Bionic) 290 { 291 void* mmap(void*, size_t, int, int, int, off_t); 292 int munmap(void*, size_t); 293 } 294 else version (CRuntime_Musl) 295 { 296 void* mmap(void*, size_t, int, int, int, off_t); 297 alias mmap64 = mmap; 298 int munmap(void*, size_t); 299 } 300 else version (CRuntime_UClibc) 301 { 302 static if (__USE_LARGEFILE64) void* mmap64(void*, size_t, int, int, int, off_t); 303 static if (__USE_FILE_OFFSET64) 304 alias mmap = mmap64; 305 else 306 void* mmap(void*, size_t, int, int, int, off_t); 307 int munmap(void*, size_t); 308 } 309 else 310 { 311 static assert(false, "Unsupported platform"); 312 } 313 314 // 315 // Memory Mapped Files (MF) 316 // 317 /* 318 MAP_SHARED (MF|SHM) 319 MAP_PRIVATE (MF|SHM) 320 MAP_FIXED (MF|SHM) 321 MAP_FAILED (MF|SHM) 322 323 MS_ASYNC (MF|SIO) 324 MS_SYNC (MF|SIO) 325 MS_INVALIDATE (MF|SIO) 326 */ 327 328 version (linux) 329 { 330 enum MAP_SHARED = 0x01; 331 enum MAP_PRIVATE = 0x02; 332 enum MAP_FIXED = 0x10; 333 334 enum MAP_FAILED = cast(void*) -1; 335 336 version (MICROBLAZE) 337 private enum DEFAULTS = true; 338 else version (Alpha) 339 { 340 private enum DEFAULTS = false; 341 enum MAP_ANON = 0x10; 342 enum MS_ASYNC = 1; 343 enum MS_SYNC = 2; 344 enum MS_INVALIDATE = 4; 345 } 346 else version (SH) 347 private enum DEFAULTS = true; 348 else version (ARM_Any) 349 private enum DEFAULTS = true; 350 else version (IBMZ_Any) 351 private enum DEFAULTS = true; 352 else version (IA64) 353 private enum DEFAULTS = true; 354 else version (HPPA_Any) 355 { 356 private enum DEFAULTS = false; 357 enum MAP_ANON = 0x10; 358 enum MS_SYNC = 1; 359 enum MS_ASYNC = 2; 360 enum MS_INVALIDATE = 4; 361 } 362 else version (M68K) 363 private enum DEFAULTS = true; 364 else version (TILE) 365 private enum DEFAULTS = true; 366 else version (X86_Any) 367 private enum DEFAULTS = true; 368 else version (MIPS_Any) 369 { 370 private enum DEFAULTS = false; 371 enum MAP_ANON = 0x0800; 372 enum MS_ASYNC = 1; 373 enum MS_INVALIDATE = 2; 374 enum MS_SYNC = 4; 375 } 376 else version (RISCV_Any) 377 private enum DEFAULTS = true; 378 else version (SPARC_Any) 379 private enum DEFAULTS = true; 380 else version (PPC_Any) 381 private enum DEFAULTS = true; 382 else version (LoongArch64) 383 private enum DEFAULTS = true; 384 else version (WebAssembly) 385 private enum DEFAULTS = true; 386 else 387 static assert(0, "unimplemented"); 388 389 static if (DEFAULTS) 390 { 391 enum MAP_ANON = 0x20; 392 enum MS_ASYNC = 1; 393 enum MS_INVALIDATE = 2; 394 enum MS_SYNC = 4; 395 } 396 } 397 else version (Darwin) 398 { 399 enum MAP_SHARED = 0x0001; 400 enum MAP_PRIVATE = 0x0002; 401 enum MAP_FIXED = 0x0010; 402 enum MAP_ANON = 0x1000; 403 404 enum MAP_FAILED = cast(void*)-1; 405 406 enum MS_ASYNC = 0x0001; 407 enum MS_INVALIDATE = 0x0002; 408 enum MS_SYNC = 0x0010; 409 } 410 else version (FreeBSD) 411 { 412 enum MAP_SHARED = 0x0001; 413 enum MAP_PRIVATE = 0x0002; 414 enum MAP_FIXED = 0x0010; 415 enum MAP_ANON = 0x1000; 416 417 enum MAP_FAILED = cast(void*)-1; 418 419 enum MS_SYNC = 0x0000; 420 enum MS_ASYNC = 0x0001; 421 enum MS_INVALIDATE = 0x0002; 422 } 423 else version (NetBSD) 424 { 425 enum MAP_SHARED = 0x0001; 426 enum MAP_PRIVATE = 0x0002; 427 enum MAP_FIXED = 0x0010; 428 enum MAP_ANON = 0x1000; 429 430 enum MAP_FAILED = cast(void*)-1; 431 432 enum MS_SYNC = 0x0004; 433 enum MS_ASYNC = 0x0001; 434 enum MS_INVALIDATE = 0x0002; 435 } 436 else version (OpenBSD) 437 { 438 enum MAP_SHARED = 0x0001; 439 enum MAP_PRIVATE = 0x0002; 440 enum MAP_FIXED = 0x0010; 441 enum MAP_ANON = 0x1000; 442 enum MAP_STACK = 0x4000; 443 444 enum MAP_FAILED = cast(void*)-1; 445 446 enum MS_SYNC = 0x0002; 447 enum MS_ASYNC = 0x0001; 448 enum MS_INVALIDATE = 0x0004; 449 } 450 else version (DragonFlyBSD) 451 { 452 enum MAP_SHARED = 0x0001; 453 enum MAP_PRIVATE = 0x0002; 454 enum MAP_FIXED = 0x0010; 455 enum MAP_ANON = 0x1000; 456 457 enum MAP_FAILED = cast(void*)-1; 458 459 enum MS_SYNC = 0x0000; 460 enum MS_ASYNC = 0x0001; 461 enum MS_INVALIDATE = 0x0002; 462 } 463 else version (Solaris) 464 { 465 enum MAP_SHARED = 0x0001; 466 enum MAP_PRIVATE = 0x0002; 467 enum MAP_FIXED = 0x0010; 468 enum MAP_ANON = 0x0100; 469 470 enum MAP_FAILED = cast(void*)-1; 471 472 enum MS_SYNC = 0x0004; 473 enum MS_ASYNC = 0x0001; 474 enum MS_INVALIDATE = 0x0002; 475 } 476 else 477 { 478 static assert(false, "Unsupported platform"); 479 } 480 481 /* 482 int msync(void*, size_t, int); (MF|SIO) 483 */ 484 485 version (CRuntime_Glibc) 486 { 487 int msync(void*, size_t, int); 488 } 489 else version (Darwin) 490 { 491 int msync(void*, size_t, int); 492 } 493 else version (FreeBSD) 494 { 495 int msync(void*, size_t, int); 496 } 497 else version (NetBSD) 498 { 499 int __msync13(void*, size_t, int); 500 alias msync = __msync13; 501 } 502 else version (OpenBSD) 503 { 504 int msync(void*, size_t, int); 505 } 506 else version (DragonFlyBSD) 507 { 508 int msync(void*, size_t, int); 509 } 510 else version (Solaris) 511 { 512 int msync(void*, size_t, int); 513 } 514 else version (CRuntime_Bionic) 515 { 516 int msync(const scope void*, size_t, int); 517 } 518 else version (CRuntime_Musl) 519 { 520 int msync(void*, size_t, int); 521 } 522 else version (CRuntime_UClibc) 523 { 524 int msync(void*, size_t, int); 525 } 526 else 527 { 528 static assert(false, "Unsupported platform"); 529 } 530 531 // 532 // Process Memory Locking (ML) 533 // 534 /* 535 MCL_CURRENT 536 MCL_FUTURE 537 */ 538 539 version (linux) 540 { 541 version (SPARC_Any) enum 542 { 543 MCL_CURRENT = 0x2000, 544 MCL_FUTURE = 0x4000, 545 } 546 else version (PPC_Any) enum 547 { 548 MCL_CURRENT = 0x2000, 549 MCL_FUTURE = 0x4000, 550 } 551 else version (Alpha) enum 552 { 553 MCL_CURRENT = 8192, 554 MCL_FUTURE = 16384, 555 } 556 else enum 557 { 558 MCL_CURRENT = 1, 559 MCL_FUTURE = 2, 560 } 561 } 562 else version (Darwin) 563 { 564 enum MCL_CURRENT = 0x0001; 565 enum MCL_FUTURE = 0x0002; 566 } 567 else version (FreeBSD) 568 { 569 enum MCL_CURRENT = 0x0001; 570 enum MCL_FUTURE = 0x0002; 571 } 572 else version (NetBSD) 573 { 574 enum MCL_CURRENT = 0x0001; 575 enum MCL_FUTURE = 0x0002; 576 } 577 else version (OpenBSD) 578 { 579 enum MCL_CURRENT = 0x0001; 580 enum MCL_FUTURE = 0x0002; 581 } 582 else version (DragonFlyBSD) 583 { 584 enum MCL_CURRENT = 0x0001; 585 enum MCL_FUTURE = 0x0002; 586 } 587 else version (Solaris) 588 { 589 enum MCL_CURRENT = 0x0001; 590 enum MCL_FUTURE = 0x0002; 591 } 592 else 593 { 594 static assert(false, "Unsupported platform"); 595 } 596 597 /* 598 int mlockall(int); 599 int munlockall(); 600 */ 601 602 version (CRuntime_Glibc) 603 { 604 int mlockall(int); 605 int munlockall(); 606 } 607 else version (Darwin) 608 { 609 int mlockall(int); 610 int munlockall(); 611 } 612 else version (FreeBSD) 613 { 614 int mlockall(int); 615 int munlockall(); 616 } 617 else version (NetBSD) 618 { 619 int mlockall(int); 620 int munlockall(); 621 } 622 else version (OpenBSD) 623 { 624 int mlockall(int); 625 int munlockall(); 626 } 627 else version (DragonFlyBSD) 628 { 629 int mlockall(int); 630 int munlockall(); 631 } 632 else version (Solaris) 633 { 634 int mlockall(int); 635 int munlockall(); 636 } 637 else version (CRuntime_Bionic) 638 { 639 int mlockall(int); 640 int munlockall(); 641 } 642 else version (CRuntime_Musl) 643 { 644 int mlockall(int); 645 int munlockall(); 646 } 647 else version (CRuntime_UClibc) 648 { 649 int mlockall(int); 650 int munlockall(); 651 } 652 else 653 { 654 static assert(false, "Unsupported platform"); 655 } 656 657 // 658 // Range Memory Locking (MLR) 659 // 660 /* 661 int mlock(const scope void*, size_t); 662 int munlock(const scope void*, size_t); 663 */ 664 665 version (CRuntime_Glibc) 666 { 667 int mlock(const scope void*, size_t); 668 int munlock(const scope void*, size_t); 669 } 670 else version (Darwin) 671 { 672 int mlock(const scope void*, size_t); 673 int munlock(const scope void*, size_t); 674 } 675 else version (FreeBSD) 676 { 677 int mlock(const scope void*, size_t); 678 int munlock(const scope void*, size_t); 679 } 680 else version (NetBSD) 681 { 682 int mlock(const scope void*, size_t); 683 int munlock(const scope void*, size_t); 684 } 685 else version (OpenBSD) 686 { 687 int mlock(const scope void*, size_t); 688 int munlock(const scope void*, size_t); 689 } 690 else version (DragonFlyBSD) 691 { 692 int mlock(const scope void*, size_t); 693 int munlock(const scope void*, size_t); 694 } 695 else version (Solaris) 696 { 697 int mlock(const scope void*, size_t); 698 int munlock(const scope void*, size_t); 699 } 700 else version (CRuntime_Bionic) 701 { 702 int mlock(const scope void*, size_t); 703 int munlock(const scope void*, size_t); 704 } 705 else version (CRuntime_Musl) 706 { 707 int mlock(const scope void*, size_t); 708 int munlock(const scope void*, size_t); 709 } 710 else version (CRuntime_UClibc) 711 { 712 int mlock(const scope void*, size_t); 713 int munlock(const scope void*, size_t); 714 } 715 else 716 { 717 static assert(false, "Unsupported platform"); 718 } 719 720 // 721 // Memory Protection (MPR) 722 // 723 /* 724 int mprotect(void*, size_t, int); 725 */ 726 727 version (CRuntime_Glibc) 728 { 729 int mprotect(void*, size_t, int); 730 } 731 else version (Darwin) 732 { 733 int mprotect(void*, size_t, int); 734 } 735 else version (FreeBSD) 736 { 737 int mprotect(void*, size_t, int); 738 } 739 else version (NetBSD) 740 { 741 int mprotect(void*, size_t, int); 742 } 743 else version (OpenBSD) 744 { 745 int mprotect(void*, size_t, int); 746 } 747 else version (DragonFlyBSD) 748 { 749 int mprotect(void*, size_t, int); 750 } 751 else version (Solaris) 752 { 753 int mprotect(void*, size_t, int); 754 } 755 else version (CRuntime_Bionic) 756 { 757 int mprotect(const scope void*, size_t, int); 758 } 759 else version (CRuntime_Musl) 760 { 761 int mprotect(void*, size_t, int); 762 } 763 else version (CRuntime_UClibc) 764 { 765 int mprotect(void*, size_t, int); 766 } 767 else 768 { 769 static assert(false, "Unsupported platform"); 770 } 771 772 // 773 // Shared Memory Objects (SHM) 774 // 775 /* 776 int shm_open(const scope char*, int, mode_t); 777 int shm_unlink(const scope char*); 778 */ 779 780 version (CRuntime_Glibc) 781 { 782 int shm_open(const scope char*, int, mode_t); 783 int shm_unlink(const scope char*); 784 } 785 else version (Darwin) 786 { 787 int shm_open(const scope char*, int, mode_t); 788 int shm_unlink(const scope char*); 789 } 790 else version (FreeBSD) 791 { 792 int shm_open(const scope char*, int, mode_t); 793 int shm_unlink(const scope char*); 794 } 795 else version (NetBSD) 796 { 797 int shm_open(const scope char*, int, mode_t); 798 int shm_unlink(const scope char*); 799 } 800 else version (OpenBSD) 801 { 802 int shm_open(const scope char*, int, mode_t); 803 int shm_unlink(const scope char*); 804 } 805 else version (DragonFlyBSD) 806 { 807 int shm_open(const scope char*, int, mode_t); 808 int shm_unlink(const scope char*); 809 } 810 else version (Solaris) 811 { 812 int shm_open(const scope char*, int, mode_t); 813 int shm_unlink(const scope char*); 814 } 815 else version (CRuntime_Bionic) 816 { 817 } 818 else version (CRuntime_Musl) 819 { 820 int shm_open(const scope char*, int, mode_t); 821 int shm_unlink(const scope char*); 822 } 823 else version (CRuntime_UClibc) 824 { 825 int shm_open(const scope char*, int, mode_t); 826 int shm_unlink(const scope char*); 827 } 828 else 829 { 830 static assert(false, "Unsupported platform"); 831 } 832 833 // 834 // Typed Memory Objects (TYM) 835 // 836 /* 837 POSIX_TYPED_MEM_ALLOCATE 838 POSIX_TYPED_MEM_ALLOCATE_CONTIG 839 POSIX_TYPED_MEM_MAP_ALLOCATABLE 840 841 struct posix_typed_mem_info 842 { 843 size_t posix_tmi_length; 844 } 845 846 int posix_mem_offset(const scope void*, size_t, off_t *, size_t *, int *); 847 int posix_typed_mem_get_info(int, struct posix_typed_mem_info *); 848 int posix_typed_mem_open(const scope char*, int, int); 849 */