From 8e0f5794d92c753757192e2c8083c7b346531cd0 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Fri, 10 Jul 2020 21:44:10 +0800 Subject: [PATCH] add files: shared_buffer.[hc] --- HISTORY | 3 +- src/Makefile.in | 6 +-- src/fast_mblock.h | 2 - src/fc_memory.h | 14 +++---- src/shared_buffer.c | 43 ++++++++++++++++++++ src/shared_buffer.h | 95 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 150 insertions(+), 13 deletions(-) create mode 100644 src/shared_buffer.c create mode 100644 src/shared_buffer.h diff --git a/HISTORY b/HISTORY index 9fb3fb5..3a9b60c 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.44 2020-07-07 +Version 1.44 2020-07-10 * add test file src/tests/test_pthread_lock.c * add uniq_skiplist.[hc] * add function split_string_ex @@ -32,6 +32,7 @@ Version 1.44 2020-07-07 * uniq_skiplist add function uniq_skiplist_replace_ex * add files: fc_queue.[hc] * add files: fc_memory.[hc] + * add files: shared_buffer.[hc] Version 1.43 2019-12-25 * replace function call system to getExecResult, diff --git a/src/Makefile.in b/src/Makefile.in index 8b7d877..8c2cdfe 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -16,7 +16,7 @@ FAST_SHARED_OBJS = hash.lo chain.lo shared_func.lo ini_file_reader.lo \ char_converter.lo char_convert_loader.lo common_blocked_queue.lo \ multi_socket_client.lo skiplist_set.lo uniq_skiplist.lo \ json_parser.lo buffered_file_writer.lo server_id_func.lo \ - fc_queue.lo fc_memory.lo + fc_queue.lo fc_memory.lo shared_buffer.lo FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \ logger.o sockopt.o base64.o sched_thread.o \ @@ -29,7 +29,7 @@ FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \ char_converter.o char_convert_loader.o common_blocked_queue.o \ multi_socket_client.o skiplist_set.o uniq_skiplist.o \ json_parser.o buffered_file_writer.o server_id_func.o \ - fc_queue.o fc_memory.o + fc_queue.o fc_memory.o shared_buffer.o HEADER_FILES = common_define.h hash.h chain.h logger.h base64.h \ shared_func.h pthread_func.h ini_file_reader.h _os_define.h \ @@ -43,7 +43,7 @@ HEADER_FILES = common_define.h hash.h chain.h logger.h base64.h \ char_convert_loader.h common_blocked_queue.h \ multi_socket_client.h skiplist_set.h uniq_skiplist.h \ fc_list.h json_parser.h buffered_file_writer.h server_id_func.h \ - fc_queue.h fc_memory.h + fc_queue.h fc_memory.h shared_buffer.h ALL_OBJS = $(FAST_STATIC_OBJS) $(FAST_SHARED_OBJS) diff --git a/src/fast_mblock.h b/src/fast_mblock.h index 72552d3..3b63534 100644 --- a/src/fast_mblock.h +++ b/src/fast_mblock.h @@ -46,8 +46,6 @@ struct fast_mblock_chain { struct fast_mblock_node *tail; }; -typedef void (*fc_oom_notify_func)(const int curr_alloc_size); - typedef int (*fast_mblock_alloc_init_func)(void *element, void *args); typedef int (*fast_mblock_malloc_trunk_check_func)( diff --git a/src/fc_memory.h b/src/fc_memory.h index 1f3007b..c93b475 100644 --- a/src/fc_memory.h +++ b/src/fc_memory.h @@ -21,9 +21,9 @@ extern "C" { void *ptr; ptr = malloc(size); if (ptr == NULL) { - logError("file: %s, line: %d, malloc %d bytes fail, " + logError("file: %s, line: %d, malloc %"PRId64" bytes fail, " "errno: %d, error info: %s", file, line, - (int)size, errno, STRERROR(errno)); + (int64_t)size, errno, STRERROR(errno)); if (g_oom_notify != NULL) { g_oom_notify(size); } @@ -38,9 +38,9 @@ extern "C" { void *new_ptr; new_ptr = realloc(ptr, size); if (new_ptr == NULL) { - logError("file: %s, line: %d, realloc %d bytes fail, " + logError("file: %s, line: %d, realloc %"PRId64" bytes fail, " "errno: %d, error info: %s", file, line, - (int)size, errno, STRERROR(errno)); + (int64_t)size, errno, STRERROR(errno)); if (g_oom_notify != NULL) { g_oom_notify(size); } @@ -68,15 +68,15 @@ extern "C" { return output; } - static inline void *fc_calloc(const char *file, + static inline void *fc_calloc_ex(const char *file, const int line, size_t count, size_t size) { void *ptr; ptr = calloc(count, size); if (ptr == NULL) { - logError("file: %s, line: %d, malloc %d bytes fail, " + logError("file: %s, line: %d, malloc %"PRId64" bytes fail, " "errno: %d, error info: %s", file, line, - (int)(count * size), errno, STRERROR(errno)); + (int64_t)(count * size), errno, STRERROR(errno)); if (g_oom_notify != NULL) { g_oom_notify(count * size); } diff --git a/src/shared_buffer.c b/src/shared_buffer.c new file mode 100644 index 0000000..9f409c2 --- /dev/null +++ b/src/shared_buffer.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include +#include "shared_buffer.h" + +static int shared_buffer_alloc_init(void *element, void *args) +{ + SharedBuffer *buffer; + SharedBufferContext *ctx; + + buffer = (SharedBuffer *)element; + ctx = (SharedBufferContext *)args; + buffer->ctx = ctx; + + return shared_buffer_check_capacity(buffer, + ctx->buffer_init_capacity); +} + +int shared_buffer_init_ex(SharedBufferContext *context, + const int alloc_elements_once, const int buffer_init_capacity, + const bool need_lock) +{ + int result; + + context->buffer_init_capacity = buffer_init_capacity; + if ((result=fast_mblock_init_ex2(&context->allocator, "shared_buffer", + sizeof(SharedBuffer), alloc_elements_once, + shared_buffer_alloc_init, context, need_lock, + NULL, NULL, NULL)) != 0) + { + return result; + } + + return 0; +} + +void shared_buffer_destroy(SharedBufferContext *context) +{ + fast_mblock_destroy(&context->allocator); +} diff --git a/src/shared_buffer.h b/src/shared_buffer.h new file mode 100644 index 0000000..3445393 --- /dev/null +++ b/src/shared_buffer.h @@ -0,0 +1,95 @@ +#ifndef __SHARED_BUFFER_H__ +#define __SHARED_BUFFER_H__ + +#include +#include "common_define.h" +#include "fast_mblock.h" +#include "logger.h" + +typedef struct shared_buffer_context { + struct fast_mblock_man allocator; + int buffer_init_capacity; +} SharedBufferContext; + +typedef struct shared_buffer { + char *buff; + int capacity; + int length; + volatile int reffer_count; + SharedBufferContext *ctx; +} SharedBuffer; + +#ifdef __cplusplus +extern "C" { +#endif + +#define shared_buffer_init(context, alloc_elements_once, buffer_init_capacity) \ + shared_buffer_init_ex(context, alloc_elements_once, \ + buffer_init_capacity, true) + +int shared_buffer_init_ex(SharedBufferContext *context, + const int alloc_elements_once, const int buffer_init_capacity, + const bool need_lock); + +void shared_buffer_destroy(SharedBufferContext *context); + +static inline SharedBuffer *shared_buffer_alloc_ex(SharedBufferContext *context, + const int init_reffer_count) +{ + SharedBuffer *buffer; + if ((buffer=(SharedBuffer *)fast_mblock_alloc_object( + &context->allocator)) != NULL) + { + if (init_reffer_count > 0) { + __sync_add_and_fetch(&buffer->reffer_count, init_reffer_count); + } + } + return buffer; +} + +static inline SharedBuffer *shared_buffer_alloc(SharedBufferContext *context) +{ + return (SharedBuffer *)fast_mblock_alloc_object(&context->allocator); +} + +static inline void shared_buffer_hold(SharedBuffer *buffer) +{ + __sync_add_and_fetch(&buffer->reffer_count, 1); +} + +static inline void shared_buffer_release(SharedBuffer *buffer) +{ + if (__sync_sub_and_fetch(&buffer->reffer_count, 1) == 0) { + logInfo("file: "__FILE__", line: %d, " + "free shared buffer: %p", __LINE__, buffer); + fast_mblock_free_object(&buffer->ctx->allocator, buffer); + } +} + +static inline int shared_buffer_check_capacity(SharedBuffer *buffer, + const int target_capacity) +{ + char *buff; + if (buffer->capacity >= target_capacity) { + return 0; + } + + buff = (char *)fc_malloc(target_capacity); + if (buff == NULL) { + return ENOMEM; + } + + if (buffer->buff != NULL) { + free(buffer->buff); + } + buffer->buff = buff; + buffer->capacity = target_capacity; + return 0; +} + +#ifdef __cplusplus +} +#endif + +#endif +