-
Notifications
You must be signed in to change notification settings - Fork 1
/
swoole_buffer.c
381 lines (327 loc) · 11.9 KB
/
swoole_buffer.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
+----------------------------------------------------------------------+
| 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 |
| license@swoole.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
/*
让PHP开发者可以像C一样直接读写内存,提升程序的性能,又不用担心内存越界
swoole_buffer申请的内存并非共享内存,所以无法在多个进程间被共享
*/
#include "php_swoole.h"
static PHP_METHOD(swoole_buffer, __construct);
static PHP_METHOD(swoole_buffer, __destruct);
static PHP_METHOD(swoole_buffer, __toString);
static PHP_METHOD(swoole_buffer, append);
static PHP_METHOD(swoole_buffer, substr);
static PHP_METHOD(swoole_buffer, read);
static PHP_METHOD(swoole_buffer, write);
static PHP_METHOD(swoole_buffer, expand);
static PHP_METHOD(swoole_buffer, recycle);
static PHP_METHOD(swoole_buffer, clear);
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_buffer_construct, 0, 0, 0)
ZEND_ARG_INFO(0, size)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_buffer_void, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_buffer_expand, 0, 0, 1)
ZEND_ARG_INFO(0, size)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_buffer_substr, 0, 0, 1)
ZEND_ARG_INFO(0, offset)
ZEND_ARG_INFO(0, length)
ZEND_ARG_INFO(0, seek)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_buffer_write, 0, 0, 2)
ZEND_ARG_INFO(0, offset)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_buffer_read, 0, 0, 2)
ZEND_ARG_INFO(0, offset)
ZEND_ARG_INFO(0, length)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_buffer_append, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
static const zend_function_entry swoole_buffer_methods[] =
{
PHP_ME(swoole_buffer, __construct, arginfo_swoole_buffer_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_buffer, __destruct, arginfo_swoole_buffer_void, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(swoole_buffer, __toString, arginfo_swoole_buffer_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_buffer, substr, arginfo_swoole_buffer_substr, ZEND_ACC_PUBLIC)
PHP_ME(swoole_buffer, write, arginfo_swoole_buffer_write, ZEND_ACC_PUBLIC)
PHP_ME(swoole_buffer, read, arginfo_swoole_buffer_read, ZEND_ACC_PUBLIC)
PHP_ME(swoole_buffer, append, arginfo_swoole_buffer_append, ZEND_ACC_PUBLIC)
PHP_ME(swoole_buffer, expand, arginfo_swoole_buffer_expand, ZEND_ACC_PUBLIC)
PHP_ME(swoole_buffer, recycle, arginfo_swoole_buffer_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_buffer, clear, arginfo_swoole_buffer_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};
zend_class_entry swoole_buffer_ce;
zend_class_entry *swoole_buffer_class_entry_ptr;
//初始化
void swoole_buffer_init(int module_number TSRMLS_DC)
{
SWOOLE_INIT_CLASS_ENTRY(swoole_buffer_ce, "swoole_buffer", "Swoole\\Buffer", swoole_buffer_methods);
swoole_buffer_class_entry_ptr = zend_register_internal_class(&swoole_buffer_ce TSRMLS_CC);
swoole_buffer_class_entry_ptr->serialize = zend_class_serialize_deny;
swoole_buffer_class_entry_ptr->unserialize = zend_class_unserialize_deny;
SWOOLE_CLASS_ALIAS(swoole_buffer, "Swoole\\Buffer");
}
static void swoole_buffer_recycle(swString *buffer)
{
if (buffer->offset == 0)
{
return;
}
long length;
length = buffer->length - buffer->offset;
if (length > 0)
{
memmove(buffer->str, buffer->str + buffer->offset, length);
}
buffer->offset = 0;
buffer->length = length;
}
//实例化 参数一个size 也就是缓冲区大小,但是不能大于1024*1024*128 即128M
static PHP_METHOD(swoole_buffer, __construct)
{
long size = SW_STRING_BUFFER_DEFAULT;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &size) == FAILURE)
{
RETURN_FALSE;
}
if (size < 1)
{
zend_throw_exception(swoole_exception_class_entry_ptr, "buffer size can't be less than 0.", SW_ERROR_INVALID_PARAMS TSRMLS_CC);
RETURN_FALSE;
}
else if (size > SW_STRING_BUFFER_MAXLEN)
{
zend_throw_exception_ex(swoole_exception_class_entry_ptr, errno TSRMLS_CC, "buffer size can't exceed %d", SW_STRING_BUFFER_MAXLEN);
RETURN_FALSE;
}
//向系统申请内存,注意不是共享内存,时进程本身向系统申请的内存。
swString *buffer = swString_new(size);
if (buffer == NULL)
{
zend_throw_exception_ex(swoole_exception_class_entry_ptr, errno TSRMLS_CC, "malloc(%ld) failed.", size);
RETURN_FALSE;
}
swoole_set_object(getThis(), buffer);//把buffer指针保存到swoole_objcet
zend_update_property_long(swoole_buffer_class_entry_ptr, getThis(), ZEND_STRL("capacity"), size TSRMLS_CC); //buffer 中的数据量
zend_update_property_long(swoole_buffer_class_entry_ptr, getThis(), ZEND_STRL("length"), 0 TSRMLS_CC); //已经使用的数量 0
}
//销毁buffer
static PHP_METHOD(woole_buffer, __destruct)
{
SW_PREVENT_USER_DESTRUCT;
swString *buffer = swoole_get_object(getThis());
if (buffer)
{
swString_free(buffer);
}
}
//向buffer 中追加字符串
static PHP_METHOD(swoole_buffer, append)
{
swString str;
bzero(&str, sizeof(str));
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str.str, &str.length) == FAILURE)
{
RETURN_FALSE;
}
if (str.length < 1)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "string empty.");
RETURN_FALSE;
}
swString *buffer = swoole_get_object(getThis());
//容量不够时,且buffer.size 大于128M时报错
if ((str.length + buffer->length) > buffer->size && (str.length + buffer->length) > SW_STRING_BUFFER_MAXLEN)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "buffer size can't exceed %d", SW_STRING_BUFFER_MAXLEN);
RETURN_FALSE;
}
//追加
size_t size_old = buffer->size;
if (swString_append(buffer, &str) == SW_OK)
{
if (buffer->size > size_old)//如果是向系统申请内存更大的内存的话更新capacity
{
zend_update_property_long(swoole_buffer_class_entry_ptr, getThis(), ZEND_STRL("capacity"), buffer->size TSRMLS_CC);
}
zend_update_property_long(swoole_buffer_class_entry_ptr, getThis(), ZEND_STRL("length"),
buffer->length - buffer->offset TSRMLS_CC);
RETURN_LONG(buffer->length - buffer->offset);
}
else
{
RETURN_FALSE;
}
}
//截取字符串
static PHP_METHOD(swoole_buffer, substr)
{
long offset;
long length = -1;
zend_bool remove = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|lb", &offset, &length, &remove) == FAILURE)
{
RETURN_FALSE;
}
swString *buffer = swoole_get_object(getThis());
if (remove && !(offset == 0 && length <= buffer->length))
{
remove = 0;
}
if (offset < 0)
{
offset = buffer->length + offset;
}
offset += buffer->offset;
if (length < 0)
{
length = buffer->length - offset;
}
if (offset + length > buffer->length)//取得的字符串超过存放的长度的话,报错
{
swoole_php_error(E_WARNING, "offset(%ld, %ld) is out of bounds.", offset, length);
RETURN_FALSE;
}
if (remove)//移除的话,取出并删除
{
buffer->offset += length;
zend_update_property_long(swoole_buffer_class_entry_ptr, getThis(), ZEND_STRL("length"), buffer->length - buffer->offset TSRMLS_CC);
if (buffer->offset > SW_STRING_BUFFER_GARBAGE_MIN && buffer->offset * SW_STRING_BUFFER_GARBAGE_RATIO > buffer->size)
{
swoole_buffer_recycle(buffer);
}
}
SW_RETURN_STRINGL(buffer->str + offset, length, 1);
}
static PHP_METHOD(swoole_buffer, __toString)
{
swString *buffer = swoole_get_object(getThis());
SW_RETURN_STRINGL(buffer->str + buffer->offset, buffer->length - buffer->offset, 1);
}
//buffer 中写数据
static PHP_METHOD(swoole_buffer, write)
{
long offset;
swString str;
bzero(&str, sizeof(str));
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &offset, &str.str, &str.length) == FAILURE)
{
RETURN_FALSE;
}
if (str.length < 1)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "string to write is empty.");
RETURN_FALSE;
}
swString *buffer = swoole_get_object(getThis());
if (offset < 0)
{
offset = buffer->length - buffer->offset + offset;
}
if (offset < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "offset(%ld) is out of bounds.", offset);
RETURN_FALSE;
}
offset += buffer->offset;
if ((str.length + offset) > buffer->size && (str.length + offset) > SW_STRING_BUFFER_MAXLEN)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "buffer size can't exceed %d", SW_STRING_BUFFER_MAXLEN);
RETURN_FALSE;
}
size_t size_old = buffer->size;
if (swString_write(buffer, offset, &str) == SW_OK) //会自动扩容
{
if (buffer->size > size_old)
{
zend_update_property_long(swoole_buffer_class_entry_ptr, getThis(), ZEND_STRL("capacity"), buffer->size TSRMLS_CC);
}
zend_update_property_long(swoole_buffer_class_entry_ptr, getThis(), ZEND_STRL("length"),
buffer->length - buffer->offset TSRMLS_CC);
RETURN_LONG(buffer->length - buffer->offset);
}
else
{
RETURN_FALSE;
}
}
//从buffer 中读
static PHP_METHOD(swoole_buffer, read)
{
long offset;
long length;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &offset, &length) == FAILURE)
{
RETURN_FALSE;
}
swString *buffer = swoole_get_object(getThis());
if (offset < 0)
{
offset = buffer->length - buffer->offset + offset;
}
if (offset < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "offset(%ld) is out of bounds.", offset);
RETURN_FALSE;
}
offset += buffer->offset;
if (length > buffer->length - offset)
{
RETURN_FALSE;
}
SW_RETURN_STRINGL(buffer->str + offset, length, 1);
}
//扩容
static PHP_METHOD(swoole_buffer, expand)
{
long size = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &size) == FAILURE)
{
RETURN_FALSE;
}
swString *buffer = swoole_get_object(getThis());
if (size <= buffer->size)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "new size must be more than %ld", buffer->size);
RETURN_FALSE;
}
if (swString_extend(buffer, size) == SW_OK)
{
zend_update_property_long(swoole_buffer_class_entry_ptr, getThis(), ZEND_STRL("capacity"), size TSRMLS_CC);
RETURN_TRUE;
}
else
{
RETURN_FALSE;
}
}
static PHP_METHOD(swoole_buffer, recycle)
{
swString *buffer = swoole_get_object(getThis());
swoole_buffer_recycle(buffer);
zend_update_property_long(swoole_buffer_class_entry_ptr, getThis(), ZEND_STRL("length"), buffer->length TSRMLS_CC);
}
//清除 buffer
static PHP_METHOD(swoole_buffer, clear)
{
swString *buffer = swoole_get_object(getThis());
buffer->length = 0;
buffer->offset = 0;
zend_update_property_long(swoole_buffer_class_entry_ptr, getThis(), ZEND_STRL("length"), 0 TSRMLS_CC);
}