xref: /PHP-8.2/main/streams/php_stream_mmap.h (revision 01b3fc03)
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 
17 /* Memory Mapping interface for streams.
18  * The intention is to provide a uniform interface over the most common
19  * operations that are used within PHP itself, rather than a complete
20  * API for all memory mapping needs.
21  *
22  * ATM, we support only mmap(), but win32 memory mapping support will
23  * follow soon.
24  * */
25 
26 typedef enum {
27 	/* Does the stream support mmap ? */
28 	PHP_STREAM_MMAP_SUPPORTED,
29 	/* Request a range and offset to be mapped;
30 	 * while mapped, you MUST NOT use any read/write functions
31 	 * on the stream (win9x compatibility) */
32 	PHP_STREAM_MMAP_MAP_RANGE,
33 	/* Unmap the last range that was mapped for the stream */
34 	PHP_STREAM_MMAP_UNMAP
35 } php_stream_mmap_operation_t;
36 
37 typedef enum {
38 	PHP_STREAM_MAP_MODE_READONLY,
39 	PHP_STREAM_MAP_MODE_READWRITE,
40 	PHP_STREAM_MAP_MODE_SHARED_READONLY,
41 	PHP_STREAM_MAP_MODE_SHARED_READWRITE
42 } php_stream_mmap_access_t;
43 
44 typedef struct {
45 	/* requested offset and length.
46 	 * If length is 0, the whole file is mapped */
47 	size_t offset;
48 	size_t length;
49 
50 	php_stream_mmap_access_t mode;
51 
52 	/* returned mapped address */
53 	char *mapped;
54 
55 } php_stream_mmap_range;
56 
57 #define PHP_STREAM_MMAP_ALL 0
58 
59 #define PHP_STREAM_MMAP_MAX (512 * 1024 * 1024)
60 
61 #define php_stream_mmap_supported(stream)	(_php_stream_set_option((stream), PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_SUPPORTED, NULL) == 0 ? 1 : 0)
62 
63 /* Returns 1 if the stream in its current state can be memory mapped,
64  * 0 otherwise */
65 #define php_stream_mmap_possible(stream)			(!php_stream_is_filtered((stream)) && php_stream_mmap_supported((stream)))
66 
67 BEGIN_EXTERN_C()
68 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);
69 #define php_stream_mmap_range(stream, offset, length, mode, mapped_len)	_php_stream_mmap_range((stream), (offset), (length), (mode), (mapped_len))
70 
71 /* un-maps the last mapped range */
72 PHPAPI int _php_stream_mmap_unmap(php_stream *stream);
73 #define php_stream_mmap_unmap(stream)				_php_stream_mmap_unmap((stream))
74 
75 PHPAPI int _php_stream_mmap_unmap_ex(php_stream *stream, zend_off_t readden);
76 #define php_stream_mmap_unmap_ex(stream, readden)			_php_stream_mmap_unmap_ex((stream), (readden))
77 END_EXTERN_C()
78