Skip to content

Commit dc4e353

Browse files
author
vip601115211
committed
修复bug
1 parent 6fa9178 commit dc4e353

File tree

4 files changed

+469
-81
lines changed

4 files changed

+469
-81
lines changed

arraylist.c

Lines changed: 95 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828

2929
#include "php_arraylist.h"
3030

31+
#ifndef ARRAY_LIST_SIZE
32+
#define ARRAY_LIST_SIZE 16
33+
#endif
34+
3135
/* For compatibility with older PHP versions */
3236
#ifndef ZEND_PARSE_PARAMETERS_NONE
3337
#define ZEND_PARSE_PARAMETERS_NONE() \
@@ -46,14 +50,6 @@ typedef struct _arraylist { /* {{{ */
4650

4751
typedef struct _arraylist_object { /* {{{ */
4852
arraylist array;
49-
zend_function *fptr_offset_get;
50-
zend_function *fptr_offset_set;
51-
zend_function *fptr_offset_has;
52-
zend_function *fptr_offset_del;
53-
zend_function *fptr_count;
54-
int current;
55-
int flags;
56-
zend_class_entry *ce_get_iterator;
5753
zend_object std;
5854
} arraylist_object;
5955
/* }}} */
@@ -71,41 +67,51 @@ static inline arraylist_object *arraylist_from_obj(zend_object *obj) /* {{{ */ {
7167

7268

7369

74-
static void arraylist_init(arraylist *array, zend_long size) /* {{{ */
70+
71+
72+
static void destruct(arraylist *array)/* {{{ */
7573
{
76-
if (size > 0) {
77-
array->size = 0; /* reset size in case ecalloc() fails */
78-
array->key = 0;
79-
array->elements = ecalloc(size, sizeof(zval));
80-
array->size = size;
81-
} else {
74+
if (array->size > 0)
75+
{
76+
efree(array->elements);
8277
array->elements = NULL;
8378
array->size = 0;
8479
array->key = 0;
8580
}
86-
8781
}
8882
/* }}} */
8983

84+
static void arraylist_init(arraylist *array, zend_long size) /* {{{ */
85+
{
86+
if (size > 0) {
87+
array->size = 0; /* reset size in case ecalloc() fails */
88+
array->key = 0;
89+
array->elements = NULL;
90+
array->elements = (zval *)ecalloc(size, sizeof(zval));
91+
array->size = size;
92+
}
9093

94+
}
95+
/* }}} */
9196

9297
static void resize(arraylist *array)
9398
{
9499
if (array->key == array->size)
95100
{
96101
zend_long oldSize = array->size == 1? 2 : array->size;
97102
zend_long newSize = oldSize + (oldSize >> 1);
98-
99103
zval *elements;
100-
elements = ecalloc(newSize, sizeof(zval));
104+
elements = (zval *)ecalloc(newSize, sizeof(zval));
101105
zend_long i = 0;
102-
for (; i < newSize; i++)
106+
for (; i < array->size; i++)
103107
{
104108
elements[i] = array->elements[i];
105109
}
106-
efree(&array->elements[i]);
110+
efree(array->elements);
111+
array->elements = NULL;
107112
array->elements = elements;
108113
array->size = newSize;
114+
109115
}
110116
}
111117

@@ -122,31 +128,6 @@ static void arraylist_add(arraylist *array, zval *val) /* {{{ */
122128
/* }}} */
123129

124130

125-
/* }}}*/
126-
127-
/* {{{ PHP_RINIT_FUNCTION
128-
*/
129-
PHP_RINIT_FUNCTION(arraylist)
130-
{
131-
#if defined(ZTS) && defined(COMPILE_DL_ARRAYLIST)
132-
ZEND_TSRMLS_CACHE_UPDATE();
133-
#endif
134-
135-
return SUCCESS;
136-
}
137-
/* }}} */
138-
139-
/* {{{ PHP_MINFO_FUNCTION phpinfo显示扩展信息
140-
*/
141-
PHP_MINFO_FUNCTION(arraylist)
142-
{
143-
php_info_print_table_start();
144-
php_info_print_table_row(2, "arraylist support", "enabled");
145-
php_info_print_table_row(2, "Version", PHP_ARRAYLIST_VERSION);
146-
php_info_print_table_end();
147-
}
148-
/* }}} */
149-
150131
/* {{{ arginfo
151132
*/
152133
ZEND_BEGIN_ARG_INFO_EX(arginfo_arraylist__construct, 0, 0, 1)
@@ -170,7 +151,7 @@ ZEND_END_ARG_INFO()
170151
*/
171152
PHP_METHOD(arraylist, __construct)
172153
{
173-
zend_long size = 16;
154+
zend_long size = ARRAY_LIST_SIZE;
174155

175156
zval *object = getThis();
176157
arraylist_object *intern;
@@ -216,14 +197,14 @@ PHP_METHOD(arraylist, get)
216197
zval *object = getThis();
217198
arraylist_object *intern;
218199
zend_long key;
219-
zval *element;
220200

221201
ZEND_PARSE_PARAMETERS_START(1, 1)
222202
Z_PARAM_LONG(key)
223203
ZEND_PARSE_PARAMETERS_END();
224204

225205

226206
intern = Z_ARRAYLIST_P(object);
207+
227208
if (key <= intern->array.size)
228209
{
229210
RETURN_ZVAL(&intern->array.elements[key], 0, 1);
@@ -290,6 +271,19 @@ PHP_METHOD(arraylist, toArray)
290271
}
291272
/* }}} */
292273

274+
PHP_METHOD(arraylist, __destruct)
275+
{
276+
zval *object = getThis();
277+
arraylist_object *intern;
278+
279+
if (zend_parse_parameters_none() == FAILURE) {
280+
return;
281+
}
282+
283+
intern = Z_ARRAYLIST_P(object);
284+
destruct(&intern->array);
285+
}
286+
293287
/* {{{ arraylist_functions[] 扩展函数
294288
*/
295289
static const zend_function_entry arraylist_functions[] = {
@@ -306,25 +300,49 @@ static const zend_function_entry arraylist_methods[] = {
306300
PHP_ME(arraylist, count, arginfo_arraylist_void, ZEND_ACC_PUBLIC)
307301
PHP_ME(arraylist, toArray, arginfo_arraylist_void, ZEND_ACC_PUBLIC)
308302
PHP_ME(arraylist, getSize, arginfo_arraylist_void, ZEND_ACC_PUBLIC)
303+
PHP_ME(arraylist, __destruct, arginfo_arraylist_void, ZEND_ACC_PUBLIC)
309304
PHP_FE_END
310305
};
311306
/* }}} */
312307

313-
/* {{{ arraylist_module_entry
308+
/* {{{ PHP_MSHUTDOWN_FUNCTION
314309
*/
315-
zend_module_entry arraylist_module_entry = {
316-
STANDARD_MODULE_HEADER,
317-
"arraylist", /* 扩展名 name */
318-
arraylist_functions, /* 注册函数,注意这里不是注册方法 */
319-
PHP_MINIT(arraylist), /* PHP_MINIT - Module initialization */
320-
NULL, /* PHP_MSHUTDOWN - Module shutdown */
321-
NULL, /* PHP_RINIT - Request initialization */
322-
NULL, /* PHP_RSHUTDOWN - Request shutdown */
323-
NULL, /*
324-
- Module info */
325-
PHP_ARRAYLIST_VERSION, /* Version */
326-
STANDARD_MODULE_PROPERTIES
327-
};
310+
PHP_MSHUTDOWN_FUNCTION(arraylist)
311+
{
312+
return SUCCESS;
313+
}
314+
/* }}} */
315+
316+
317+
/* {{{ PHP_RSHUTDOWN_FUNCTION
318+
*/
319+
PHP_RSHUTDOWN_FUNCTION(arraylist)
320+
{
321+
return SUCCESS;
322+
}
323+
324+
325+
/* {{{ PHP_RINIT_FUNCTION
326+
*/
327+
PHP_RINIT_FUNCTION(arraylist)
328+
{
329+
#if defined(ZTS) && defined(COMPILE_DL_ARRAYLIST)
330+
ZEND_TSRMLS_CACHE_UPDATE();
331+
#endif
332+
333+
return SUCCESS;
334+
}
335+
/* }}} */
336+
337+
/* {{{ PHP_MINFO_FUNCTION phpinfo显示扩展信息
338+
*/
339+
PHP_MINFO_FUNCTION(arraylist)
340+
{
341+
php_info_print_table_start();
342+
php_info_print_table_row(2, "arraylist support", "enabled");
343+
php_info_print_table_row(2, "Version", PHP_ARRAYLIST_VERSION);
344+
php_info_print_table_end();
345+
}
328346
/* }}} */
329347

330348
// 模块初始化时调用
@@ -350,11 +368,25 @@ PHP_MINIT_FUNCTION(arraylist) /* {{{ */ {
350368
}
351369
/* }}} */
352370

371+
/* {{{ arraylist_module_entry
372+
*/
373+
zend_module_entry arraylist_module_entry = {
374+
STANDARD_MODULE_HEADER,
375+
"arraylist", /* 扩展名 name */
376+
arraylist_functions, /* 注册函数,注意这里不是注册方法 */
377+
PHP_MINIT(arraylist), /* PHP_MINIT - Module initialization */
378+
PHP_MSHUTDOWN(arraylist), /* PHP_MSHUTDOWN - Module shutdown */
379+
PHP_RINIT(arraylist), /* PHP_RINIT - Request initialization */
380+
PHP_RSHUTDOWN(arraylist), /* PHP_RSHUTDOWN - Request shutdown */
381+
PHP_MINFO(arraylist), /* - Module info */
382+
PHP_ARRAYLIST_VERSION, /* Version */
383+
STANDARD_MODULE_PROPERTIES
384+
};
385+
/* }}} */
353386

354387
#ifdef COMPILE_DL_ARRAYLIST
355388
# ifdef ZTS
356389
ZEND_TSRMLS_CACHE_DEFINE()
357390
# endif
358391
ZEND_GET_MODULE(arraylist)
359-
#endif
360-
392+
#endif

arraylist.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php
2-
$ret = new ArrayList();
3-
$ret->add(1);
4-
var_dump($ret->get(0));
5-
var_dump($ret->count());
6-
var_dump($ret->getSize());
2+
$ret = new ArrayList(3);
3+
//$ret->add(22);
4+
const SIZE = 100;
5+
6+
$ret2 = new ArrayList(SIZE);
7+
for ($i = 0; $i < SIZE; $i++)
8+
{
9+
$ret2->add($i);
10+
}

0 commit comments

Comments
 (0)