fast_allocator.[hc]: add function fast_allocator_strdup

pull/37/head
YuQing 2020-02-27 22:26:17 +08:00
parent 3e3bcda2df
commit 8005b18198
5 changed files with 110 additions and 26 deletions

View File

@ -11,6 +11,8 @@ Version 1.44 2020-02-27
* add files: server_id_func.[hc]
* common_blocked_queue support pop all nodes
* shared_func.[hc]: add functions fc_floor_prime and fc_ceil_prime
* fast_mpool.[hc]: change function fast_mpool_strdup
* fast_allocator.[hc]: add function fast_allocator_strdup
Version 1.43 2019-12-25
* replace function call system to getExecResult,

View File

@ -495,3 +495,17 @@ void fast_allocator_free(struct fast_allocator_context *acontext, void *ptr)
}
}
char *fast_allocator_strdup_ex(struct fast_allocator_context *acontext,
const char *src, const int len)
{
char *dest;
dest = (char *)fast_allocator_alloc(acontext, len + 1);
if (dest == NULL) {
logError("file: "__FILE__", line: %d, "
"malloc %d bytes fail", __LINE__, len + 1);
return NULL;
}
memcpy(dest, src, len + 1);
return dest;
}

View File

@ -139,6 +139,29 @@ return error no, 0 for success, != 0 fail
int fast_allocator_retry_reclaim(struct fast_allocator_context *acontext,
int64_t *total_reclaim_bytes);
char *fast_allocator_strdup_ex(struct fast_allocator_context *acontext,
const char *src, const int len);
static inline char *fast_allocator_strdup(struct fast_allocator_context *
acontext, const char *src)
{
return fast_allocator_strdup_ex(acontext, src, strlen(src));
}
static inline int fast_allocator_alloc_string_ex(struct fast_allocator_context
*acontext, string_t *dest, const char *src, const int len)
{
dest->str = fast_allocator_strdup_ex(acontext, src, len);
dest->len = len;
return dest->str != NULL ? 0 : ENOMEM;
}
static inline int fast_allocator_alloc_string(struct fast_allocator_context
*acontext, string_t *dest, const string_t *src)
{
return fast_allocator_alloc_string_ex(acontext, dest, src->str, src->len);
}
#ifdef __cplusplus
}
#endif

View File

@ -165,22 +165,22 @@ void *fast_mpool_alloc(struct fast_mpool_man *mpool, const int size)
return NULL;
}
int fast_mpool_strdup_ex(struct fast_mpool_man *mpool, string_t *dest,
const char *src, const int len)
void *fast_mpool_memdup(struct fast_mpool_man *mpool,
const void *src, const int len)
{
dest->str = (char *)fast_mpool_alloc(mpool, len);
if (dest->str == NULL)
void *dest;
dest = (char *)fast_mpool_alloc(mpool, len);
if (dest == NULL)
{
logError("file: "__FILE__", line: %d, "
"alloc %d bytes from mpool fail", __LINE__, len);
return ENOMEM;
return NULL;
}
if (len > 0) {
memcpy(dest->str, src, len);
memcpy(dest, src, len);
}
dest->len = len;
return 0;
return dest;
}
void fast_mpool_reset(struct fast_mpool_man *mpool)

View File

@ -83,6 +83,46 @@ return the alloced ptr, return NULL if fail
void *fast_mpool_alloc(struct fast_mpool_man *mpool, const int size);
/**
alloc and copy memory from the mpool
parameters:
mpool: the mpool pointer
src: the source memory pointer
len: the length of the source memory
return alloc and duplicate memory pointer, NULL for fail
*/
void *fast_mpool_memdup(struct fast_mpool_man *mpool,
const void *src, const int len);
/**
alloc and copy string from the mpool
parameters:
mpool: the mpool pointer
src: the source '\0' terminated string
len: the length of the source string
return alloc and duplicate string pointer, NULL for fail
*/
static inline char *fast_mpool_strdup_ex(struct fast_mpool_man *mpool,
const char *src, const int len)
{
return (char *)fast_mpool_memdup(mpool, src, len + 1);
}
/**
alloc and copy string from the mpool
parameters:
mpool: the mpool pointer
src: the source '\0' terminated string
len: the length of the source string
return alloc and duplicate string pointer, NULL for fail
*/
static inline char *fast_mpool_strdup(struct fast_mpool_man *mpool,
const char *src)
{
return (char *)fast_mpool_memdup(mpool, src, strlen(src) + 1);
}
/**
alloc and copy string from the mpool
parameters:
@ -92,23 +132,12 @@ parameters:
len: the length of the source string
return error no, 0 for success, != 0 fail
*/
int fast_mpool_strdup_ex(struct fast_mpool_man *mpool, string_t *dest,
const char *src, const int len);
/**
alloc and copy string from the mpool
parameters:
mpool: the mpool pointer
dest: the dest string (return the alloced memory in dest->str)
src: the source string
return error no, 0 for success, != 0 fail
*/
static inline int fast_mpool_strdup(struct fast_mpool_man *mpool,
string_t *dest, const char *src)
static inline int fast_mpool_alloc_string_ex(struct fast_mpool_man *mpool,
string_t *dest, const char *src, const int len)
{
int len;
len = (src != NULL) ? strlen(src) : 0;
return fast_mpool_strdup_ex(mpool, dest, src, len);
dest->str = (char *)fast_mpool_memdup(mpool, src, len);
dest->len = len;
return dest->str != NULL ? 0 : ENOMEM;
}
/**
@ -119,10 +148,26 @@ parameters:
src: the source string
return error no, 0 for success, != 0 fail
*/
static inline int fast_mpool_strdup2(struct fast_mpool_man *mpool,
static inline int fast_mpool_alloc_string(struct fast_mpool_man *mpool,
string_t *dest, const char *src)
{
int len;
len = (src != NULL) ? strlen(src) : 0;
return fast_mpool_alloc_string_ex(mpool, dest, src, len);
}
/**
alloc and copy string from the mpool
parameters:
mpool: the mpool pointer
dest: the dest string (return the alloced memory in dest->str)
src: the source string
return error no, 0 for success, != 0 fail
*/
static inline int fast_mpool_alloc_string_ex2(struct fast_mpool_man *mpool,
string_t *dest, const string_t *src)
{
return fast_mpool_strdup_ex(mpool, dest, src->str, src->len);
return fast_mpool_alloc_string_ex(mpool, dest, src->str, src->len);
}
/**