xref: /PHP-7.4/main/streams/mmap.c (revision 333d607d)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 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 
19 /* Memory Mapping interface for streams */
20 #include "php.h"
21 #include "php_streams_int.h"
22 
_php_stream_mmap_range(php_stream * stream,size_t offset,size_t length,php_stream_mmap_access_t mode,size_t * mapped_len)23 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)
24 {
25 	php_stream_mmap_range range;
26 
27 	range.offset = offset;
28 	range.length = length;
29 	range.mode = mode;
30 	range.mapped = NULL;
31 
32 	if (PHP_STREAM_OPTION_RETURN_OK == php_stream_set_option(stream, PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_MAP_RANGE, &range)) {
33 		if (mapped_len) {
34 			*mapped_len = range.length;
35 		}
36 		return range.mapped;
37 	}
38 	return NULL;
39 }
40 
_php_stream_mmap_unmap(php_stream * stream)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;
44 }
45 
_php_stream_mmap_unmap_ex(php_stream * stream,zend_off_t readden)46 PHPAPI int _php_stream_mmap_unmap_ex(php_stream *stream, zend_off_t readden)
47 {
48 	int ret = 1;
49 
50 	if (php_stream_seek(stream, readden, SEEK_CUR) != 0) {
51 		ret = 0;
52 	}
53 	if (php_stream_mmap_unmap(stream) == 0) {
54 		ret = 0;
55 	}
56 
57 	return ret;
58 }
59