Skip to content

Commit 85033b3

Browse files
committed
add swoole_mmap
1 parent 7859c88 commit 85033b3

File tree

1 file changed

+224
-0
lines changed

1 file changed

+224
-0
lines changed

swoole_mmap.c

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

0 commit comments

Comments
 (0)