cpl_virtualmem.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**********************************************************************
  2. * $Id: cpl_virtualmem.h 29330 2015-06-14 12:11:11Z rouault $
  3. *
  4. * Name: cpl_virtualmem.h
  5. * Project: CPL - Common Portability Library
  6. * Purpose: Virtual memory
  7. * Author: Even Rouault, <even dot rouault at mines dash paris dot org>
  8. *
  9. **********************************************************************
  10. * Copyright (c) 2014, Even Rouault <even dot rouault at mines-paris dot org>
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a
  13. * copy of this software and associated documentation files (the "Software"),
  14. * to deal in the Software without restriction, including without limitation
  15. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  16. * and/or sell copies of the Software, and to permit persons to whom the
  17. * Software is furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included
  20. * in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  28. * DEALINGS IN THE SOFTWARE.
  29. ****************************************************************************/
  30. #ifndef _CPL_VIRTUAL_MEM_INCLUDED
  31. #define _CPL_VIRTUAL_MEM_INCLUDED
  32. #include "cpl_port.h"
  33. #include "cpl_vsi.h"
  34. CPL_C_START
  35. /**
  36. * \file cpl_virtualmem.h
  37. *
  38. * Virtual memory management.
  39. *
  40. * This file provides mechanism to define virtual memory mappings, whose content
  41. * is allocated transparently and filled on-the-fly. Those virtual memory mappings
  42. * can be much larger than the available RAM, but only parts of the virtual
  43. * memory mapping, in the limit of the allowed the cache size, will actually be
  44. * physically allocated.
  45. *
  46. * This exploits low-level mechanisms of the operating system (virtual memory
  47. * allocation, page protection and handler of virtual memory exceptions).
  48. *
  49. * It is also possible to create a virtual memory mapping from a file or part
  50. * of a file.
  51. *
  52. * The current implementation is Linux only.
  53. */
  54. /** Opaque type that represents a virtual memory mapping. */
  55. typedef struct CPLVirtualMem CPLVirtualMem;
  56. /** Callback triggered when a still unmapped page of virtual memory is accessed.
  57. * The callback has the responsibility of filling the page with relevant values
  58. *
  59. * @param ctxt virtual memory handle.
  60. * @param nOffset offset of the page in the memory mapping.
  61. * @param pPageToFill address of the page to fill. Note that the address might
  62. * be a temporary location, and not at CPLVirtualMemGetAddr() + nOffset.
  63. * @param nToFill number of bytes of the page.
  64. * @param pUserData user data that was passed to CPLVirtualMemNew().
  65. */
  66. typedef void (*CPLVirtualMemCachePageCbk)(CPLVirtualMem* ctxt,
  67. size_t nOffset,
  68. void* pPageToFill,
  69. size_t nToFill,
  70. void* pUserData);
  71. /** Callback triggered when a dirty mapped page is going to be freed.
  72. * (saturation of cache, or termination of the virtual memory mapping).
  73. *
  74. * @param ctxt virtual memory handle.
  75. * @param nOffset offset of the page in the memory mapping.
  76. * @param pPageToBeEvicted address of the page that will be flushed. Note that the address might
  77. * be a temporary location, and not at CPLVirtualMemGetAddr() + nOffset.
  78. * @param nToBeEvicted number of bytes of the page.
  79. * @param pUserData user data that was passed to CPLVirtualMemNew().
  80. */
  81. typedef void (*CPLVirtualMemUnCachePageCbk)(CPLVirtualMem* ctxt,
  82. size_t nOffset,
  83. const void* pPageToBeEvicted,
  84. size_t nToBeEvicted,
  85. void* pUserData);
  86. /** Callback triggered when a virtual memory mapping is destroyed.
  87. * @param pUserData user data that was passed to CPLVirtualMemNew().
  88. */
  89. typedef void (*CPLVirtualMemFreeUserData)(void* pUserData);
  90. /** Access mode of a virtual memory mapping. */
  91. typedef enum
  92. {
  93. /*! The mapping is meant at being read-only, but writes will not be prevented.
  94. Note that any content written will be lost. */
  95. VIRTUALMEM_READONLY,
  96. /*! The mapping is meant at being read-only, and this will be enforced
  97. through the operating system page protection mechanism. */
  98. VIRTUALMEM_READONLY_ENFORCED,
  99. /*! The mapping is meant at being read-write, and modified pages can be saved
  100. thanks to the pfnUnCachePage callback */
  101. VIRTUALMEM_READWRITE
  102. } CPLVirtualMemAccessMode;
  103. /** Return the size of a page of virtual memory.
  104. *
  105. * @return the page size.
  106. *
  107. * @since GDAL 1.11
  108. */
  109. size_t CPL_DLL CPLGetPageSize(void);
  110. /** Create a new virtual memory mapping.
  111. *
  112. * This will reserve an area of virtual memory of size nSize, whose size
  113. * might be potentially much larger than the physical memory available. Initially,
  114. * no physical memory will be allocated. As soon as memory pages will be accessed,
  115. * they will be allocated transparently and filled with the pfnCachePage callback.
  116. * When the allowed cache size is reached, the least recently used pages will
  117. * be unallocated.
  118. *
  119. * On Linux AMD64 platforms, the maximum value for nSize is 128 TB.
  120. * On Linux x86 platforms, the maximum value for nSize is 2 GB.
  121. *
  122. * Only supported on Linux for now.
  123. *
  124. * Note that on Linux, this function will install a SIGSEGV handler. The
  125. * original handler will be restored by CPLVirtualMemManagerTerminate().
  126. *
  127. * @param nSize size in bytes of the virtual memory mapping.
  128. * @param nCacheSize size in bytes of the maximum memory that will be really
  129. * allocated (must ideally fit into RAM).
  130. * @param nPageSizeHint hint for the page size. Must be a multiple of the
  131. * system page size, returned by CPLGetPageSize().
  132. * Minimum value is generally 4096. Might be set to 0 to
  133. * let the function determine a default page size.
  134. * @param bSingleThreadUsage set to TRUE if there will be no concurrent threads
  135. * that will access the virtual memory mapping. This can
  136. * optimize performance a bit.
  137. * @param eAccessMode permission to use for the virtual memory mapping.
  138. * @param pfnCachePage callback triggered when a still unmapped page of virtual
  139. * memory is accessed. The callback has the responsibility
  140. * of filling the page with relevant values.
  141. * @param pfnUnCachePage callback triggered when a dirty mapped page is going to
  142. * be freed (saturation of cache, or termination of the
  143. * virtual memory mapping). Might be NULL.
  144. * @param pfnFreeUserData callback that can be used to free pCbkUserData. Might be
  145. * NULL
  146. * @param pCbkUserData user data passed to pfnCachePage and pfnUnCachePage.
  147. *
  148. * @return a virtual memory object that must be freed by CPLVirtualMemFree(),
  149. * or NULL in case of failure.
  150. *
  151. * @since GDAL 1.11
  152. */
  153. CPLVirtualMem CPL_DLL *CPLVirtualMemNew(size_t nSize,
  154. size_t nCacheSize,
  155. size_t nPageSizeHint,
  156. int bSingleThreadUsage,
  157. CPLVirtualMemAccessMode eAccessMode,
  158. CPLVirtualMemCachePageCbk pfnCachePage,
  159. CPLVirtualMemUnCachePageCbk pfnUnCachePage,
  160. CPLVirtualMemFreeUserData pfnFreeUserData,
  161. void *pCbkUserData);
  162. /** Return if virtual memory mapping of a file is available.
  163. *
  164. * @return TRUE if virtual memory mapping of a file is available.
  165. * @since GDAL 1.11
  166. */
  167. int CPL_DLL CPLIsVirtualMemFileMapAvailable(void);
  168. /** Create a new virtual memory mapping from a file.
  169. *
  170. * The file must be a "real" file recognized by the operating system, and not
  171. * a VSI extended virtual file.
  172. *
  173. * In VIRTUALMEM_READWRITE mode, updates to the memory mapping will be written
  174. * in the file.
  175. *
  176. * On Linux AMD64 platforms, the maximum value for nLength is 128 TB.
  177. * On Linux x86 platforms, the maximum value for nLength is 2 GB.
  178. *
  179. * Only supported on Linux for now.
  180. *
  181. * @param fp Virtual file handle.
  182. * @param nOffset Offset in the file to start the mapping from.
  183. * @param nLength Length of the portion of the file to map into memory.
  184. * @param eAccessMode Permission to use for the virtual memory mapping. This must
  185. * be consistent with how the file has been opened.
  186. * @param pfnFreeUserData callback that is called when the object is destroyed.
  187. * @param pCbkUserData user data passed to pfnFreeUserData.
  188. * @return a virtual memory object that must be freed by CPLVirtualMemFree(),
  189. * or NULL in case of failure.
  190. *
  191. * @since GDAL 1.11
  192. */
  193. CPLVirtualMem CPL_DLL *CPLVirtualMemFileMapNew( VSILFILE* fp,
  194. vsi_l_offset nOffset,
  195. vsi_l_offset nLength,
  196. CPLVirtualMemAccessMode eAccessMode,
  197. CPLVirtualMemFreeUserData pfnFreeUserData,
  198. void *pCbkUserData );
  199. /** Create a new virtual memory mapping derived from an other virtual memory
  200. * mapping.
  201. *
  202. * This may be useful in case of creating mapping for pixel interleaved data.
  203. *
  204. * The new mapping takes a reference on the base mapping.
  205. *
  206. * @param pVMemBase Base virtual memory mapping
  207. * @param nOffset Offset in the base virtual memory mapping from which to start
  208. * the new mapping.
  209. * @param nSize Size of the base virtual memory mapping to expose in the
  210. * the new mapping.
  211. * @param pfnFreeUserData callback that is called when the object is destroyed.
  212. * @param pCbkUserData user data passed to pfnFreeUserData.
  213. * @return a virtual memory object that must be freed by CPLVirtualMemFree(),
  214. * or NULL in case of failure.
  215. *
  216. * @since GDAL 1.11
  217. */
  218. CPLVirtualMem CPL_DLL *CPLVirtualMemDerivedNew(CPLVirtualMem* pVMemBase,
  219. vsi_l_offset nOffset,
  220. vsi_l_offset nSize,
  221. CPLVirtualMemFreeUserData pfnFreeUserData,
  222. void *pCbkUserData);
  223. /** Free a virtual memory mapping.
  224. *
  225. * The pointer returned by CPLVirtualMemGetAddr() will no longer be valid.
  226. * If the virtual memory mapping was created with read/write permissions and that
  227. * they are dirty (i.e. modified) pages, they will be flushed through the
  228. * pfnUnCachePage callback before being freed.
  229. *
  230. * @param ctxt context returned by CPLVirtualMemNew().
  231. *
  232. * @since GDAL 1.11
  233. */
  234. void CPL_DLL CPLVirtualMemFree(CPLVirtualMem* ctxt);
  235. /** Return the pointer to the start of a virtual memory mapping.
  236. *
  237. * The bytes in the range [p:p+CPLVirtualMemGetSize()-1] where p is the pointer
  238. * returned by this function will be valid, until CPLVirtualMemFree() is called.
  239. *
  240. * Note that if a range of bytes used as an argument of a system call
  241. * (such as read() or write()) contains pages that have not been "realized", the
  242. * system call will fail with EFAULT. CPLVirtualMemPin() can be used to work
  243. * around this issue.
  244. *
  245. * @param ctxt context returned by CPLVirtualMemNew().
  246. * @return the pointer to the start of a virtual memory mapping.
  247. *
  248. * @since GDAL 1.11
  249. */
  250. void CPL_DLL *CPLVirtualMemGetAddr(CPLVirtualMem* ctxt);
  251. /** Return the size of the virtual memory mapping.
  252. *
  253. * @param ctxt context returned by CPLVirtualMemNew().
  254. * @return the size of the virtual memory mapping.
  255. *
  256. * @since GDAL 1.11
  257. */
  258. size_t CPL_DLL CPLVirtualMemGetSize(CPLVirtualMem* ctxt);
  259. /** Return if the virtal memory mapping is a direct file mapping.
  260. *
  261. * @param ctxt context returned by CPLVirtualMemNew().
  262. * @return TRUE if the virtal memory mapping is a direct file mapping.
  263. *
  264. * @since GDAL 1.11
  265. */
  266. int CPL_DLL CPLVirtualMemIsFileMapping(CPLVirtualMem* ctxt);
  267. /** Return the access mode of the virtual memory mapping.
  268. *
  269. * @param ctxt context returned by CPLVirtualMemNew().
  270. * @return the access mode of the virtual memory mapping.
  271. *
  272. * @since GDAL 1.11
  273. */
  274. CPLVirtualMemAccessMode CPL_DLL CPLVirtualMemGetAccessMode(CPLVirtualMem* ctxt);
  275. /** Return the page size associated to a virtual memory mapping.
  276. *
  277. * The value returned will be at least CPLGetPageSize(), but potentially
  278. * larger.
  279. *
  280. * @param ctxt context returned by CPLVirtualMemNew().
  281. * @return the page size
  282. *
  283. * @since GDAL 1.11
  284. */
  285. size_t CPL_DLL CPLVirtualMemGetPageSize(CPLVirtualMem* ctxt);
  286. /** Return TRUE if this memory mapping can be accessed safely from concurrent
  287. * threads.
  288. *
  289. * The situation that can cause problems is when several threads try to access
  290. * a page of the mapping that is not yet mapped.
  291. *
  292. * The return value of this function depends on whether bSingleThreadUsage has
  293. * been set of not in CPLVirtualMemNew() and/or the implementation.
  294. *
  295. * On Linux, this will always return TRUE if bSingleThreadUsage = FALSE.
  296. *
  297. * @param ctxt context returned by CPLVirtualMemNew().
  298. * @return TRUE if this memory mapping can be accessed safely from concurrent
  299. * threads.
  300. *
  301. * @since GDAL 1.11
  302. */
  303. int CPL_DLL CPLVirtualMemIsAccessThreadSafe(CPLVirtualMem* ctxt);
  304. /** Declare that a thread will access a virtual memory mapping.
  305. *
  306. * This function must be called by a thread that wants to access the
  307. * content of a virtual memory mapping, except if the virtual memory mapping has
  308. * been created with bSingleThreadUsage = TRUE.
  309. *
  310. * This function must be paired with CPLVirtualMemUnDeclareThread().
  311. *
  312. * @param ctxt context returned by CPLVirtualMemNew().
  313. *
  314. * @since GDAL 1.11
  315. */
  316. void CPL_DLL CPLVirtualMemDeclareThread(CPLVirtualMem* ctxt);
  317. /** Declare that a thread will stop accessing a virtual memory mapping.
  318. *
  319. * This function must be called by a thread that will no longer access the
  320. * content of a virtual memory mapping, except if the virtual memory mapping has
  321. * been created with bSingleThreadUsage = TRUE.
  322. *
  323. * This function must be paired with CPLVirtualMemDeclareThread().
  324. *
  325. * @param ctxt context returned by CPLVirtualMemNew().
  326. *
  327. * @since GDAL 1.11
  328. */
  329. void CPL_DLL CPLVirtualMemUnDeclareThread(CPLVirtualMem* ctxt);
  330. /** Make sure that a region of virtual memory will be realized.
  331. *
  332. * Calling this function is not required, but might be useful when debugging
  333. * a process with tools like gdb or valgrind that do not naturally like
  334. * segmentation fault signals.
  335. *
  336. * It is also needed when wanting to provide part of virtual memory mapping
  337. * to a system call such as read() or write(). If read() or write() is called
  338. * on a memory region not yet realized, the call will fail with EFAULT.
  339. *
  340. * @param ctxt context returned by CPLVirtualMemNew().
  341. * @param pAddr the memory region to pin.
  342. * @param nSize the size of the memory region.
  343. * @param bWriteOp set to TRUE if the memory are will be accessed in write mode.
  344. *
  345. * @since GDAL 1.11
  346. */
  347. void CPL_DLL CPLVirtualMemPin(CPLVirtualMem* ctxt,
  348. void* pAddr, size_t nSize, int bWriteOp);
  349. /** Cleanup any resource and handlers related to virtual memory.
  350. *
  351. * This function must be called after the last CPLVirtualMem object has
  352. * been freed.
  353. *
  354. * @since GDAL 2.0
  355. */
  356. void CPL_DLL CPLVirtualMemManagerTerminate(void);
  357. CPL_C_END
  358. #endif /* _CPL_VIRTUAL_MEM_INCLUDED */