Init Repo

This commit is contained in:
root
2019-09-06 23:53:10 +08:00
commit f0ef89dfbb
7905 changed files with 914138 additions and 0 deletions

249
vendor/swoole/src/core/Channel.c vendored Executable file
View File

@ -0,0 +1,249 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| 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: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "swoole.h"
#define SW_CHANNEL_MIN_MEM (1024*64)
typedef struct _swChannel_item
{
int length;
char data[0];
} swChannel_item;
swChannel* swChannel_new(size_t size, int maxlen, int flags)
{
assert(size >= maxlen);
int ret;
void *mem;
//use shared memory
if (flags & SW_CHAN_SHM)
{
mem = sw_shm_malloc(size + sizeof(swChannel));
}
else
{
mem = sw_malloc(size + sizeof(swChannel));
}
if (mem == NULL)
{
swWarn("swChannel_create: malloc(%ld) failed.", size);
return NULL;
}
swChannel *object = mem;
mem += sizeof(swChannel);
bzero(object, sizeof(swChannel));
//overflow space
object->size = size;
object->mem = mem;
object->maxlen = maxlen;
object->flag = flags;
//use lock
if (flags & SW_CHAN_LOCK)
{
//init lock
if (swMutex_create(&object->lock, 1) < 0)
{
swWarn("mutex init failed.");
return NULL;
}
}
//use notify
if (flags & SW_CHAN_NOTIFY)
{
ret = swPipeNotify_auto(&object->notify_fd, 1, 1);
if (ret < 0)
{
swWarn("notify_fd init failed.");
return NULL;
}
}
return object;
}
/**
* push data(no lock)
*/
int swChannel_in(swChannel *object, void *in, int data_length)
{
assert(data_length <= object->maxlen);
if (swChannel_full(object))
{
return SW_ERR;
}
swChannel_item *item;
int msize = sizeof(item->length) + data_length;
if (object->tail < object->head)
{
//no enough memory space
if ((object->head - object->tail) < msize)
{
return SW_ERR;
}
item = object->mem + object->tail;
object->tail += msize;
}
else
{
item = object->mem + object->tail;
object->tail += msize;
if (object->tail >= object->size)
{
object->tail = 0;
object->tail_tag = 1 - object->tail_tag;
}
}
object->num++;
object->bytes += data_length;
item->length = data_length;
memcpy(item->data, in, data_length);
return SW_OK;
}
/**
* pop data(no lock)
*/
int swChannel_out(swChannel *object, void *out, int buffer_length)
{
if (swChannel_empty(object))
{
return SW_ERR;
}
swChannel_item *item = object->mem + object->head;
assert(buffer_length >= item->length);
memcpy(out, item->data, item->length);
object->head += (item->length + sizeof(item->length));
if (object->head >= object->size)
{
object->head = 0;
object->head_tag = 1 - object->head_tag;
}
object->num--;
object->bytes -= item->length;
return item->length;
}
/**
* peek data
*/
int swChannel_peek(swChannel *object, void *out, int buffer_length)
{
if (swChannel_empty(object))
{
return SW_ERR;
}
int length;
object->lock.lock(&object->lock);
swChannel_item *item = object->mem + object->head;
assert(buffer_length >= item->length);
memcpy(out, item->data, item->length);
length = item->length;
object->lock.unlock(&object->lock);
return length;
}
/**
* wait notify
*/
int swChannel_wait(swChannel *object)
{
assert(object->flag & SW_CHAN_NOTIFY);
uint64_t flag;
return object->notify_fd.read(&object->notify_fd, &flag, sizeof(flag));
}
/**
* new data coming, notify to customer
*/
int swChannel_notify(swChannel *object)
{
assert(object->flag & SW_CHAN_NOTIFY);
uint64_t flag = 1;
return object->notify_fd.write(&object->notify_fd, &flag, sizeof(flag));
}
/**
* push data (lock)
*/
int swChannel_push(swChannel *object, void *in, int data_length)
{
assert(object->flag & SW_CHAN_LOCK);
object->lock.lock(&object->lock);
int ret = swChannel_in(object, in, data_length);
object->lock.unlock(&object->lock);
return ret;
}
/**
* free channel
*/
void swChannel_free(swChannel *object)
{
if (object->flag & SW_CHAN_LOCK)
{
object->lock.free(&object->lock);
}
if (object->flag & SW_CHAN_NOTIFY)
{
object->notify_fd.close(&object->notify_fd);
}
if (object->flag & SW_CHAN_SHM)
{
sw_shm_free(object);
}
else
{
sw_free(object);
}
}
/**
* pop data (lock)
*/
int swChannel_pop(swChannel *object, void *out, int buffer_length)
{
assert(object->flag & SW_CHAN_LOCK);
object->lock.lock(&object->lock);
int n = swChannel_out(object, out, buffer_length);
object->lock.unlock(&object->lock);
return n;
}
void swChannel_print(swChannel *chan)
{
printf("swChannel\n{\n"
" off_t head = %ld;\n"
" off_t tail = %ld;\n"
" size_t size = %ld;\n"
" char head_tag = %d;\n"
" char tail_tag = %d;\n"
" int num = %d;\n"
" size_t bytes = %ld;\n"
" int flag = %d;\n"
" int maxlen = %d;\n"
"\n}\n", (long)chan->head, (long)chan->tail, chan->size, chan->tail_tag, chan->head_tag, chan->num, chan->bytes,
chan->flag, chan->maxlen);
}

151
vendor/swoole/src/core/RingQueue.c vendored Executable file
View File

@ -0,0 +1,151 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| 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: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include <stdio.h>
#include "swoole.h"
#include "RingQueue.h"
#ifdef SW_USE_RINGQUEUE_TS
int swRingQueue_init(swRingQueue *queue, int buffer_size)
{
queue->size = buffer_size;
queue->flags = (char *)sw_malloc(queue->size);
if (queue->flags == NULL)
{
return -1;
}
queue->data = (void **)sw_calloc(queue->size, sizeof(void*));
if (queue->data == NULL)
{
sw_free(queue->flags);
return -1;
}
queue->head = 0;
queue->tail = 0;
memset(queue->flags, 0, queue->size);
memset(queue->data, 0, queue->size * sizeof(void*));
return 0;
}
int swRingQueue_push(swRingQueue *queue, void * ele)
{
if (!(queue->num < queue->size))
{
return -1;
}
int cur_tail_index = queue->tail;
char * cur_tail_flag_index = queue->flags + cur_tail_index;
//TODO Scheld
while (!sw_atomic_cmp_set(cur_tail_flag_index, 0, 1))
{
cur_tail_index = queue->tail;
cur_tail_flag_index = queue->flags + cur_tail_index;
}
// 两个入队线程之间的同步
//TODO 取模操作可以优化
int update_tail_index = (cur_tail_index + 1) % queue->size;
// 如果已经被其他的线程更新过,则不需要更新;
// 否则,更新为 (cur_tail_index+1) % size;
sw_atomic_cmp_set(&queue->tail, cur_tail_index, update_tail_index);
// 申请到可用的存储空间
*(queue->data + cur_tail_index) = ele;
sw_atomic_fetch_add(cur_tail_flag_index, 1);
sw_atomic_fetch_add(&queue->num, 1);
return 0;
}
int swRingQueue_pop(swRingQueue *queue, void **ele)
{
if (!(queue->num > 0))
return -1;
int cur_head_index = queue->head;
char * cur_head_flag_index = queue->flags + cur_head_index;
while (!sw_atomic_cmp_set(cur_head_flag_index, 2, 3))
{
cur_head_index = queue->head;
cur_head_flag_index = queue->flags + cur_head_index;
}
//TODO 取模操作可以优化
int update_head_index = (cur_head_index + 1) % queue->size;
sw_atomic_cmp_set(&queue->head, cur_head_index, update_head_index);
*ele = *(queue->data + cur_head_index);
sw_atomic_fetch_sub(cur_head_flag_index, 3);
sw_atomic_fetch_sub(&queue->num, 1);
return 0;
}
#else
int swRingQueue_init(swRingQueue *queue, int buffer_size)
{
queue->data = sw_calloc(buffer_size, sizeof(void*));
if (queue->data == NULL)
{
swWarn("malloc failed.");
return -1;
}
queue->size = buffer_size;
queue->head = 0;
queue->tail = 0;
queue->tag = 0;
return 0;
}
void swRingQueue_free(swRingQueue *queue)
{
sw_free(queue->data);
}
int swRingQueue_push(swRingQueue *queue, void *push_data)
{
if (swRingQueue_full(queue))
{
return SW_ERR;
}
queue->data[queue->tail] = push_data;
queue->tail = (queue->tail + 1) % queue->size;
if (queue->tail == queue->head)
{
queue->tag = 1;
}
return SW_OK;
}
int swRingQueue_pop(swRingQueue *queue, void **pop_data)
{
if (swRingQueue_empty(queue))
{
return SW_ERR;
}
*pop_data = queue->data[queue->head];
queue->head = (queue->head + 1) % queue->size;
if (queue->tail == queue->head)
{
queue->tag = 0;
}
return SW_OK;
}
#endif

92
vendor/swoole/src/core/UnitTest.c vendored Executable file
View File

@ -0,0 +1,92 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| 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: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "swoole.h"
#include "tests.h"
#include "uthash.h"
typedef struct
{
swUnitTest_Func func;
char *comment;
int run_times;
char *key;
} swHashTable_unitTest;
static swHashMap *utmap = NULL;
void _swUnitTest_setup(swUnitTest_Func func, char *func_name, int run_times, char *comment)
{
if (!utmap)
{
utmap = swHashMap_new(32, free);
}
swHashTable_unitTest *u;
u = (swHashTable_unitTest *) malloc(sizeof(swHashTable_unitTest));
u->key = func_name;
u->func = func;
u->run_times = run_times;
u->comment = comment;
swHashMap_add(utmap, func_name, strlen(func_name), u);
}
int swUnitTest_run(swUnitTest *object)
{
int max_len = 128;
int argc = object->argc;
char **argv = object->argv;
int ret;
char *key;
swUnitTest_Func func;
swHashTable_unitTest *tmp;
int i = 0;
if (argc < 2)
{
printf("Please enter %s unitTest_name\n", argv[0]);
while (1)
{
tmp = swHashMap_each(utmap, &key);
if (!tmp)
break;
printf("#%d.\t%s: %s\n", ++i, tmp->key, tmp->comment);
}
return 0;
}
do
{
tmp = swHashMap_each(utmap, &key);
if (strncmp(argv[1], key, max_len) == 0)
{
func = tmp->func;
printf("running\n");
ret = func(object);
break;
}
} while (tmp);
printf("finish\n");
return ret;
}
void p_str(void *str)
{
printf("Str: %s|len=%ld\n", (char *) str, strlen((char *) str));
}

149
vendor/swoole/src/core/array.c vendored Executable file
View File

@ -0,0 +1,149 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| 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: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "swoole.h"
#include "array.h"
/**
* Create new array
*/
swArray *swArray_new(int page_size, size_t item_size)
{
swArray *array = sw_malloc(sizeof(swArray));
if (array == NULL)
{
swoole_error_log(SW_LOG_ERROR, SW_ERROR_MALLOC_FAIL, "malloc[0] failed.");
return NULL;
}
bzero(array, sizeof(swArray));
array->pages = sw_malloc(sizeof(void*) * SW_ARRAY_PAGE_MAX);
if (array->pages == NULL)
{
sw_free(array);
swoole_error_log(SW_LOG_ERROR, SW_ERROR_MALLOC_FAIL, "malloc[1] failed.");
return NULL;
}
array->item_size = item_size;
array->page_size = page_size;
swArray_extend(array);
return array;
}
/**
* Destory the array
*/
void swArray_free(swArray *array)
{
int i;
for (i = 0; i < array->page_num; i++)
{
sw_free(array->pages[i]);
}
sw_free(array->pages);
sw_free(array);
}
/**
* Extend the memory pages of the array
*/
int swArray_extend(swArray *array)
{
if (array->page_num == SW_ARRAY_PAGE_MAX)
{
swWarn("max page_num is %d", array->page_num);
return SW_ERR;
}
array->pages[array->page_num] = sw_calloc(array->page_size, array->item_size);
if (array->pages[array->page_num] == NULL)
{
swWarn("malloc[1] failed.");
return SW_ERR;
}
array->page_num++;
return SW_OK;
}
/**
* Fetch data by index of the array
*/
void *swArray_fetch(swArray *array, uint32_t n)
{
int page = swArray_page(array, n);
if (page >= array->page_num)
{
return NULL;
}
return array->pages[page] + (swArray_offset(array, n) * array->item_size);
}
/**
* Append to the array
*/
int swArray_append(swArray *array, void *data)
{
int n = array->offset++;
int page = swArray_page(array, n);
if (page >= array->page_num && swArray_extend(array) < 0)
{
return SW_ERR;
}
array->item_num++;
memcpy(array->pages[page] + (swArray_offset(array, n) * array->item_size), data, array->item_size);
return n;
}
int swArray_store(swArray *array, uint32_t n, void *data)
{
int page = swArray_page(array, n);
if (page >= array->page_num)
{
swWarn("fetch index[%d] out of array", n);
return SW_ERR;
}
memcpy(array->pages[page] + (swArray_offset(array, n) * array->item_size), data, array->item_size);
return SW_OK;
}
void *swArray_alloc(swArray *array, uint32_t n)
{
while (n >= array->page_num * array->page_size)
{
if (swArray_extend(array) < 0)
{
return NULL;
}
}
int page = swArray_page(array, n);
if (page >= array->page_num)
{
swWarn("fetch index[%d] out of array", n);
return NULL;
}
return array->pages[page] + (swArray_offset(array, n) * array->item_size);
}
void swArray_clear(swArray *array)
{
array->offset = 0;
array->item_num = 0;
}

1255
vendor/swoole/src/core/base.c vendored Executable file

File diff suppressed because it is too large Load Diff

40
vendor/swoole/src/core/error.cc vendored Executable file
View File

@ -0,0 +1,40 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| 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: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "swoole.h"
#include <string>
namespace swoole
{
class Exception
{
public:
int code;
Exception(enum swErrorCode _code)
{
code = _code;
}
};
}
;
void swoole_throw_error(enum swErrorCode code)
{
throw swoole::Exception(code);
}

515
vendor/swoole/src/core/hashmap.c vendored Executable file
View File

@ -0,0 +1,515 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| 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: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "swoole.h"
#include "uthash.h"
#include "hash.h"
typedef struct swHashMap_node
{
uint64_t key_int;
char *key_str;
void *data;
UT_hash_handle hh;
} swHashMap_node;
static int swHashMap_node_delete(swHashMap_node *root, swHashMap_node *del_node);
static sw_inline void swHashMap_node_dtor(swHashMap *hmap, swHashMap_node *node)
{
if (hmap->dtor)
{
hmap->dtor(node->data);
}
}
static sw_inline void swHashMap_node_free(swHashMap *hmap, swHashMap_node *node)
{
swHashMap_node_dtor(hmap, node);
sw_free(node->key_str);
sw_free(node);
}
static sw_inline int swHashMap_node_add(swHashMap_node *root, swHashMap_node *add)
{
unsigned _ha_bkt;
add->hh.next = NULL;
add->hh.key = add->key_str;
add->hh.keylen = add->key_int;
root->hh.tbl->tail->next = add;
add->hh.prev = ELMT_FROM_HH(root->hh.tbl, root->hh.tbl->tail);
root->hh.tbl->tail = &(add->hh);
root->hh.tbl->num_items++;
add->hh.tbl = root->hh.tbl;
add->hh.hashv = swoole_hash_jenkins(add->key_str, add->key_int);
_ha_bkt = add->hh.hashv & (root->hh.tbl->num_buckets - 1);
HASH_ADD_TO_BKT(root->hh.tbl->buckets[_ha_bkt], &add->hh);
return SW_OK;
}
static sw_inline swHashMap_node* swHashMap_node_each(swHashMap* hmap)
{
swHashMap_node *iterator = hmap->iterator;
swHashMap_node *tmp;
if (hmap->root->hh.tbl->num_items == 0)
{
return NULL;
}
if (iterator == NULL)
{
iterator = hmap->root;
}
tmp = iterator->hh.next;
if (tmp)
{
hmap->iterator = tmp;
return tmp;
}
else
{
hmap->iterator = NULL;
return NULL;
}
}
swHashMap* swHashMap_new(uint32_t bucket_num, swHashMap_dtor dtor)
{
swHashMap *hmap = sw_malloc(sizeof(swHashMap));
if (!hmap)
{
swWarn("malloc[1] failed.");
return NULL;
}
swHashMap_node *root = sw_malloc(sizeof(swHashMap_node));
if (!root)
{
swWarn("malloc[2] failed.");
sw_free(hmap);
return NULL;
}
bzero(hmap, sizeof(swHashMap));
hmap->root = root;
bzero(root, sizeof(swHashMap_node));
root->hh.tbl = (UT_hash_table*) sw_malloc(sizeof(UT_hash_table));
if (!(root->hh.tbl))
{
swWarn("malloc for table failed.");
sw_free(hmap);
return NULL;
}
memset(root->hh.tbl, 0, sizeof(UT_hash_table));
root->hh.tbl->tail = &(root->hh);
root->hh.tbl->num_buckets = SW_HASHMAP_INIT_BUCKET_N;
root->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2;
root->hh.tbl->hho = (char*) (&root->hh) - (char*) root;
root->hh.tbl->buckets = (UT_hash_bucket*) sw_malloc(SW_HASHMAP_INIT_BUCKET_N * sizeof(struct UT_hash_bucket));
if (!root->hh.tbl->buckets)
{
swWarn("malloc for buckets failed.");
sw_free(hmap);
return NULL;
}
memset(root->hh.tbl->buckets, 0, SW_HASHMAP_INIT_BUCKET_N * sizeof(struct UT_hash_bucket));
root->hh.tbl->signature = HASH_SIGNATURE;
hmap->dtor = dtor;
return hmap;
}
int swHashMap_add(swHashMap* hmap, char *key, uint16_t key_len, void *data)
{
swHashMap_node *node = (swHashMap_node*) sw_malloc(sizeof(swHashMap_node));
if (node == NULL)
{
swWarn("malloc failed.");
return SW_ERR;
}
bzero(node, sizeof(swHashMap_node));
swHashMap_node *root = hmap->root;
node->key_str = sw_strndup(key, key_len);
node->key_int = key_len;
node->data = data;
return swHashMap_node_add(root, node);
}
int swHashMap_add_int(swHashMap *hmap, uint64_t key, void *data)
{
swHashMap_node *node = (swHashMap_node*) sw_malloc(sizeof(swHashMap_node));
swHashMap_node *root = hmap->root;
if (node == NULL)
{
swWarn("malloc failed");
return SW_ERR;
}
node->key_int = key;
node->data = data;
node->key_str = NULL;
HASH_ADD_INT(root, key_int, node);
return SW_OK;
}
static sw_inline swHashMap_node *swHashMap_node_find(swHashMap_node *root, char *key_str, uint16_t key_len)
{
swHashMap_node *out;
unsigned bucket, hash;
out = NULL;
if (root)
{
hash = swoole_hash_jenkins(key_str, key_len);
bucket = hash & (root->hh.tbl->num_buckets - 1);
HASH_FIND_IN_BKT(root->hh.tbl, hh, (root)->hh.tbl->buckets[bucket], key_str, key_len, out);
}
return out;
}
static int swHashMap_node_delete(swHashMap_node *root, swHashMap_node *del_node)
{
unsigned bucket;
struct UT_hash_handle *_hd_hh_del;
if ((del_node->hh.prev == NULL) && (del_node->hh.next == NULL))
{
sw_free(root->hh.tbl->buckets);
sw_free(root->hh.tbl);
}
else
{
_hd_hh_del = &(del_node->hh);
if (del_node == ELMT_FROM_HH(root->hh.tbl, root->hh.tbl->tail))
{
root->hh.tbl->tail = (UT_hash_handle*) ((ptrdiff_t) (del_node->hh.prev) + root->hh.tbl->hho);
}
if (del_node->hh.prev)
{
((UT_hash_handle*) ((ptrdiff_t) (del_node->hh.prev) + root->hh.tbl->hho))->next = del_node->hh.next;
}
else
{
DECLTYPE_ASSIGN(root, del_node->hh.next);
}
if (_hd_hh_del->next)
{
((UT_hash_handle*) ((ptrdiff_t) _hd_hh_del->next + root->hh.tbl->hho))->prev = _hd_hh_del->prev;
}
HASH_TO_BKT(_hd_hh_del->hashv, root->hh.tbl->num_buckets, bucket);
HASH_DEL_IN_BKT(hh, root->hh.tbl->buckets[bucket], _hd_hh_del);
root->hh.tbl->num_items--;
}
return SW_OK;
}
void* swHashMap_find(swHashMap* hmap, char *key, uint16_t key_len)
{
swHashMap_node *root = hmap->root;
swHashMap_node *ret = swHashMap_node_find(root, key, key_len);
if (ret == NULL)
{
return NULL;
}
return ret->data;
}
void* swHashMap_find_int(swHashMap* hmap, uint64_t key)
{
swHashMap_node *ret = NULL;
swHashMap_node *root = hmap->root;
HASH_FIND_INT(root, &key, ret);
if (ret == NULL)
{
return NULL;
}
return ret->data;
}
int swHashMap_update(swHashMap* hmap, char *key, uint16_t key_len, void *data)
{
swHashMap_node *root = hmap->root;
swHashMap_node *node = swHashMap_node_find(root, key, key_len);
if (node == NULL)
{
return SW_ERR;
}
swHashMap_node_dtor(hmap, node);
node->data = data;
return SW_OK;
}
void swHashMap_update_int(swHashMap* hmap, uint64_t key, void *data)
{
swHashMap_node *ret = NULL;
swHashMap_node *root = hmap->root;
HASH_FIND_INT(root, &key, ret);
if (ret == NULL)
{
return;
}
swHashMap_node_dtor(hmap, ret);
ret->data = data;
}
int swHashMap_del(swHashMap* hmap, char *key, uint16_t key_len)
{
swHashMap_node *root = hmap->root;
swHashMap_node *node = swHashMap_node_find(root, key, key_len);
if (node == NULL)
{
return SW_ERR;
}
swHashMap_node_delete(root, node);
swHashMap_node_free(hmap, node);
return SW_OK;
}
int swHashMap_del_int(swHashMap *hmap, uint64_t key)
{
swHashMap_node *ret = NULL;
swHashMap_node *root = hmap->root;
HASH_FIND_INT(root, &key, ret);
if (ret == NULL)
{
return SW_ERR;
}
HASH_DEL(root, ret);
swHashMap_node_free(hmap, ret);
return SW_OK;
}
int swHashMap_move(swHashMap *hmap, char *old_key, uint16_t old_key_len, char *new_key, uint16_t new_key_len)
{
swHashMap_node *root = hmap->root;
swHashMap_node *node = swHashMap_node_find(root, old_key, old_key_len);
if (node == NULL)
{
return SW_ERR;
}
swHashMap_node_delete(root, node);
sw_free(node->key_str);
node->key_str = sw_strndup(new_key, new_key_len);
node->key_int = new_key_len;
return swHashMap_node_add(root, node);
}
int swHashMap_move_int(swHashMap *hmap, uint64_t old_key, uint64_t new_key)
{
swHashMap_node *ret = NULL;
swHashMap_node *root = hmap->root;
HASH_FIND_INT(root, &old_key, ret);
if (ret == NULL)
{
return SW_ERR;
}
HASH_DEL(root, ret);
ret->key_int = new_key;
HASH_ADD_INT(root, key_int, ret);
return SW_OK;
}
void* swHashMap_each(swHashMap* hmap, char **key)
{
swHashMap_node *node = swHashMap_node_each(hmap);
if (node)
{
*key = node->key_str;
return node->data;
}
else
{
return NULL;
}
}
void* swHashMap_each_int(swHashMap* hmap, uint64_t *key)
{
swHashMap_node *node = swHashMap_node_each(hmap);
if (node)
{
*key = node->key_int;
return node->data;
}
else
{
return NULL;
}
}
uint32_t swHashMap_count(swHashMap* hmap)
{
if (hmap == NULL)
{
return 0;
}
return HASH_COUNT(hmap->root);
}
void swHashMap_free(swHashMap* hmap)
{
swHashMap_node *find, *tmp = NULL;
swHashMap_node *root = hmap->root;
HASH_ITER(hh, root, find, tmp)
{
if (find == root) continue;
swHashMap_node_delete(root, find);
swHashMap_node_free(hmap, find);
}
sw_free(hmap->root->hh.tbl->buckets);
sw_free(hmap->root->hh.tbl);
sw_free(hmap->root);
sw_free(hmap);
}
/* {{{ COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or
* code or tables extracted from it, as desired without restriction.
*
* First, the polynomial itself and its table of feedback terms. The
* polynomial is
* X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
*
* Note that we take it "backwards" and put the highest-order term in
* the lowest-order bit. The X^32 term is "implied"; the LSB is the
* X^31 term, etc. The X^0 term (usually shown as "+1") results in
* the MSB being 1
*
* Note that the usual hardware shift register implementation, which
* is what we're using (we're merely optimizing it by doing eight-bit
* chunks at a time) shifts bits into the lowest-order term. In our
* implementation, that means shifting towards the right. Why do we
* do it this way? Because the calculated CRC must be transmitted in
* order from highest-order term to lowest-order term. UARTs transmit
* characters in order from LSB to MSB. By storing the CRC this way
* we hand it to the UART in the order low-byte to high-byte; the UART
* sends each low-bit to hight-bit; and the result is transmission bit
* by bit from highest- to lowest-order term without requiring any bit
* shuffling on our part. Reception works similarly
*
* The feedback terms table consists of 256, 32-bit entries. Notes
*
* The table can be generated at runtime if desired; code to do so
* is shown later. It might not be obvious, but the feedback
* terms simply represent the results of eight shift/xor opera
* tions for all combinations of data and CRC register values
*
* The values must be right-shifted by eight bits by the "updcrc
* logic; the shift must be unsigned (bring in zeroes). On some
* hardware you could probably optimize the shift in assembler by
* using byte-swap instructions
* polynomial $edb88320
*
*
* CRC32 code derived from work by Gary S. Brown.
*/
static unsigned int crc32_tab[] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
};
static inline uint32_t crc32(char *buf, unsigned int size)
{
const char *p;
register int crc = 0;
p = buf;
while (size--)
{
crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
}
return crc ^ ~0U;
}
uint32_t swoole_crc32(char *data, uint32_t size)
{
if (size < CRC_STRING_MAXLEN)
{
return crc32(data, size);
}
else
{
int i = 0;
char crc_contents[CRC_STRING_MAXLEN];
int head = CRC_STRING_MAXLEN >> 2;
int tail = CRC_STRING_MAXLEN >> 4;
int body = CRC_STRING_MAXLEN - head - tail;
char *p = data + head;
char *q = crc_contents + head;
int step = (size - tail - head) / body;
memcpy(crc_contents, data, head);
for (; i < body; i++, q++, p += step)
{
*q = *p;
}
memcpy(q, p, tail);
return crc32(crc_contents, CRC_STRING_MAXLEN);
}
}

219
vendor/swoole/src/core/heap.c vendored Executable file
View File

@ -0,0 +1,219 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| 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: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "swoole.h"
#include "heap.h"
#define left(i) ((i) << 1)
#define right(i) (((i) << 1) + 1)
#define parent(i) ((i) >> 1)
static void swHeap_bubble_up(swHeap *heap, uint32_t i);
static uint32_t swHeap_maxchild(swHeap *heap, uint32_t i);
static void swHeap_percolate_down(swHeap *heap, uint32_t i);
swHeap *swHeap_new(size_t n, uint8_t type)
{
swHeap *heap = sw_malloc(sizeof(swHeap));
if (!heap)
{
return NULL;
}
if (!(heap->nodes = sw_malloc((n + 1) * sizeof(void *))))
{
sw_free(heap);
return NULL;
}
heap->num = 1;
heap->size = (n + 1);
heap->type = type;
return heap;
}
void swHeap_free(swHeap *heap)
{
sw_free(heap->nodes);
sw_free(heap);
}
static sw_inline int swHeap_compare(uint8_t type, uint64_t a, uint64_t b)
{
if (type == SW_MIN_HEAP)
{
return a > b;
}
else
{
return a < b;
}
}
uint32_t swHeap_size(swHeap *q)
{
return (q->num - 1);
}
static uint32_t swHeap_maxchild(swHeap *heap, uint32_t i)
{
uint32_t child_i = left(i);
if (child_i >= heap->num)
{
return 0;
}
swHeap_node * child_node = heap->nodes[child_i];
if ((child_i + 1) < heap->num && swHeap_compare(heap->type, child_node->priority, heap->nodes[child_i + 1]->priority))
{
child_i++;
}
return child_i;
}
static void swHeap_bubble_up(swHeap *heap, uint32_t i)
{
swHeap_node *moving_node = heap->nodes[i];
uint32_t parent_i;
for (parent_i = parent(i);
(i > 1) && swHeap_compare(heap->type, heap->nodes[parent_i]->priority, moving_node->priority);
i = parent_i, parent_i = parent(i))
{
heap->nodes[i] = heap->nodes[parent_i];
heap->nodes[i]->position = i;
}
heap->nodes[i] = moving_node;
moving_node->position = i;
}
static void swHeap_percolate_down(swHeap *heap, uint32_t i)
{
uint32_t child_i;
swHeap_node *moving_node = heap->nodes[i];
while ((child_i = swHeap_maxchild(heap, i))
&& swHeap_compare(heap->type, moving_node->priority, heap->nodes[child_i]->priority))
{
heap->nodes[i] = heap->nodes[child_i];
heap->nodes[i]->position = i;
i = child_i;
}
heap->nodes[i] = moving_node;
moving_node->position = i;
}
swHeap_node* swHeap_push(swHeap *heap, uint64_t priority, void *data)
{
void *tmp;
uint32_t i;
uint32_t newsize;
if (heap->num >= heap->size)
{
newsize = heap->size * 2;
if (!(tmp = sw_realloc(heap->nodes, sizeof(void *) * newsize)))
{
return NULL;
}
heap->nodes = tmp;
heap->size = newsize;
}
swHeap_node *node = sw_malloc(sizeof(swHeap_node));
if (!node)
{
return NULL;
}
node->priority = priority;
node->data = data;
i = heap->num++;
heap->nodes[i] = node;
swHeap_bubble_up(heap, i);
return node;
}
void swHeap_change_priority(swHeap *heap, uint64_t new_priority, void* ptr)
{
swHeap_node *node = ptr;
uint32_t pos = node->position;
uint64_t old_pri = node->priority;
node->priority = new_priority;
if (swHeap_compare(heap->type, old_pri, new_priority))
{
swHeap_bubble_up(heap, pos);
}
else
{
swHeap_percolate_down(heap, pos);
}
}
int swHeap_remove(swHeap *heap, swHeap_node *node)
{
uint32_t pos = node->position;
heap->nodes[pos] = heap->nodes[--heap->num];
if (swHeap_compare(heap->type, node->priority, heap->nodes[pos]->priority))
{
swHeap_bubble_up(heap, pos);
}
else
{
swHeap_percolate_down(heap, pos);
}
return SW_OK;
}
void *swHeap_pop(swHeap *heap)
{
swHeap_node *head;
if (!heap || heap->num == 1)
{
return NULL;
}
head = heap->nodes[1];
heap->nodes[1] = heap->nodes[--heap->num];
swHeap_percolate_down(heap, 1);
void *data = head->data;
sw_free(head);
return data;
}
void *swHeap_peek(swHeap *heap)
{
if (heap->num == 1)
{
return NULL;
}
swHeap_node *node = heap->nodes[1];
if (!node)
{
return NULL;
}
return node->data;
}
void swHeap_print(swHeap *heap)
{
int i;
for(i = 1; i < heap->num; i++)
{
printf("#%d\tpriority=%ld, data=%p\n", i, (long)heap->nodes[i]->priority, heap->nodes[i]->data);
}
}

225
vendor/swoole/src/core/list.c vendored Executable file
View File

@ -0,0 +1,225 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| 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: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "swoole.h"
swLinkedList* swLinkedList_new(uint8_t type, swDestructor dtor)
{
swLinkedList *q = sw_malloc(sizeof(swLinkedList));
if (q == NULL)
{
swWarn("malloc(%ld) failed.", sizeof(swLinkedList));
return NULL;
}
bzero(q, sizeof(swLinkedList));
q->type = type;
q->dtor = dtor;
return q;
}
int swLinkedList_append(swLinkedList *ll, void *data)
{
swLinkedList_node *node = sw_malloc(sizeof(swLinkedList_node));
if (node == NULL)
{
swWarn("malloc(%ld) failed.", sizeof(swLinkedList_node));
return SW_ERR;
}
node->data = data;
node->next = NULL;
ll->num++;
if (ll->tail)
{
ll->tail->next = node;
node->prev = ll->tail;
ll->tail = node;
}
else
{
node->next = NULL;
node->prev = NULL;
ll->head = node;
ll->tail = node;
}
return SW_OK;
}
int swLinkedList_prepend(swLinkedList *ll, void *data)
{
swLinkedList_node *node = sw_malloc(sizeof(swLinkedList_node));
if (node == NULL)
{
swWarn("malloc(%ld) failed.", sizeof(swLinkedList_node));
return SW_ERR;
}
node->data = data;
node->prev = NULL;
ll->num ++;
if (ll->head)
{
ll->head->prev = node;
node->next = ll->head;
ll->head = node;
}
else
{
node->next = NULL;
node->prev = NULL;
ll->head = node;
ll->tail = node;
}
return SW_OK;
}
/**
* Pop the element off the end of queue
*/
void* swLinkedList_pop(swLinkedList *ll)
{
if (ll->tail == NULL)
{
return NULL;
}
swLinkedList_node *node = ll->tail;
if (node == ll->head)
{
ll->head = NULL;
ll->tail = NULL;
}
else
{
swLinkedList_node *prev = ll->tail->prev;
prev->next = NULL;
ll->tail = prev;
}
ll->num--;
void *data = node->data;
sw_free(node);
return data;
}
void swLinkedList_remove_node(swLinkedList *ll, swLinkedList_node *remove_node)
{
if (ll->num == 0 || remove_node == NULL)
{
return;
}
swLinkedList_node *prev = remove_node->prev;
swLinkedList_node *next = remove_node->next;
if (remove_node == ll->head)
{
ll->head = next;
if (next == NULL)
{
ll->tail = NULL;
}
else
{
next->prev = NULL;
}
}
else if (remove_node == ll->tail)
{
ll->tail = prev;
if (prev == NULL)
{
ll->head = NULL;
}
else
{
prev->next = NULL;
}
}
else
{
next->prev = prev;
prev->next = next;
}
ll->num--;
sw_free(remove_node);
}
swLinkedList_node* swLinkedList_find(swLinkedList *ll, void *data)
{
if (ll->num == 0)
{
return NULL;
}
swLinkedList_node *node = ll->head;
swLinkedList_node *tmp;
while (node)
{
tmp = node->next;
if (node->data == data)
{
return node;
}
node = tmp;
}
return NULL;
}
/**
* Shift an element off the beginning of queue
*/
void* swLinkedList_shift(swLinkedList *ll)
{
if (ll->head == NULL)
{
return NULL;
}
swLinkedList_node *node = ll->head;
if (node == ll->tail)
{
ll->head = NULL;
ll->tail = NULL;
}
else
{
swLinkedList_node *next = ll->head->next;
next->prev = NULL;
ll->head = next;
}
ll->num--;
void *data = node->data;
sw_free(node);
return data;
}
void swLinkedList_free(swLinkedList *ll)
{
swLinkedList_node *node = ll->head;
swLinkedList_node *tmp;
while (node)
{
tmp = node->next;
if (ll->dtor)
{
ll->dtor(node->data);
}
sw_free(node);
node = tmp;
}
sw_free(ll);
}

106
vendor/swoole/src/core/log.c vendored Executable file
View File

@ -0,0 +1,106 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| 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: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "swoole.h"
#define SW_LOG_BUFFER_SIZE 1024
#define SW_LOG_DATE_STRLEN 64
int swLog_init(char *logfile)
{
SwooleG.log_fd = open(logfile, O_APPEND| O_RDWR | O_CREAT, 0666);
if (SwooleG.log_fd < 0)
{
printf("open(%s) failed. Error: %s[%d]\n", logfile, strerror(errno), errno);
SwooleG.log_fd = 0;
return SW_ERR;
}
return SW_OK;
}
void swLog_free(void)
{
if (SwooleG.log_fd > STDOUT_FILENO)
{
close(SwooleG.log_fd);
}
}
void swLog_put(int level, char *cnt)
{
const char *level_str;
char date_str[SW_LOG_DATE_STRLEN];
char log_str[SW_LOG_BUFFER_SIZE];
int n;
switch (level)
{
case SW_LOG_DEBUG:
level_str = "DEBUG";
break;
case SW_LOG_NOTICE:
level_str = "NOTICE";
break;
case SW_LOG_ERROR:
level_str = "ERROR";
break;
case SW_LOG_WARNING:
level_str = "WARNING";
break;
case SW_LOG_TRACE:
level_str = "TRACE";
break;
default:
level_str = "INFO";
break;
}
time_t t;
struct tm *p;
t = time(NULL);
p = localtime(&t);
snprintf(date_str, SW_LOG_DATE_STRLEN, "%d-%02d-%02d %02d:%02d:%02d", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
char process_flag = '@';
int process_id = 0;
switch(SwooleG.process_type)
{
case SW_PROCESS_MASTER:
process_flag = '#';
process_id = SwooleTG.id;
break;
case SW_PROCESS_MANAGER:
process_flag = '$';
break;
case SW_PROCESS_WORKER:
process_flag = '*';
process_id = SwooleWG.id;
break;
case SW_PROCESS_TASKWORKER:
process_flag = '^';
process_id = SwooleWG.id;
break;
default:
break;
}
n = snprintf(log_str, SW_LOG_BUFFER_SIZE, "[%s %c%d.%d]\t%s\t%s\n", date_str, process_flag, SwooleG.pid, process_id, level_str, cnt);
if (write(SwooleG.log_fd, log_str, n) < 0)
{
printf("write(log_fd, size=%d) failed. Error: %s[%d].\n", n, strerror(errno), errno);
}
}

405
vendor/swoole/src/core/rbtree.c vendored Executable file
View File

@ -0,0 +1,405 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| 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: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "swoole.h"
#include "rbtree.h"
static inline void swRbtree_left_rotate(swRbtree_node **root, swRbtree_node *sentinel, swRbtree_node *node);
static inline void swRbtree_right_rotate(swRbtree_node **root, swRbtree_node *sentinel, swRbtree_node *node);
static inline void swRbtree_insert_value(swRbtree_node *temp, swRbtree_node *node, swRbtree_node *sentinel);
void swRbtree_insert_value(swRbtree_node *temp, swRbtree_node *node, swRbtree_node *sentinel)
{
swRbtree_node **p;
while (1)
{
p = (node->key < temp->key) ? &temp->left : &temp->right;
if (*p == sentinel)
{
break;
}
temp = *p;
}
*p = node;
node->parent = temp;
node->left = sentinel;
node->right = sentinel;
swRbtree_red(node);
}
void swRbtree_insert(swRbtree *tree, uint32_t key, void *value)
{
swRbtree_node **root, *temp, *sentinel;
root = (swRbtree_node **) &tree->root;
sentinel = tree->sentinel;
swRbtree_node *node = (swRbtree_node *) malloc(sizeof(swRbtree_node));
node->value = value;
node->key = key;
if (*root == sentinel)
{
node->parent = NULL;
node->left = sentinel;
node->right = sentinel;
swRbtree_black(node);
*root = node;
return;
}
swRbtree_insert_value(*root, node, sentinel);
/* re-balance tree */
while (node != *root && swRbtree_is_red(node->parent))
{
if (node->parent == node->parent->parent->left)
{
temp = node->parent->parent->right;
if (swRbtree_is_red(temp))
{
swRbtree_black(node->parent);
swRbtree_black(temp);
swRbtree_red(node->parent->parent);
node = node->parent->parent;
}
else
{
if (node == node->parent->right)
{
node = node->parent;
swRbtree_left_rotate(root, sentinel, node);
}
swRbtree_black(node->parent);
swRbtree_red(node->parent->parent);
swRbtree_right_rotate(root, sentinel, node->parent->parent);
}
}
else
{
temp = node->parent->parent->left;
if (swRbtree_is_red(temp))
{
swRbtree_black(node->parent);
swRbtree_black(temp);
swRbtree_red(node->parent->parent);
node = node->parent->parent;
}
else
{
if (node == node->parent->left)
{
node = node->parent;
swRbtree_right_rotate(root, sentinel, node);
}
swRbtree_black(node->parent);
swRbtree_red(node->parent->parent);
swRbtree_left_rotate(root, sentinel, node->parent->parent);
}
}
}
swRbtree_black(*root);
}
void swRbtree_delete(swRbtree *tree, uint32_t key)
{
uint32_t red;
swRbtree_node find_node;
swRbtree_node **root, *sentinel, *subst, *temp, *w;
swRbtree_node *node = &find_node;
node->key = key;
root = (swRbtree_node **) &tree->root;
sentinel = tree->sentinel;
if (node->left == sentinel)
{
temp = node->right;
subst = node;
}
else if (node->right == sentinel)
{
temp = node->left;
subst = node;
}
else
{
subst = swRbtree_min(node->right, sentinel);
if (subst->left != sentinel)
{
temp = subst->left;
}
else
{
temp = subst->right;
}
}
if (subst == *root)
{
*root = temp;
swRbtree_black(temp);
/* DEBUG stuff */
node->left = NULL;
node->right = NULL;
node->parent = NULL;
node->key = 0;
return;
}
red = swRbtree_is_red(subst);
if (subst == subst->parent->left)
{
subst->parent->left = temp;
}
else
{
subst->parent->right = temp;
}
if (subst == node)
{
temp->parent = subst->parent;
}
else
{
if (subst->parent == node)
{
temp->parent = subst;
}
else
{
temp->parent = subst->parent;
}
subst->left = node->left;
subst->right = node->right;
subst->parent = node->parent;
swRbtree_copy_color(subst, node);
if (node == *root)
{
*root = subst;
}
else
{
if (node == node->parent->left)
{
node->parent->left = subst;
}
else
{
node->parent->right = subst;
}
}
if (subst->left != sentinel)
{
subst->left->parent = subst;
}
if (subst->right != sentinel)
{
subst->right->parent = subst;
}
}
if (red)
{
return;
}
/* a delete fixup */
while (temp != *root && swRbtree_is_black(temp))
{
if (temp == temp->parent->left)
{
w = temp->parent->right;
if (swRbtree_is_red(w))
{
swRbtree_black(w);
swRbtree_red(temp->parent);
swRbtree_left_rotate(root, sentinel, temp->parent);
w = temp->parent->right;
}
if (swRbtree_is_black(w->left) && swRbtree_is_black(w->right))
{
swRbtree_red(w);
temp = temp->parent;
}
else
{
if (swRbtree_is_black(w->right))
{
swRbtree_black(w->left);
swRbtree_red(w);
swRbtree_right_rotate(root, sentinel, w);
w = temp->parent->right;
}
swRbtree_copy_color(w, temp->parent);
swRbtree_black(temp->parent);
swRbtree_black(w->right);
swRbtree_left_rotate(root, sentinel, temp->parent);
temp = *root;
}
}
else
{
w = temp->parent->left;
if (swRbtree_is_red(w))
{
swRbtree_black(w);
swRbtree_red(temp->parent);
swRbtree_right_rotate(root, sentinel, temp->parent);
w = temp->parent->left;
}
if (swRbtree_is_black(w->left) && swRbtree_is_black(w->right))
{
swRbtree_red(w);
temp = temp->parent;
}
else
{
if (swRbtree_is_black(w->left))
{
swRbtree_black(w->right);
swRbtree_red(w);
swRbtree_left_rotate(root, sentinel, w);
w = temp->parent->left;
}
swRbtree_copy_color(w, temp->parent);
swRbtree_black(temp->parent);
swRbtree_black(w->left);
swRbtree_right_rotate(root, sentinel, temp->parent);
temp = *root;
}
}
}
swRbtree_black(temp);
}
static inline void swRbtree_left_rotate(swRbtree_node **root, swRbtree_node *sentinel, swRbtree_node *node)
{
swRbtree_node *temp;
temp = node->right;
node->right = temp->left;
if (temp->left != sentinel)
{
temp->left->parent = node;
}
temp->parent = node->parent;
if (node == *root)
{
*root = temp;
}
else if (node == node->parent->left)
{
node->parent->left = temp;
}
else
{
node->parent->right = temp;
}
temp->left = node;
node->parent = temp;
}
static inline void swRbtree_right_rotate(swRbtree_node **root, swRbtree_node *sentinel, swRbtree_node *node)
{
swRbtree_node *temp;
temp = node->left;
node->left = temp->right;
if (temp->right != sentinel)
{
temp->right->parent = node;
}
temp->parent = node->parent;
if (node == *root)
{
*root = temp;
}
else if (node == node->parent->right)
{
node->parent->right = temp;
}
else
{
node->parent->left = temp;
}
temp->right = node;
node->parent = temp;
}
void *swRbtree_find(swRbtree *tree, uint32_t key)
{
swRbtree_node *tmp = tree->root;
swRbtree_node *sentinel = tree->sentinel;
while (tmp != sentinel)
{
if (key != tmp->key)
{
tmp = (key < tmp->key) ? tmp->left : tmp->right;
continue;
}
return tmp->value;
}
return NULL;
}
swRbtree* swRbtree_new()
{
swRbtree *rbtree = sw_malloc(sizeof(swRbtree));
swRbtree_node *sentinel = sw_malloc(sizeof(swRbtree_node));
sentinel->color = 0;
rbtree->root = sentinel;
rbtree->sentinel = sentinel;
return rbtree;
}
void swRbtree_free(swRbtree* rbtree)
{
sw_free(rbtree->root);
sw_free(rbtree);
}

499
vendor/swoole/src/core/socket.c vendored Executable file
View File

@ -0,0 +1,499 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| 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: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "swoole.h"
#include <sys/stat.h>
#include <poll.h>
int swSocket_sendfile_sync(int sock, char *filename, off_t offset, size_t length, double timeout)
{
int timeout_ms = timeout < 0 ? -1 : timeout * 1000;
int file_fd = open(filename, O_RDONLY);
if (file_fd < 0)
{
swWarn("open(%s) failed. Error: %s[%d]", filename, strerror(errno), errno);
return SW_ERR;
}
if (length == 0)
{
struct stat file_stat;
if (fstat(file_fd, &file_stat) < 0)
{
swWarn("fstat() failed. Error: %s[%d]", strerror(errno), errno);
close(file_fd);
return SW_ERR;
}
length = file_stat.st_size;
}
else
{
length = offset + length;
}
int n, sendn;
while (offset < length)
{
if (swSocket_wait(sock, timeout_ms, SW_EVENT_WRITE) < 0)
{
close(file_fd);
return SW_ERR;
}
else
{
sendn = (length - offset > SW_SENDFILE_CHUNK_SIZE) ? SW_SENDFILE_CHUNK_SIZE : length - offset;
n = swoole_sendfile(sock, file_fd, &offset, sendn);
if (n <= 0)
{
close(file_fd);
swSysError("sendfile(%d, %s) failed.", sock, filename);
return SW_ERR;
}
else
{
continue;
}
}
}
close(file_fd);
return SW_OK;
}
/**
* clear socket buffer.
*/
void swSocket_clean(int fd)
{
char buf[2048];
while (recv(fd, buf, sizeof(buf), MSG_DONTWAIT) > 0);
}
/**
* Wait socket can read or write.
*/
int swSocket_wait(int fd, int timeout_ms, int events)
{
struct pollfd event;
event.fd = fd;
event.events = 0;
if (events & SW_EVENT_READ)
{
event.events |= POLLIN;
}
if (events & SW_EVENT_WRITE)
{
event.events |= POLLOUT;
}
while (1)
{
int ret = poll(&event, 1, timeout_ms);
if (ret == 0)
{
return SW_ERR;
}
else if (ret < 0 && errno != EINTR)
{
swWarn("poll() failed. Error: %s[%d]", strerror(errno), errno);
return SW_ERR;
}
else
{
return SW_OK;
}
}
return SW_OK;
}
/**
* Wait some sockets can read or write.
*/
int swSocket_wait_multi(int *list_of_fd, int n_fd, int timeout_ms, int events)
{
assert(n_fd < 65535);
struct pollfd *event_list = sw_calloc(n_fd, sizeof(struct pollfd));
int i;
int _events = 0;
if (events & SW_EVENT_READ)
{
_events |= POLLIN;
}
if (events & SW_EVENT_WRITE)
{
_events |= POLLOUT;
}
for (i = 0; i < n_fd; i++)
{
event_list[i].fd = list_of_fd[i];
event_list[i].events = _events;
}
while (1)
{
int ret = poll(event_list, n_fd, timeout_ms);
if (ret == 0)
{
sw_free(event_list);
return SW_ERR;
}
else if (ret < 0 && errno != EINTR)
{
swWarn("poll() failed. Error: %s[%d]", strerror(errno), errno);
sw_free(event_list);
return SW_ERR;
}
else
{
sw_free(event_list);
return ret;
}
}
sw_free(event_list);
return SW_OK;
}
int swSocket_write_blocking(int __fd, void *__data, int __len)
{
int n = 0;
int written = 0;
while (written < __len)
{
n = write(__fd, __data + written, __len - written);
if (n < 0)
{
if (errno == EINTR)
{
continue;
}
#ifdef HAVE_KQUEUE
else if (errno == EAGAIN || errno == ENOBUFS)
#else
else if (errno == EAGAIN)
#endif
{
swSocket_wait(__fd, SW_WORKER_WAIT_TIMEOUT, SW_EVENT_WRITE);
continue;
}
else
{
swSysError("write %d bytes failed.", __len);
return SW_ERR;
}
}
written += n;
}
return written;
}
int swSocket_recv_blocking(int fd, void *__data, size_t __len, int flags)
{
int ret;
size_t read_bytes = 0;
while (read_bytes != __len)
{
errno = 0;
ret = recv(fd, __data + read_bytes, __len - read_bytes, flags);
if (ret > 0)
{
read_bytes += ret;
}
else if (ret == 0 && errno == 0)
{
return read_bytes;
}
else if (ret <= 0 && errno != 0 && errno != EINTR)
{
return ret;
}
}
return read_bytes;
}
int swSocket_udp_sendto(int server_sock, char *dst_ip, int dst_port, char *data, uint32_t len)
{
struct sockaddr_in addr;
if (inet_aton(dst_ip, &addr.sin_addr) == 0)
{
swWarn("ip[%s] is invalid.", dst_ip);
return SW_ERR;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(dst_port);
return swSocket_sendto_blocking(server_sock, data, len, 0, (struct sockaddr *) &addr, sizeof(addr));
}
int swSocket_udp_sendto6(int server_sock, char *dst_ip, int dst_port, char *data, uint32_t len)
{
struct sockaddr_in6 addr;
bzero(&addr, sizeof(addr));
if (inet_pton(AF_INET6, dst_ip, &addr.sin6_addr) < 0)
{
swWarn("ip[%s] is invalid.", dst_ip);
return SW_ERR;
}
addr.sin6_port = (uint16_t) htons(dst_port);
addr.sin6_family = AF_INET6;
return swSocket_sendto_blocking(server_sock, data, len, 0, (struct sockaddr *) &addr, sizeof(addr));
}
int swSocket_unix_sendto(int server_sock, char *dst_path, char *data, uint32_t len)
{
struct sockaddr_un addr;
bzero(&addr, sizeof(addr));
strncpy(addr.sun_path, dst_path, sizeof(addr.sun_path));
return swSocket_sendto_blocking(server_sock, data, len, 0, (struct sockaddr *) &addr, sizeof(addr));
}
int swSocket_sendto_blocking(int fd, void *__buf, size_t __n, int flag, struct sockaddr *__addr, socklen_t __addr_len)
{
int n = 0;
while (1)
{
n = sendto(fd, __buf, __n, flag, __addr, __addr_len);
if (n >= 0)
{
break;
}
else
{
if (errno == EINTR)
{
continue;
}
else if (errno == EAGAIN)
{
swSocket_wait(fd, 1000, SW_EVENT_WRITE);
continue;
}
else
{
break;
}
}
}
return n;
}
int swSocket_create(int type)
{
int _domain;
int _type;
switch (type)
{
case SW_SOCK_TCP:
_domain = PF_INET;
_type = SOCK_STREAM;
break;
case SW_SOCK_TCP6:
_domain = PF_INET6;
_type = SOCK_STREAM;
break;
case SW_SOCK_UDP:
_domain = PF_INET;
_type = SOCK_DGRAM;
break;
case SW_SOCK_UDP6:
_domain = PF_INET6;
_type = SOCK_DGRAM;
break;
case SW_SOCK_UNIX_DGRAM:
_domain = PF_UNIX;
_type = SOCK_DGRAM;
break;
case SW_SOCK_UNIX_STREAM:
_domain = PF_UNIX;
_type = SOCK_STREAM;
break;
default:
swWarn("unknown socket type [%d]", type);
return SW_ERR;
}
return socket(_domain, _type, 0);
}
int swSocket_bind(int sock, int type, char *host, int *port)
{
int ret;
struct sockaddr_in addr_in4;
struct sockaddr_in6 addr_in6;
struct sockaddr_un addr_un;
socklen_t len;
//SO_REUSEADDR option
int option = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(int)) < 0)
{
swoole_error_log(SW_LOG_WARNING, SW_ERROR_SYSTEM_CALL_FAIL, "setsockopt(%d, SO_REUSEADDR) failed.", sock);
}
//reuse port
#ifdef HAVE_REUSEPORT
if (SwooleG.reuse_port)
{
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &option, sizeof(int)) < 0)
{
swSysError("setsockopt(SO_REUSEPORT) failed.");
SwooleG.reuse_port = 0;
}
}
#endif
//unix socket
if (type == SW_SOCK_UNIX_DGRAM || type == SW_SOCK_UNIX_STREAM)
{
bzero(&addr_un, sizeof(addr_un));
unlink(host);
addr_un.sun_family = AF_UNIX;
strncpy(addr_un.sun_path, host, sizeof(addr_un.sun_path) - 1);
ret = bind(sock, (struct sockaddr*) &addr_un, sizeof(addr_un));
}
//IPv6
else if (type > SW_SOCK_UDP)
{
bzero(&addr_in6, sizeof(addr_in6));
inet_pton(AF_INET6, host, &(addr_in6.sin6_addr));
addr_in6.sin6_port = htons(*port);
addr_in6.sin6_family = AF_INET6;
ret = bind(sock, (struct sockaddr *) &addr_in6, sizeof(addr_in6));
if (ret == 0 && *port == 0)
{
len = sizeof(addr_in6);
if (getsockname(sock, (struct sockaddr *) &addr_in6, &len) != -1)
{
*port = ntohs(addr_in6.sin6_port);
}
}
}
//IPv4
else
{
bzero(&addr_in4, sizeof(addr_in4));
inet_pton(AF_INET, host, &(addr_in4.sin_addr));
addr_in4.sin_port = htons(*port);
addr_in4.sin_family = AF_INET;
ret = bind(sock, (struct sockaddr *) &addr_in4, sizeof(addr_in4));
if (ret == 0 && *port == 0)
{
len = sizeof(addr_in4);
if (getsockname(sock, (struct sockaddr *) &addr_in4, &len) != -1)
{
*port = ntohs(addr_in4.sin_port);
}
}
}
//bind failed
if (ret < 0)
{
swoole_error_log(SW_LOG_WARNING, SW_ERROR_SYSTEM_CALL_FAIL, "bind(%s:%d) failed. Error: %s [%d]", host, *port, strerror(errno), errno);
return SW_ERR;
}
return ret;
}
int swSocket_set_buffer_size(int fd, int buffer_size)
{
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buffer_size, sizeof(buffer_size)) < 0)
{
swSysError("setsockopt(%d, SOL_SOCKET, SO_SNDBUF, %d) failed.", fd, buffer_size);
return SW_ERR;
}
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &buffer_size, sizeof(buffer_size)) < 0)
{
swSysError("setsockopt(%d, SOL_SOCKET, SO_RCVBUF, %d) failed.", fd, buffer_size);
return SW_ERR;
}
return SW_OK;
}
int swSocket_set_timeout(int sock, double timeout)
{
int ret;
struct timeval timeo;
timeo.tv_sec = (int) timeout;
timeo.tv_usec = (int) ((timeout - timeo.tv_sec) * 1000 * 1000);
ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (void *) &timeo, sizeof(timeo));
if (ret < 0)
{
swWarn("setsockopt(SO_SNDTIMEO) failed. Error: %s[%d]", strerror(errno), errno);
return SW_ERR;
}
ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *) &timeo, sizeof(timeo));
if (ret < 0)
{
swWarn("setsockopt(SO_RCVTIMEO) failed. Error: %s[%d]", strerror(errno), errno);
return SW_ERR;
}
return SW_OK;
}
int swSocket_create_server(int type, char *address, int port, int backlog)
{
#if 0
int type;
char host[32];
int port = 0;
if (strncasecmp(address, "unix:/", 6) == 0)
{
address += 5;
type = SW_SOCK_UNIX_STREAM;
}
else
{
char *port_str = strchr(address, ':');
if (!port_str)
{
swoole_error_log(SW_LOG_ERROR, SW_ERROR_INVALID_PARAMS, "invalid address[%s]", address);
return SW_ERR;
}
type = SW_SOCK_TCP6;
memcpy(host, address, port_str - address);
host[port_str - address] = 0;
port = atoi(port_str + 1);
address = host;
}
#endif
int fd = swSocket_create(type);
if (fd < 0)
{
swoole_error_log(SW_LOG_ERROR, SW_ERROR_SYSTEM_CALL_FAIL, "socket() failed. Error: %s[%d]", strerror(errno), errno);
return SW_ERR;
}
if (swSocket_bind(fd, type, address, &port) < 0)
{
return SW_ERR;
}
if (listen(fd, backlog) < 0)
{
swoole_error_log(SW_LOG_ERROR, SW_ERROR_SYSTEM_CALL_FAIL, "listen(%s:%d, %d) failed. Error: %s[%d]", address, port, backlog, strerror(errno), errno);
return SW_ERR;
}
return fd;
}

294
vendor/swoole/src/core/string.c vendored Executable file
View File

@ -0,0 +1,294 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| 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: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "swoole.h"
swString *swString_new(size_t size)
{
swString *str = sw_malloc(sizeof(swString));
if (str == NULL)
{
swWarn("malloc[1] failed.");
return NULL;
}
bzero(str, sizeof(swString));
str->size = size;
str->str = sw_malloc(size);
if (str->str == NULL)
{
swSysError("malloc[2](%ld) failed.", size);
sw_free(str);
return NULL;
}
return str;
}
void swString_print(swString *str)
{
printf("String[length=%d,size=%d,offset=%d]=%s\n", (int) str->length, (int) str->size, (int) str->offset,
str->str);
}
swString *swString_dup2(swString *src)
{
swString *dst = swString_new(src->size);
if (dst)
{
swTrace("string dup2. new=%p, old=%p\n", dst, src);
dst->length = src->length;
dst->offset = src->offset;
memcpy(dst->str, src->str, src->length);
}
return dst;
}
swString *swString_dup(const char *src_str, int length)
{
swString *str = sw_malloc(sizeof(swString));
if (str == NULL)
{
swWarn("malloc[1] failed.");
return NULL;
}
bzero(str, sizeof(swString));
str->length = length;
str->size = length + 1;
str->str = sw_malloc(str->size);
if (str->str == NULL)
{
swWarn("malloc[2] failed.");
sw_free(str);
return NULL;
}
memcpy(str->str, src_str, length + 1);
return str;
}
int swString_append(swString *str, swString *append_str)
{
int new_size = str->length + append_str->length;
if (new_size > str->size)
{
if (swString_extend(str, swoole_size_align(new_size * 2, sysconf(_SC_PAGESIZE))) < 0)
{
return SW_ERR;
}
}
memcpy(str->str + str->length, append_str->str, append_str->length);
str->length += append_str->length;
return SW_OK;
}
int swString_append_int(swString *str, int value)
{
char buf[16];
int s_len = swoole_itoa(buf, value);
int new_size = str->length + s_len;
if (new_size > str->size)
{
if (swString_extend(str, swoole_size_align(new_size * 2, sysconf(_SC_PAGESIZE))) < 0)
{
return SW_ERR;
}
}
memcpy(str->str + str->length, buf, s_len);
str->length += s_len;
return SW_OK;
}
int swString_append_ptr(swString *str, char *append_str, int length)
{
int new_size = str->length + length;
if (new_size > str->size)
{
if (swString_extend(str, swoole_size_align(new_size * 2, sysconf(_SC_PAGESIZE))) < 0)
{
return SW_ERR;
}
}
memcpy(str->str + str->length, append_str, length);
str->length += length;
return SW_OK;
}
int swString_write(swString *str, off_t offset, swString *write_str)
{
int new_length = offset + write_str->length;
if (new_length > str->size)
{
if (swString_extend(str, swoole_size_align(new_length * 2, sysconf(_SC_PAGESIZE))) < 0)
{
return SW_ERR;
}
}
memcpy(str->str + offset, write_str->str, write_str->length);
if (new_length > str->length)
{
str->length = new_length;
}
return SW_OK;
}
int swString_write_ptr(swString *str, off_t offset, char *write_str, int length)
{
int new_length = offset + length;
if (new_length > str->size)
{
if (swString_extend(str, swoole_size_align(new_length * 2, sysconf(_SC_PAGESIZE))) < 0)
{
return SW_ERR;
}
}
memcpy(str->str + offset, write_str, length);
if (new_length > str->length)
{
str->length = new_length;
}
return SW_OK;
}
int swString_extend(swString *str, size_t new_size)
{
assert(new_size > str->size);
char *new_str = sw_realloc(str->str, new_size);
if (new_str == NULL)
{
swSysError("realloc(%ld) failed.", new_size);
return SW_ERR;
}
str->str = new_str;
str->size = new_size;
return SW_OK;
}
char* swString_alloc(swString *str, size_t __size)
{
if (str->length + __size < str->size)
{
if (swString_extend_align(str, str->length + __size) < 0)
{
return NULL;
}
}
char *tmp = str->str + str->length;
str->length += __size;
return tmp;
}
uint32_t swoole_utf8_decode(u_char **p, size_t n)
{
size_t len;
uint32_t u, i, valid;
u = **p;
if (u >= 0xf0)
{
u &= 0x07;
valid = 0xffff;
len = 3;
}
else if (u >= 0xe0)
{
u &= 0x0f;
valid = 0x7ff;
len = 2;
}
else if (u >= 0xc2)
{
u &= 0x1f;
valid = 0x7f;
len = 1;
}
else
{
(*p)++;
return 0xffffffff;
}
if (n - 1 < len)
{
return 0xfffffffe;
}
(*p)++;
while (len)
{
i = *(*p)++;
if (i < 0x80)
{
return 0xffffffff;
}
u = (u << 6) | (i & 0x3f);
len--;
}
if (u > valid)
{
return u;
}
return 0xffffffff;
}
size_t swoole_utf8_length(u_char *p, size_t n)
{
u_char c, *last;
size_t len;
last = p + n;
for (len = 0; p < last; len++)
{
c = *p;
if (c < 0x80)
{
p++;
continue;
}
if (swoole_utf8_decode(&p, n) > 0x10ffff)
{
/* invalid UTF-8 */
return n;
}
}
return len;
}
static char characters[] =
{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', };
void swoole_random_string(char *buf, size_t size)
{
int i;
for (i = 0; i < size; i++)
{
buf[i] = characters[swoole_rand(0, sizeof(characters) - 1)];
}
buf[i] = '\0';
}