fast_buffer.[hc]: add function fast_buffer_append_binary

storage_pool
YuQing 2021-01-27 21:10:43 +08:00
parent e104d2f7f6
commit 41a4ca78c7
5 changed files with 54 additions and 20 deletions

View File

@ -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 Version 1.47 2021-01-20
* fc_atomic.h: add FC_ATOMIC_GET and FC_ATOMIC_SET etc. * fc_atomic.h: add FC_ATOMIC_GET and FC_ATOMIC_SET etc.
* fast_mblock.[hc]: support wait with element limit * fast_mblock.[hc]: support wait with element limit

View File

@ -153,6 +153,25 @@ int fast_buffer_append_buff(FastBuffer *buffer, const char *data, const int len)
return 0; 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 fast_buffer_append_int(FastBuffer *buffer, const int n)
{ {
int result; int result;

View File

@ -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(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); int fast_buffer_append_int(FastBuffer *buffer, const int n);

View File

@ -3087,6 +3087,7 @@ int64_t fc_ceil_prime(const int64_t n)
} }
int fc_init_buffer(BufferInfo *buffer, const int buffer_size) int fc_init_buffer(BufferInfo *buffer, const int buffer_size)
{ {
buffer->buff = (char *)fc_malloc(buffer_size); buffer->buff = (char *)fc_malloc(buffer_size);
if (buffer->buff == NULL) 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) { if (mkdir(path, mode) != 0) {
result = errno != 0 ? errno : EPERM; result = errno != 0 ? errno : EPERM;
if (result == EEXIST) {
return 0;
}
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
"mkdir %s fail, errno: %d, error info: %s", "mkdir %s fail, errno: %d, error info: %s",
__LINE__, path, result, STRERROR(result)); __LINE__, path, result, STRERROR(result));

View File

@ -215,7 +215,7 @@ static int uniq_skiplist_grow(UniqSkiplist *sl)
int i; int i;
struct fast_mblock_man *top_mblock; struct fast_mblock_man *top_mblock;
UniqSkiplistNode *old_top; UniqSkiplistNode *old_top;
UniqSkiplistNode *top; UniqSkiplistNode *new_top;
top_level_index = sl->top_level_index + 1; top_level_index = sl->top_level_index + 1;
if (top_level_index >= sl->factory->max_level_count) { 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_mblock = sl->factory->node_allocators + top_level_index;
top = (UniqSkiplistNode *)fast_mblock_alloc_object(top_mblock); new_top = (UniqSkiplistNode *)fast_mblock_alloc_object(top_mblock);
if (top == NULL) { if (new_top == NULL) {
return ENOMEM; return ENOMEM;
} }
memset(top, 0, top_mblock->info.element_size); memset(new_top, 0, top_mblock->info.element_size);
top->level_index = top_level_index; new_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];
}
old_top_level_index = sl->top_level_index; old_top_level_index = sl->top_level_index;
old_top = sl->top; old_top = sl->top;
sl->top = top;
compile_barrier(); for (i=0; i<=old_top_level_index; i++) {
sl->top_level_index = top_level_index; new_top->links[i] = old_top->links[i];
}
new_top->links[top_level_index] = sl->factory->tail;
if (sl->factory->bidirection) { if (sl->factory->bidirection) {
if (top->links[0] != sl->factory->tail) { //not empty if (new_top->links[0] != sl->factory->tail) { //not empty
LEVEL0_DOUBLE_CHAIN_PREV_LINK(top->links[0]) = top; LEVEL0_DOUBLE_CHAIN_PREV_LINK(new_top->links[0]) = new_top;
LEVEL0_DOUBLE_CHAIN_TAIL(sl) = LEVEL0_DOUBLE_CHAIN_PREV_LINK(new_top) =
LEVEL0_DOUBLE_CHAIN_PREV_LINK(old_top); LEVEL0_DOUBLE_CHAIN_PREV_LINK(old_top);
} else { } 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); UNIQ_SKIPLIST_FREE_MBLOCK_OBJECT(sl, old_top_level_index, old_top);
return 0; return 0;
} }
@ -270,6 +269,7 @@ void uniq_skiplist_free(UniqSkiplist *sl)
while (node != sl->factory->tail) { while (node != sl->factory->tail) {
deleted = node; deleted = node;
node = node->links[0]; node = node->links[0];
sl->factory->free_func(deleted->data, 0); sl->factory->free_func(deleted->data, 0);
fast_mblock_free_object(sl->factory->node_allocators + fast_mblock_free_object(sl->factory->node_allocators +
deleted->level_index, (void *)deleted); deleted->level_index, (void *)deleted);
@ -278,16 +278,18 @@ void uniq_skiplist_free(UniqSkiplist *sl)
while (node != sl->factory->tail) { while (node != sl->factory->tail) {
deleted = node; deleted = node;
node = node->links[0]; node = node->links[0];
fast_mblock_free_object(sl->factory->node_allocators + fast_mblock_free_object(sl->factory->node_allocators +
deleted->level_index, (void *)deleted); deleted->level_index, (void *)deleted);
} }
} }
fast_mblock_free_object(sl->factory->node_allocators + 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->top = NULL;
sl->element_count = 0; sl->element_count = 0;
sl->top_level_index = 0;
fast_mblock_free_object(&sl->factory->skiplist_allocator, sl); fast_mblock_free_object(&sl->factory->skiplist_allocator, sl);
} }