add files: shared_buffer.[hc]

pull/37/head
YuQing 2020-07-10 21:44:10 +08:00
parent 4aad4f78b9
commit 8e0f5794d9
6 changed files with 150 additions and 13 deletions

View File

@ -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,

View File

@ -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)

View File

@ -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)(

View File

@ -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);
}

43
src/shared_buffer.c Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include <errno.h>
#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);
}

95
src/shared_buffer.h Normal file
View File

@ -0,0 +1,95 @@
#ifndef __SHARED_BUFFER_H__
#define __SHARED_BUFFER_H__
#include <stdint.h>
#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