1 Star 0 Fork 347

黄隐翁/swoole-src

forked from swoole/swoole-src 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
swoole_ringqueue.c 4.79 KB
一键复制 编辑 原始数据 按行查看 历史
韩天峰 提交于 2018-03-08 16:41 . fix redis bugs
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| Copyright (c) 2012-2015 The Swoole Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache 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.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@swoole.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Xinyu Zhu <xyzhu1120@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
static PHP_METHOD(swoole_ringqueue, __construct);
static PHP_METHOD(swoole_ringqueue, __destruct);
static PHP_METHOD(swoole_ringqueue, push);
static PHP_METHOD(swoole_ringqueue, pop);
static PHP_METHOD(swoole_ringqueue, count);
static PHP_METHOD(swoole_ringqueue, isFull);
static PHP_METHOD(swoole_ringqueue, isEmpty);
static zend_class_entry swoole_ringqueue_ce;
zend_class_entry *swoole_ringqueue_class_entry_ptr;
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_ringqueue_construct, 0, 0, 1)
ZEND_ARG_INFO(0, len)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_ringqueue_push, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_void, 0, 0, 0)
ZEND_END_ARG_INFO()
static const zend_function_entry swoole_ringqueue_methods[] =
{
PHP_ME(swoole_ringqueue, __construct, arginfo_swoole_ringqueue_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_ringqueue, __destruct, arginfo_swoole_void, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(swoole_ringqueue, push, arginfo_swoole_ringqueue_push, ZEND_ACC_PUBLIC)
PHP_ME(swoole_ringqueue, pop, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_ringqueue, count, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_ringqueue, isFull, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_ringqueue, isEmpty, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};
void swoole_ringqueue_init(int module_number TSRMLS_DC)
{
SWOOLE_INIT_CLASS_ENTRY(swoole_ringqueue_ce, "swoole_ringqueue", "Swoole\\RingQueue", swoole_ringqueue_methods);
swoole_ringqueue_class_entry_ptr = zend_register_internal_class(&swoole_ringqueue_ce TSRMLS_CC);
SWOOLE_CLASS_ALIAS(swoole_ringqueue, "Swoole\\RingQueue");
}
static PHP_METHOD(swoole_ringqueue, __construct)
{
long len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &len) == FAILURE)
{
RETURN_FALSE;
}
if (len < 0)
{
len = SW_RINGQUEUE_LEN;
}
swRingQueue *queue = emalloc(sizeof(swRingQueue));
if (queue == NULL)
{
zend_throw_exception(swoole_exception_class_entry_ptr, "failed to create ringqueue.", SW_ERROR_MALLOC_FAIL TSRMLS_CC);
RETURN_FALSE;
}
if (swRingQueue_init(queue, len))
{
zend_throw_exception(swoole_exception_class_entry_ptr, "failed to init ringqueue.", SW_ERROR_MALLOC_FAIL TSRMLS_CC);
RETURN_FALSE;
}
swoole_set_object(getThis(), queue);
}
static PHP_METHOD(swoole_ringqueue, __destruct)
{
swRingQueue *queue = swoole_get_object(getThis());
efree(queue);
swoole_set_object(getThis(), NULL);
}
static PHP_METHOD(swoole_ringqueue, push)
{
swRingQueue *queue = swoole_get_object(getThis());
zval *zdata;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) == FAILURE)
{
RETURN_FALSE;
}
zdata = sw_zval_dup(zdata);
if (swRingQueue_push(queue, zdata) < 0)
{
efree(zdata);
RETURN_FALSE;
}
else
{
Z_TRY_ADDREF_P(zdata);
RETURN_TRUE;
}
}
static PHP_METHOD(swoole_ringqueue, pop)
{
swRingQueue *queue = swoole_get_object(getThis());
zval *zdata;
int n = swRingQueue_pop(queue, (void**)&zdata);
if (n < 0)
{
RETURN_FALSE;
}
RETVAL_ZVAL(zdata, 0, 0);
efree(zdata);
}
static PHP_METHOD(swoole_ringqueue, isFull)
{
swRingQueue *queue = swoole_get_object(getThis());
RETURN_BOOL(swRingQueue_full(queue));
}
static PHP_METHOD(swoole_ringqueue, isEmpty)
{
swRingQueue *queue = swoole_get_object(getThis());
RETURN_BOOL(swRingQueue_empty(queue));
}
static PHP_METHOD(swoole_ringqueue, count)
{
swRingQueue *queue = swoole_get_object(getThis());
RETURN_LONG(swRingQueue_count(queue));
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/hzysoft/swoole.git
git@gitee.com:hzysoft/swoole.git
hzysoft
swoole
swoole-src
master

搜索帮助