mmap.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Wez Furlong <wez@thebrainroom.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. /* Memory Mapping interface for streams */
  17. #include "php.h"
  18. #include "php_streams_int.h"
  19. 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)
  20. {
  21. php_stream_mmap_range range;
  22. range.offset = offset;
  23. range.length = length;
  24. range.mode = mode;
  25. range.mapped = NULL;
  26. if (PHP_STREAM_OPTION_RETURN_OK == php_stream_set_option(stream, PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_MAP_RANGE, &range)) {
  27. if (mapped_len) {
  28. *mapped_len = range.length;
  29. }
  30. return range.mapped;
  31. }
  32. return NULL;
  33. }
  34. PHPAPI int _php_stream_mmap_unmap(php_stream *stream)
  35. {
  36. return php_stream_set_option(stream, PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_UNMAP, NULL) == PHP_STREAM_OPTION_RETURN_OK;
  37. }
  38. PHPAPI int _php_stream_mmap_unmap_ex(php_stream *stream, zend_off_t readden)
  39. {
  40. int ret = 1;
  41. if (php_stream_seek(stream, readden, SEEK_CUR) != 0) {
  42. ret = 0;
  43. }
  44. if (php_stream_mmap_unmap(stream) == 0) {
  45. ret = 0;
  46. }
  47. return ret;
  48. }