From 3c9054f2150fc06095cf1c29b086673fbcce6499 Mon Sep 17 00:00:00 2001 From: yuqing Date: Fri, 14 Sep 2018 11:58:49 +0800 Subject: [PATCH] add fast_buffer_append_string2 --- src/fast_buffer.h | 6 ++++++ src/fast_mpool.c | 15 +++++++-------- src/fast_mpool.h | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/fast_buffer.h b/src/fast_buffer.h index 7f7ee4c..38f462a 100644 --- a/src/fast_buffer.h +++ b/src/fast_buffer.h @@ -2,6 +2,7 @@ #define __FAST_BUFFER_H__ #include +#include "common_define.h" typedef struct fast_buffer { char *data; @@ -55,6 +56,11 @@ static inline int fast_buffer_append_string(FastBuffer *buffer, const char *str) return fast_buffer_append_buff(buffer, str, strlen(str)); } +static inline int fast_buffer_append_string2(FastBuffer *buffer, const string_t *add) +{ + return fast_buffer_append_buff(buffer, add->str, add->len); +} + static inline int fast_buffer_append_buffer(FastBuffer *buffer, FastBuffer *src) { return fast_buffer_append_buff(buffer, src->data, src->length); diff --git a/src/fast_mpool.c b/src/fast_mpool.c index df78432..46b3302 100644 --- a/src/fast_mpool.c +++ b/src/fast_mpool.c @@ -165,22 +165,21 @@ void *fast_mpool_alloc(struct fast_mpool_man *mpool, const int size) return NULL; } - -int fast_mpool_strdup(struct fast_mpool_man *mpool, string_t *dest, - const string_t *src) +int fast_mpool_strdup_ex(struct fast_mpool_man *mpool, string_t *dest, + const char *src, const int len) { - dest->str = (char *)fast_mpool_alloc(mpool, src->len); + dest->str = (char *)fast_mpool_alloc(mpool, len); if (dest->str == NULL) { logError("file: "__FILE__", line: %d, " - "alloc %d bytes from mpool fail", __LINE__, src->len); + "alloc %d bytes from mpool fail", __LINE__, len); return ENOMEM; } - if (src->len > 0) { - memcpy(dest->str, src->str, src->len); + if (len > 0) { + memcpy(dest->str, src, len); } - dest->len = src->len; + dest->len = len; return 0; } diff --git a/src/fast_mpool.h b/src/fast_mpool.h index b58b899..216a82f 100644 --- a/src/fast_mpool.h +++ b/src/fast_mpool.h @@ -89,10 +89,41 @@ parameters: mpool: the mpool pointer dest: the dest string (return the alloced memory in dest->str) src: the source string + len: the length of the source string return error no, 0 for success, != 0 fail */ -int fast_mpool_strdup(struct fast_mpool_man *mpool, string_t *dest, - const string_t *src); +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) +{ + int len; + len = (src != NULL) ? strlen(src) : 0; + return fast_mpool_strdup_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_strdup2(struct fast_mpool_man *mpool, + string_t *dest, const string_t *src) +{ + return fast_mpool_strdup_ex(mpool, dest, src->str, src->len); +} /** get stats