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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/560082.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java根据uml图写出实现代码,根据java代码生成UML图

根据java代码生成UML图根据java代码生成UML图这里介绍一个简单易用的eclipse插件ModelGoon&#xff0c;用来对已有代码生成UML图&#xff0c;下面以之前文章中的spring mvc工程为例如何安装和使用这个插件&#xff1b;这个spring mvc工程的代码在githbu上&#xff0c;地址是&am…

php递归 返回数组,php 递归 无限级分类并返回数组的例子

/*** 递归 无限级分类 返回数组* link&#xff1a;www.jquerycn.cn* date&#xff1a;2013/2/21*/$conn mysql_connect(localhost,root,123456);mysql_select_db(test);mysql_query("set names utf8");function getCate($pid 0){$sql "select * from cate wh…

php电商网站开发流程图,php网上购物平台设计+ER图+流程图.doc

php网上购物平台设计ER图流程图php网上购物平台设计ER图流程图摘要&#xff1a;广义来说&#xff0c;电子商务是指电子工具在商务活动中的应用。狭义来说&#xff0c;电子商务是在技术、经济高度发达的现代社会里&#xff0c;掌握信息技术和商务规则的人&#xff0c;系统化运用…

常见php面试题,常见的 PHP 面试题和答案分享

搜索热词如何直接将输出显示给浏览器&#xff1f;将输出直接显示给浏览器&#xff0c;我们必须使用特殊标记 。PHP 是否支持多重继承&#xff1f;PHP 只支持单继承。PHP 的类使用关键字 extends 继承另一个类获取图片属性(size,width,和 height)的函数是什么&#xff1f;获取图…

php 查找数组相同元素,查找数组中重复的元素

本文收集整理关于查找数组中重复的元素的相关议题&#xff0c;使用内容导航快速到达。内容导航&#xff1a;Q1&#xff1a;在c语言中输入数组两个数组&#xff0c;查找重复元素并输出怎么写啊可以一次读入N个数据。可以考虑以回车结束读入的一组。参考如下写法&#xff1a;#inc…

highcharts php 动态数据,php动态传数据到highcharts的方法

本文主要介绍了通过php动态传数据到highcharts的相关知识。具有很好的参考价值。1&#xff1a;在平时工作中&#xff0c;在对数据进行展示的时候&#xff0c;是直接通过后台提供的接口来获取json串&#xff0c;用来展示。今天别人问怎么在本地演示一下请求的动态数据。2&#x…

asm 5 java,java – 使用ASM(5.x)在字节代码中检测运行时的递归方法调用:howto?

问题如下;Java代码中的方法是&#xff1a;Rule foo(){return sequence(foo(), x());}这将引发解析循环,当然应该避免;但是,这是合法的&#xff1a;Rule foo(){return sequence(x(), foo());}现在,代码中的其他地方我可以访问RuleMethod,这是一个扩展MethodNode的类,因此我可以访…

mysql外键设置sql语句,SQL Server 2008之SQL语句外键

xin3721网络学院为广大学员&#xff0c;准备了丰富了教学视频。为了更好的让大学配合视频进行学习&#xff0c;拓展学员的知识面&#xff0c;我站特整理了大量的&#xff0c;技术文章&#xff0c;供学员参考。因此本教案需配合视频教程学习&#xff0c;视频教程地址为&#xff…

nginx index.php 端口,请教下 nginx 配置域名反代到本地端口这里面应该怎么加。

域名 1.31.tw 怎么添加反代可以正常访问 127.0.0.1:5000 端口? 我自己加的反代在域名开启 ssl 下 css 不正常&#xff0c;错位。弄一天了没弄明白&#xff0c;求大佬。谢谢下面是配置文件&#xff1a;server {listen 80;listen 443 ssl http2;ssl_certificate /usr/local/ngin…

php tar.gz文件,PHP解压tar.gz格式文件的方法,_PHP教程

PHP解压tar.gz格式文件的方法&#xff0c;本文实例讲述了PHP解压tar.gz格式文件的方法。分享给大家供大家参考&#xff0c;具体如下&#xff1a;1、运用php自带压缩与归档扩展(phar)$phar new PharData(song.tar.gz);//路径 要解压的文件 是否覆盖$phar->extractTo(c:/tmp,…

java 像素级碰撞检测,» 像素级碰撞检测类

//像素级碰撞检测package{import flash.display.BitmapData;import flash.display.BlendMode;import flash.display.DisplayObject;import flash.display.Sprite;import flash.geom.ColorTransform;import flash.geom.Matrix;import flash.geom.Point;import flash.geom.Rectan…

matlab暂态信号,MATLAB6在电力暂态波形仿真实现中的应用

1概述现代继电保护不但要测量电力系统稳态情况下的特性,还要测量电子系统暂态情况下的特性。对绝大多数保护装置来说,不可能利用实际电力系统的人工短路试验来检验其性能。继电保护试验设备应具有仿真能力,能模拟电力系统发生各种故障和不正常状态时的暂态过程,特别是严重畸变的…

创建数组表格PHP苹果价格,如何从PHP数组创建HTML表?

Cats萌萌这是我的&#xff1a;<?php function build_table($array){ // start table $html . htmlspecialchars($key) . . htmlspecialchars($value2) .

python打包exe报错编码问题,使用Python打包含有pymssql成exe所躺的坑

一、如何打包Python打包exe文件简单运用pyinstaller库就行了1)安装pyinstaller库(自行安装)2)winR打开运行窗口输入“powershell”3)输入pyinstaller -F 路径\文件名.py(打包py文件的路径&#xff0c;py不能省略)看到successfully即为打包成功&#xff0c;但不一定能运用的&…

centos 6.5装mysql 5.7,centos 6.5装mysql5.7

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼报错er-5.7.17-1.el7.i686 需要--> 处理依赖关系 libc.so.6(GLIBC_2.17)&#xff0c;它被软件包 mysql-community-server-5.7.17-1.el7.i686 需要--> 完成依赖关系计算错误&#xff1a;Package: mysql-community-client-5.7.…

php超大树形分页,PHP+MySql千万级数据limit分页优化方案

PHPMySql千万级数据limit分页优化方案1年前阅读 2750评论 0喜欢 0### 原因徒弟突然有个需求&#xff0c;就是他发现limit分页&#xff0c;页数越大之后&#xff0c;mysql的消耗越大&#xff0c;查询时间越长&#xff0c;当突破百万级数据之后&#xff0c;一个简单的翻页都需要5…

oracle数据库连接满了,ORACLE数据库连接数满的分析及优化

最近在使用Oracle的过程中&#xff0c;出现了数据库连接数满的情况&#xff0c;导致程序及数据库连接工具连接不上。主要从两个方面来考虑这件事&#xff0c;从程序方面来看&#xff1a;1.进行数据库连接操作后未释放连接&#xff1b;2.若使用了数据库连接池&#xff0c;则考虑…

oracle移动硬盘盒,oracle-linux下挂载移动硬盘 NTFS类型

环境&#xff1a;ORACLE-LINUX 5.7全新移动硬盘(未使用过)移动硬盘空间3T在默认情况下&#xff0c;Linux系统不支持NTFS分区挂载1、服务器&#xff1a;A服务器和B服务器为一套ORACLE-RAC&#xff0c;移动硬盘插在A服务器上&#xff1b;2、下载ntfs-3g包&#xff0c;在A服务器上…

linux自启动配置文件,Linux中如何设置服务自启动?

有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务&#xff0c;主要用三种方式进行这一操作&#xff1a;ln -s 在/etc/rc.d/rc*.d目录中建立/etc/init.d/服务的软链接(*代表0&#xff5e;6七个运行级别之一)chkonfig 命令行运行级别设置nts…

linux系统下升级node,linux下安装指定版本的nodejs(升级到指定版本)

原因最近需要全栈开发但是服务器是linux系统&#xff0c;服务本身通过yum安装软件包&#xff0c;不过yum安装的nodejs版本太低。所以需要自己安装。方案下载编译好的文件解压后直接运行即可&#xff0c;不过我们需要全局运行node命令。只需要把目录设置为全局即可(建立软链接 l…