forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
swoole_mmap.c
221 lines (197 loc) · 6.11 KB
/
swoole_mmap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
typedef struct
{
size_t size;
off_t offset;
char *filename;
void *memory;
void *ptr;
} swMmapFile;
static size_t mmap_stream_write(php_stream * stream, const char *buffer, size_t length TSRMLS_DC);
static size_t mmap_stream_read(php_stream *stream, char *buffer, size_t length TSRMLS_DC);
static int mmap_stream_flush(php_stream *stream TSRMLS_DC);
static int mmap_stream_seek(php_stream *stream, off_t offset, int whence, off_t *newoffset TSRMLS_DC);
static int mmap_stream_close(php_stream *stream, int close_handle TSRMLS_DC);
static PHP_METHOD(swoole_mmap, open);
static zend_class_entry swoole_mmap_ce;
zend_class_entry *swoole_mmap_class_entry_ptr;
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_mmap_open, 0, 0, 1)
ZEND_ARG_INFO(0, filename)
ZEND_ARG_INFO(0, size)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()
static const zend_function_entry swoole_mmap_methods[] =
{
PHP_ME(swoole_mmap, open, arginfo_swoole_mmap_open, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_FE_END
};
php_stream_ops mmap_ops =
{
mmap_stream_write,
mmap_stream_read,
mmap_stream_close,
mmap_stream_flush,
"swoole_mmap",
mmap_stream_seek,
NULL,
NULL,
NULL
};
static size_t mmap_stream_write(php_stream * stream, const char *buffer, size_t length TSRMLS_DC)
{
swMmapFile *res = stream->abstract;
int n_write = MIN(res->memory + res->size - res->ptr, length);
if (n_write == 0)
{
return 0;
}
memcpy(res->ptr, buffer, n_write);
res->ptr += n_write;
return n_write;
}
static size_t mmap_stream_read(php_stream *stream, char *buffer, size_t length TSRMLS_DC)
{
swMmapFile *res = stream->abstract;
int n_read = MIN(res->memory + res->size - res->ptr, length);
if (n_read == 0)
{
return 0;
}
memcpy(buffer, res->ptr, n_read);
res->ptr += n_read;
return n_read;
}
static int mmap_stream_flush(php_stream *stream TSRMLS_DC)
{
swMmapFile *res = stream->abstract;
return msync(res->memory, res->size, MS_SYNC | MS_INVALIDATE);
}
static int mmap_stream_seek(php_stream *stream, off_t offset, int whence, off_t *newoffset TSRMLS_DC)
{
swMmapFile *res = stream->abstract;
switch (whence)
{
case SEEK_SET:
if (offset < 0 || offset > res->size)
{
*newoffset = (off_t) -1;
return -1;
}
res->ptr = res->memory + offset;
*newoffset = offset;
return 0;
case SEEK_CUR:
if (res->ptr + offset < res->memory || res->ptr + offset > res->memory + res->size)
{
*newoffset = (off_t) -1;
return -1;
}
res->ptr += offset;
*newoffset = res->ptr - res->memory;
return 0;
case SEEK_END:
if (offset > 0 || -1 * offset > res->size)
{
*newoffset = (off_t) -1;
return -1;
}
res->ptr += offset;
*newoffset = res->ptr - res->memory;
return 0;
default:
*newoffset = (off_t) -1;
return -1;
}
}
static int mmap_stream_close(php_stream *stream, int close_handle TSRMLS_DC)
{
swMmapFile *res = stream->abstract;
if (close_handle)
{
munmap(res->memory, res->size);
}
efree(res);
return 0;
}
void swoole_mmap_init(int module_number TSRMLS_DC)
{
SWOOLE_INIT_CLASS_ENTRY(swoole_mmap_ce, "swoole_mmap", "Swoole\\Mmap", swoole_mmap_methods);
swoole_mmap_class_entry_ptr = zend_register_internal_class(&swoole_mmap_ce TSRMLS_CC);
SWOOLE_CLASS_ALIAS(swoole_mmap, "Swoole\\Mmap");
}
static PHP_METHOD(swoole_mmap, open)
{
char *filename;
zend_size_t l_filename;
long offset = 0;
long size = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &filename, &l_filename, &size, &offset) == FAILURE)
{
RETURN_FALSE;
}
if (l_filename <= 0)
{
swoole_php_fatal_error(E_WARNING, "file name is required.");
RETURN_FALSE;
}
int fd;
if ((fd = open(filename, O_RDWR)) < 0)
{
swoole_php_sys_error(E_WARNING, "open(%s, O_RDWR) failed.", filename);
RETURN_FALSE;
}
if (size <= 0)
{
struct stat _stat;
if (fstat(fd, &_stat) < 0)
{
swoole_php_sys_error(E_WARNING, "fstat(%s) failed.", filename);
close(fd);
RETURN_FALSE;
}
if (_stat.st_size == 0)
{
swoole_php_sys_error(E_WARNING, "file[%s] is empty.", filename);
close(fd);
RETURN_FALSE;
}
if (offset > 0)
{
size = _stat.st_size - offset;
}
else
{
size = _stat.st_size;
}
}
void *addr = mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED, fd, offset);
if (addr == NULL)
{
swoole_php_sys_error(E_WARNING, "mmap(%ld) failed.", size);
RETURN_FALSE;
}
swMmapFile *res = emalloc(sizeof(swMmapFile));
res->filename = filename;
res->size = size;
res->offset = offset;
res->memory = addr;
res->ptr = addr;
close(fd);
php_stream *stream = php_stream_alloc(&mmap_ops, res, NULL, "r+");
php_stream_to_zval(stream, return_value);
}