From 7fbb5c620bbfaadbbd2f7e871227fd23cbd7a1c7 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Sun, 3 Aug 2025 15:10:28 +0800 Subject: [PATCH] change int2buff, buff2int etc. functions to static inline --- HISTORY | 1 + src/shared_func.c | 60 ------------------------------------ src/shared_func.h | 61 ++++++++++++++++++++++++++++++++----- src/tests/cpool_benchmark.c | 2 +- 4 files changed, 56 insertions(+), 68 deletions(-) diff --git a/HISTORY b/HISTORY index e0e69cd..acb8afa 100644 --- a/HISTORY +++ b/HISTORY @@ -3,6 +3,7 @@ Version 1.78 2025-08-02 * getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6 * add files: spinlock.[hc] * connection_pool.[hc]: use CAS instead of pthread mutex lock + * shared_func.[hc]: change int2buff, buff2int etc. functions to static inline Version 1.77 2025-03-18 * impl. shorten_path for /./ and /../ diff --git a/src/shared_func.c b/src/shared_func.c index 557094b..d663023 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -1565,66 +1565,6 @@ int fc_copy_to_path(const char *src_filename, const char *dest_path) return fc_copy_file(src_filename, dest_filename); } -void short2buff(const short n, char *buff) -{ - unsigned char *p; - p = (unsigned char *)buff; - *p++ = (n >> 8) & 0xFF; - *p++ = n & 0xFF; -} - -short buff2short(const char *buff) -{ - return (short)((((unsigned char)(*(buff))) << 8) | \ - ((unsigned char)(*(buff+1)))); -} - -void int2buff(const int n, char *buff) -{ - unsigned char *p; - p = (unsigned char *)buff; - *p++ = (n >> 24) & 0xFF; - *p++ = (n >> 16) & 0xFF; - *p++ = (n >> 8) & 0xFF; - *p++ = n & 0xFF; -} - -int buff2int(const char *buff) -{ - return (((unsigned char)(*buff)) << 24) | \ - (((unsigned char)(*(buff+1))) << 16) | \ - (((unsigned char)(*(buff+2))) << 8) | \ - ((unsigned char)(*(buff+3))); -} - -void long2buff(int64_t n, char *buff) -{ - unsigned char *p; - p = (unsigned char *)buff; - *p++ = (n >> 56) & 0xFF; - *p++ = (n >> 48) & 0xFF; - *p++ = (n >> 40) & 0xFF; - *p++ = (n >> 32) & 0xFF; - *p++ = (n >> 24) & 0xFF; - *p++ = (n >> 16) & 0xFF; - *p++ = (n >> 8) & 0xFF; - *p++ = n & 0xFF; -} - -int64_t buff2long(const char *buff) -{ - unsigned char *p; - p = (unsigned char *)buff; - return (((int64_t)(*p)) << 56) | \ - (((int64_t)(*(p+1))) << 48) | \ - (((int64_t)(*(p+2))) << 40) | \ - (((int64_t)(*(p+3))) << 32) | \ - (((int64_t)(*(p+4))) << 24) | \ - (((int64_t)(*(p+5))) << 16) | \ - (((int64_t)(*(p+6))) << 8) | \ - ((int64_t)(*(p+7))); -} - int fd_gets(int fd, char *buff, const int size, int once_bytes) { char *pDest; diff --git a/src/shared_func.h b/src/shared_func.h index 27f0fa5..5e0d83c 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -196,14 +196,24 @@ void printBuffHex(const char *s, const int len); * buff: the buffer, at least 2 bytes space, no tail \0 * return: none */ -void short2buff(const short n, char *buff); +static inline void short2buff(const short n, char *buff) +{ + unsigned char *p; + p = (unsigned char *)buff; + *p++ = (n >> 8) & 0xFF; + *p++ = n & 0xFF; +} /** buffer convert to 16 bits int * parameters: * buff: big-endian 2 bytes buffer * return: 16 bits int value */ -short buff2short(const char *buff); +static inline short buff2short(const char *buff) +{ + return (short)((((unsigned char)(*(buff))) << 8) | + ((unsigned char)(*(buff+1)))); +} /** 32 bits int convert to buffer (big-endian) * parameters: @@ -211,14 +221,28 @@ short buff2short(const char *buff); * buff: the buffer, at least 4 bytes space, no tail \0 * return: none */ -void int2buff(const int n, char *buff); +static inline void int2buff(const int n, char *buff) +{ + unsigned char *p; + p = (unsigned char *)buff; + *p++ = (n >> 24) & 0xFF; + *p++ = (n >> 16) & 0xFF; + *p++ = (n >> 8) & 0xFF; + *p++ = n & 0xFF; +} /** buffer convert to 32 bits int * parameters: * buff: big-endian 4 bytes buffer * return: 32 bits int value */ -int buff2int(const char *buff); +static inline int buff2int(const char *buff) +{ + return (((unsigned char)(*buff)) << 24) | + (((unsigned char)(*(buff+1))) << 16) | + (((unsigned char)(*(buff+2))) << 8) | + ((unsigned char)(*(buff+3))); +} /** long (64 bits) convert to buffer (big-endian) * parameters: @@ -226,15 +250,38 @@ int buff2int(const char *buff); * buff: the buffer, at least 8 bytes space, no tail \0 * return: none */ -void long2buff(int64_t n, char *buff); +static inline void long2buff(int64_t n, char *buff) +{ + unsigned char *p; + p = (unsigned char *)buff; + *p++ = (n >> 56) & 0xFF; + *p++ = (n >> 48) & 0xFF; + *p++ = (n >> 40) & 0xFF; + *p++ = (n >> 32) & 0xFF; + *p++ = (n >> 24) & 0xFF; + *p++ = (n >> 16) & 0xFF; + *p++ = (n >> 8) & 0xFF; + *p++ = n & 0xFF; +} /** buffer convert to 64 bits int * parameters: * buff: big-endian 8 bytes buffer * return: 64 bits int value */ -int64_t buff2long(const char *buff); - +static inline int64_t buff2long(const char *buff) +{ + unsigned char *p; + p = (unsigned char *)buff; + return (((int64_t)(*p)) << 56) | + (((int64_t)(*(p+1))) << 48) | + (((int64_t)(*(p+2))) << 40) | + (((int64_t)(*(p+3))) << 32) | + (((int64_t)(*(p+4))) << 24) | + (((int64_t)(*(p+5))) << 16) | + (((int64_t)(*(p+6))) << 8) | + ((int64_t)(*(p+7))); +} /** 32 bits float convert to buffer (big-endian) * parameters: diff --git a/src/tests/cpool_benchmark.c b/src/tests/cpool_benchmark.c index 4b88929..8ad69ce 100644 --- a/src/tests/cpool_benchmark.c +++ b/src/tests/cpool_benchmark.c @@ -30,7 +30,7 @@ #define USE_CONN_POOL 1 //#define USE_CAS_LOCK 1 -static int thread_count = 24; +static int thread_count = 16; static int64_t loop_count = 10000000; static ConnectionPool cpool; static char buff[1024];