mmap.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Wez Furlong <wez@thebrainroom.com> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* Memory Mapping interface for streams */
  19. #include "php.h"
  20. #include "php_streams_int.h"
  21. PHPAPI char *_php_stream_mmap_range(php_stream *stream, size_t offset, size_t length, php_stream_mmap_access_t mode, size_t *mapped_len)
  22. {
  23. php_stream_mmap_range range;
  24. range.offset = offset;
  25. range.length = length;
  26. range.mode = mode;
  27. range.mapped = NULL;
  28. /* For now, we impose an arbitrary limit to avoid
  29. * runaway swapping when large files are passed through. */
  30. if (length > 4 * 1024 * 1024) {
  31. return NULL;
  32. }
  33. if (PHP_STREAM_OPTION_RETURN_OK == php_stream_set_option(stream, PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_MAP_RANGE, &range)) {
  34. if (mapped_len) {
  35. *mapped_len = range.length;
  36. }
  37. return range.mapped;
  38. }
  39. return NULL;
  40. }
  41. PHPAPI int _php_stream_mmap_unmap(php_stream *stream)
  42. {
  43. return php_stream_set_option(stream, PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_UNMAP, NULL) == PHP_STREAM_OPTION_RETURN_OK ? 1 : 0;
  44. }
  45. PHPAPI int _php_stream_mmap_unmap_ex(php_stream *stream, zend_off_t readden)
  46. {
  47. int ret = 1;
  48. if (php_stream_seek(stream, readden, SEEK_CUR) != 0) {
  49. ret = 0;
  50. }
  51. if (php_stream_mmap_unmap(stream) == 0) {
  52. ret = 0;
  53. }
  54. return ret;
  55. }
  56. /*
  57. * Local variables:
  58. * tab-width: 4
  59. * c-basic-offset: 4
  60. * End:
  61. * vim600: noet sw=4 ts=4 fdm=marker
  62. * vim<600: noet sw=4 ts=4
  63. */