日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux php c 扩展,linux下编写php5.6的C扩展模块(双向链表)

發(fā)布時間:2025/3/12 linux 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux php c 扩展,linux下编写php5.6的C扩展模块(双向链表) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

cd /usr/local/src/php-5.6.7/ext/

./ext_skel --extname=php_list

cd php_list

vim config.m4

PHP_ARG_ENABLE(php_list, whether to enable php_list support,

dnl Make sure that the comment is aligned:

[? --enable-php_list?????????? Enable php_list support])

(dnl是注釋標(biāo)簽)

vim list.h

typedef struct list_node

{

zval *value;

struct list_node *prev;

struct list_node *next;

} list_node;

typedef struct list_head

{

int size;

list_node *head;

list_node *tail;

} list_head;

list_head *list_create()

{

list_head *head;

head = (list_head *) malloc(sizeof(list_head));

if(head)

{

head->size = 0;

head->head = NULL;

head->tail = NULL;

}

return head;

}

int list_add_head(list_head *head, zval *value)

{

list_node *node;

node = (list_node *)malloc(sizeof(*node));

if(!node)

{

return 0;

}

node->value = value;

node->prev = NULL;

node->next = head->head;

if(head->head)

{

head->head->prev = node;

}

head->head = node;

if(!head->tail)

{

head->tail = head->head;

}

head->size++;

return 1;

}

int list_add_tail(list_head *head, zval *value)

{

list_node *node;

node = (list_node *)malloc(sizeof(*node));

if(!node)

{

return 0;

}

node->value = value;

node->prev = head->tail;

node->next = NULL;

if(head->tail)

{

head->tail->next = node;

}

head->tail = node;

if(!head->head)

{

head->head = head->tail;

}

head->size++;

return 1;

}

int list_delete_index(list_head *head, int index)

{

list_node *curr;

if(index < 0)

{

index = (-index)-1;

curr = head->tail;

while(index > 0)

{

curr = curr->prev;

index--;

}

}

else

{

curr = head->head;

while(index > 0)

{

curr = curr->next;

index--;

}

}

if(!curr || index > 0)

{

return 0;

}

if(curr->prev)

{

curr->prev->next = curr->next;

}

else

{

head->head = curr->next;

}

if(curr->next)

{

curr->next->prev = curr->prev;

head->tail = curr->prev;

}

return 1;

}

int list_fetch(list_head *head, int index, zval **retval)

{

list_node *node;

if(index >= 0)

{

node = head->head;

while(node && index > 0)

{

node = node->next;

index--;

}

}

else

{

index = (-index) - 1;

node = head->tail;

while(node && index > 0)

{

node = node->prev;

index--;

}

}

if(!node || index > 0)

{

return 0;

}

*retval = node->value;

return 1;

}

int list_length(list_head *head)

{

if(head)

{

return head->size;

}

else

{

return 0;

}

}

void list_destroy(list_head *head)

{

list_node *curr, *next;

curr = head->head;

while(curr)

{

next = curr->next;

free(curr);

curr = next;

}

free(head);

}

vi php_list.c

/*

+----------------------------------------------------------------------+

| PHP Version 5??????????????????????????????????????????????????????? |

+----------------------------------------------------------------------+

| Copyright (c) 1997-2015 The PHP Group??????????????????????????????? |

+----------------------------------------------------------------------+

| This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt????????????????????????????????? |

| If you did not receive a copy of the PHP license and are unable to?? |

| obtain it through the world-wide-web, please send a note to????????? |

| license@php.net so we can mail you a copy immediately.?????????????? |

+----------------------------------------------------------------------+

| Author:????????????????????????????????????????????????????????????? |

+----------------------------------------------------------------------+

*/

/* $Id$ */

#ifdef HAVE_CONFIG_H

#include "config.h"

#endif

#include "php.h"

#include "php_ini.h"

#include "ext/standard/info.h"

#include "php_php_list.h"

#include "list.h"

/* True global resources - no need for thread safety here */

static int le_php_list;

static int freed = 0;

void list_destroy_handler(zend_rsrc_list_entry *rsrc TSRMLS_DC)

{

if(!freed)

{

list_head *list;

list = (list_head *)rsrc->ptr;

list_destroy(list);

freed = 1;

}

}

/* {{{ PHP_INI

*/

/* Remove comments and fill if you need to have entries in php.ini

PHP_INI_BEGIN()

PHP_INI_END()

*/

/* }}} */

/* Remove the following function when you have successfully modified config.m4

so that your module can be compiled into PHP, it exists only for testing

purposes. */

/* Every user-visible function in PHP should document itself in the source */

/* {{{ proto string confirm_php_list_compiled(string arg)

Return a string to confirm that the module is compiled in */

PHP_FUNCTION(confirm_php_list_compiled)

{

char *arg = NULL;

int arg_len, len;

char *strg;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {

return;

}

RETURN_STRINGL(strg, len, 0);

}

PHP_FUNCTION(list_create)

{

list_head *list;

list = list_create();

if(!list)

{

RETURN_NULL();

}

else

{

ZEND_REGISTER_RESOURCE(return_value, list, le_php_list);

}

}

PHP_FUNCTION(list_add_head)

{

zval *value;

zval *lrc;

list_head *list;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &lrc, &value) == FAILURE)

{

RETURN_FALSE;

}

ZEND_FETCH_RESOURCE(list, list_head *, &lrc, -1, "List Resource", le_php_list);

list_add_head(list, value);

RETURN_TRUE;

}

PHP_FUNCTION(list_fetch_head)

{

zval *lrc, *retval;

list_head *list;

int res;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &lrc) == FAILURE)

{

RETURN_FALSE;

}

ZEND_FETCH_RESOURCE(list, list_head *, &lrc, -1, "List Resource", le_php_list);

res = list_fetch(list, 0, &retval);

if(!res)

{

RETURN_NULL();

}

else

{

RETURN_ZVAL(retval, 1, 0);

}

}

PHP_FUNCTION(list_add_tail)

{

zval *value;

zval *lrc;

list_head *list;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &lrc, &value) == FAILURE)

{

RETURN_FALSE;

}

ZEND_FETCH_RESOURCE(list, list_head *, &lrc, -1, "List Resource", le_php_list);

list_add_tail(list, value);

RETURN_TRUE;

}

PHP_FUNCTION(list_fetch_tail)

{

zval *lrc, *retval;

list_head *list;

int res;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &lrc) == FAILURE)

{

RETURN_FALSE;

}

ZEND_FETCH_RESOURCE(list, list_head *, &lrc, -1, "List Resource", le_php_list);

res = list_fetch(list, list_length(list)-1, &retval);

if(!res)

{

RETURN_NULL();

}

else

{

RETURN_ZVAL(retval, 1, 0);

}

}

PHP_FUNCTION(list_fetch_index)

{

zval *lrc, *retval;

list_head *list;

long index;

int res;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &lrc, &index) == FAILURE)

{

RETURN_FALSE;

}

ZEND_FETCH_RESOURCE(list, list_head *, &lrc, -1, "List Resource", le_php_list);

res = list_fetch(list, index, &retval);

if(!res)

{

RETURN_NULL();

}

else

{

RETURN_ZVAL(retval, 1, 0);

}

}

PHP_FUNCTION(list_element_nums)

{

zval *lrc;

list_head *list;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &lrc) == FAILURE)

{

RETURN_FALSE;

}

ZEND_FETCH_RESOURCE(list, list_head *, &lrc, -1, "List Resource", le_php_list);

RETURN_LONG(list_length(list));

}

PHP_FUNCTION(list_delete_index)

{

zval *lrc;

list_head *list;

long index;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &lrc, &index) == FAILURE)

{

RETURN_FALSE;

}

ZEND_FETCH_RESOURCE(list, list_head *, &lrc, -1, "List Resource", le_php_list);

if(list_delete(list, index))

{

RETURN_TRUE;

}

else

{

RETURN_FALSE;

}

}

PHP_FUNCTION(list_destroy)

{

zval *lrc;

list_head *list;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &lrc) == FAILURE)

{

RETURN_FALSE;

}

ZEND_FETCH_RESOURCE(list, list_head *, &lrc, -1, "List Resource", le_php_list);

if(!freed)

{

list_destroy(list);

freed = 1;

}

}

/* }}} */

/* The previous line is meant for vim and emacs, so it can correctly fold and

unfold functions in source code. See the corresponding marks just before

function definition, where the functions purpose is also documented. Please

follow this convention for the convenience of others editing your code.

*/

/* {{{ php_php_list_init_globals

*/

/* Uncomment this function if you have INI entries

static void php_php_list_init_globals(zend_php_list_globals *php_list_globals)

{

php_list_globals->global_value = 0;

php_list_globals->global_string = NULL;

}

*/

/* }}} */

/* {{{ PHP_MINIT_FUNCTION

*/

PHP_MINIT_FUNCTION(php_list)

{

/* If you have INI entries, uncomment these lines

REGISTER_INI_ENTRIES();

*/

le_php_list = zend_register_list_destructors_ex(list_destroy_handler, NULL, "list_resource", module_number);

return SUCCESS;

}

/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION

*/

PHP_MSHUTDOWN_FUNCTION(php_list)

{

/* uncomment this line if you have INI entries

UNREGISTER_INI_ENTRIES();

*/

return SUCCESS;

}

/* }}} */

/* Remove if there's nothing to do at request start */

/* {{{ PHP_RINIT_FUNCTION

*/

PHP_RINIT_FUNCTION(php_list)

{

return SUCCESS;

}

/* }}} */

/* Remove if there's nothing to do at request end */

/* {{{ PHP_RSHUTDOWN_FUNCTION

*/

PHP_RSHUTDOWN_FUNCTION(php_list)

{

return SUCCESS;

}

/* }}} */

/* {{{ PHP_MINFO_FUNCTION

*/

PHP_MINFO_FUNCTION(php_list)

{

php_info_print_table_start();

php_info_print_table_header(2, "php_list support", "enabled");

php_info_print_table_end();

/* Remove comments if you have entries in php.ini

DISPLAY_INI_ENTRIES();

*/

}

/* }}} */

/* {{{ php_list_functions[]

*

* Every user visible function must have an entry in php_list_functions[].

*/

const zend_function_entry php_list_functions[] = {

PHP_FE(list_create,???????????? NULL)

PHP_FE(list_add_head,?????????? NULL)

PHP_FE(list_fetch_head,???????? NULL)

PHP_FE(list_add_tail,?????????? NULL)

PHP_FE(list_fetch_tail,???????? NULL)

PHP_FE(list_fetch_index,??????? NULL)

PHP_FE(list_delete_index,?????? NULL)

PHP_FE(list_destroy,??????????? NULL)

PHP_FE(list_element_nums,?????? NULL)

PHP_FE(confirm_php_list_compiled,?????? NULL)?????????? /* For testing, remove later. */

PHP_FE_END????? /* Must be the last line in php_list_functions[] */

};

/* }}} */

/* {{{ php_list_module_entry

*/

zend_module_entry php_list_module_entry = {

STANDARD_MODULE_HEADER,

"php_list",

php_list_functions,

PHP_MINIT(php_list),

PHP_MSHUTDOWN(php_list),

PHP_RINIT(php_list),??????????? /* Replace with NULL if there's nothing to do at request start */

PHP_RSHUTDOWN(php_list),??????? /* Replace with NULL if there's nothing to do at request end */

PHP_MINFO(php_list),

PHP_PHP_LIST_VERSION,

STANDARD_MODULE_PROPERTIES

};

/* }}} */

#ifdef COMPILE_DL_PHP_LIST

ZEND_GET_MODULE(php_list)

#endif

/*

* Local variables:

* tab-width: 4

* c-basic-offset: 4

* End:

* vim600: noet sw=4 ts=4 fdm=marker

* vim<600: noet sw=4 ts=4

*/

phpize

./configure --with-php-config=/usr/local/php/bin/php-config

make

make install

查看php_list模塊是否安裝成功

/usr/local/php/bin/php -m | grep php_list

如果顯示php_list,表明安裝成功

重啟php-fpm

編寫測試的php文件

$list = list_create();

$tmp = array();

for($i=0; $i<10; $i++)

{

$tmp[$i] = "elements".$i;

echo $tmp[$i].'
';

list_add_head($list, $tmp[$i]);

//list_add_head($list, "element[$i]");

}

$list_nums = list_element_nums($list);

echo "numbers of list:".$list_nums.'
';

echo '
';

for($i=0; $i

{

$res = list_fetch_index($list, $i);

echo $res.'
';

}

顯示結(jié)果為:

elements0

elements1

elements2

elements3

elements4

elements5

elements6

elements7

elements8

elements9

numbers of list:10

elements9

elements8

elements7

elements6

elements5

elements4

elements3

elements2

elements1

elements0

總結(jié)

以上是生活随笔為你收集整理的linux php c 扩展,linux下编写php5.6的C扩展模块(双向链表)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。