mmap.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2017 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. #include "file.h"
  34. /* ----------------------------------------------------------------------- *
  35. * Unix-style memory mapping, using mmap().
  36. * ----------------------------------------------------------------------- */
  37. #if defined(HAVE_FILENO) && defined(HAVE_MMAP)
  38. /*
  39. * System page size
  40. */
  41. /* File scope since not all compilers like static data in inline functions */
  42. static size_t nasm_pagemask;
  43. static size_t get_pagemask(void)
  44. {
  45. size_t ps = 0;
  46. # if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
  47. ps = sysconf(_SC_PAGESIZE);
  48. # elif defined(HAVE_GETPAGESIZE)
  49. ps = getpagesize();
  50. # elif defined(PAGE_SIZE)
  51. ps = PAGE_SIZE;
  52. # endif
  53. nasm_pagemask = ps = is_power2(ps) ? (ps - 1) : 0;
  54. return ps;
  55. }
  56. static inline size_t pagemask(void)
  57. {
  58. size_t pm = nasm_pagemask;
  59. if (unlikely(!pm))
  60. return get_pagemask();
  61. return pm;
  62. }
  63. /*
  64. * Try to map an input file into memory
  65. */
  66. const void *nasm_map_file(FILE *fp, off_t start, off_t len)
  67. {
  68. const char *p;
  69. off_t astart; /* Aligned start */
  70. size_t salign; /* Amount of start adjustment */
  71. size_t alen; /* Aligned length */
  72. const size_t page_mask = pagemask();
  73. if (unlikely(!page_mask))
  74. return NULL; /* Page size undefined? */
  75. if (unlikely(!len))
  76. return NULL; /* Mapping nothing... */
  77. if (unlikely(len != (off_t)(size_t)len))
  78. return NULL; /* Address space insufficient */
  79. astart = start & ~(off_t)page_mask;
  80. salign = start - astart;
  81. alen = (len + salign + page_mask) & ~page_mask;
  82. p = mmap(NULL, alen, PROT_READ, MAP_SHARED, fileno(fp), astart);
  83. return unlikely(p == MAP_FAILED) ? NULL : p + salign;
  84. }
  85. /*
  86. * Unmap an input file
  87. */
  88. void nasm_unmap_file(const void *p, size_t len)
  89. {
  90. const size_t page_mask = pagemask();
  91. uintptr_t astart;
  92. size_t salign;
  93. size_t alen;
  94. if (unlikely(!page_mask))
  95. return;
  96. astart = (uintptr_t)p & ~(uintptr_t)page_mask;
  97. salign = (uintptr_t)p - astart;
  98. alen = (len + salign + page_mask) & ~page_mask;
  99. munmap((void *)astart, alen);
  100. }
  101. /* ----------------------------------------------------------------------- *
  102. * No memory map support at all
  103. * XXX: Add a section with Windows support
  104. * ----------------------------------------------------------------------- */
  105. #else
  106. const void *nasm_map_file(FILE *fp, off_t start, off_t len)
  107. {
  108. (void)fp; (void)start; (void)len;
  109. return NULL;
  110. }
  111. void nasm_unmap_file(const void *p, size_t len)
  112. {
  113. (void)p; (void)len;
  114. }
  115. #endif