mman.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU Lesser General Public
  4. License version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details.
  6. Module Name:
  7. mman.h
  8. Abstract:
  9. This header contains definitions for memory management operations.
  10. Author:
  11. Chris Stevens 6-Mar-2014
  12. --*/
  13. #ifndef _SYS_MMAN_H
  14. #define _SYS_MMAN_H
  15. //
  16. // ------------------------------------------------------------------- Includes
  17. //
  18. #include <sys/types.h>
  19. //
  20. // ---------------------------------------------------------------- Definitions
  21. //
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. //
  26. // Define protection options for memory mapped files and shared memory objects.
  27. //
  28. //
  29. // Map the file or object so that it cannot be accessed.
  30. //
  31. #define PROT_NONE 0x0000
  32. //
  33. // Map the file or object so that data can be read from it.
  34. //
  35. #define PROT_READ 0x0001
  36. //
  37. // Map the file or object so that data can be written to it.
  38. //
  39. #define PROT_WRITE 0x0002
  40. //
  41. // Map the file or object so that it can be executed.
  42. //
  43. #define PROT_EXEC 0x0004
  44. //
  45. // Define mapping flags for memory mapped files and shared memory objects.
  46. //
  47. //
  48. // Map the file or object so that changes modify the underlying object.
  49. //
  50. #define MAP_SHARED 0x0001
  51. //
  52. // Map the file or object so that changes are only visible to the modifying
  53. // process.
  54. //
  55. #define MAP_PRIVATE 0x0002
  56. //
  57. // Map the file or object at the supplied virtual address.
  58. //
  59. #define MAP_FIXED 0x0004
  60. //
  61. // Create a mapping that is not backed by a file or object. The supplied file
  62. // descriptor and offset are ignored.
  63. //
  64. #define MAP_ANONYMOUS 0x0008
  65. #define MAP_ANON MAP_ANONYMOUS
  66. //
  67. // Define flags use for memory synchronization.
  68. //
  69. //
  70. // Perform the synchronization with asynchronous writes.
  71. //
  72. #define MS_ASYNC 0x0001
  73. //
  74. // Perform the synchronization with synchronous writes.
  75. //
  76. #define MS_SYNC 0x0002
  77. //
  78. // Set this flag to invalidate all cached copies of mapped data that are
  79. // inconsistent with the permanent storage locations such that subsequent
  80. // references obtain data consistent with permanent storage sometime between
  81. // the call to msync and its first subsequent memory reference.
  82. //
  83. // In practice this flag does nothing.
  84. //
  85. #define MS_INVALIDATE 0x0004
  86. //
  87. // Define the value used to indicate a failed mapping.
  88. //
  89. #define MAP_FAILED ((void *)-1)
  90. //
  91. // ------------------------------------------------------ Data Type Definitions
  92. //
  93. //
  94. // -------------------------------------------------------------------- Globals
  95. //
  96. //
  97. // -------------------------------------------------------- Function Prototypes
  98. //
  99. LIBC_API
  100. void *
  101. mmap (
  102. void *Address,
  103. size_t Length,
  104. int ProtectionFlags,
  105. int MapFlags,
  106. int FileDescriptor,
  107. off_t Offset
  108. );
  109. /*++
  110. Routine Description:
  111. This routine maps the given file or memory object into the current process'
  112. address space.
  113. Arguments:
  114. Address - Supplies an optional suggested virtual address for the mapping.
  115. If MAP_FIXED is supplied then the routine attempts to create the
  116. mapping at the exact address supplied.
  117. Length - Supplies the length, in bytes, of the region to map.
  118. ProtectionFlags - Supplies a set of flags ORed together. See PROT_* for
  119. definitions.
  120. MapFlags - Supplies a set of flags ORed together. See MAP_* for definitions.
  121. FileDescriptor - Supplies the file descriptor of the file or memory object
  122. to map.
  123. Offset - Supplies the offset, in bytes, within the file or memory object
  124. where the mapping should begin.
  125. Return Value:
  126. Returns the address where the mapping was made on sucess.
  127. MAP_FAILED on failure. The errno variable will be set to indicate the error.
  128. --*/
  129. LIBC_API
  130. int
  131. munmap (
  132. void *Address,
  133. size_t Length
  134. );
  135. /*++
  136. Routine Description:
  137. This routine removes any mappings in the the current process' address space
  138. that are within the specified region.
  139. Arguments:
  140. Address - Supplies the start of the address region to unmap.
  141. Length - Supplies the size, in bytes, of the region to unmap.
  142. Return Value:
  143. Returns 0 on success.
  144. -1 on failure. The errno variable will be set to indicate the error.
  145. --*/
  146. LIBC_API
  147. int
  148. mprotect (
  149. const void *Address,
  150. size_t Length,
  151. int ProtectionFlags
  152. );
  153. /*++
  154. Routine Description:
  155. This routine changes the memory protection attributes for the given region
  156. of memory.
  157. Arguments:
  158. Address - Supplies the starting address (inclusive) to change the memory
  159. protection for. This must be aligned to a page boundary.
  160. Length - Supplies the length, in bytes, of the region to change attributes
  161. for.
  162. ProtectionFlags - Supplies a bitfield of flags describing the desired
  163. attributes of the region. See PROT_* definitions.
  164. Return Value:
  165. 0 on success.
  166. -1 on error, and errno is set to contain more information.
  167. --*/
  168. LIBC_API
  169. int
  170. msync (
  171. const void *Address,
  172. size_t Length,
  173. int Flags
  174. );
  175. /*++
  176. Routine Description:
  177. This routine synchronizes a region of the current process' memory address
  178. space with the permanent storage that backs it. If there is no storage
  179. backing the supplied region, than this routine has no effect.
  180. Arguments:
  181. Address - Supplies the start of the address region to synchronize.
  182. Length - Supplies the size, in bytes, of the region to synchronize.
  183. Flags - Supplies a set of flags ORed together. See MS_* for definitions.
  184. Return Value:
  185. Returns 0 on success.
  186. -1 on failure. The errno variable will be set to indicate the error.
  187. --*/
  188. LIBC_API
  189. int
  190. shm_open (
  191. const char *Name,
  192. int OpenFlags,
  193. mode_t Mode
  194. );
  195. /*++
  196. Routine Description:
  197. This routine opens a shared memory object and connects it to a file
  198. descriptor.
  199. Arguments:
  200. Name - Supplies a pointer to a null terminated string containing the name
  201. of the shared memory objecet.
  202. OpenFlags - Supplies a set of flags ORed together. See O_* definitions.
  203. Mode - Supplies the permissions mask to set if the shared memory object is
  204. to be created by this call.
  205. Return Value:
  206. Returns a file descriptor on success.
  207. -1 on failure. The errno variable will be set to indicate the error.
  208. --*/
  209. LIBC_API
  210. int
  211. shm_unlink (
  212. const char *Name
  213. );
  214. /*++
  215. Routine Description:
  216. This routine removes the shared memory object as identified by the given
  217. name from the namespace of shared memory objects.
  218. Arguments:
  219. Name - Supplies a pointer to the name of the shared memory object to remove.
  220. Return Value:
  221. Returns 0 on success.
  222. -1 on failure. The errno variable will be set to indicate the error.
  223. --*/
  224. #ifdef __cplusplus
  225. }
  226. #endif
  227. #endif