drop math library

This commit is contained in:
2025-08-05 01:11:30 +02:00
parent 1502ef4116
commit deca6d6558
5 changed files with 84 additions and 52 deletions

View File

@@ -7,5 +7,4 @@ It is not production-ready. Use at your own risk.
- Hashmaps - Thread-safe (MT Safe) - Hashmaps - Thread-safe (MT Safe)
- Dynamic strings - Thread-safe (MT Safe) - Dynamic strings - Thread-safe (MT Safe)
> Requires linking with the C math library (`-lm`)
- Circular Queue - Thread-safe (MT Safe) - Circular Queue - Thread-safe (MT Safe)

View File

@@ -1,11 +1,12 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "../../string/mystring.h" #include "../../string/mystring.h"
int main(void) { int main(void) {
size_t length; size_t length;
size_t capacity; size_t capacity;
const char *c_str; char *c_str;
/* Allocate a new dynamic string with an initial capacity */ /* Allocate a new dynamic string with an initial capacity */
/* Always remember to check return values */ /* Always remember to check return values */
@@ -16,6 +17,7 @@ int main(void) {
length = mcl_string_length(str); length = mcl_string_length(str);
capacity = mcl_string_capacity(str); capacity = mcl_string_capacity(str);
printf("%s\nlength: %ld, capacity: %ld\n", c_str, length, capacity); printf("%s\nlength: %ld, capacity: %ld\n", c_str, length, capacity);
free(c_str);
/* Append text to a mcl_string */ /* Append text to a mcl_string */
mcl_string_append(str, " How are you?"); mcl_string_append(str, " How are you?");
@@ -25,6 +27,7 @@ int main(void) {
length = mcl_string_length(str); length = mcl_string_length(str);
capacity = mcl_string_capacity(str); capacity = mcl_string_capacity(str);
printf("%s\nsize: %ld, cap: %ld\n", c_str, length, capacity); printf("%s\nsize: %ld, cap: %ld\n", c_str, length, capacity);
free(c_str);
/* Always deallocate memory */ /* Always deallocate memory */
mcl_string_free(str); mcl_string_free(str);

20
examples/string/str2.c Normal file
View File

@@ -0,0 +1,20 @@
#include <stdio.h>
#include <stdlib.h>
#include "../../string/mystring.h"
int main(void) {
mcl_string *str = mcl_string_new("Hello, world!", 0);
mcl_string_append(str, " How are you?");
char *c_str = mcl_string_cstr(str);
size_t len = mcl_string_length(str);
size_t cap = mcl_string_capacity(str);
fprintf(stdout, "%s\nlen: %ld\ncapacity: %ld\n", c_str, len, cap);
mcl_string_free(str);
free(c_str);
return 0;
}

View File

@@ -1,11 +1,21 @@
#include "mystring.h" #include "mystring.h"
#include <math.h>
#include <pthread.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
mcl_string *mcl_string_new(const char *text, long initial_capacity) { static size_t mcl_next_power_two(size_t len) {
len--;
len |= len >> 1;
len |= len >> 2;
len |= len >> 4;
len |= len >> 8;
len |= len >> 16;
len++;
return len;
}
mcl_string *mcl_string_new(const char *text, size_t initial_capacity) {
if (text == NULL) { if (text == NULL) {
return NULL; return NULL;
} }
@@ -16,26 +26,18 @@ mcl_string *mcl_string_new(const char *text, long initial_capacity) {
return NULL; return NULL;
} }
/* Init pthread mutex */
int ret = pthread_mutex_init(&str->lock, NULL);
if (ret != 0) {
free(str);
return NULL;
}
/* Calculate size and capacity */ /* Calculate size and capacity */
str->size = strlen(text); str->size = strlen(text);
size_t capacity = initial_capacity; size_t capacity = initial_capacity;
if (capacity != -1 && capacity - 1 < str->size) { if (capacity != 0 && capacity - 1 < str->size) {
free(str); free(str);
return NULL; return NULL;
} }
if (capacity == -1) { if (capacity == 0) {
capacity = (unsigned long)pow(2, (unsigned)log2(str->size) + 1); capacity = mcl_next_power_two(str->size + 1);
} }
str->capacity = capacity; str->capacity = capacity;
@@ -49,10 +51,18 @@ mcl_string *mcl_string_new(const char *text, long initial_capacity) {
} }
/* Copy the text and ensure null termination */ /* Copy the text and ensure null termination */
memset(str->data, 0, str->capacity);
memcpy(str->data, text, str->size); memcpy(str->data, text, str->size);
str->data[str->size] = '\0'; str->data[str->size] = '\0';
/* Init pthread mutex */
int ret = pthread_mutex_init(&str->lock, NULL);
if (ret != 0) {
free(str->data);
free(str);
return NULL;
}
return str; return str;
} }
@@ -79,7 +89,7 @@ int mcl_string_append(mcl_string *string, const char *text) {
/* Check if we need to resize */ /* Check if we need to resize */
if (new_size + 1 > string->capacity) { if (new_size + 1 > string->capacity) {
size_t new_capacity = (unsigned long)pow(2, (unsigned)log2(new_size) + 1); size_t new_capacity = mcl_next_power_two(new_size + 1);
/* Reallocate the buffer */ /* Reallocate the buffer */
void *new_data = realloc(string->data, sizeof(char) * new_capacity); void *new_data = realloc(string->data, sizeof(char) * new_capacity);
if (!new_data) { if (!new_data) {
@@ -114,16 +124,10 @@ void mcl_string_free(mcl_string *string) {
return; return;
} }
int ret = pthread_mutex_lock(&string->lock);
if (ret != 0) {
return;
}
if (string->data) { if (string->data) {
free(string->data); free(string->data);
} }
pthread_mutex_unlock(&string->lock);
pthread_mutex_destroy(&string->lock); pthread_mutex_destroy(&string->lock);
free(string); free(string);
@@ -163,9 +167,9 @@ size_t mcl_string_capacity(mcl_string *string) {
return cap; return cap;
} }
const char *mcl_string_cstr(mcl_string *string) { char *mcl_string_cstr(mcl_string *string) {
if (string == NULL || string->data == NULL) { if (string == NULL || string->data == NULL) {
return ""; return NULL;
} }
int ret = pthread_mutex_lock(&string->lock); int ret = pthread_mutex_lock(&string->lock);
@@ -173,7 +177,14 @@ const char *mcl_string_cstr(mcl_string *string) {
return NULL; return NULL;
} }
char *data = string->data; char *data = malloc(sizeof(char) * string->size + 1);
if (data == NULL) {
pthread_mutex_unlock(&string->lock);
return NULL;
}
memcpy(data, string->data, string->size + 1);
pthread_mutex_unlock(&string->lock); pthread_mutex_unlock(&string->lock);

View File

@@ -5,69 +5,68 @@
#include <stddef.h> #include <stddef.h>
/** /**
* @brief Thread-safe dynamic string structure * @brief Thread-safe dynamic string structure.
*/ */
typedef struct mcl_string_t { typedef struct mcl_string_t {
size_t size; /**< Current length of the string (excluding null terminator) */ size_t size; /**< Current length of the string (excluding null terminator) */
size_t capacity; /**< Allocated capacity */ size_t capacity; /**< Allocated capacity including null terminator */
char *data; /**< Pointer to string data */ char *data; /**< Pointer to string data (null-terminated) */
pthread_mutex_t lock; /**< Mutex for thread safety */ pthread_mutex_t lock; /**< Mutex for thread safety */
} mcl_string; } mcl_string;
/** /**
* @brief Create a new string initialized with given text * @brief Create a new string initialized with the given text.
* *
* @param text Initial text (cannot be NULL) * @param text Initial text (must not be NULL)
* @param initial_capacity Initial buffer capacity; pass -1 to auto-calculate * @param initial_capacity Initial buffer capacity (0 to auto-calculate)
* @return Pointer to new string, or NULL on failure * @return Pointer to new string on success, or NULL on failure.
* *
* @note Caller must free the string with mcl_string_free() * @note Caller must release the string using mcl_string_free().
*/ */
mcl_string *mcl_string_new(const char *text, long initial_capacity); mcl_string *mcl_string_new(const char *text, size_t initial_capacity);
/** /**
* @brief Append text to an existing string * @brief Append text to the string.
* *
* @param string The string to append to * @param string String to modify
* @param text Text to append (can be empty) * @param text Text to append (must not be NULL, can be empty)
* @return 0 on success, -1 on failure * @return 0 on success, -1 on failure
* *
* @note Original string remains unchanged if append fails * @note On failure, the original string remains unchanged.
*/ */
int mcl_string_append(mcl_string *string, const char *text); int mcl_string_append(mcl_string *string, const char *text);
/** /**
* @brief Free a string and its resources * @brief Free the string and its resources.
* *
* @param string String to free (NULL is safe) * @param string String to free (safe to call with NULL)
*/ */
void mcl_string_free(mcl_string *string); void mcl_string_free(mcl_string *string);
/** /**
* @brief Get the current length of the string * @brief Get the current length of the string.
* *
* @param string String to query * @param string String to query
* @return Length of string, or 0 if NULL * @return Length of the string (excluding null terminator), or 0 if NULL
*/ */
size_t mcl_string_length(mcl_string *string); size_t mcl_string_length(mcl_string *string);
/** /**
* @brief Get the current capacity of the string buffer * @brief Get the total allocated capacity of the string buffer.
* *
* @param string String to query * @param string String to query
* @return Capacity, or 0 if NULL * @return Capacity (in bytes, including null terminator), or 0 if NULL
*/ */
size_t mcl_string_capacity(mcl_string *string); size_t mcl_string_capacity(mcl_string *string);
/** /**
* @brief Get a read-only C-string pointer * @brief Get a copy of the string as a null-terminated C-string.
* *
* @param string String to access * @param string String to copy
* @return Null-terminated string pointer, or "" if NULL * @return Newly allocated copy of the string, or NULL on failure
* *
* @warning Do not modify returned pointer. * @warning Caller is responsible for freeing the returned pointer.
* Pointer may become invalid after string modifications.
*/ */
const char *mcl_string_cstr(mcl_string *string); char *mcl_string_cstr(mcl_string *string);
#endif /* MYCLIB_STRING_H */ #endif /* MYCLIB_STRING_H */