forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
swoole_memory_pool.c
354 lines (307 loc) · 10 KB
/
swoole_memory_pool.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
/*
+----------------------------------------------------------------------+
| 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: Xinyu Zhu <[email protected]> |
| shiguangqi <[email protected]> |
| Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
enum memory_pool_type
{
memory_pool_type_fixed = 0,
memory_pool_type_ring = 1,
memory_pool_type_global = 2,
memory_pool_type_malloc = 3,
memory_pool_type_emalloc = 4,
};
static PHP_METHOD(swoole_memory_pool, __construct);
static PHP_METHOD(swoole_memory_pool, __destruct);
static PHP_METHOD(swoole_memory_pool, alloc);
static PHP_METHOD(swoole_memory_pool_slice, read);
static PHP_METHOD(swoole_memory_pool_slice, write);
static PHP_METHOD(swoole_memory_pool_slice, __destruct);
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_void, 0, 0, 0)
ZEND_END_ARG_INFO()
zend_class_entry *ce;
zend_class_entry *ce_slice;
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_memory_pool_construct, 0, 0, 2)
ZEND_ARG_INFO(0, size)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, slice_size)
ZEND_ARG_INFO(0, shared)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_memory_pool_alloc, 0, 0, 0)
ZEND_ARG_INFO(0, size)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_memory_pool_slice_read, 0, 0, 0)
ZEND_ARG_INFO(0, size)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_memory_pool_slice_write, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()
typedef struct
{
size_t size;
size_t slice_size;
uint8_t type;
zend_bool shared;
zend_bool released;
swMemoryPool* pool;
sw_atomic_t slice_count;
} MemoryPool;
typedef struct
{
size_t size;
uint8_t type;
MemoryPool* pool;
void *memory;
} MemorySlice;
static const zend_function_entry swoole_memory_pool_methods[] =
{
PHP_ME(swoole_memory_pool, __construct, arginfo_swoole_memory_pool_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_memory_pool, __destruct, arginfo_swoole_void, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(swoole_memory_pool, alloc, arginfo_swoole_memory_pool_alloc, ZEND_ACC_PUBLIC)
PHP_FE_END
};
static const zend_function_entry swoole_memory_pool_slice_methods[] =
{
PHP_ME(swoole_memory_pool_slice, read, arginfo_swoole_memory_pool_slice_read, ZEND_ACC_PUBLIC)
PHP_ME(swoole_memory_pool_slice, write, arginfo_swoole_memory_pool_slice_write, ZEND_ACC_PUBLIC)
PHP_ME(swoole_memory_pool_slice, __destruct, arginfo_swoole_void, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_FE_END
};
void swoole_memory_pool_init(int module_number TSRMLS_DC)
{
static zend_class_entry _ce;
INIT_CLASS_ENTRY(_ce, "Swoole\\Memory\\Pool", swoole_memory_pool_methods);
ce = zend_register_internal_class(&_ce TSRMLS_CC);
static zend_class_entry _ce_slice;
INIT_CLASS_ENTRY(_ce_slice, "Swoole\\Memory\\Pool\\Slice", swoole_memory_pool_slice_methods);
ce_slice = zend_register_internal_class(&_ce_slice TSRMLS_CC);
zend_declare_class_constant_long(ce, SW_STRL("TYPE_RING")-1, memory_pool_type_ring TSRMLS_CC);
zend_declare_class_constant_long(ce, SW_STRL("TYPE_GLOBAL")-1, memory_pool_type_global TSRMLS_CC);
zend_declare_class_constant_long(ce, SW_STRL("TYPE_FIXED")-1, memory_pool_type_fixed TSRMLS_CC);
zend_declare_class_constant_long(ce, SW_STRL("TYPE_MALLOC")-1, memory_pool_type_malloc TSRMLS_CC);
zend_declare_class_constant_long(ce, SW_STRL("TYPE_EMALLOC")-1, memory_pool_type_emalloc TSRMLS_CC);
}
static PHP_METHOD(swoole_memory_pool, __construct)
{
zend_long size, type, slice_size;
zend_bool shared = 0;
ZEND_PARSE_PARAMETERS_START(2, 4)
Z_PARAM_LONG(size)
Z_PARAM_LONG(type)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(slice_size)
Z_PARAM_BOOL(shared)
ZEND_PARSE_PARAMETERS_END();
swMemoryPool* pool = NULL;
if (type == memory_pool_type_fixed)
{
void *memory = (shared == 1) ? sw_shm_malloc(size) : sw_malloc(size);
if (memory == NULL)
{
zend_throw_exception(swoole_exception_class_entry_ptr, "malloc failed.", SW_ERROR_MALLOC_FAIL TSRMLS_CC);
RETURN_FALSE;
}
pool = swFixedPool_new2(slice_size, memory, size);
}
else if (type == memory_pool_type_ring)
{
pool = swRingBuffer_new(size, shared);
}
else if (type == memory_pool_type_global)
{
pool = swMemoryGlobal_new(slice_size, shared);
}
else if (type == memory_pool_type_malloc || type == memory_pool_type_malloc)
{
shared = 0;
}
else
{
zend_throw_exception(swoole_exception_class_entry_ptr, "unknown memory pool type.", SW_ERROR_INVALID_PARAMS TSRMLS_CC);
RETURN_FALSE;
}
MemoryPool *mp;
if (shared)
{
mp = (MemoryPool *) SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(MemorySlice));
}
else
{
mp = (MemoryPool *) emalloc(sizeof(MemorySlice));
}
if (mp == NULL)
{
zend_throw_exception(swoole_exception_class_entry_ptr, "malloc failed.", SW_ERROR_MALLOC_FAIL TSRMLS_CC);
RETURN_FALSE;
}
mp->size = size;
mp->slice_size = slice_size;
mp->shared = shared;
mp->type = type;
mp->pool = pool;
mp->slice_count = 0;
mp->released = 0;
swoole_set_object(getThis(), mp);
}
static PHP_METHOD(swoole_memory_pool, alloc)
{
MemoryPool* mp = (MemoryPool*) swoole_get_object(getThis());
zend_long size = mp->slice_size;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(size)
ZEND_PARSE_PARAMETERS_END();
if (mp->type != memory_pool_type_fixed && size <= 0)
{
zend_throw_exception(swoole_exception_class_entry_ptr, "invalid size.", SW_ERROR_INVALID_PARAMS TSRMLS_CC);
RETURN_FALSE;
}
void *memory;
if (mp->type == memory_pool_type_malloc)
{
memory = sw_malloc(size);
}
else if (mp->type == memory_pool_type_emalloc)
{
memory = emalloc(size);
}
else
{
memory = mp->pool->alloc(mp->pool, size);
}
if (memory == NULL)
{
RETURN_FALSE;
}
MemorySlice *info = (MemorySlice *) emalloc(sizeof(MemorySlice));
object_init_ex(return_value, ce_slice);
info->pool = mp;
info->size = size;
info->memory = memory;
info->type = mp->type;
sw_atomic_fetch_add(&mp->slice_count, 1);
swoole_set_object(return_value, info);
}
static PHP_METHOD(swoole_memory_pool, __destruct)
{
SW_PREVENT_USER_DESTRUCT;
MemoryPool* mp = (MemoryPool*) swoole_get_object(getThis());
if (mp == NULL)
{
return;
}
swoole_set_object(getThis(), NULL);
if (mp->type == memory_pool_type_malloc || mp->type == memory_pool_type_malloc)
{
efree(mp);
return;
}
mp->released = 1;
if (mp->slice_count == 0)
{
mp->pool->destroy(mp->pool);
if (mp->shared == 0)
{
efree(mp);
}
}
}
static PHP_METHOD(swoole_memory_pool_slice, read)
{
zend_long size = 0;
zend_long offset = 0;
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(size)
Z_PARAM_LONG(offset)
ZEND_PARSE_PARAMETERS_END();
MemorySlice *info = (MemorySlice *) swoole_get_object(getThis());
if (size <= 0)
{
size = info->size;
}
else if (size > info->size)
{
size = info->size;
swoole_php_error(E_WARNING, "size(%ld) is too big.", size);
}
if (offset < 0 || offset + size > info->size)
{
swoole_php_error(E_WARNING, "offset(%ld) is out of bounds.", offset);
RETURN_FALSE;
}
RETURN_STRINGL((char * )info->memory + offset, size);
}
static PHP_METHOD(swoole_memory_pool_slice, write)
{
zend_string *data;
zend_long size = 0;
zend_long offset = 0;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(data);
Z_PARAM_OPTIONAL
Z_PARAM_LONG(offset)
ZEND_PARSE_PARAMETERS_END();
MemorySlice *info = (MemorySlice *) swoole_get_object(getThis());
size = data->len;
if (size > info->size)
{
swoole_php_error(E_WARNING, "data is too large:%zd.", size);
RETURN_FALSE;
}
if (offset < 0 || offset + size > info->size)
{
swoole_php_error(E_WARNING, "offset(%ld) is out of bounds.", offset);
RETURN_FALSE;
}
memcpy((char *) info->memory + offset, data->val, size);
RETURN_TRUE;
}
static PHP_METHOD(swoole_memory_pool_slice, __destruct)
{
SW_PREVENT_USER_DESTRUCT;
MemorySlice *info = (MemorySlice *) swoole_get_object(getThis());
if (info == NULL)
{
return;
}
MemoryPool *mp = info->pool;
if (info->type == memory_pool_type_malloc)
{
sw_free(info->memory);
}
else if (info->type == memory_pool_type_emalloc)
{
efree(info->memory);
}
else
{
mp->pool->free(mp->pool, info->memory);
sw_atomic_fetch_sub(&mp->slice_count, 1);
if (mp->released && mp->slice_count == 0)
{
mp->pool->destroy(mp->pool);
if (mp->shared == 0)
{
efree(mp);
}
}
}
swoole_set_object(getThis(), NULL);
efree(info);
}