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 385 static assert(0, "unimplemented"); 386 387 static if (DEFAULTS) 388 { 389 enum MAP_ANON = 0x20; 390 enum MS_ASYNC = 1; 391 enum MS_INVALIDATE = 2; 392 enum MS_SYNC = 4; 393 } 394 } 395 else version (Darwin) 396 { 397 enum MAP_SHARED = 0x0001; 398 enum MAP_PRIVATE = 0x0002; 399 enum MAP_FIXED = 0x0010; 400 enum MAP_ANON = 0x1000; 401 402 enum MAP_FAILED = cast(void*)-1; 403 404 enum MS_ASYNC = 0x0001; 405 enum MS_INVALIDATE = 0x0002; 406 enum MS_SYNC = 0x0010; 407 } 408 else version (FreeBSD) 409 { 410 enum MAP_SHARED = 0x0001; 411 enum MAP_PRIVATE = 0x0002; 412 enum MAP_FIXED = 0x0010; 413 enum MAP_ANON = 0x1000; 414 415 enum MAP_FAILED = cast(void*)-1; 416 417 enum MS_SYNC = 0x0000; 418 enum MS_ASYNC = 0x0001; 419 enum MS_INVALIDATE = 0x0002; 420 } 421 else version (NetBSD) 422 { 423 enum MAP_SHARED = 0x0001; 424 enum MAP_PRIVATE = 0x0002; 425 enum MAP_FIXED = 0x0010; 426 enum MAP_ANON = 0x1000; 427 428 enum MAP_FAILED = cast(void*)-1; 429 430 enum MS_SYNC = 0x0004; 431 enum MS_ASYNC = 0x0001; 432 enum MS_INVALIDATE = 0x0002; 433 } 434 else version (OpenBSD) 435 { 436 enum MAP_SHARED = 0x0001; 437 enum MAP_PRIVATE = 0x0002; 438 enum MAP_FIXED = 0x0010; 439 enum MAP_ANON = 0x1000; 440 enum MAP_STACK = 0x4000; 441 442 enum MAP_FAILED = cast(void*)-1; 443 444 enum MS_SYNC = 0x0002; 445 enum MS_ASYNC = 0x0001; 446 enum MS_INVALIDATE = 0x0004; 447 } 448 else version (DragonFlyBSD) 449 { 450 enum MAP_SHARED = 0x0001; 451 enum MAP_PRIVATE = 0x0002; 452 enum MAP_FIXED = 0x0010; 453 enum MAP_ANON = 0x1000; 454 455 enum MAP_FAILED = cast(void*)-1; 456 457 enum MS_SYNC = 0x0000; 458 enum MS_ASYNC = 0x0001; 459 enum MS_INVALIDATE = 0x0002; 460 } 461 else version (Solaris) 462 { 463 enum MAP_SHARED = 0x0001; 464 enum MAP_PRIVATE = 0x0002; 465 enum MAP_FIXED = 0x0010; 466 enum MAP_ANON = 0x0100; 467 468 enum MAP_FAILED = cast(void*)-1; 469 470 enum MS_SYNC = 0x0004; 471 enum MS_ASYNC = 0x0001; 472 enum MS_INVALIDATE = 0x0002; 473 } 474 else 475 { 476 static assert(false, "Unsupported platform"); 477 } 478 479 /* 480 int msync(void*, size_t, int); (MF|SIO) 481 */ 482 483 version (CRuntime_Glibc) 484 { 485 int msync(void*, size_t, int); 486 } 487 else version (Darwin) 488 { 489 int msync(void*, size_t, int); 490 } 491 else version (FreeBSD) 492 { 493 int msync(void*, size_t, int); 494 } 495 else version (NetBSD) 496 { 497 int __msync13(void*, size_t, int); 498 alias msync = __msync13; 499 } 500 else version (OpenBSD) 501 { 502 int msync(void*, size_t, int); 503 } 504 else version (DragonFlyBSD) 505 { 506 int msync(void*, size_t, int); 507 } 508 else version (Solaris) 509 { 510 int msync(void*, size_t, int); 511 } 512 else version (CRuntime_Bionic) 513 { 514 int msync(const scope void*, size_t, int); 515 } 516 else version (CRuntime_Musl) 517 { 518 int msync(void*, size_t, int); 519 } 520 else version (CRuntime_UClibc) 521 { 522 int msync(void*, size_t, int); 523 } 524 else 525 { 526 static assert(false, "Unsupported platform"); 527 } 528 529 // 530 // Process Memory Locking (ML) 531 // 532 /* 533 MCL_CURRENT 534 MCL_FUTURE 535 */ 536 537 version (linux) 538 { 539 version (SPARC_Any) enum 540 { 541 MCL_CURRENT = 0x2000, 542 MCL_FUTURE = 0x4000, 543 } 544 else version (PPC_Any) enum 545 { 546 MCL_CURRENT = 0x2000, 547 MCL_FUTURE = 0x4000, 548 } 549 else version (Alpha) enum 550 { 551 MCL_CURRENT = 8192, 552 MCL_FUTURE = 16384, 553 } 554 else enum 555 { 556 MCL_CURRENT = 1, 557 MCL_FUTURE = 2, 558 } 559 } 560 else version (Darwin) 561 { 562 enum MCL_CURRENT = 0x0001; 563 enum MCL_FUTURE = 0x0002; 564 } 565 else version (FreeBSD) 566 { 567 enum MCL_CURRENT = 0x0001; 568 enum MCL_FUTURE = 0x0002; 569 } 570 else version (NetBSD) 571 { 572 enum MCL_CURRENT = 0x0001; 573 enum MCL_FUTURE = 0x0002; 574 } 575 else version (OpenBSD) 576 { 577 enum MCL_CURRENT = 0x0001; 578 enum MCL_FUTURE = 0x0002; 579 } 580 else version (DragonFlyBSD) 581 { 582 enum MCL_CURRENT = 0x0001; 583 enum MCL_FUTURE = 0x0002; 584 } 585 else version (Solaris) 586 { 587 enum MCL_CURRENT = 0x0001; 588 enum MCL_FUTURE = 0x0002; 589 } 590 else 591 { 592 static assert(false, "Unsupported platform"); 593 } 594 595 /* 596 int mlockall(int); 597 int munlockall(); 598 */ 599 600 version (CRuntime_Glibc) 601 { 602 int mlockall(int); 603 int munlockall(); 604 } 605 else version (Darwin) 606 { 607 int mlockall(int); 608 int munlockall(); 609 } 610 else version (FreeBSD) 611 { 612 int mlockall(int); 613 int munlockall(); 614 } 615 else version (NetBSD) 616 { 617 int mlockall(int); 618 int munlockall(); 619 } 620 else version (OpenBSD) 621 { 622 int mlockall(int); 623 int munlockall(); 624 } 625 else version (DragonFlyBSD) 626 { 627 int mlockall(int); 628 int munlockall(); 629 } 630 else version (Solaris) 631 { 632 int mlockall(int); 633 int munlockall(); 634 } 635 else version (CRuntime_Bionic) 636 { 637 int mlockall(int); 638 int munlockall(); 639 } 640 else version (CRuntime_Musl) 641 { 642 int mlockall(int); 643 int munlockall(); 644 } 645 else version (CRuntime_UClibc) 646 { 647 int mlockall(int); 648 int munlockall(); 649 } 650 else 651 { 652 static assert(false, "Unsupported platform"); 653 } 654 655 // 656 // Range Memory Locking (MLR) 657 // 658 /* 659 int mlock(const scope void*, size_t); 660 int munlock(const scope void*, size_t); 661 */ 662 663 version (CRuntime_Glibc) 664 { 665 int mlock(const scope void*, size_t); 666 int munlock(const scope void*, size_t); 667 } 668 else version (Darwin) 669 { 670 int mlock(const scope void*, size_t); 671 int munlock(const scope void*, size_t); 672 } 673 else version (FreeBSD) 674 { 675 int mlock(const scope void*, size_t); 676 int munlock(const scope void*, size_t); 677 } 678 else version (NetBSD) 679 { 680 int mlock(const scope void*, size_t); 681 int munlock(const scope void*, size_t); 682 } 683 else version (OpenBSD) 684 { 685 int mlock(const scope void*, size_t); 686 int munlock(const scope void*, size_t); 687 } 688 else version (DragonFlyBSD) 689 { 690 int mlock(const scope void*, size_t); 691 int munlock(const scope void*, size_t); 692 } 693 else version (Solaris) 694 { 695 int mlock(const scope void*, size_t); 696 int munlock(const scope void*, size_t); 697 } 698 else version (CRuntime_Bionic) 699 { 700 int mlock(const scope void*, size_t); 701 int munlock(const scope void*, size_t); 702 } 703 else version (CRuntime_Musl) 704 { 705 int mlock(const scope void*, size_t); 706 int munlock(const scope void*, size_t); 707 } 708 else version (CRuntime_UClibc) 709 { 710 int mlock(const scope void*, size_t); 711 int munlock(const scope void*, size_t); 712 } 713 else 714 { 715 static assert(false, "Unsupported platform"); 716 } 717 718 // 719 // Memory Protection (MPR) 720 // 721 /* 722 int mprotect(void*, size_t, int); 723 */ 724 725 version (CRuntime_Glibc) 726 { 727 int mprotect(void*, size_t, int); 728 } 729 else version (Darwin) 730 { 731 int mprotect(void*, size_t, int); 732 } 733 else version (FreeBSD) 734 { 735 int mprotect(void*, size_t, int); 736 } 737 else version (NetBSD) 738 { 739 int mprotect(void*, size_t, int); 740 } 741 else version (OpenBSD) 742 { 743 int mprotect(void*, size_t, int); 744 } 745 else version (DragonFlyBSD) 746 { 747 int mprotect(void*, size_t, int); 748 } 749 else version (Solaris) 750 { 751 int mprotect(void*, size_t, int); 752 } 753 else version (CRuntime_Bionic) 754 { 755 int mprotect(const scope void*, size_t, int); 756 } 757 else version (CRuntime_Musl) 758 { 759 int mprotect(void*, size_t, int); 760 } 761 else version (CRuntime_UClibc) 762 { 763 int mprotect(void*, size_t, int); 764 } 765 else 766 { 767 static assert(false, "Unsupported platform"); 768 } 769 770 // 771 // Shared Memory Objects (SHM) 772 // 773 /* 774 int shm_open(const scope char*, int, mode_t); 775 int shm_unlink(const scope char*); 776 */ 777 778 version (CRuntime_Glibc) 779 { 780 int shm_open(const scope char*, int, mode_t); 781 int shm_unlink(const scope char*); 782 } 783 else version (Darwin) 784 { 785 int shm_open(const scope char*, int, mode_t); 786 int shm_unlink(const scope char*); 787 } 788 else version (FreeBSD) 789 { 790 int shm_open(const scope char*, int, mode_t); 791 int shm_unlink(const scope char*); 792 } 793 else version (NetBSD) 794 { 795 int shm_open(const scope char*, int, mode_t); 796 int shm_unlink(const scope char*); 797 } 798 else version (OpenBSD) 799 { 800 int shm_open(const scope char*, int, mode_t); 801 int shm_unlink(const scope char*); 802 } 803 else version (DragonFlyBSD) 804 { 805 int shm_open(const scope char*, int, mode_t); 806 int shm_unlink(const scope char*); 807 } 808 else version (Solaris) 809 { 810 int shm_open(const scope char*, int, mode_t); 811 int shm_unlink(const scope char*); 812 } 813 else version (CRuntime_Bionic) 814 { 815 } 816 else version (CRuntime_Musl) 817 { 818 int shm_open(const scope char*, int, mode_t); 819 int shm_unlink(const scope char*); 820 } 821 else version (CRuntime_UClibc) 822 { 823 int shm_open(const scope char*, int, mode_t); 824 int shm_unlink(const scope char*); 825 } 826 else 827 { 828 static assert(false, "Unsupported platform"); 829 } 830 831 // 832 // Typed Memory Objects (TYM) 833 // 834 /* 835 POSIX_TYPED_MEM_ALLOCATE 836 POSIX_TYPED_MEM_ALLOCATE_CONTIG 837 POSIX_TYPED_MEM_MAP_ALLOCATABLE 838 839 struct posix_typed_mem_info 840 { 841 size_t posix_tmi_length; 842 } 843 844 int posix_mem_offset(const scope void*, size_t, off_t *, size_t *, int *); 845 int posix_typed_mem_get_info(int, struct posix_typed_mem_info *); 846 int posix_typed_mem_open(const scope char*, int, int); 847 */