Skip to content

Commit 12958ab

Browse files
author
wxxiong6
authored
Update php_arraylist.h
1 parent 8ebf4f3 commit 12958ab

File tree

1 file changed

+12
-364
lines changed

1 file changed

+12
-364
lines changed

php_arraylist.h

Lines changed: 12 additions & 364 deletions
Original file line numberDiff line numberDiff line change
@@ -18,375 +18,23 @@
1818
*/
1919

2020

21-
#ifdef HAVE_CONFIG_H
22-
# include "config.h"
23-
#endif
24-
25-
#include "php.h"
26-
#include "ext/standard/info.h"
27-
#include "zend_exceptions.h"
21+
#ifndef PHP_ARRAYLIST_H
22+
# define PHP_ARRAYLIST_H
2823

29-
#include "php_arraylist.h"
24+
extern zend_module_entry arraylist_module_entry;
25+
# define phpext_arraylist_ptr &arraylist_module_entry
3026

31-
#ifndef ARRAY_LIST_SIZE
32-
#define ARRAY_LIST_SIZE 16
33-
#endif
27+
# define PHP_ARRAYLIST_VERSION "0.1.0"
3428

35-
/* For compatibility with older PHP versions */
36-
#ifndef ZEND_PARSE_PARAMETERS_NONE
37-
#define ZEND_PARSE_PARAMETERS_NONE() \
38-
ZEND_PARSE_PARAMETERS_START(0, 0) \
39-
ZEND_PARSE_PARAMETERS_END()
29+
#ifndef PHP_FE_END
30+
#define PHP_FE_END { NULL, NULL, NULL }
4031
#endif
4132

42-
zend_class_entry *array_list_ce;
43-
44-
typedef struct _arraylist { /* {{{ */
45-
zend_long size; //数组大小
46-
zend_long key; //数组当前使用key
47-
zval *elements; //数组元素
48-
} arraylist;
49-
/* }}} */
50-
51-
typedef struct _arraylist_object { /* {{{ */
52-
arraylist array;
53-
zend_object std;
54-
} arraylist_object;
55-
/* }}} */
56-
57-
58-
59-
60-
static inline arraylist_object *arraylist_from_obj(zend_object *obj) /* {{{ */ {
61-
return (arraylist_object*)((char*)(obj) - XtOffsetOf(arraylist_object, std));
62-
}
63-
/* }}} */
64-
65-
#define Z_ARRAYLIST_P(zv) arraylist_from_obj(Z_OBJ_P((zv)))
66-
67-
68-
69-
70-
71-
72-
static void destruct(arraylist *array)/* {{{ */
73-
{
74-
if (array->size > 0)
75-
{
76-
efree(array->elements);
77-
array->elements = NULL;
78-
array->size = 0;
79-
array->key = 0;
80-
}
81-
}
82-
/* }}} */
83-
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-
}
93-
94-
}
95-
/* }}} */
96-
97-
static void resize(arraylist *array)
98-
{
99-
if (array->key == array->size)
100-
{
101-
zend_long oldSize = array->size == 1? 2 : array->size;
102-
zend_long newSize = oldSize + (oldSize >> 1);
103-
zval *elements;
104-
elements = (zval *)ecalloc(newSize, sizeof(zval));
105-
zend_long i = 0;
106-
for (; i < array->size; i++)
107-
{
108-
elements[i] = array->elements[i];
109-
}
110-
efree(array->elements);
111-
array->elements = NULL;
112-
array->elements = elements;
113-
array->size = newSize;
114-
115-
}
116-
}
117-
118-
static void arraylist_add(arraylist *array, zval *val) /* {{{ */
119-
{
120-
if (!array)
121-
{
122-
return ;
123-
}
124-
resize(array);
125-
ZVAL_COPY(&array->elements[array->key], val);
126-
array->key++;
127-
}
128-
/* }}} */
129-
130-
131-
/* {{{ arginfo
132-
*/
133-
ZEND_BEGIN_ARG_INFO_EX(arginfo_arraylist__construct, 0, 0, 1)
134-
ZEND_ARG_INFO(0, size)
135-
ZEND_END_ARG_INFO()
136-
137-
ZEND_BEGIN_ARG_INFO_EX(arginfo_arraylist_add, 0, 0, 1)
138-
ZEND_ARG_INFO(0, val)
139-
ZEND_END_ARG_INFO()
140-
141-
ZEND_BEGIN_ARG_INFO_EX(arginfo_arraylist_get, 0, 0, 1)
142-
ZEND_ARG_INFO(0, key)
143-
ZEND_END_ARG_INFO()
144-
145-
ZEND_BEGIN_ARG_INFO(arginfo_arraylist_void, 0)
146-
ZEND_END_ARG_INFO()
147-
/* }}} */
148-
149-
150-
/* {{{ void arraylist::__construct()
151-
*/
152-
PHP_METHOD(arraylist, __construct)
153-
{
154-
zend_long size = ARRAY_LIST_SIZE;
155-
156-
zval *object = getThis();
157-
arraylist_object *intern;
158-
intern = Z_ARRAYLIST_P(object);
159-
160-
ZEND_PARSE_PARAMETERS_START(0, 1)
161-
Z_PARAM_OPTIONAL
162-
Z_PARAM_LONG(size)
163-
ZEND_PARSE_PARAMETERS_END();
164-
165-
166-
167-
arraylist_init(&intern->array, size);
168-
169-
}
170-
/* }}} */
171-
172-
/* {{{ string arraylist::add( [ $val ] )
173-
*/
174-
PHP_METHOD(arraylist, add)
175-
{
176-
177-
zval *object = getThis();
178-
arraylist_object *intern;
179-
size_t var_len;
180-
zval *val;
181-
182-
ZEND_PARSE_PARAMETERS_START(1, 1)
183-
Z_PARAM_ZVAL(val)
184-
ZEND_PARSE_PARAMETERS_END();
185-
186-
187-
intern = Z_ARRAYLIST_P(object);
188-
arraylist_add(&intern->array, val);
189-
190-
RETURN_TRUE;
191-
}
192-
193-
/* {{{ zval arraylist::get(int $i)
194-
*/
195-
PHP_METHOD(arraylist, get)
196-
{
197-
zval *object = getThis();
198-
arraylist_object *intern;
199-
zend_long key;
200-
201-
ZEND_PARSE_PARAMETERS_START(1, 1)
202-
Z_PARAM_LONG(key)
203-
ZEND_PARSE_PARAMETERS_END();
204-
205-
206-
intern = Z_ARRAYLIST_P(object);
207-
208-
if (key <= intern->array.size)
209-
{
210-
RETURN_ZVAL(&intern->array.elements[key], 0, 1);
211-
}
212-
else
213-
{
214-
RETURN_NULL();
215-
}
216-
}
217-
218-
PHP_METHOD(arraylist, count)
219-
{
220-
zval *object = getThis();
221-
arraylist_object *intern;
222-
223-
if (zend_parse_parameters_none() == FAILURE) {
224-
return;
225-
}
226-
227-
intern = Z_ARRAYLIST_P(object);
228-
RETURN_LONG(intern->array.key);
229-
}
230-
231-
PHP_METHOD(arraylist, getSize)
232-
{
233-
zval *object = getThis();
234-
arraylist_object *intern;
235-
236-
if (zend_parse_parameters_none() == FAILURE) {
237-
return;
238-
}
239-
240-
intern = Z_ARRAYLIST_P(object);
241-
RETURN_LONG(intern->array.size);
242-
}
243-
244-
/* {{{ proto object arraylist::toArray()
245-
*/
246-
PHP_METHOD(arraylist, toArray)
247-
{
248-
arraylist_object *intern;
249-
250-
if (zend_parse_parameters_none() == FAILURE) {
251-
return;
252-
}
253-
254-
intern = Z_ARRAYLIST_P(getThis());
255-
256-
if (intern->array.size > 0) {
257-
int i = 0;
258-
259-
array_init_size(return_value, intern->array.size);
260-
for (; i < intern->array.size; i++) {
261-
if (!Z_ISUNDEF(intern->array.elements[i])) {
262-
zend_hash_index_update(Z_ARRVAL_P(return_value), i, &intern->array.elements[i]);
263-
Z_TRY_ADDREF(intern->array.elements[i]);
264-
} else {
265-
zend_hash_index_update(Z_ARRVAL_P(return_value), i, &EG(uninitialized_zval));
266-
}
267-
}
268-
} else {
269-
RETURN_NULL();
270-
}
271-
}
272-
/* }}} */
273-
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-
287-
/* {{{ arraylist_functions[] 扩展函数
288-
*/
289-
static const zend_function_entry arraylist_functions[] = {
290-
PHP_FE_END
291-
};
292-
/* }}} */
293-
294-
/* {{{ arraylist__methods[] 扩展类方法
295-
*/
296-
static const zend_function_entry arraylist_methods[] = {
297-
PHP_ME(arraylist, __construct, arginfo_arraylist__construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
298-
PHP_ME(arraylist, add, arginfo_arraylist_add, ZEND_ACC_PUBLIC)
299-
PHP_ME(arraylist, get, arginfo_arraylist_get, ZEND_ACC_PUBLIC)
300-
PHP_ME(arraylist, count, arginfo_arraylist_void, ZEND_ACC_PUBLIC)
301-
PHP_ME(arraylist, toArray, arginfo_arraylist_void, ZEND_ACC_PUBLIC)
302-
PHP_ME(arraylist, getSize, arginfo_arraylist_void, ZEND_ACC_PUBLIC)
303-
PHP_ME(arraylist, __destruct, arginfo_arraylist_void, ZEND_ACC_PUBLIC)
304-
PHP_FE_END
305-
};
306-
/* }}} */
307-
308-
/* {{{ PHP_MSHUTDOWN_FUNCTION
309-
*/
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-
}
346-
/* }}} */
347-
348-
// 模块初始化时调用
349-
PHP_MINIT_FUNCTION(arraylist) /* {{{ */ {
350-
zend_class_entry ce;
351-
//REGISTER_INI_ENTRIES(); // 注册ini
352-
353-
//注册常量
354-
//第一个参数是常量的名字,
355-
//第二个参数来表明长度
356-
//CONST_CS标识是否大小写敏感
357-
REGISTER_STRINGL_CONSTANT(
358-
"ARRAYLIST_VERSION",
359-
PHP_ARRAYLIST_VERSION,
360-
sizeof(PHP_ARRAYLIST_VERSION)-1,
361-
CONST_CS|CONST_PERSISTENT
362-
);
363-
364-
INIT_CLASS_ENTRY(ce, "ArrayList", arraylist_methods); //注册类及类方法
365-
array_list_ce = zend_register_internal_class(&ce);
33+
# if defined(ZTS) && defined(COMPILE_DL_ARRAYLIST)
34+
ZEND_TSRMLS_CACHE_EXTERN()
35+
# endif
36+
PHP_MINIT_FUNCTION(arraylist);
36637

367-
return SUCCESS;
368-
}
369-
/* }}} */
37038

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-
/* }}} */
38639

387-
#ifdef COMPILE_DL_ARRAYLIST
388-
# ifdef ZTS
389-
ZEND_TSRMLS_CACHE_DEFINE()
390-
# endif
391-
ZEND_GET_MODULE(arraylist)
392-
#endif
40+
#endif /* PHP_ARRAYLIST_H */

0 commit comments

Comments
 (0)