mirror of
https://github.com/tezc/sc.git
synced 2025-01-14 06:43:04 +08:00
commit
b6f78c41c7
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
@ -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 ()
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user