From 2561a007b8d8999a4750046d0cfb3b1ad5af50ac Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期二, 09 四月 2024 15:29:32 +0800 Subject: [PATCH] test for perf --- src/bus_error.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/bus_error.cpp b/src/bus_error.cpp index 49244cd..3836ebf 100644 --- a/src/bus_error.cpp +++ b/src/bus_error.cpp @@ -24,7 +24,8 @@ "Service not supported", "Resource busy", "Resource not provide", - "Invalid parameters" + "Invalid parameters", + "No enough memory" }; @@ -48,15 +49,11 @@ } char * -bus_strerror(int err) +bus_strerror(int err, int flag) { int s; char *buf; /* Make first caller allocate key for thread-specific data */ - - if (err == 0) { - err = EBUS_BASE; - } s = pthread_once(&once, createKey); if (s != 0) @@ -67,15 +64,23 @@ { /* If first call from this thread, allocate buffer for thread, and save its location */ - buf = (char *)malloc(MAX_ERROR_LEN); + buf = (char *)malloc(MAX_ERROR_LEN + sizeof(int)); if (buf == NULL) err_exit(errno, "malloc"); + memset(buf, 0x00, MAX_ERROR_LEN + sizeof(int)); s = pthread_setspecific(strerrorKey, buf); if (s != 0) err_exit(s, "pthread_setspecific"); } + if (flag != 0) { + err = *(int *)(buf + MAX_ERROR_LEN); + } + + if (err == 0) { + err = EBUS_BASE; + } if(err < EBUS_BASE) { // libc閿欒 @@ -105,3 +110,33 @@ return buf; } + +void bus_errorset(int err) +{ + int s; + char *buf; + /* Make first caller allocate key for thread-specific data */ + s = pthread_once(&once, createKey); + if (s != 0) + err_exit(s, "pthread_once"); + + buf = (char *)pthread_getspecific(strerrorKey); + if (buf == NULL) + { + /* If first call from this thread, allocate + buffer for thread, and save its location */ + buf = (char *)malloc(MAX_ERROR_LEN + sizeof(int)); + if (buf == NULL) + err_exit(errno, "malloc"); + + s = pthread_setspecific(strerrorKey, buf); + if (s != 0) + err_exit(s, "pthread_setspecific"); + } + + *(int *)(buf + MAX_ERROR_LEN) = err; + +} + + + -- Gitblit v1.8.0