From 41a4ca78c758a52576925477881599e6534eb5c1 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Wed, 27 Jan 2021 21:10:43 +0800 Subject: [PATCH] fast_buffer.[hc]: add function fast_buffer_append_binary --- HISTORY | 4 ++++ src/fast_buffer.c | 19 +++++++++++++++++++ src/fast_buffer.h | 6 +++++- src/shared_func.c | 5 +++++ src/uniq_skiplist.c | 40 +++++++++++++++++++++------------------- 5 files changed, 54 insertions(+), 20 deletions(-) diff --git a/HISTORY b/HISTORY index dc1a70d..14f8873 100644 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,8 @@ +Version 1.48 2021-01-27 + * fast_buffer.[hc]: add function fast_buffer_append_binary + * fc_check_mkdir_ex return 0 when mkdir with errno EEXIST + Version 1.47 2021-01-20 * fc_atomic.h: add FC_ATOMIC_GET and FC_ATOMIC_SET etc. * fast_mblock.[hc]: support wait with element limit diff --git a/src/fast_buffer.c b/src/fast_buffer.c index c9121e1..d07ccec 100644 --- a/src/fast_buffer.c +++ b/src/fast_buffer.c @@ -153,6 +153,25 @@ int fast_buffer_append_buff(FastBuffer *buffer, const char *data, const int len) return 0; } +int fast_buffer_append_binary(FastBuffer *buffer, + const void *data, const int len) +{ + int result; + + if (len <= 0) + { + return 0; + } + if ((result=fast_buffer_check(buffer, len)) != 0) + { + return result; + } + + memcpy(buffer->data + buffer->length, data, len); + buffer->length += len; + return 0; +} + int fast_buffer_append_int(FastBuffer *buffer, const int n) { int result; diff --git a/src/fast_buffer.h b/src/fast_buffer.h index aeabe27..735d1ac 100644 --- a/src/fast_buffer.h +++ b/src/fast_buffer.h @@ -80,7 +80,11 @@ static inline int fast_buffer_check_inc_size(FastBuffer *buffer, int fast_buffer_append(FastBuffer *buffer, const char *format, ...); -int fast_buffer_append_buff(FastBuffer *buffer, const char *data, const int len); +int fast_buffer_append_buff(FastBuffer *buffer, + const char *data, const int len); + +int fast_buffer_append_binary(FastBuffer *buffer, + const void *data, const int len); int fast_buffer_append_int(FastBuffer *buffer, const int n); diff --git a/src/shared_func.c b/src/shared_func.c index 947e811..1c5a192 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -3087,6 +3087,7 @@ int64_t fc_ceil_prime(const int64_t n) } int fc_init_buffer(BufferInfo *buffer, const int buffer_size) + { buffer->buff = (char *)fc_malloc(buffer_size); if (buffer->buff == NULL) @@ -3127,6 +3128,10 @@ int fc_check_mkdir_ex(const char *path, const mode_t mode, bool *create) if (mkdir(path, mode) != 0) { result = errno != 0 ? errno : EPERM; + if (result == EEXIST) { + return 0; + } + logError("file: "__FILE__", line: %d, " "mkdir %s fail, errno: %d, error info: %s", __LINE__, path, result, STRERROR(result)); diff --git a/src/uniq_skiplist.c b/src/uniq_skiplist.c index 687e815..706a76f 100644 --- a/src/uniq_skiplist.c +++ b/src/uniq_skiplist.c @@ -215,7 +215,7 @@ static int uniq_skiplist_grow(UniqSkiplist *sl) int i; struct fast_mblock_man *top_mblock; UniqSkiplistNode *old_top; - UniqSkiplistNode *top; + UniqSkiplistNode *new_top; top_level_index = sl->top_level_index + 1; if (top_level_index >= sl->factory->max_level_count) { @@ -223,35 +223,34 @@ static int uniq_skiplist_grow(UniqSkiplist *sl) } top_mblock = sl->factory->node_allocators + top_level_index; - top = (UniqSkiplistNode *)fast_mblock_alloc_object(top_mblock); - if (top == NULL) { + new_top = (UniqSkiplistNode *)fast_mblock_alloc_object(top_mblock); + if (new_top == NULL) { return ENOMEM; } - memset(top, 0, top_mblock->info.element_size); - top->level_index = top_level_index; - - top->links[top_level_index] = sl->factory->tail; - for (i=0; i<=sl->top_level_index; i++) { - top->links[i] = sl->top->links[i]; - } - + memset(new_top, 0, top_mblock->info.element_size); + new_top->level_index = top_level_index; old_top_level_index = sl->top_level_index; old_top = sl->top; - sl->top = top; - compile_barrier(); - sl->top_level_index = top_level_index; + for (i=0; i<=old_top_level_index; i++) { + new_top->links[i] = old_top->links[i]; + } + new_top->links[top_level_index] = sl->factory->tail; if (sl->factory->bidirection) { - if (top->links[0] != sl->factory->tail) { //not empty - LEVEL0_DOUBLE_CHAIN_PREV_LINK(top->links[0]) = top; - LEVEL0_DOUBLE_CHAIN_TAIL(sl) = + if (new_top->links[0] != sl->factory->tail) { //not empty + LEVEL0_DOUBLE_CHAIN_PREV_LINK(new_top->links[0]) = new_top; + LEVEL0_DOUBLE_CHAIN_PREV_LINK(new_top) = LEVEL0_DOUBLE_CHAIN_PREV_LINK(old_top); } else { - LEVEL0_DOUBLE_CHAIN_TAIL(sl) = sl->top; + LEVEL0_DOUBLE_CHAIN_PREV_LINK(new_top) = new_top; } } + sl->top = new_top; + compile_barrier(); + sl->top_level_index = top_level_index; + UNIQ_SKIPLIST_FREE_MBLOCK_OBJECT(sl, old_top_level_index, old_top); return 0; } @@ -270,6 +269,7 @@ void uniq_skiplist_free(UniqSkiplist *sl) while (node != sl->factory->tail) { deleted = node; node = node->links[0]; + sl->factory->free_func(deleted->data, 0); fast_mblock_free_object(sl->factory->node_allocators + deleted->level_index, (void *)deleted); @@ -278,16 +278,18 @@ void uniq_skiplist_free(UniqSkiplist *sl) while (node != sl->factory->tail) { deleted = node; node = node->links[0]; + fast_mblock_free_object(sl->factory->node_allocators + deleted->level_index, (void *)deleted); } } fast_mblock_free_object(sl->factory->node_allocators + - sl->top_level_index, (void *)sl->top); + sl->top_level_index, sl->top); sl->top = NULL; sl->element_count = 0; + sl->top_level_index = 0; fast_mblock_free_object(&sl->factory->skiplist_allocator, sl); }