refactor(string): remove thread-specific storage

This commit is contained in:
2025-12-03 07:59:49 +01:00
parent f15d94b02c
commit f8dcae213d
2 changed files with 69 additions and 70 deletions

View File

@@ -8,29 +8,6 @@
#include <string.h> #include <string.h>
#include <threads.h> #include <threads.h>
/* Initialize Thread-Specific Storage */
static tss_t buffer_key;
static once_flag buffer_once = ONCE_FLAG_INIT;
typedef struct {
char *buf; /**< Allocated buffer */
size_t cap; /**< Buffer's capacity */
} tl_buffer_s;
static void buffer_destructor(void *buf) {
tl_buffer_s *tb = (tl_buffer_s *)buf;
if (tb == NULL) {
return;
}
free(tb->buf);
free(tb);
}
static void buffer_key_init(void) {
tss_create(&buffer_key, buffer_destructor);
}
/* Returns the next power of two of a number */ /* Returns the next power of two of a number */
static size_t next_power_two(size_t len) { static size_t next_power_two(size_t len) {
if (len == 0) if (len == 0)
@@ -227,65 +204,59 @@ size_t string_cap(string_s *string) {
return cap; return cap;
} }
int string_lock(string_s *string) {
if (string == NULL) {
return -1;
}
if (mtx_lock(&string->lock) != thrd_success) {
return -1;
}
return 0;
}
int string_unlock(string_s *string) {
if (string == NULL) {
return -1;
}
if (mtx_unlock(&string->lock) != thrd_success) {
return -1;
}
return 0;
}
char *string_cstr(string_s *string) { char *string_cstr(string_s *string) {
if (string == NULL || string->data == NULL) { if (string == NULL || string->data == NULL) {
return NULL; return NULL;
} }
call_once(&buffer_once, buffer_key_init); return string->data;
}
char *string_copy(string_s *string) {
if (string == NULL || string->data == NULL) {
return NULL;
}
if (mtx_lock(&string->lock) != thrd_success) { if (mtx_lock(&string->lock) != thrd_success) {
return NULL; return NULL;
} }
size_t need = string->size + 1; char *cpy = malloc(string->size + 1);
if (!cpy) {
/* Retrieve thread local buffer */ mtx_unlock(&string->lock);
tl_buffer_s *tb = (tl_buffer_s *)tss_get(buffer_key); return NULL;
if (tb == NULL) {
/* Not found, make a new one */
tb = malloc(sizeof(tl_buffer_s));
if (tb == NULL) {
mtx_unlock(&string->lock);
return NULL;
}
tb->cap = next_power_two(need);
tb->buf = malloc(tb->cap);
if (tb->buf == NULL) {
free(tb);
mtx_unlock(&string->lock);
return NULL;
}
if (tss_set(buffer_key, tb) != thrd_success) {
free(tb->buf);
free(tb);
mtx_unlock(&string->lock);
return NULL;
}
} else if (tb->cap < need) {
/* Found, but we need a bigger buffer */
size_t newcap = next_power_two(need);
char *tmp = realloc(tb->buf, newcap);
if (tmp == NULL) {
mtx_unlock(&string->lock);
return NULL;
}
tb->buf = tmp;
tb->cap = newcap;
} }
memcpy(tb->buf, string->data, need); memcpy(cpy, string->data, string->size);
cpy[string->size] = '\0';
mtx_unlock(&string->lock); mtx_unlock(&string->lock);
return tb->buf; return cpy;
} }
int string_compare(string_s *s1, string_s *s2) { int string_compare(string_s *s1, string_s *s2) {

View File

@@ -77,13 +77,41 @@ size_t string_cap(string_s *string);
* @brief Get a pointer to a null-terminated C-string. * @brief Get a pointer to a null-terminated C-string.
* *
* @param string String to read. * @param string String to read.
* @return Pointer to a thread-local buffer, or NULL on failure. * @return Pointer to string->data.
* *
* @note Valid until the next call in the same thread. Do NOT free the returned pointer. * @note See string_lock().
* Do NOT call more than once this function in a print function.
*/ */
char *string_cstr(string_s *string); char *string_cstr(string_s *string);
/**
* @brief Create a heap-allocated copy of the string content.
*
* @param string String to copy.
* @return Newly allocated null-terminated buffer, or NULL on failure.
*
* @note The caller is responsible for freeing the returned buffer with free().
* See string_lock().
*/
char *string_copy(string_s *string);
/**
* @brief Lock the string for safe reading or writing.
*
* @param string String to lock.
* @return 0 on success, -1 on failure.
*
* @note Use this before calling string_cstr() if you want a stable pointer.
*/
int string_lock(string_s *string);
/**
* @brief Unlock a previously locked string.
*
* @param string String to unlock.
* @return 0 on success, -1 on failure.
*/
int string_unlock(string_s *string);
/** /**
* @brief Compare two strings. * @brief Compare two strings.
* *