From 4aad4f78b920ff6a7abe7a765956b8fff9919a1e Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Tue, 7 Jul 2020 10:32:16 +0800 Subject: [PATCH] add files: fc_memory.[hc] --- HISTORY | 5 +- src/Makefile.in | 6 +-- src/avl_tree.c | 8 +--- src/base64.c | 5 +- src/buffered_file_writer.c | 6 +-- src/chain.c | 7 +-- src/fast_allocator.c | 27 +++-------- src/fast_buffer.c | 9 ++-- src/fast_mblock.c | 11 ++--- src/fast_mblock.h | 3 ++ src/fast_mpool.c | 8 +--- src/fast_task_queue.c | 34 +++---------- src/fast_timer.c | 5 +- src/fc_memory.c | 5 ++ src/fc_memory.h | 97 ++++++++++++++++++++++++++++++++++++++ src/fc_queue.c | 34 +++++++++++++ src/fc_queue.h | 23 +++++++++ src/flat_skiplist.c | 15 ++---- src/hash.c | 11 +++-- src/http_func.c | 24 ++-------- src/ini_file_reader.c | 67 +++++++------------------- src/ioevent.c | 7 +-- src/json_parser.c | 13 ++--- src/multi_skiplist.c | 15 ++---- src/pthread_func.c | 11 ++--- src/sched_thread.c | 42 ++++------------- src/server_id_func.c | 31 +++--------- src/shared_func.c | 71 +++++++--------------------- src/shared_func.h | 6 +-- src/skiplist_set.c | 15 ++---- src/system_info.c | 17 ++----- src/uniq_skiplist.c | 8 ++-- 32 files changed, 305 insertions(+), 341 deletions(-) create mode 100644 src/fc_memory.c create mode 100644 src/fc_memory.h diff --git a/HISTORY b/HISTORY index b12a757..9fb3fb5 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.44 2020-06-28 +Version 1.44 2020-07-07 * add test file src/tests/test_pthread_lock.c * add uniq_skiplist.[hc] * add function split_string_ex @@ -30,7 +30,8 @@ Version 1.44 2020-06-28 * fast_task_queue.[hc]: free_queue support init_callback * ini_file_reader.c: use mutex lock when access dynamic content array * uniq_skiplist add function uniq_skiplist_replace_ex - * add fc_queue.[hc] + * add files: fc_queue.[hc] + * add files: fc_memory.[hc] Version 1.43 2019-12-25 * replace function call system to getExecResult, diff --git a/src/Makefile.in b/src/Makefile.in index 511edc5..8b7d877 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_queue.lo fc_memory.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_queue.o fc_memory.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_queue.h fc_memory.h ALL_OBJS = $(FAST_STATIC_OBJS) $(FAST_SHARED_OBJS) diff --git a/src/avl_tree.c b/src/avl_tree.c index 65c7e36..7b75938 100644 --- a/src/avl_tree.c +++ b/src/avl_tree.c @@ -1,3 +1,4 @@ +#include "fc_memory.h" #include "avl_tree.h" int avl_tree_init(AVLTreeInfo *tree, FreeDataFunc free_data_func, \ @@ -45,14 +46,9 @@ void avl_tree_destroy(AVLTreeInfo *tree) static AVLTreeNode *createTreeNode(AVLTreeNode *pParentNode, void *target_data) { AVLTreeNode *pNewNode; - pNewNode = (AVLTreeNode *)malloc(sizeof(AVLTreeNode)); + pNewNode = (AVLTreeNode *)fc_malloc(sizeof(AVLTreeNode)); if (pNewNode == NULL) { - /* - fprintf(stderr, "file: "__FILE__", line: %d, " \ - "malloc %d bytes fail!\n", __LINE__, \ - (int)sizeof(AVLTreeNode)); - */ return NULL; } diff --git a/src/base64.c b/src/base64.c index 35c7e04..5e7294d 100644 --- a/src/base64.c +++ b/src/base64.c @@ -16,6 +16,7 @@ #include #include #include +#include "fc_memory.h" #include "base64.h" /** @@ -281,11 +282,9 @@ char *base64_decode_auto(struct base64_context *context, const char *src, \ } else { - pBuff = (char *)malloc(nNewLen); + pBuff = (char *)fc_malloc(nNewLen); if (pBuff == NULL) { - fprintf(stderr, "Can't malloc %d bytes\n", \ - nSrcLen + nPadLen + 1); *dest_len = 0; *dest = '\0'; return dest; diff --git a/src/buffered_file_writer.c b/src/buffered_file_writer.c index 5a52c25..2cc1a58 100644 --- a/src/buffered_file_writer.c +++ b/src/buffered_file_writer.c @@ -15,6 +15,7 @@ #include #include "shared_func.h" #include "logger.h" +#include "fc_memory.h" #include "buffered_file_writer.h" int buffered_file_writer_open_ex(BufferedFileWriter *writer, @@ -34,12 +35,9 @@ int buffered_file_writer_open_ex(BufferedFileWriter *writer, return EINVAL; } - writer->buff = (char *)malloc(writer->buffer_size); + writer->buff = (char *)fc_malloc(writer->buffer_size); if (writer->buff == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", - __LINE__, writer->buffer_size); return ENOMEM; } diff --git a/src/chain.c b/src/chain.c index 1a7e9a9..5e5a276 100644 --- a/src/chain.c +++ b/src/chain.c @@ -11,6 +11,7 @@ #include #include #include "chain.h" +#include "fc_memory.h" //#include "use_mmalloc.h" void chain_init(ChainList *pList, const int type, FreeDataFunc freeDataFunc, \ @@ -69,7 +70,7 @@ int insertNodePrior(ChainList *pList, void *data) return EINVAL; } - pNode = (ChainNode *)malloc(sizeof(ChainNode)); + pNode = (ChainNode *)fc_malloc(sizeof(ChainNode)); if (pNode == NULL) { return ENOMEM; @@ -95,7 +96,7 @@ int appendNode(ChainList *pList, void *data) return EINVAL; } - pNode = (ChainNode *)malloc(sizeof(ChainNode)); + pNode = (ChainNode *)fc_malloc(sizeof(ChainNode)); if (pNode == NULL) { return ENOMEM; @@ -128,7 +129,7 @@ int insertNodeAsc(ChainList *pList, void *data) return EINVAL; } - pNew = (ChainNode *)malloc(sizeof(ChainNode)); + pNew = (ChainNode *)fc_malloc(sizeof(ChainNode)); if (pNew == NULL) { return ENOMEM; diff --git a/src/fast_allocator.c b/src/fast_allocator.c index 48f8b38..f818735 100644 --- a/src/fast_allocator.c +++ b/src/fast_allocator.c @@ -64,7 +64,6 @@ static void fast_allocator_malloc_trunk_notify_func(const int alloc_bytes, void static int allocator_array_check_capacity(struct fast_allocator_context *acontext, const int allocator_count) { - int result; int bytes; int target_count; int alloc_count; @@ -108,14 +107,10 @@ static int allocator_array_check_capacity(struct fast_allocator_context *acontex } bytes = sizeof(struct fast_allocator_info *) * alloc_count; - new_allocators = (struct fast_allocator_info **)malloc(bytes); + new_allocators = (struct fast_allocator_info **)fc_malloc(bytes); if (new_allocators == NULL) { - result = errno != 0 ? errno : ENOMEM; - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail, errno: %d, error info: %s", - __LINE__, bytes, result, STRERROR(result)); - return result; + return ENOMEM; } if (acontext->allocator_array.allocators != NULL) @@ -144,14 +139,10 @@ static int region_init(struct fast_allocator_context *acontext, region->pad_mask = region->step - 1; allocator_count = (region->end - region->start) / region->step; bytes = sizeof(struct fast_allocator_info) * allocator_count; - region->allocators = (struct fast_allocator_info *)malloc(bytes); + region->allocators = (struct fast_allocator_info *)fc_malloc(bytes); if (region->allocators == NULL) { - result = errno != 0 ? errno : ENOMEM; - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail, errno: %d, error info: %s", - __LINE__, bytes, result, STRERROR(result)); - return result; + return ENOMEM; } memset(region->allocators, 0, bytes); @@ -227,14 +218,10 @@ int fast_allocator_init_ex(struct fast_allocator_context *acontext, } bytes = sizeof(struct fast_region_info) * region_count; - acontext->regions = (struct fast_region_info *)malloc(bytes); + acontext->regions = (struct fast_region_info *)fc_malloc(bytes); if (acontext->regions == NULL) { - result = errno != 0 ? errno : ENOMEM; - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail, errno: %d, error info: %s", - __LINE__, bytes, result, STRERROR(result)); - return result; + return ENOMEM; } memcpy(acontext->regions, regions, bytes); acontext->region_count = region_count; @@ -470,7 +457,7 @@ void *fast_allocator_alloc(struct fast_allocator_context *acontext, { return NULL; } - ptr = malloc(alloc_bytes); + ptr = fc_malloc(alloc_bytes); if (ptr == NULL) { return NULL; diff --git a/src/fast_buffer.c b/src/fast_buffer.c index 8111025..39a0108 100644 --- a/src/fast_buffer.c +++ b/src/fast_buffer.c @@ -8,6 +8,7 @@ #include #include "logger.h" #include "shared_func.h" +#include "fc_memory.h" #include "fast_buffer.h" int fast_buffer_init_ex(FastBuffer *buffer, const int init_capacity) @@ -21,11 +22,9 @@ int fast_buffer_init_ex(FastBuffer *buffer, const int init_capacity) { buffer->alloc_size = 256; } - buffer->data = (char *)malloc(buffer->alloc_size); + buffer->data = (char *)fc_malloc(buffer->alloc_size); if (buffer->data == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, buffer->alloc_size); return ENOMEM; } *(buffer->data) = '\0'; @@ -68,10 +67,8 @@ int fast_buffer_set_capacity(FastBuffer *buffer, const int capacity) alloc_size *= 2; } - buff = (char *)malloc(alloc_size); + buff = (char *)fc_malloc(alloc_size); if (buff == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, alloc_size); return ENOMEM; } diff --git a/src/fast_mblock.c b/src/fast_mblock.c index ae3e583..9c3067e 100644 --- a/src/fast_mblock.c +++ b/src/fast_mblock.c @@ -222,7 +222,7 @@ int fast_mblock_manager_stat_print_ex(const bool hide_empty, const int order_by) while (result == EOVERFLOW) { alloc_size *= 2; - stats = realloc(stats, sizeof(struct fast_mblock_info) * alloc_size); + stats = fc_realloc(stats, sizeof(struct fast_mblock_info) * alloc_size); if (stats == NULL) { return ENOMEM; @@ -435,15 +435,10 @@ static int fast_mblock_prealloc(struct fast_mblock_man *mblock) return ENOMEM; } - pNew = (char *)malloc(mblock->info.trunk_size); + pNew = (char *)fc_malloc(mblock->info.trunk_size); if (pNew == NULL) { - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes fail, " \ - "errno: %d, error info: %s", \ - __LINE__, mblock->info.trunk_size, - errno, STRERROR(errno)); - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } memset(pNew, 0, mblock->info.trunk_size); diff --git a/src/fast_mblock.h b/src/fast_mblock.h index 084ed27..72552d3 100644 --- a/src/fast_mblock.h +++ b/src/fast_mblock.h @@ -16,6 +16,7 @@ #include #include #include "common_define.h" +#include "fc_memory.h" #include "chain.h" #define FAST_MBLOCK_NAME_SIZE 32 @@ -45,6 +46,8 @@ 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/fast_mpool.c b/src/fast_mpool.c index 759a1d1..a18a9c6 100644 --- a/src/fast_mpool.c +++ b/src/fast_mpool.c @@ -44,14 +44,10 @@ static int fast_mpool_prealloc(struct fast_mpool_man *mpool, int bytes; bytes = sizeof(struct fast_mpool_malloc) + alloc_size; - pMallocNode = (struct fast_mpool_malloc *)malloc(bytes); + pMallocNode = (struct fast_mpool_malloc *)fc_malloc(bytes); if (pMallocNode == NULL) { - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes fail, " \ - "errno: %d, error info: %s", \ - __LINE__, bytes, errno, STRERROR(errno)); - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } pMallocNode->alloc_size = alloc_size; diff --git a/src/fast_task_queue.c b/src/fast_task_queue.c index be83b45..e669a40 100644 --- a/src/fast_task_queue.c +++ b/src/fast_task_queue.c @@ -4,10 +4,11 @@ #include #include #include -#include "fast_task_queue.h" #include "logger.h" #include "shared_func.h" #include "pthread_func.h" +#include "fc_memory.h" +#include "fast_task_queue.h" static struct fast_task_queue g_free_queue; @@ -61,26 +62,16 @@ static struct mpool_node *malloc_mpool(const int total_alloc_size) char *pCharEnd; struct mpool_node *mpool; - mpool = (struct mpool_node *)malloc(sizeof(struct mpool_node)); + mpool = (struct mpool_node *)fc_malloc(sizeof(struct mpool_node)); if (mpool == NULL) { - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes fail, " \ - "errno: %d, error info: %s", \ - __LINE__, (int)sizeof(struct mpool_node), \ - errno, STRERROR(errno)); return NULL; } mpool->next = NULL; - mpool->blocks = (struct fast_task_info *)malloc(total_alloc_size); + mpool->blocks = (struct fast_task_info *)fc_malloc(total_alloc_size); if (mpool->blocks == NULL) { - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes fail, " \ - "errno: %d, error info: %s", \ - __LINE__, total_alloc_size, \ - errno, STRERROR(errno)); free(mpool); return NULL; } @@ -100,15 +91,9 @@ static struct mpool_node *malloc_mpool(const int total_alloc_size) } else { - pTask->data = (char *)malloc(pTask->size); + pTask->data = (char *)fc_malloc(pTask->size); if (pTask->data == NULL) { - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes fail, " \ - "errno: %d, error info: %s", \ - __LINE__, pTask->size, \ - errno, STRERROR(errno)); - free_mpool(mpool, p); return NULL; } @@ -463,15 +448,10 @@ static int _realloc_buffer(struct fast_task_info *pTask, const int new_size, const bool copy_data) { char *new_buff; - new_buff = (char *)malloc(new_size); + new_buff = (char *)fc_malloc(new_size); if (new_buff == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail, " - "errno: %d, error info: %s", - __LINE__, new_size, - errno, STRERROR(errno)); - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } else { diff --git a/src/fast_timer.c b/src/fast_timer.c index b9c3c5c..d699120 100644 --- a/src/fast_timer.c +++ b/src/fast_timer.c @@ -4,6 +4,7 @@ #include #include #include "logger.h" +#include "fc_memory.h" #include "fast_timer.h" int fast_timer_init(FastTimer *timer, const int slot_count, @@ -18,9 +19,9 @@ int fast_timer_init(FastTimer *timer, const int slot_count, timer->base_time = current_time; //base time for slot 0 timer->current_time = current_time; bytes = sizeof(FastTimerSlot) * slot_count; - timer->slots = (FastTimerSlot *)malloc(bytes); + timer->slots = (FastTimerSlot *)fc_malloc(bytes); if (timer->slots == NULL) { - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } memset(timer->slots, 0, bytes); return 0; diff --git a/src/fc_memory.c b/src/fc_memory.c new file mode 100644 index 0000000..e051393 --- /dev/null +++ b/src/fc_memory.c @@ -0,0 +1,5 @@ +//fc_memory.c + +#include "fc_memory.h" + +fc_memory_oom_notify_func g_oom_notify = NULL; diff --git a/src/fc_memory.h b/src/fc_memory.h new file mode 100644 index 0000000..1f3007b --- /dev/null +++ b/src/fc_memory.h @@ -0,0 +1,97 @@ +//fc_memory.h + +#ifndef _FC_MEMORY_H +#define _FC_MEMORY_H + +#include +#include "common_define.h" +#include "logger.h" + +typedef void (*fc_memory_oom_notify_func)(const size_t curr_size); + +#ifdef __cplusplus +extern "C" { +#endif + + extern fc_memory_oom_notify_func g_oom_notify; + + static inline void *fc_malloc_ex(const char *file, + const int line, size_t size) + { + void *ptr; + ptr = malloc(size); + if (ptr == NULL) { + logError("file: %s, line: %d, malloc %d bytes fail, " + "errno: %d, error info: %s", file, line, + (int)size, errno, STRERROR(errno)); + if (g_oom_notify != NULL) { + g_oom_notify(size); + } + } + + return ptr; + } + + static inline void *fc_realloc_ex(const char *file, + const int line, void *ptr, size_t size) + { + void *new_ptr; + new_ptr = realloc(ptr, size); + if (new_ptr == NULL) { + logError("file: %s, line: %d, realloc %d bytes fail, " + "errno: %d, error info: %s", file, line, + (int)size, errno, STRERROR(errno)); + if (g_oom_notify != NULL) { + g_oom_notify(size); + } + } + + return new_ptr; + } + + static inline char *fc_strdup_ex(const char *file, + const int line, const char *str) + { + char *output; + int len; + + len = strlen(str); + output = (char *)fc_malloc_ex(file, line, len + 1); + if (output == NULL) { + return NULL; + } + + if (len > 0) { + memcpy(output, str, len); + } + *(output + len) = '\0'; + return output; + } + + static inline void *fc_calloc(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, " + "errno: %d, error info: %s", file, line, + (int)(count * size), errno, STRERROR(errno)); + if (g_oom_notify != NULL) { + g_oom_notify(count * size); + } + } + + return ptr; + } + +#define fc_malloc(size) fc_malloc_ex(__FILE__, __LINE__, size) +#define fc_realloc(ptr, size) fc_realloc_ex(__FILE__, __LINE__, ptr, size) +#define fc_calloc(count, size) fc_calloc_ex(__FILE__, __LINE__, count, size) +#define fc_strdup(str) fc_strdup_ex(__FILE__, __LINE__, str) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/fc_queue.c b/src/fc_queue.c index d523d83..9100efd 100644 --- a/src/fc_queue.c +++ b/src/fc_queue.c @@ -110,3 +110,37 @@ void *fc_queue_pop_all_ex(struct fc_queue *queue, const bool blocked) PTHREAD_MUTEX_UNLOCK(&queue->lock); return data; } + +void fc_queue_push_queue_to_head_ex(struct fc_queue *queue, + struct fc_queue_info *qinfo, bool *notify) +{ + if (qinfo->head == NULL) { + *notify = false; + return; + } + + PTHREAD_MUTEX_LOCK(&queue->lock); + FC_QUEUE_NEXT_PTR(queue, qinfo->tail) = queue->head; + queue->head = qinfo->head; + if (queue->tail == NULL) { + queue->tail = qinfo->tail; + *notify = true; + } else { + *notify = false; + } + PTHREAD_MUTEX_UNLOCK(&queue->lock); +} + +void fc_queue_pop_to_queue(struct fc_queue *queue, + struct fc_queue_info *qinfo) +{ + PTHREAD_MUTEX_LOCK(&queue->lock); + if (queue->head != NULL) { + qinfo->head = queue->head; + qinfo->tail = queue->tail; + queue->head = queue->tail = NULL; + } else { + qinfo->head = qinfo->tail = NULL; + } + PTHREAD_MUTEX_UNLOCK(&queue->lock); +} diff --git a/src/fc_queue.h b/src/fc_queue.h index 7d60c8a..08888ca 100644 --- a/src/fc_queue.h +++ b/src/fc_queue.h @@ -10,6 +10,12 @@ #include "common_define.h" #include "fast_mblock.h" +struct fc_queue_info +{ + void *head; + void *tail; +}; + struct fc_queue { void *head; @@ -54,6 +60,20 @@ static inline void fc_queue_push(struct fc_queue *queue, void *data) } } +void fc_queue_push_queue_to_head_ex(struct fc_queue *queue, + struct fc_queue_info *qinfo, bool *notify); + +static inline void fc_queue_push_queue_to_head(struct fc_queue *queue, + struct fc_queue_info *qinfo) +{ + bool notify; + + fc_queue_push_queue_to_head_ex(queue, qinfo, ¬ify); + if (notify) { + pthread_cond_signal(&(queue->cond)); + } +} + void *fc_queue_pop_ex(struct fc_queue *queue, const bool blocked); #define fc_queue_pop(queue) fc_queue_pop_ex(queue, true) #define fc_queue_try_pop(queue) fc_queue_pop_ex(queue, false) @@ -62,6 +82,9 @@ void *fc_queue_pop_all_ex(struct fc_queue *queue, const bool blocked); #define fc_queue_pop_all(queue) fc_queue_pop_all_ex(queue, true) #define fc_queue_try_pop_all(queue) fc_queue_pop_all_ex(queue, false) +void fc_queue_pop_to_queue(struct fc_queue *queue, + struct fc_queue_info *qinfo); + #ifdef __cplusplus } #endif diff --git a/src/flat_skiplist.c b/src/flat_skiplist.c index 1a83dc5..6ee479b 100644 --- a/src/flat_skiplist.c +++ b/src/flat_skiplist.c @@ -15,6 +15,7 @@ #include #include #include "logger.h" +#include "fc_memory.h" #include "flat_skiplist.h" int flat_skiplist_init_ex(FlatSkiplist *sl, const int level_count, @@ -43,21 +44,15 @@ int flat_skiplist_init_ex(FlatSkiplist *sl, const int level_count, } bytes = sizeof(FlatSkiplistNode *) * level_count; - sl->tmp_previous = (FlatSkiplistNode **)malloc(bytes); + sl->tmp_previous = (FlatSkiplistNode **)fc_malloc(bytes); if (sl->tmp_previous == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail, errno: %d, error info: %s", - __LINE__, bytes, errno, STRERROR(errno)); - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } bytes = sizeof(struct fast_mblock_man) * level_count; - sl->mblocks = (struct fast_mblock_man *)malloc(bytes); + sl->mblocks = (struct fast_mblock_man *)fc_malloc(bytes); if (sl->mblocks == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail, errno: %d, error info: %s", - __LINE__, bytes, errno, STRERROR(errno)); - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } memset(sl->mblocks, 0, bytes); diff --git a/src/hash.c b/src/hash.c index 2f8d524..070ce4a 100644 --- a/src/hash.c +++ b/src/hash.c @@ -12,6 +12,7 @@ #include #include #include "pthread_func.h" +#include "fc_memory.h" #include "hash.h" static unsigned int prime_array[] = { @@ -59,7 +60,7 @@ static int _hash_alloc_buckets(HashArray *pHash, const unsigned int old_capacity return ENOSPC; } - pHash->buckets = (HashData **)malloc(bytes); + pHash->buckets = (HashData **)fc_malloc(bytes); if (pHash->buckets == NULL) { return ENOMEM; @@ -144,7 +145,7 @@ int hash_set_locks(HashArray *pHash, const int lock_count) } bytes = sizeof(pthread_mutex_t) * lock_count; - pHash->locks = (pthread_mutex_t *)malloc(bytes); + pHash->locks = (pthread_mutex_t *)fc_malloc(bytes); if (pHash->locks == NULL) { return ENOMEM; @@ -461,7 +462,7 @@ int hash_best_op(HashArray *pHash, const int suggest_capacity) } old_capacity = *pHash->capacity; - new_capacity = (unsigned int *)malloc(sizeof(unsigned int)); + new_capacity = (unsigned int *)fc_malloc(sizeof(unsigned int)); if (new_capacity == NULL) { return -ENOMEM; @@ -704,7 +705,7 @@ int hash_insert_ex(HashArray *pHash, const void *key, const int key_len, \ return -ENOSPC; } - pBuff = (char *)malloc(bytes); + pBuff = (char *)fc_malloc(bytes); if (pBuff == NULL) { return -ENOMEM; @@ -861,7 +862,7 @@ int hash_partial_set(HashArray *pHash, const void *key, const int key_len, break; } - pNewBuff = (char *)malloc(offset + value_len); + pNewBuff = (char *)fc_malloc(offset + value_len); if (pNewBuff == NULL) { result = errno != 0 ? errno : ENOMEM; diff --git a/src/http_func.c b/src/http_func.c index ae42527..9d29784 100644 --- a/src/http_func.c +++ b/src/http_func.c @@ -21,6 +21,7 @@ #include "sockopt.h" #include "logger.h" #include "shared_func.h" +#include "fc_memory.h" #include "http_func.h" int get_url_content_ex(const char *url, const int url_len, @@ -156,18 +157,11 @@ int get_url_content_ex(const char *url, const int url_len, if (bNeedAlloc) { - *content = (char *)malloc(alloc_size + 1); + *content = (char *)fc_malloc(alloc_size + 1); if (*content == NULL) { close(sock); - result = errno != 0 ? errno : ENOMEM; - - sprintf(error_info, "file: "__FILE__", line: %d, " \ - "malloc %d bytes fail, errno: %d, " \ - "error info: %s", __LINE__, alloc_size + 1, \ - result, STRERROR(result)); - - return result; + return ENOMEM; } } @@ -179,20 +173,12 @@ int get_url_content_ex(const char *url, const int url_len, if (bNeedAlloc) { alloc_size *= 2; - *content = (char *)realloc(*content, alloc_size + 1); + *content = (char *)fc_realloc(*content, alloc_size + 1); if (*content == NULL) { *content_len = 0; close(sock); - result = errno != 0 ? errno : ENOMEM; - - sprintf(error_info, "file: "__FILE__", line: %d, " \ - "realloc %d bytes fail, errno: %d, " \ - "error info: %s", __LINE__, \ - alloc_size + 1, \ - result, STRERROR(result)); - - return result; + return ENOMEM; } recv_bytes = alloc_size - *content_len; diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index 86fb6f4..6793149 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -20,6 +20,7 @@ #include "http_func.h" #include "local_ip_func.h" #include "pthread_func.h" +#include "fc_memory.h" #include "ini_file_reader.h" #define _LINE_BUFFER_SIZE 512 @@ -245,11 +246,8 @@ static int iniAnnotationFuncShellExec(IniContext *context, char *output; count = 0; - output = (char *)malloc(FAST_INI_ITEM_VALUE_SIZE); + output = (char *)fc_malloc(FAST_INI_ITEM_VALUE_SIZE); if (output == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", - __LINE__, FAST_INI_ITEM_VALUE_LEN + 1); return count; } @@ -310,11 +308,8 @@ static char *doReplaceVars(IniContext *pContext, const char *param, int name_len; int len; - output = (char *)malloc(max_size); + output = (char *)fc_malloc(max_size); if (output == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", - __LINE__, FAST_INI_ITEM_VALUE_SIZE); return NULL; } @@ -493,12 +488,9 @@ int iniSetAnnotationCallBack(AnnotationEntry *annotations, int count) } bytes = sizeof(AnnotationEntry) * (g_annotation_count + count + 1); - g_annotations = (AnnotationEntry *)realloc(g_annotations, bytes); + g_annotations = (AnnotationEntry *)fc_realloc(g_annotations, bytes); if (g_annotations == NULL) { - logError("file: "__FILE__", line: %d, " - "realloc %d fail, errno: %d, error info: %s", - __LINE__, bytes, errno, STRERROR(errno)); return ENOMEM; } @@ -999,13 +991,10 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) strncasecmp(pLine+1, "include", 7) == 0 && \ (*(pLine+8) == ' ' || *(pLine+8) == '\t')) { - pIncludeFilename = strdup(pLine + 9); + pIncludeFilename = fc_strdup(pLine + 9); if (pIncludeFilename == NULL) { - logError("file: "__FILE__", line: %d, " \ - "strdup %d bytes fail", __LINE__, \ - (int)strlen(pLine + 9) + 1); - result = errno != 0 ? errno : ENOMEM; + result = ENOMEM; break; } @@ -1127,17 +1116,10 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) section_name, section_len); if (pSection == NULL) { - pSection = (IniSection *)malloc(sizeof(IniSection)); + pSection = (IniSection *)fc_malloc(sizeof(IniSection)); if (pSection == NULL) { - result = errno != 0 ? errno : ENOMEM; - logError("file: "__FILE__", line: %d, "\ - "malloc %d bytes fail, " \ - "errno: %d, error info: %s", \ - __LINE__, \ - (int)sizeof(IniSection), \ - result, STRERROR(result)); - + result = ENOMEM; break; } @@ -1361,11 +1343,9 @@ static int checkAllocDynamicContentArray() alloc = (g_dynamic_content_array.alloc == 0) ? _INIT_DYNAMIC_CONTENTS : g_dynamic_content_array.alloc * 2; bytes = sizeof(CDCPair) * alloc; - contents = (CDCPair *)malloc(bytes); + contents = (CDCPair *)fc_malloc(bytes); if (contents == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, bytes); return ENOMEM; } memset(contents, 0, bytes); @@ -1525,12 +1505,9 @@ static SetDirectiveVars *iniAllocVars(IniContext *pContext, const bool initVars) if (initVars && set->vars == NULL) { - set->vars = (HashArray *)malloc(sizeof(HashArray)); + set->vars = (HashArray *)fc_malloc(sizeof(HashArray)); if (set->vars == NULL) { - logWarning("file: "__FILE__", line: %d, " - "malloc %d bytes fail", - __LINE__, (int)sizeof(HashArray)); return NULL; } if (hash_init_ex(set->vars, simple_hash, 17, 0.75, 0, true) != 0) @@ -1645,11 +1622,9 @@ static char *iniAllocContent(IniContext *pContext, const int content_len) alloc = pDynamicContents->alloc * 2; } bytes = sizeof(char *) * alloc; - contents = (char **)malloc(bytes); + contents = (char **)fc_malloc(bytes); if (contents == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, bytes); return NULL; } memset(contents, 0, bytes); @@ -1663,11 +1638,9 @@ static char *iniAllocContent(IniContext *pContext, const int content_len) pDynamicContents->alloc = alloc; } - buff = malloc(content_len); + buff = fc_malloc(content_len); if (buff == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, content_len); return NULL; } pDynamicContents->contents[pDynamicContents->count++] = buff; @@ -1700,11 +1673,9 @@ static int iniCheckAllocAnnotations(DynamicAnnotations *pDynamicAnnotations, alloc *= 2; } bytes = sizeof(AnnotationEntry) * alloc; - annotations = (AnnotationEntry *)malloc(bytes); + annotations = (AnnotationEntry *)fc_malloc(bytes); if (annotations == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, bytes); return ENOMEM; } memset(annotations, 0, bytes); @@ -2149,10 +2120,8 @@ static int iniDoProccessSet(char *pSet, char **ppSetEnd, if (new_value != value) { free(new_value); } - new_value = strdup(output); + new_value = fc_strdup(output); if (new_value == NULL) { - logWarning("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, value_len + 1); new_value = value; value_len = 0; } @@ -2718,7 +2687,6 @@ static int iniLoadItemsFromBuffer(char *content, IniContext *pContext) static int remallocSection(IniSection *pSection, IniItem **pItem) { int bytes; - int result; int alloc; IniItem *pNew; @@ -2731,13 +2699,10 @@ static int remallocSection(IniSection *pSection, IniItem **pItem) alloc = pSection->alloc * 2; } bytes = sizeof(IniItem) * alloc; - pNew = (IniItem *)malloc(bytes); + pNew = (IniItem *)fc_malloc(bytes); if (pNew == NULL) { - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes fail", __LINE__, bytes); - result = errno != 0 ? errno : ENOMEM; - return result; + return ENOMEM; } if (pSection->count > 0) diff --git a/src/ioevent.c b/src/ioevent.c index 43551df..3d24f6b 100644 --- a/src/ioevent.c +++ b/src/ioevent.c @@ -3,6 +3,7 @@ #include #include #include +#include "fc_memory.h" #include "ioevent.h" #if IOEVENT_USE_KQUEUE @@ -45,21 +46,21 @@ int ioevent_init(IOEventPoller *ioevent, const int size, return errno != 0 ? errno : ENOMEM; } bytes = sizeof(struct epoll_event) * size; - ioevent->events = (struct epoll_event *)malloc(bytes); + ioevent->events = (struct epoll_event *)fc_malloc(bytes); #elif IOEVENT_USE_KQUEUE ioevent->poll_fd = kqueue(); if (ioevent->poll_fd < 0) { return errno != 0 ? errno : ENOMEM; } bytes = sizeof(struct kevent) * size; - ioevent->events = (struct kevent *)malloc(bytes); + ioevent->events = (struct kevent *)fc_malloc(bytes); #elif IOEVENT_USE_PORT ioevent->poll_fd = port_create(); if (ioevent->poll_fd < 0) { return errno != 0 ? errno : ENOMEM; } bytes = sizeof(port_event_t) * size; - ioevent->events = (port_event_t *)malloc(bytes); + ioevent->events = (port_event_t *)fc_malloc(bytes); #endif if (ioevent->events == NULL) { diff --git a/src/json_parser.c b/src/json_parser.c index 54f7f92..694ea99 100644 --- a/src/json_parser.c +++ b/src/json_parser.c @@ -1,6 +1,7 @@ #include #include #include "shared_func.h" +#include "fc_memory.h" #include "json_parser.h" #define EXPECT_STR_LEN 80 @@ -63,7 +64,7 @@ static int json_escape_string(const string_t *input, string_t *output, int size; size = 2 * input->len + 1; - output->str = (char *)malloc(size); + output->str = (char *)fc_malloc(size); if (output->str == NULL) { snprintf(error_info, error_size, "malloc %d bytes fail", size); return ENOMEM; @@ -201,9 +202,9 @@ static int check_alloc_array(common_array_t *array, } bytes = array->element_size * array->alloc; - array->elements = realloc(array->elements, bytes); + array->elements = fc_realloc(array->elements, bytes); if (array->elements == NULL) { - snprintf(error_info, error_size, "malloc %d bytes fail", bytes); + snprintf(error_info, error_size, "realloc %d bytes fail", bytes); return ENOMEM; } @@ -249,7 +250,7 @@ static int prepare_json_parse(const string_t *input, common_array_t *array, } buff_len = input->len - 2; - array->buff = (char *)malloc(buff_len + 1); + array->buff = (char *)fc_malloc(buff_len + 1); if (array->buff == NULL) { snprintf(error_info, error_size, "malloc %d bytes fail", buff_len + 1); @@ -384,7 +385,7 @@ int encode_json_array(json_array_t *array, string_t *output, size += 2 * el->len + 3; } - output->str = (char *)malloc(size); + output->str = (char *)fc_malloc(size); if (output->str == NULL) { snprintf(error_info, error_size, "malloc %d bytes fail", size); return ENOMEM; @@ -511,7 +512,7 @@ int encode_json_map(json_map_t *map, string_t *output, size += 2 * (pair->key.len + pair->value.len + 2) + 1; } - output->str = (char *)malloc(size); + output->str = (char *)fc_malloc(size); if (output->str == NULL) { snprintf(error_info, error_size, "malloc %d bytes fail", size); return ENOMEM; diff --git a/src/multi_skiplist.c b/src/multi_skiplist.c index 82aae6c..6749841 100644 --- a/src/multi_skiplist.c +++ b/src/multi_skiplist.c @@ -15,6 +15,7 @@ #include #include #include "logger.h" +#include "fc_memory.h" #include "multi_skiplist.h" int multi_skiplist_init_ex(MultiSkiplist *sl, const int level_count, @@ -44,21 +45,15 @@ int multi_skiplist_init_ex(MultiSkiplist *sl, const int level_count, } bytes = sizeof(MultiSkiplistNode *) * level_count; - sl->tmp_previous = (MultiSkiplistNode **)malloc(bytes); + sl->tmp_previous = (MultiSkiplistNode **)fc_malloc(bytes); if (sl->tmp_previous == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail, errno: %d, error info: %s", - __LINE__, bytes, errno, STRERROR(errno)); - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } bytes = sizeof(struct fast_mblock_man) * level_count; - sl->mblocks = (struct fast_mblock_man *)malloc(bytes); + sl->mblocks = (struct fast_mblock_man *)fc_malloc(bytes); if (sl->mblocks == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail, errno: %d, error info: %s", - __LINE__, bytes, errno, STRERROR(errno)); - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } memset(sl->mblocks, 0, bytes); diff --git a/src/pthread_func.c b/src/pthread_func.c index 8bc71bd..224495b 100644 --- a/src/pthread_func.c +++ b/src/pthread_func.c @@ -21,8 +21,9 @@ #include #include #include -#include "pthread_func.h" +#include "fc_memory.h" #include "logger.h" +#include "pthread_func.h" int init_pthread_lock(pthread_mutex_t *pthread_lock) { @@ -147,10 +148,8 @@ int create_work_threads(int *count, void *(*start_func)(void *), } else { int bytes; bytes = sizeof(pthread_t) * *count; - the_tids = (pthread_t *)malloc(bytes); + the_tids = (pthread_t *)fc_malloc(bytes); if (the_tids == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, bytes); pthread_attr_destroy(&thread_attr); return ENOMEM; } @@ -200,10 +199,8 @@ int create_work_threads_ex(int *count, void *(*start_func)(void *), } else { int bytes; bytes = sizeof(void *) * (*count); - pp_args = (void **)malloc(bytes); + pp_args = (void **)fc_malloc(bytes); if (pp_args == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, bytes); return ENOMEM; } } diff --git a/src/sched_thread.c b/src/sched_thread.c index e69bc0e..4b659b4 100644 --- a/src/sched_thread.c +++ b/src/sched_thread.c @@ -13,6 +13,7 @@ #include "shared_func.h" #include "pthread_func.h" #include "logger.h" +#include "fc_memory.h" #include "sched_thread.h" volatile bool g_schedule_flag = false; @@ -232,7 +233,6 @@ static int do_check_waiting(ScheduleContext *pContext) ScheduleEntry *pSchedEnd; int allocCount; int newCount; - int result; int deleteCount; pScheduleArray = &(pContext->scheduleArray); @@ -283,21 +283,14 @@ static int do_check_waiting(ScheduleContext *pContext) } allocCount = pScheduleArray->count + waiting_schedule_array.count; - newEntries = (ScheduleEntry *)malloc(sizeof(ScheduleEntry) * allocCount); + newEntries = (ScheduleEntry *)fc_malloc(sizeof(ScheduleEntry) * allocCount); if (newEntries == NULL) { - result = errno != 0 ? errno : ENOMEM; - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes failed, " \ - "errno: %d, error info: %s", \ - __LINE__, (int)sizeof(ScheduleEntry) * allocCount, \ - result, STRERROR(result)); - if (deleteCount > 0) { sched_make_chain(pContext); } - return result; + return ENOMEM; } if (pScheduleArray->count > 0) @@ -546,7 +539,6 @@ static void *sched_thread_entrance(void *args) static int sched_dup_array(const ScheduleArray *pSrcArray, \ ScheduleArray *pDestArray) { - int result; int bytes; if (pSrcArray->count == 0) @@ -557,15 +549,10 @@ static int sched_dup_array(const ScheduleArray *pSrcArray, \ } bytes = sizeof(ScheduleEntry) * pSrcArray->count; - pDestArray->entries = (ScheduleEntry *)malloc(bytes); + pDestArray->entries = (ScheduleEntry *)fc_malloc(bytes); if (pDestArray->entries == NULL) { - result = errno != 0 ? errno : ENOMEM; - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes failed, " \ - "errno: %d, error info: %s", \ - __LINE__, bytes, result, STRERROR(result)); - return result; + return ENOMEM; } memcpy(pDestArray->entries, pSrcArray->entries, bytes); @@ -587,15 +574,10 @@ static int sched_append_array(const ScheduleArray *pSrcArray, \ } bytes = sizeof(ScheduleEntry) * (pDestArray->count + pSrcArray->count); - new_entries = (ScheduleEntry *)malloc(bytes); + new_entries = (ScheduleEntry *)fc_malloc(bytes); if (new_entries == NULL) { - result = errno != 0 ? errno : ENOMEM; - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes failed, " \ - "errno: %d, error info: %s", \ - __LINE__, bytes, result, STRERROR(result)); - return result; + return ENOMEM; } if (pDestArray->entries != NULL) @@ -681,16 +663,10 @@ int sched_start_ex(ScheduleArray *pScheduleArray, pthread_t *ptid, pthread_attr_t thread_attr; ScheduleContext *pContext; - pContext = (ScheduleContext *)malloc(sizeof(ScheduleContext)); + pContext = (ScheduleContext *)fc_malloc(sizeof(ScheduleContext)); if (pContext == NULL) { - result = errno != 0 ? errno : ENOMEM; - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes failed, " \ - "errno: %d, error info: %s", \ - __LINE__, (int)sizeof(ScheduleContext), \ - result, STRERROR(result)); - return result; + return ENOMEM; } memset(pContext, 0, sizeof(ScheduleContext)); diff --git a/src/server_id_func.c b/src/server_id_func.c index afc7416..aea0562 100644 --- a/src/server_id_func.c +++ b/src/server_id_func.c @@ -119,10 +119,8 @@ static int fc_server_check_alloc_group_addresses(FCAddressPtrArray *array) new_alloc = array->alloc > 0 ? 2 * array->alloc : 2; bytes = sizeof(FCAddressInfo *) * new_alloc; - new_addrs = (FCAddressInfo **)malloc(bytes); + new_addrs = (FCAddressInfo **)fc_malloc(bytes); if (new_addrs == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, bytes); return ENOMEM; } memset(new_addrs, 0, bytes); @@ -154,11 +152,8 @@ static FCAddressInfo *fc_server_add_to_uniq_addresses( if (fc_server_check_alloc_group_addresses(addr_ptr_array) != 0) { return NULL; } - p = (FCAddressInfo *)malloc(sizeof(FCAddressInfo)); + p = (FCAddressInfo *)fc_malloc(sizeof(FCAddressInfo)); if (p == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, - (int)sizeof(FCAddressInfo)); return NULL; } @@ -169,7 +164,6 @@ static FCAddressInfo *fc_server_add_to_uniq_addresses( static int fc_server_init_ip_port_array(FCServerConfig *ctx) { - int result; int count; int bytes; FCServerMapArray *map_array; @@ -183,14 +177,9 @@ static int fc_server_init_ip_port_array(FCServerConfig *ctx) count = fc_server_calc_ip_port_count(ctx); bytes = sizeof(FCServerMap) * count; - map_array->maps = (FCServerMap *)malloc(bytes); + map_array->maps = (FCServerMap *)fc_malloc(bytes); if (map_array->maps == NULL) { - result = errno != 0 ? errno : ENOMEM; - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail, " - "errno: %d, error info: %s", __LINE__, - bytes, result, STRERROR(result)); - return result; + return ENOMEM; } memset(map_array->maps, 0, bytes); @@ -471,10 +460,8 @@ static int fc_server_alloc_servers(FCServerInfoArray *array, int bytes; bytes = sizeof(FCServerInfo) * target_count; - array->servers = (FCServerInfo *)malloc(bytes); + array->servers = (FCServerInfo *)fc_malloc(bytes); if (array->servers == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, bytes); return ENOMEM; } memset(array->servers, 0, bytes); @@ -495,10 +482,8 @@ static int fc_server_check_alloc_group_address_ptrs(FCAddressPtrArray *array) new_alloc = array->alloc > 0 ? 2 * array->alloc : 1; bytes = sizeof(FCAddressInfo *) * new_alloc; - new_addrs = (FCAddressInfo **)malloc(bytes); + new_addrs = (FCAddressInfo **)fc_malloc(bytes); if (new_addrs == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, bytes); return ENOMEM; } memset(new_addrs, 0, bytes); @@ -1104,10 +1089,8 @@ static int fc_server_load_servers(FCServerConfig *ctx, sections = fixed; } else { alloc_bytes = sizeof(IniSectionInfo) * count; - sections = (IniSectionInfo *)malloc(alloc_bytes); + sections = (IniSectionInfo *)fc_malloc(alloc_bytes); if (sections == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, alloc_bytes); return ENOMEM; } } diff --git a/src/shared_func.c b/src/shared_func.c index a478079..84e0adb 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -26,9 +26,10 @@ #include #include -#include "shared_func.h" #include "logger.h" #include "sockopt.h" +#include "fc_memory.h" +#include "shared_func.h" #ifdef OS_LINUX #include @@ -295,12 +296,9 @@ int getUserProcIds(const char *progName, const bool bAllOwners, \ return -1; } - pTargetProg = (char *)malloc(strlen(progName) + 1); + pTargetProg = (char *)fc_malloc(strlen(progName) + 1); if (pTargetProg == NULL) { - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes fail", __LINE__, \ - (int)strlen(progName) + 1); return -1; } @@ -704,11 +702,8 @@ int fc_get_file_line_count_ex(const char *filename, char *buff; *line_count = 0; - buff = (char *)malloc(READ_BUFFER_SIZE); + buff = (char *)fc_malloc(READ_BUFFER_SIZE); if (buff == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", - __LINE__, READ_BUFFER_SIZE); return ENOMEM; } @@ -779,12 +774,9 @@ char **split(char *src, const char seperator, const int nMaxCols, int *nColCount *nColCount = nMaxCols; } - pCurrent = pCols = (char **)malloc(sizeof(char *) * (*nColCount)); + pCurrent = pCols = (char **)fc_malloc(sizeof(char *) * (*nColCount)); if (pCols == NULL) { - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes fail", __LINE__, \ - (int)sizeof(char *) * (*nColCount)); return NULL; } @@ -1202,16 +1194,13 @@ int getFileContent(const char *filename, char **buff, int64_t *file_size) return errno != 0 ? errno : EIO; } - *buff = (char *)malloc(*file_size + 1); + *buff = (char *)fc_malloc(*file_size + 1); if (*buff == NULL) { *file_size = 0; close(fd); - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes fail", __LINE__, \ - (int)(*file_size + 1)); - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } if (lseek(fd, 0, SEEK_SET) < 0) @@ -1765,15 +1754,10 @@ static int check_realloc_allow_ips(in_addr_t **allow_ip_addrs, { *alloc_count = target_ip_count; bytes = sizeof(in_addr_t) * (*alloc_count); - *allow_ip_addrs = (in_addr_t *)realloc(*allow_ip_addrs, bytes); + *allow_ip_addrs = (in_addr_t *)fc_realloc(*allow_ip_addrs, bytes); if (*allow_ip_addrs == NULL) { - logError("file: "__FILE__", line: %d, "\ - "malloc %d bytes fail, " \ - "errno: %d, error info: %s", \ - __LINE__, bytes, errno, STRERROR(errno)); - - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } } @@ -2062,14 +2046,10 @@ int load_allow_hosts(IniContext *pIniContext, \ alloc_count = count; *allow_ip_count = 0; - *allow_ip_addrs = (in_addr_t *)malloc(sizeof(in_addr_t) * alloc_count); + *allow_ip_addrs = (in_addr_t *)fc_malloc(sizeof(in_addr_t) * alloc_count); if (*allow_ip_addrs == NULL) { - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes fail, errno: %d, error info: %s.", \ - __LINE__, (int)sizeof(in_addr_t) * alloc_count, \ - errno, STRERROR(errno)); - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } for (pItem=pItemStart; pItemalloc_size = pBuff->length + 1; - pBuff->buff = (char *)malloc(pBuff->alloc_size); + pBuff->buff = (char *)fc_malloc(pBuff->alloc_size); if (pBuff->buff == NULL) { - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes fail, " \ - "errno: %d, error info: %s", \ - __LINE__, pBuff->alloc_size, \ - errno, STRERROR(errno)); pBuff->alloc_size = 0; - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } } @@ -2425,16 +2400,11 @@ int buffer_memcpy(BufferInfo *pBuff, const char *buff, const int len) } pBuff->alloc_size = pBuff->length; - pBuff->buff = (char *)malloc(pBuff->alloc_size); + pBuff->buff = (char *)fc_malloc(pBuff->alloc_size); if (pBuff->buff == NULL) { - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes fail, " \ - "errno: %d, error info: %s", \ - __LINE__, pBuff->alloc_size, \ - errno, STRERROR(errno)); pBuff->alloc_size = 0; - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } } @@ -2876,15 +2846,12 @@ bool ends_with(const char *str, const char *needle) return memcmp(str + start_offset, needle, needle_len) == 0; } -char *fc_strdup(const char *str, const int len) +char *fc_strdup1(const char *str, const int len) { char *output; - output = (char *)malloc(len + 1); + output = (char *)fc_malloc(len + 1); if (output == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", - __LINE__, len + 1); return NULL; } @@ -3081,11 +3048,9 @@ int fc_ceil_prime(const int n) int fc_init_buffer(BufferInfo *buffer, const int buffer_size) { - buffer->buff = (char *)malloc(buffer_size); + buffer->buff = (char *)fc_malloc(buffer_size); if (buffer->buff == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, buffer_size); return ENOMEM; } buffer->alloc_size = buffer_size; diff --git a/src/shared_func.h b/src/shared_func.h index 8c54d12..f66540d 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -855,13 +855,13 @@ bool starts_with(const char *str, const char *needle); */ bool ends_with(const char *str, const char *needle); -/** strdup +/** strdup extension * parameters: - * str: the string to duplicate + * str: the string to duplicate * len: the length of string * return: the duplicated string, NULL for fail */ -char *fc_strdup(const char *str, const int len); +char *fc_strdup1(const char *str, const int len); /** memmem * parameters: diff --git a/src/skiplist_set.c b/src/skiplist_set.c index bf9a823..c678f97 100644 --- a/src/skiplist_set.c +++ b/src/skiplist_set.c @@ -15,6 +15,7 @@ #include #include #include "logger.h" +#include "fc_memory.h" #include "skiplist_set.h" int skiplist_set_init_ex(SkiplistSet *sl, const int level_count, @@ -43,21 +44,15 @@ int skiplist_set_init_ex(SkiplistSet *sl, const int level_count, } bytes = sizeof(SkiplistSetNode *) * level_count; - sl->tmp_previous = (SkiplistSetNode **)malloc(bytes); + sl->tmp_previous = (SkiplistSetNode **)fc_malloc(bytes); if (sl->tmp_previous == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail, errno: %d, error info: %s", - __LINE__, bytes, errno, STRERROR(errno)); - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } bytes = sizeof(struct fast_mblock_man) * level_count; - sl->mblocks = (struct fast_mblock_man *)malloc(bytes); + sl->mblocks = (struct fast_mblock_man *)fc_malloc(bytes); if (sl->mblocks == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail, errno: %d, error info: %s", - __LINE__, bytes, errno, STRERROR(errno)); - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } memset(sl->mblocks, 0, bytes); diff --git a/src/system_info.c b/src/system_info.c index 2e638b2..6269421 100644 --- a/src/system_info.c +++ b/src/system_info.c @@ -22,6 +22,7 @@ #include #include "logger.h" #include "shared_func.h" +#include "fc_memory.h" #include "system_info.h" #ifdef OS_LINUX @@ -327,11 +328,9 @@ static int check_process_capacity(FastProcessArray *proc_array) alloc_size = proc_array->alloc_size > 0 ? proc_array->alloc_size * 2 : 128; bytes = sizeof(struct fast_process_info) * alloc_size; - procs = (struct fast_process_info *)malloc(bytes); + procs = (struct fast_process_info *)fc_malloc(bytes); if (procs == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, bytes); return ENOMEM; } @@ -632,14 +631,10 @@ int get_processes(struct fast_process_info **processes, int *count) } size = sizeof(struct kinfo_proc) * nproc; - procs = (struct kinfo_proc *)malloc(size); + procs = (struct kinfo_proc *)fc_malloc(size); if (procs == NULL) { - logError("file: "__FILE__", line: %d, " \ - "malloc %d bytes fail, " \ - "errno: %d, error info: %s", \ - __LINE__, (int)size, errno, STRERROR(errno)); - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } if (sysctl(mib, 4, procs, &size, NULL, 0) == 0) @@ -668,11 +663,9 @@ int get_processes(struct fast_process_info **processes, int *count) nproc = size / sizeof(struct kinfo_proc); bytes = sizeof(struct fast_process_info) * nproc; - *processes = (struct fast_process_info *)malloc(bytes); + *processes = (struct fast_process_info *)fc_malloc(bytes); if (*processes == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail", __LINE__, bytes); free(procs); return ENOMEM; } diff --git a/src/uniq_skiplist.c b/src/uniq_skiplist.c index 807437c..00c6784 100644 --- a/src/uniq_skiplist.c +++ b/src/uniq_skiplist.c @@ -14,6 +14,7 @@ #include #include #include "logger.h" +#include "fc_memory.h" #include "uniq_skiplist.h" static int best_element_counts[SKIPLIST_MAX_LEVEL_COUNT] = {0}; @@ -75,12 +76,9 @@ int uniq_skiplist_init_ex2(UniqSkiplistFactory *factory, } bytes = sizeof(struct fast_mblock_man) * max_level_count; - factory->node_allocators = (struct fast_mblock_man *)malloc(bytes); + factory->node_allocators = (struct fast_mblock_man *)fc_malloc(bytes); if (factory->node_allocators == NULL) { - logError("file: "__FILE__", line: %d, " - "malloc %d bytes fail, errno: %d, error info: %s", - __LINE__, bytes, errno, STRERROR(errno)); - return errno != 0 ? errno : ENOMEM; + return ENOMEM; } memset(factory->node_allocators, 0, bytes);