apr_mmap.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #ifndef APR_MMAP_H
  17. #define APR_MMAP_H
  18. /**
  19. * @file apr_mmap.h
  20. * @brief APR MMAP routines
  21. */
  22. #include "apr.h"
  23. #include "apr_pools.h"
  24. #include "apr_errno.h"
  25. #include "apr_ring.h"
  26. #include "apr_file_io.h" /* for apr_file_t */
  27. #ifdef BEOS
  28. #include <kernel/OS.h>
  29. #endif
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif /* __cplusplus */
  33. /**
  34. * @defgroup apr_mmap MMAP (Memory Map) Routines
  35. * @ingroup APR
  36. * @{
  37. */
  38. /** MMap opened for reading */
  39. #define APR_MMAP_READ 1
  40. /** MMap opened for writing */
  41. #define APR_MMAP_WRITE 2
  42. /** @see apr_mmap_t */
  43. typedef struct apr_mmap_t apr_mmap_t;
  44. /**
  45. * @remark
  46. * As far as I can tell the only really sane way to store an MMAP is as a
  47. * void * and a length. BeOS requires this area_id, but that's just a little
  48. * something extra. I am exposing this type, because it doesn't make much
  49. * sense to keep it private, and opening it up makes some stuff easier in
  50. * Apache.
  51. */
  52. /** The MMAP structure */
  53. struct apr_mmap_t {
  54. /** The pool the mmap structure was allocated out of. */
  55. apr_pool_t *cntxt;
  56. #ifdef BEOS
  57. /** An area ID. Only valid on BeOS */
  58. area_id area;
  59. #endif
  60. #ifdef WIN32
  61. /** The handle of the file mapping */
  62. HANDLE mhandle;
  63. /** The start of the real memory page area (mapped view) */
  64. void *mv;
  65. /** The physical start, size and offset */
  66. apr_off_t pstart;
  67. apr_size_t psize;
  68. apr_off_t poffset;
  69. #endif
  70. /** The start of the memory mapped area */
  71. void *mm;
  72. /** The amount of data in the mmap */
  73. apr_size_t size;
  74. /** ring of apr_mmap_t's that reference the same
  75. * mmap'ed region; acts in place of a reference count */
  76. APR_RING_ENTRY(apr_mmap_t) link;
  77. };
  78. #if APR_HAS_MMAP || defined(DOXYGEN)
  79. /** @def APR_MMAP_THRESHOLD
  80. * Files have to be at least this big before they're mmap()d. This is to deal
  81. * with systems where the expense of doing an mmap() and an munmap() outweighs
  82. * the benefit for small files. It shouldn't be set lower than 1.
  83. */
  84. #ifdef MMAP_THRESHOLD
  85. # define APR_MMAP_THRESHOLD MMAP_THRESHOLD
  86. #else
  87. # ifdef SUNOS4
  88. # define APR_MMAP_THRESHOLD (8*1024)
  89. # else
  90. # define APR_MMAP_THRESHOLD 1
  91. # endif /* SUNOS4 */
  92. #endif /* MMAP_THRESHOLD */
  93. /** @def APR_MMAP_LIMIT
  94. * Maximum size of MMap region
  95. */
  96. #ifdef MMAP_LIMIT
  97. # define APR_MMAP_LIMIT MMAP_LIMIT
  98. #else
  99. # define APR_MMAP_LIMIT (4*1024*1024)
  100. #endif /* MMAP_LIMIT */
  101. /** Can this file be MMaped */
  102. #define APR_MMAP_CANDIDATE(filelength) \
  103. ((filelength >= APR_MMAP_THRESHOLD) && (filelength < APR_MMAP_LIMIT))
  104. /* Function definitions */
  105. /**
  106. * Create a new mmap'ed file out of an existing APR file.
  107. * @param newmmap The newly created mmap'ed file.
  108. * @param file The file to turn into an mmap.
  109. * @param offset The offset into the file to start the data pointer at.
  110. * @param size The size of the file
  111. * @param flag bit-wise or of:
  112. * <PRE>
  113. * APR_MMAP_READ MMap opened for reading
  114. * APR_MMAP_WRITE MMap opened for writing
  115. * </PRE>
  116. * @param cntxt The pool to use when creating the mmap.
  117. */
  118. APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **newmmap,
  119. apr_file_t *file, apr_off_t offset,
  120. apr_size_t size, apr_int32_t flag,
  121. apr_pool_t *cntxt);
  122. /**
  123. * Duplicate the specified MMAP.
  124. * @param new_mmap The structure to duplicate into.
  125. * @param old_mmap The mmap to duplicate.
  126. * @param p The pool to use for new_mmap.
  127. */
  128. APR_DECLARE(apr_status_t) apr_mmap_dup(apr_mmap_t **new_mmap,
  129. apr_mmap_t *old_mmap,
  130. apr_pool_t *p);
  131. /**
  132. * Remove a mmap'ed.
  133. * @param mm The mmap'ed file.
  134. */
  135. APR_DECLARE(apr_status_t) apr_mmap_delete(apr_mmap_t *mm);
  136. /**
  137. * Move the pointer into the mmap'ed file to the specified offset.
  138. * @param addr The pointer to the offset specified.
  139. * @param mm The mmap'ed file.
  140. * @param offset The offset to move to.
  141. */
  142. APR_DECLARE(apr_status_t) apr_mmap_offset(void **addr, apr_mmap_t *mm,
  143. apr_off_t offset);
  144. #endif /* APR_HAS_MMAP */
  145. /** @} */
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. #endif /* ! APR_MMAP_H */