diff --git a/src/fast_mblock.c b/src/fast_mblock.c index af3e977..3f94717 100644 --- a/src/fast_mblock.c +++ b/src/fast_mblock.c @@ -4,11 +4,11 @@ #include #include #include -#include "fast_mblock.h" #include "logger.h" #include "shared_func.h" #include "pthread_func.h" #include "sched_thread.h" +#include "fast_mblock.h" struct _fast_mblock_manager { @@ -18,7 +18,7 @@ struct _fast_mblock_manager }; #define INIT_HEAD(head) (head)->next = (head)->prev = head -#define IS_EMPTY(head) ((head)->next == (head)->prev) +#define IS_EMPTY(head) ((head)->next == head) static struct _fast_mblock_manager mblock_manager = {false}; @@ -75,9 +75,10 @@ static void add_to_mblock_list(struct fast_mblock_man *mblock) } mblock->next = current; - current->prev = mblock; mblock->prev = current->prev; current->prev->next = mblock; + current->prev = mblock; + pthread_mutex_unlock(&(mblock_manager.lock)); } @@ -96,6 +97,16 @@ static void delete_from_mblock_list(struct fast_mblock_man *mblock) INIT_HEAD(mblock); } +#define STAT_DUP(pStat, current) \ + do { \ + strcpy(pStat->name, current->info.name); \ + pStat->element_size = current->info.element_size; \ + pStat->total_count += current->info.total_count; \ + pStat->used_count += current->info.used_count; \ + /* logInfo("name: %s, element_size: %d, total_count: %d, used_count: %d", */ \ + /* pStat->name, pStat->element_size, pStat->total_count, pStat->used_count); */\ + } while (0) + int fast_mblock_manager_stat(struct fast_mblock_info *stats, const int size, int *count) { @@ -122,10 +133,6 @@ int fast_mblock_manager_stat(struct fast_mblock_info *stats, current = mblock_manager.head.next; while (current != &mblock_manager.head) { - strcpy(pStat->name, current->info.name); - pStat->element_size = current->info.element_size; - pStat->total_count += current->info.total_count; - pStat->used_count += current->info.used_count; if (current->prev != &mblock_manager.head) { if (cmp_mblock_info(current, current->prev) != 0) @@ -135,6 +142,7 @@ int fast_mblock_manager_stat(struct fast_mblock_info *stats, result = EOVERFLOW; break; } + STAT_DUP(pStat, current->prev); pStat++; } } @@ -149,6 +157,7 @@ int fast_mblock_manager_stat(struct fast_mblock_info *stats, } else { + STAT_DUP(pStat, current->prev); pStat++; } } @@ -185,14 +194,16 @@ int fast_mblock_manager_stat_print() if (result == 0) { - logInfo("mblock manager stat count: %d", count); - logInfo("name element_size alloc_count used_count used_ratio"); + logInfo("mblock stat count: %d", count); + logInfo("%32s %12s %12s %12s %12s", "name", "element_size", + "alloc_count", "used_count", "used_ratio"); stat_end = stats + count; for (pStat=stats; pStatname, pStat->total_count, - pStat->used_count, (double)pStat->used_count / - (double)pStat->total_count); + logInfo("%32s %12d %12d %12d %12.4f", pStat->name, pStat->element_size, + pStat->total_count, pStat->used_count, + pStat->total_count > 0 ? (double)pStat->used_count / + (double)pStat->total_count : 0.00); } } @@ -200,6 +211,14 @@ int fast_mblock_manager_stat_print() return 0; } +int fast_mblock_init_ex(struct fast_mblock_man *mblock, + const int element_size, const int alloc_elements_once, + fast_mblock_alloc_init_func init_func, const bool need_lock) +{ + return fast_mblock_init_ex2(mblock, NULL, element_size, + alloc_elements_once, init_func, need_lock); +} + int fast_mblock_init_ex2(struct fast_mblock_man *mblock, const char *name, const int element_size, const int alloc_elements_once, fast_mblock_alloc_init_func init_func, const bool need_lock) diff --git a/src/fast_mblock.h b/src/fast_mblock.h index e10a069..bd85323 100644 --- a/src/fast_mblock.h +++ b/src/fast_mblock.h @@ -18,7 +18,7 @@ #include "common_define.h" #include "chain.h" -#define FAST_MBLOCK_NAME_SIZE 64 +#define FAST_MBLOCK_NAME_SIZE 32 /* free node chain */ struct fast_mblock_node @@ -72,10 +72,21 @@ extern "C" { #endif #define fast_mblock_init(mblock, element_size, alloc_elements_once) \ - fast_mblock_init_ex2(mblock, NULL, element_size, alloc_elements_once, NULL, true) + fast_mblock_init_ex(mblock, element_size, alloc_elements_once, NULL, true) -#define fast_mblock_init_ex(mblock, element_size, alloc_elements_once, init_func, need_lock) \ - fast_mblock_init_ex2(mblock, NULL, element_size, alloc_elements_once, init_func, need_lock) +/** +mblock init +parameters: + mblock: the mblock pointer + element_size: element size, such as sizeof(struct xxx) + alloc_elements_once: malloc elements once, 0 for malloc 1MB memory once + init_func: the init function + need_lock: if need lock +return error no, 0 for success, != 0 fail +*/ +int fast_mblock_init_ex(struct fast_mblock_man *mblock, + const int element_size, const int alloc_elements_once, + fast_mblock_alloc_init_func init_func, const bool need_lock); /** mblock init