设为首页 收藏本站
查看: 646|回复: 0

[经验分享] linux下编写php5.6的C扩展模块(双向链表)

[复制链接]

尚未签到

发表于 2018-12-19 09:19:23 | 显示全部楼层 |阅读模式
  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>  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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-653055-1-1.html 上篇帖子: 安装XCache实现对php加速 下篇帖子: php中常用字符串处理函数的知识总结
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表