From 8005b18198f5f8018da9bec58a85d2eb4ddebec9 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Thu, 27 Feb 2020 22:26:17 +0800 Subject: [PATCH] fast_allocator.[hc]: add function fast_allocator_strdup --- HISTORY | 2 ++ src/fast_allocator.c | 14 ++++++++ src/fast_allocator.h | 23 +++++++++++++ src/fast_mpool.c | 16 ++++----- src/fast_mpool.h | 81 ++++++++++++++++++++++++++++++++++---------- 5 files changed, 110 insertions(+), 26 deletions(-) diff --git a/HISTORY b/HISTORY index e64045a..dc871da 100644 --- a/HISTORY +++ b/HISTORY @@ -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, diff --git a/src/fast_allocator.c b/src/fast_allocator.c index 59b112b..8fb1e65 100644 --- a/src/fast_allocator.c +++ b/src/fast_allocator.c @@ -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; +} diff --git a/src/fast_allocator.h b/src/fast_allocator.h index 37a939f..5338565 100644 --- a/src/fast_allocator.h +++ b/src/fast_allocator.h @@ -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 diff --git a/src/fast_mpool.c b/src/fast_mpool.c index acaf201..759a1d1 100644 --- a/src/fast_mpool.c +++ b/src/fast_mpool.c @@ -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) diff --git a/src/fast_mpool.h b/src/fast_mpool.h index 4276612..e2b7eb9 100644 --- a/src/fast_mpool.h +++ b/src/fast_mpool.h @@ -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); } /**