From 4b049d3fd62d9021415a3f7199d2cc5de5fcb34b Mon Sep 17 00:00:00 2001 From: tezc Date: Thu, 14 Jan 2021 02:55:27 +0300 Subject: [PATCH] buf ref and data --- buffer/buf_test.c | 4 ++-- buffer/sc_buf.c | 12 +++++------- buffer/sc_buf.h | 10 ++++++++-- logger/CMakeLists.txt | 3 ++- logger/log_test.c | 12 ++++++++++++ logger/sc_log.c | 19 ++++++------------- 6 files changed, 35 insertions(+), 25 deletions(-) diff --git a/buffer/buf_test.c b/buffer/buf_test.c index 7290bb1..dc81be5 100644 --- a/buffer/buf_test.c +++ b/buffer/buf_test.c @@ -126,7 +126,7 @@ void test1() sc_buf_term(&buf2); char text[128]; - buf = sc_buf_wrap(text, sizeof(text), true); + buf = sc_buf_wrap(text, sizeof(text), SC_BUF_REF); sc_buf_put_text(&buf, "test %d test %s", 1, "test"); assert(strcmp(sc_buf_rbuf(&buf), "test 1 test test") == 0); } @@ -372,7 +372,7 @@ void fail_test() assert(sc_buf_quota(&buf) > 100); sc_buf_term(&buf); - buf = sc_buf_wrap(&p, 8, true); + buf = sc_buf_wrap(&p, 8, SC_BUF_REF); assert(sc_buf_reserve(&buf, 100) == false); fail_vsnprintf_at = -1; diff --git a/buffer/sc_buf.c b/buffer/sc_buf.c index fcd94a3..f3191d7 100644 --- a/buffer/sc_buf.c +++ b/buffer/sc_buf.c @@ -44,19 +44,19 @@ bool sc_buf_init(struct sc_buf *buf, uint32_t cap) } } - *buf = sc_buf_wrap(mem, cap, false); + *buf = sc_buf_wrap(mem, cap, 0); return true; } -struct sc_buf sc_buf_wrap(void *data, uint32_t len, bool ref) +struct sc_buf sc_buf_wrap(void *data, uint32_t len, int flags) { struct sc_buf buf = {.mem = data, .cap = len, - .limit = ref ? len : UINT32_MAX, - .wpos = 0, + .limit = flags & SC_BUF_REF ? len : UINT32_MAX, + .wpos = flags & SC_BUF_DATA ? len : 0, .rpos = 0, - .ref = ref, + .ref = flags & SC_BUF_REF, .error = 0}; return buf; @@ -752,5 +752,3 @@ void sc_buf_put_blob(struct sc_buf *buf, const void *ptr, uint32_t len) sc_buf_put_32(buf, len); sc_buf_put_raw(buf, ptr, len); } - - diff --git a/buffer/sc_buf.h b/buffer/sc_buf.h index f19ba62..bf42c21 100644 --- a/buffer/sc_buf.h +++ b/buffer/sc_buf.h @@ -34,6 +34,10 @@ #define SC_BUF_CORRUPT 1u #define SC_BUF_OOM 3u +#define SC_BUF_REF 8 +#define SC_BUF_DATA 16 +#define SC_BUF_READ (SC_BUF_REF | SC_BUF_DATA) + struct sc_buf { unsigned char *mem; @@ -71,11 +75,13 @@ void sc_buf_term(struct sc_buf *buf); * * @param data data pointer * @param len len - * @param ref if set 'true', buffer will not try to expand itself and + * @param flags if set 'SC_BUF_REF', buffer will not try to expand itself and * 'sc_buf_term' will not try to 'free()' buffer. + * if set 'SC_BUF_DATA', buffer wpos will be 'len'. + * flags can be combined : SC_BUF_REF | SC_BUF_REF * @return */ -struct sc_buf sc_buf_wrap(void *data, uint32_t len, bool ref); +struct sc_buf sc_buf_wrap(void *data, uint32_t len, int flags); /** * Set limit of the buffer, when buffer reaches limit, it will set buffer's diff --git a/logger/CMakeLists.txt b/logger/CMakeLists.txt index aad925f..e0e8b82 100644 --- a/logger/CMakeLists.txt +++ b/logger/CMakeLists.txt @@ -31,7 +31,8 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-builtin) target_link_options(${PROJECT_NAME}_test PRIVATE -Wl,--wrap=fprintf,--wrap=vfprintf,--wrap=fopen,--wrap=localtime - -Wl,--wrap=pthread_mutexattr_init,--wrap=pthread_mutex_init) + -Wl,--wrap=pthread_mutexattr_init,--wrap=pthread_mutex_init + -Wl,--wrap=fclose) endif () endif () diff --git a/logger/log_test.c b/logger/log_test.c index 0365391..ae33551 100644 --- a/logger/log_test.c +++ b/logger/log_test.c @@ -142,6 +142,17 @@ FILE *__wrap_fopen(const char *filename, const char *mode) return NULL; } +bool mock_fclose = false; +extern int __real_fclose (FILE *__stream); +int __wrap_fclose (FILE *__stream) +{ + if (!mock_fclose) { + return __real_fclose(__stream); + } + + return 0; +} + bool mock_localtime = false; extern struct tm *__real_localtime(const time_t *timer); struct tm *__wrap_localtime(const time_t *timer) @@ -261,6 +272,7 @@ void fail_test(void) mock_fopen = false; sc_log_term(); + mock_fclose = false; mock_fprintf = false; mock_vfprintf = false; mock_localtime = false; diff --git a/logger/sc_log.c b/logger/sc_log.c index fde11b9..c508446 100644 --- a/logger/sc_log.c +++ b/logger/sc_log.c @@ -108,12 +108,9 @@ int sc_log_mutex_init(struct sc_log_mutex *mtx) return rc == 0 ? 0 : -1; } -int sc_log_mutex_term(struct sc_log_mutex *mtx) +void sc_log_mutex_term(struct sc_log_mutex *mtx) { - int rc; - - rc = pthread_mutex_destroy(&mtx->mtx); - return rc == 0 ? 0 : -1; + pthread_mutex_destroy(&mtx->mtx); } void sc_log_mutex_lock(struct sc_log_mutex *mtx) @@ -165,21 +162,17 @@ int sc_log_init(void) int sc_log_term(void) { - int rc = 0, rv; + int rc = 0; if (sc_log.fp) { - rv = fclose(sc_log.fp); - if (rv != 0) { + rc = fclose(sc_log.fp); + if (rc != 0) { rc = -1; strncpy(sc_log.err, strerror(errno), sizeof(sc_log.err) - 1); } } - rv = sc_log_mutex_term(&sc_log.mtx); - if (rv == -1) { - strncpy(sc_log.err, "Mutex term failed.", sizeof(sc_log.err) - 1); - rc = -1; - } + sc_log_mutex_term(&sc_log.mtx); return rc; }