fast_allocator.[hc]: add function fast_allocator_strdup
parent
3e3bcda2df
commit
8005b18198
2
HISTORY
2
HISTORY
|
|
@ -11,6 +11,8 @@ Version 1.44 2020-02-27
|
||||||
* add files: server_id_func.[hc]
|
* add files: server_id_func.[hc]
|
||||||
* common_blocked_queue support pop all nodes
|
* common_blocked_queue support pop all nodes
|
||||||
* shared_func.[hc]: add functions fc_floor_prime and fc_ceil_prime
|
* 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
|
Version 1.43 2019-12-25
|
||||||
* replace function call system to getExecResult,
|
* replace function call system to getExecResult,
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -139,6 +139,29 @@ return error no, 0 for success, != 0 fail
|
||||||
int fast_allocator_retry_reclaim(struct fast_allocator_context *acontext,
|
int fast_allocator_retry_reclaim(struct fast_allocator_context *acontext,
|
||||||
int64_t *total_reclaim_bytes);
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -165,22 +165,22 @@ void *fast_mpool_alloc(struct fast_mpool_man *mpool, const int size)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fast_mpool_strdup_ex(struct fast_mpool_man *mpool, string_t *dest,
|
void *fast_mpool_memdup(struct fast_mpool_man *mpool,
|
||||||
const char *src, const int len)
|
const void *src, const int len)
|
||||||
{
|
{
|
||||||
dest->str = (char *)fast_mpool_alloc(mpool, len);
|
void *dest;
|
||||||
if (dest->str == NULL)
|
dest = (char *)fast_mpool_alloc(mpool, len);
|
||||||
|
if (dest == NULL)
|
||||||
{
|
{
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"alloc %d bytes from mpool fail", __LINE__, len);
|
"alloc %d bytes from mpool fail", __LINE__, len);
|
||||||
return ENOMEM;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
memcpy(dest->str, src, len);
|
memcpy(dest, src, len);
|
||||||
}
|
}
|
||||||
dest->len = len;
|
return dest;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void fast_mpool_reset(struct fast_mpool_man *mpool)
|
void fast_mpool_reset(struct fast_mpool_man *mpool)
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,46 @@ return the alloced ptr, return NULL if fail
|
||||||
void *fast_mpool_alloc(struct fast_mpool_man *mpool, const int size);
|
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
|
alloc and copy string from the mpool
|
||||||
parameters:
|
parameters:
|
||||||
|
|
@ -92,23 +132,12 @@ parameters:
|
||||||
len: the length of the source string
|
len: the length of the source string
|
||||||
return error no, 0 for success, != 0 fail
|
return error no, 0 for success, != 0 fail
|
||||||
*/
|
*/
|
||||||
int fast_mpool_strdup_ex(struct fast_mpool_man *mpool, string_t *dest,
|
static inline int fast_mpool_alloc_string_ex(struct fast_mpool_man *mpool,
|
||||||
const char *src, const int len);
|
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)
|
|
||||||
{
|
{
|
||||||
int len;
|
dest->str = (char *)fast_mpool_memdup(mpool, src, len);
|
||||||
len = (src != NULL) ? strlen(src) : 0;
|
dest->len = len;
|
||||||
return fast_mpool_strdup_ex(mpool, dest, src, len);
|
return dest->str != NULL ? 0 : ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -119,10 +148,26 @@ parameters:
|
||||||
src: the source string
|
src: the source string
|
||||||
return error no, 0 for success, != 0 fail
|
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)
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue