mmap.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. /* $Id$ */
  19. /* Memory Mapping interface for streams */
  20. #include "php.h"
  21. #include "php_streams_int.h"
  22. PHPAPI char *_php_stream_mmap_range(php_stream *stream, size_t offset, size_t length, php_stream_mmap_operation_t mode, size_t *mapped_len TSRMLS_DC)
  23. {
  24. php_stream_mmap_range range;
  25. range.offset = offset;
  26. range.length = length;
  27. range.mode = mode;
  28. range.mapped = NULL;
  29. /* For now, we impose an arbitrary limit to avoid
  30. * runaway swapping when large files are passed thru. */
  31. if (length > 4 * 1024 * 1024) {
  32. return NULL;
  33. }
  34. if (PHP_STREAM_OPTION_RETURN_OK == php_stream_set_option(stream, PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_MAP_RANGE, &range)) {
  35. if (mapped_len) {
  36. *mapped_len = range.length;
  37. }
  38. return range.mapped;
  39. }
  40. return NULL;
  41. }
  42. PHPAPI int _php_stream_mmap_unmap(php_stream *stream TSRMLS_DC)
  43. {
  44. return php_stream_set_option(stream, PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_UNMAP, NULL) == PHP_STREAM_OPTION_RETURN_OK ? 1 : 0;
  45. }
  46. PHPAPI int _php_stream_mmap_unmap_ex(php_stream *stream, off_t readden TSRMLS_DC)
  47. {
  48. int ret = 1;
  49. if (php_stream_seek(stream, readden, SEEK_CUR) != 0) {
  50. ret = 0;
  51. }
  52. if (php_stream_mmap_unmap(stream) == 0) {
  53. ret = 0;
  54. }
  55. return ret;
  56. }
  57. /*
  58. * Local variables:
  59. * tab-width: 4
  60. * c-basic-offset: 4
  61. * End:
  62. * vim600: noet sw=4 ts=4 fdm=marker
  63. * vim<600: noet sw=4 ts=4
  64. */