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是注释标签)

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.'
';

}

显示结果为:

elements0

elements1

elements2

elements3

elements4

elements5

elements6

elements7

elements8

elements9

numbers of list:10

elements9

elements8

elements7

elements6

elements5

elements4

elements3

elements2

elements1

elements0

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

  1. g++ linux 编译开栈_Linux下编写C++服务器(配置C++编译调试环境)

    Linux下编写C++服务器(配置C++编译调试环境) 安装好linux虚拟机,确定能上网后,我们可以开始编写C++程序了,但在这之前我们需要下载编译器和调试器 下载gcc 1.在终端输入yum se ...

  2. linux redis 5.6扩展,Windows下为PHP5.6安装Redis扩展和memcached扩展

    2.根据PHP版本号,编译器版本号和CPU架构, 选择php_redis-2.2.5-5.6-ts-vc11-x64.zip和php_igbinary-1.2.1-5.5-ts-vc11-x64.zi ...

  3. linux 安装 php 5.2_Linux下安装PHP5.5

    下载安装包后,在安装php之前必须先安装libxml2,因此可以通过下载libxml2安装包,编译安装,我通过yum -y install libxml2 libxml2-devel(不安装这个的话, ...

  4. qtdll在linux系统运行,在QT下编写带DLL的程序

    注:我的工作目录是: D:\My Documents\MyProject 一.运行QtCreator 1.新建工程/选择C++ Library  这里设计被调用的DLL 下一步: 然后输入类名:它会生 ...

  5. linux内核能否扩展,Linux内核用到的GCC扩展

    GNC CC是一个功能非常强大的跨平台C编译器,它对C 语言提供了很多扩展,这些扩展对优化.目标代码布局.更安全的检查等方面提供了很强的支持.本文把支持GNU 扩展的C 语言称为GNU C. Linu ...

  6. linux系统分区扩展,linux系统扩展根分区容量大小

    #查看新增加的磁盘 [root@centos002 ~]# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 secto ...

  7. linux通配符与扩展,Linux 文件通配符与命令行扩展

    *匹配零个或多个字符 ?匹配任何单个字符 ~当前用户家目录 ~mage用户mage家目录 ~+当前工作目录 ~-前一个工作目录 [0-9]匹配数字范围 [a-z]:字母 [A-Z]:字母 [wang] ...

  8. linux php ftp扩展,Linux中如何安装 PHP 扩展?(方法介绍)

    一般会选用源码安装 php,安装 php 的过程指定要安装的扩展,但是避免不了缺少某个扩展未安装导致程序运行报错的问题.以 fileinfo 为例,介绍一下怎么添加 php 扩展. 1. 准备 通常遇 ...

  9. linux php c 扩展,linux php添加扩展库

    CentOS_7.2编译安装PHP_5.6.20添加扩展模块 添加ZendGuardLoader扩展: # 解压ZendGuardLoader.so到"/usr/local/php/lib/ ...

最新文章

  1. R语言复相关或者多重相关性系数计算实战:Multiple Correlation Coefficient
  2. Oracle表里的照片怎么导出来,如何导出oracle数据库中某张表到excel_oracle数据库表格导出到excel...
  3. 联合登陆【支付宝、网易、QQ】
  4. 如何设计一门语言(十一)——删减语言的功能
  5. 第二章 MCS-51单片机硬件结构与工作原理
  6. 【图像分类】没有人工收银,吃饭买单全自动化,是谁的功劳?
  7. JQuery Datatables editor进行增删改查操作(二)
  8. 树莓派vnc用法 linux,怎样使用VNC在树莓派上运行远程桌面
  9. mongodb的java驱动包_mongodb的java驱动包
  10. 设计模式のFactoryPattern(工厂模式)----创建模式
  11. android 下拉刷新listview,实现Android下拉刷新的ListView
  12. react 动态获取数据
  13. SQL Server索引超出了数组界限解决方法
  14. !peb和PEB结构
  15. RT-Thread Studio开发GD32VF103
  16. 如何写好一篇英文科技论文
  17. Excel怎么将两列数据合并成一列
  18. 我的第一份实习工作结束了!!!
  19. python程序 爱意_情人节到了,隔离在家的你还不快用Python给你的她表达下爱意?...
  20. GitHub 上受欢迎的 Android UI Library(part_one)

热门文章

  1. 简易的遍历文件加密解密
  2. CentOS 安装go client调用Kubernetes API
  3. 优秀小程序demo 源码
  4. 如何在Jupyter中运行R语言(两种解决方案)
  5. Windows10 64位 安装 Postgresql 数据库
  6. 启动成功浏览器显示不了_移动端利用chrome浏览器在PC端进行调试方法
  7. python asyncio 并发编程_asyncio并发编程
  8. 恋与制作人 服务器错误,恋与制作人安装失败怎么办_恋与制作人安装失败解决方法_游戏吧...
  9. mysql创建表属性引_【学习之Mysql数据库】mysql数据库创建表的属性详解
  10. 数字图像处理基础与应用学习,第二章