mmap.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr.h"
  17. #include "fspr_private.h"
  18. #include "fspr_general.h"
  19. #include "fspr_mmap.h"
  20. #include "fspr_errno.h"
  21. #include "fspr_arch_file_io.h"
  22. #include "fspr_portable.h"
  23. #include "fspr_strings.h"
  24. #if APR_HAS_MMAP
  25. static fspr_status_t mmap_cleanup(void *themmap)
  26. {
  27. fspr_mmap_t *mm = themmap;
  28. fspr_mmap_t *next = APR_RING_NEXT(mm,link);
  29. fspr_status_t rv = 0;
  30. /* we no longer refer to the mmaped region */
  31. APR_RING_REMOVE(mm,link);
  32. APR_RING_NEXT(mm,link) = NULL;
  33. APR_RING_PREV(mm,link) = NULL;
  34. if (next != mm) {
  35. /* more references exist, so we're done */
  36. return APR_SUCCESS;
  37. }
  38. if (mm->mv) {
  39. if (!UnmapViewOfFile(mm->mv))
  40. {
  41. fspr_status_t rv = fspr_get_os_error();
  42. CloseHandle(mm->mhandle);
  43. mm->mv = NULL;
  44. mm->mhandle = NULL;
  45. return rv;
  46. }
  47. mm->mv = NULL;
  48. }
  49. if (mm->mhandle)
  50. {
  51. if (!CloseHandle(mm->mhandle))
  52. {
  53. fspr_status_t rv = fspr_get_os_error();
  54. CloseHandle(mm->mhandle);
  55. mm->mhandle = NULL;
  56. return rv;
  57. }
  58. mm->mhandle = NULL;
  59. }
  60. return APR_SUCCESS;
  61. }
  62. APR_DECLARE(fspr_status_t) fspr_mmap_create(fspr_mmap_t **new, fspr_file_t *file,
  63. fspr_off_t offset, fspr_size_t size,
  64. fspr_int32_t flag, fspr_pool_t *cont)
  65. {
  66. static DWORD memblock = 0;
  67. DWORD fmaccess = 0;
  68. DWORD mvaccess = 0;
  69. DWORD offlo;
  70. DWORD offhi;
  71. if (size == 0)
  72. return APR_EINVAL;
  73. if (flag & APR_MMAP_WRITE)
  74. fmaccess |= PAGE_READWRITE;
  75. else if (flag & APR_MMAP_READ)
  76. fmaccess |= PAGE_READONLY;
  77. if (flag & APR_MMAP_READ)
  78. mvaccess |= FILE_MAP_READ;
  79. if (flag & APR_MMAP_WRITE)
  80. mvaccess |= FILE_MAP_WRITE;
  81. if (!file || !file->filehand || file->filehand == INVALID_HANDLE_VALUE
  82. || file->buffered)
  83. return APR_EBADF;
  84. if (!memblock)
  85. {
  86. SYSTEM_INFO si;
  87. GetSystemInfo(&si);
  88. memblock = si.dwAllocationGranularity;
  89. }
  90. *new = fspr_pcalloc(cont, sizeof(fspr_mmap_t));
  91. (*new)->pstart = (offset / memblock) * memblock;
  92. (*new)->poffset = offset - (*new)->pstart;
  93. (*new)->psize = (fspr_size_t)((*new)->poffset) + size;
  94. /* The size of the CreateFileMapping object is the current size
  95. * of the size of the mmap object (e.g. file size), not the size
  96. * of the mapped region!
  97. */
  98. (*new)->mhandle = CreateFileMapping(file->filehand, NULL, fmaccess,
  99. 0, 0, NULL);
  100. if (!(*new)->mhandle || (*new)->mhandle == INVALID_HANDLE_VALUE)
  101. {
  102. *new = NULL;
  103. return fspr_get_os_error();
  104. }
  105. offlo = (DWORD)(*new)->pstart;
  106. offhi = (DWORD)((*new)->pstart >> 32);
  107. (*new)->mv = MapViewOfFile((*new)->mhandle, mvaccess, offhi,
  108. offlo, (*new)->psize);
  109. if (!(*new)->mv)
  110. {
  111. fspr_status_t rv = fspr_get_os_error();
  112. CloseHandle((*new)->mhandle);
  113. *new = NULL;
  114. return rv;
  115. }
  116. (*new)->mm = (char*)((*new)->mv) + (*new)->poffset;
  117. (*new)->size = size;
  118. (*new)->cntxt = cont;
  119. APR_RING_ELEM_INIT(*new, link);
  120. /* register the cleanup... */
  121. fspr_pool_cleanup_register((*new)->cntxt, (void*)(*new), mmap_cleanup,
  122. fspr_pool_cleanup_null);
  123. return APR_SUCCESS;
  124. }
  125. APR_DECLARE(fspr_status_t) fspr_mmap_dup(fspr_mmap_t **new_mmap,
  126. fspr_mmap_t *old_mmap,
  127. fspr_pool_t *p)
  128. {
  129. *new_mmap = (fspr_mmap_t *)fspr_pmemdup(p, old_mmap, sizeof(fspr_mmap_t));
  130. (*new_mmap)->cntxt = p;
  131. APR_RING_INSERT_AFTER(old_mmap, *new_mmap, link);
  132. fspr_pool_cleanup_register(p, *new_mmap, mmap_cleanup,
  133. fspr_pool_cleanup_null);
  134. return APR_SUCCESS;
  135. }
  136. APR_DECLARE(fspr_status_t) fspr_mmap_delete(fspr_mmap_t *mm)
  137. {
  138. return fspr_pool_cleanup_run(mm->cntxt, mm, mmap_cleanup);
  139. }
  140. #endif