fast_mblock add fast_mblock_delay_free
parent
7a2c942863
commit
f296a3e459
3
HISTORY
3
HISTORY
|
|
@ -1,4 +1,7 @@
|
||||||
|
|
||||||
|
Version 1.16 2015-06-19
|
||||||
|
* fast_mblock add fast_mblock_delay_free
|
||||||
|
|
||||||
Version 1.15 2015-06-16
|
Version 1.15 2015-06-16
|
||||||
* fast_mblock.c support none lock
|
* fast_mblock.c support none lock
|
||||||
* ioevent support set timeout
|
* ioevent support set timeout
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
Name: libfastcommon
|
Name: libfastcommon
|
||||||
Version: 1.0.15
|
Version: 1.0.16
|
||||||
Release: 1%{?dist}
|
Release: 1%{?dist}
|
||||||
Summary: c common functions library extracted from my open source projects FastDFS
|
Summary: c common functions library extracted from my open source projects FastDFS
|
||||||
License: GPL
|
License: GPL
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
#include "shared_func.h"
|
#include "shared_func.h"
|
||||||
#include "pthread_func.h"
|
#include "pthread_func.h"
|
||||||
|
#include "sched_thread.h"
|
||||||
|
|
||||||
int fast_mblock_init_ex(struct fast_mblock_man *mblock, const int element_size,
|
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 int alloc_elements_once, fast_mblock_alloc_init_func init_func,
|
||||||
|
|
@ -47,6 +48,8 @@ int fast_mblock_init_ex(struct fast_mblock_man *mblock, const int element_size,
|
||||||
mblock->alloc_init_func = init_func;
|
mblock->alloc_init_func = init_func;
|
||||||
mblock->malloc_chain_head = NULL;
|
mblock->malloc_chain_head = NULL;
|
||||||
mblock->free_chain_head = NULL;
|
mblock->free_chain_head = NULL;
|
||||||
|
mblock->delay_free_chain.head = NULL;
|
||||||
|
mblock->delay_free_chain.tail = NULL;
|
||||||
mblock->total_count = 0;
|
mblock->total_count = 0;
|
||||||
mblock->need_lock = need_lock;
|
mblock->need_lock = need_lock;
|
||||||
|
|
||||||
|
|
@ -165,7 +168,17 @@ struct fast_mblock_node *fast_mblock_alloc(struct fast_mblock_man *mblock)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if ((result=fast_mblock_prealloc(mblock)) == 0)
|
if (mblock->delay_free_chain.head != NULL &&
|
||||||
|
mblock->delay_free_chain.head->recycle_timestamp <= get_current_time())
|
||||||
|
{
|
||||||
|
pNode = mblock->delay_free_chain.head;
|
||||||
|
mblock->delay_free_chain.head = pNode->next;
|
||||||
|
if (mblock->delay_free_chain.tail == pNode)
|
||||||
|
{
|
||||||
|
mblock->delay_free_chain.tail = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((result=fast_mblock_prealloc(mblock)) == 0)
|
||||||
{
|
{
|
||||||
pNode = mblock->free_chain_head;
|
pNode = mblock->free_chain_head;
|
||||||
mblock->free_chain_head = pNode->next;
|
mblock->free_chain_head = pNode->next;
|
||||||
|
|
@ -215,7 +228,45 @@ int fast_mblock_free(struct fast_mblock_man *mblock, \
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fast_mblock_free_count(struct fast_mblock_man *mblock)
|
int fast_mblock_delay_free(struct fast_mblock_man *mblock,
|
||||||
|
struct fast_mblock_node *pNode, const int deley)
|
||||||
|
{
|
||||||
|
int result;
|
||||||
|
|
||||||
|
if (mblock->need_lock && (result=pthread_mutex_lock(&(mblock->lock))) != 0)
|
||||||
|
{
|
||||||
|
logError("file: "__FILE__", line: %d, " \
|
||||||
|
"call pthread_mutex_lock fail, " \
|
||||||
|
"errno: %d, error info: %s", \
|
||||||
|
__LINE__, result, STRERROR(result));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
pNode->recycle_timestamp = get_current_time() + deley;
|
||||||
|
if (mblock->delay_free_chain.head == NULL)
|
||||||
|
{
|
||||||
|
mblock->delay_free_chain.head = pNode;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mblock->delay_free_chain.tail->next = pNode;
|
||||||
|
}
|
||||||
|
mblock->delay_free_chain.tail = pNode;
|
||||||
|
pNode->next = NULL;
|
||||||
|
|
||||||
|
if (mblock->need_lock && (result=pthread_mutex_unlock(&(mblock->lock))) != 0)
|
||||||
|
{
|
||||||
|
logError("file: "__FILE__", line: %d, " \
|
||||||
|
"call pthread_mutex_unlock fail, " \
|
||||||
|
"errno: %d, error info: %s", \
|
||||||
|
__LINE__, result, STRERROR(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int fast_mblock_chain_count(struct fast_mblock_man *mblock,
|
||||||
|
struct fast_mblock_node *head)
|
||||||
{
|
{
|
||||||
struct fast_mblock_node *pNode;
|
struct fast_mblock_node *pNode;
|
||||||
int count;
|
int count;
|
||||||
|
|
@ -231,7 +282,7 @@ int fast_mblock_free_count(struct fast_mblock_man *mblock)
|
||||||
}
|
}
|
||||||
|
|
||||||
count = 0;
|
count = 0;
|
||||||
pNode = mblock->free_chain_head;
|
pNode = head;
|
||||||
while (pNode != NULL)
|
while (pNode != NULL)
|
||||||
{
|
{
|
||||||
pNode = pNode->next;
|
pNode = pNode->next;
|
||||||
|
|
@ -249,3 +300,13 @@ int fast_mblock_free_count(struct fast_mblock_man *mblock)
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int fast_mblock_free_count(struct fast_mblock_man *mblock)
|
||||||
|
{
|
||||||
|
return fast_mblock_chain_count(mblock, mblock->free_chain_head);
|
||||||
|
}
|
||||||
|
|
||||||
|
int fast_mblock_delay_free_count(struct fast_mblock_man *mblock)
|
||||||
|
{
|
||||||
|
return fast_mblock_chain_count(mblock, mblock->delay_free_chain.head);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@
|
||||||
struct fast_mblock_node
|
struct fast_mblock_node
|
||||||
{
|
{
|
||||||
struct fast_mblock_node *next;
|
struct fast_mblock_node *next;
|
||||||
|
int recycle_timestamp;
|
||||||
char data[0]; //the data buffer
|
char data[0]; //the data buffer
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -31,11 +32,17 @@ struct fast_mblock_malloc
|
||||||
struct fast_mblock_malloc *next;
|
struct fast_mblock_malloc *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct fast_mblock_chain {
|
||||||
|
struct fast_mblock_node *head;
|
||||||
|
struct fast_mblock_node *tail;
|
||||||
|
};
|
||||||
|
|
||||||
typedef int (*fast_mblock_alloc_init_func)(void *element);
|
typedef int (*fast_mblock_alloc_init_func)(void *element);
|
||||||
|
|
||||||
struct fast_mblock_man
|
struct fast_mblock_man
|
||||||
{
|
{
|
||||||
struct fast_mblock_node *free_chain_head; //free node chain
|
struct fast_mblock_node *free_chain_head; //free node chain
|
||||||
|
struct fast_mblock_chain delay_free_chain; //delay free node chain
|
||||||
struct fast_mblock_malloc *malloc_chain_head; //malloc chain to be freed
|
struct fast_mblock_malloc *malloc_chain_head; //malloc chain to be freed
|
||||||
fast_mblock_alloc_init_func alloc_init_func;
|
fast_mblock_alloc_init_func alloc_init_func;
|
||||||
int element_size; //element size
|
int element_size; //element size
|
||||||
|
|
@ -93,6 +100,17 @@ return 0 for success, return none zero if fail
|
||||||
int fast_mblock_free(struct fast_mblock_man *mblock,
|
int fast_mblock_free(struct fast_mblock_man *mblock,
|
||||||
struct fast_mblock_node *pNode);
|
struct fast_mblock_node *pNode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
delay free a node (put a node to the mblock)
|
||||||
|
parameters:
|
||||||
|
mblock: the mblock pointer
|
||||||
|
pNode: the node to free
|
||||||
|
delay: delay seconds to free
|
||||||
|
return 0 for success, return none zero if fail
|
||||||
|
*/
|
||||||
|
int fast_mblock_delay_free(struct fast_mblock_man *mblock,
|
||||||
|
struct fast_mblock_node *pNode, const int delay);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
alloc a object from the mblock
|
alloc a object from the mblock
|
||||||
parameters:
|
parameters:
|
||||||
|
|
@ -123,6 +141,20 @@ static inline int fast_mblock_free_object(struct fast_mblock_man *mblock,
|
||||||
return fast_mblock_free(mblock, fast_mblock_to_node_ptr(object));
|
return fast_mblock_free(mblock, fast_mblock_to_node_ptr(object));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
delay free a object (put a node to the mblock)
|
||||||
|
parameters:
|
||||||
|
mblock: the mblock pointer
|
||||||
|
pNode: the node to free
|
||||||
|
delay: delay seconds to free
|
||||||
|
return 0 for success, return none zero if fail
|
||||||
|
*/
|
||||||
|
int fast_mblock_delay_free_object(struct fast_mblock_man *mblock,
|
||||||
|
void *object, const int delay)
|
||||||
|
{
|
||||||
|
return fast_mblock_delay_free(mblock, fast_mblock_to_node_ptr(object), delay);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
get node count of the mblock
|
get node count of the mblock
|
||||||
parameters:
|
parameters:
|
||||||
|
|
@ -131,6 +163,14 @@ return the free node count of the mblock, return -1 if fail
|
||||||
*/
|
*/
|
||||||
int fast_mblock_free_count(struct fast_mblock_man *mblock);
|
int fast_mblock_free_count(struct fast_mblock_man *mblock);
|
||||||
|
|
||||||
|
/**
|
||||||
|
get delay free node count of the mblock
|
||||||
|
parameters:
|
||||||
|
mblock: the mblock pointer
|
||||||
|
return the delay free node count of the mblock, return -1 if fail
|
||||||
|
*/
|
||||||
|
int fast_mblock_delay_free_count(struct fast_mblock_man *mblock);
|
||||||
|
|
||||||
#define fast_mblock_total_count(mblock) (mblock)->total_count
|
#define fast_mblock_total_count(mblock) (mblock)->total_count
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue