From 4f71eed36fa0418e5f82b880e8f800d63148cef2 Mon Sep 17 00:00:00 2001 From: Francesco Date: Fri, 5 Sep 2025 00:18:08 +0200 Subject: [PATCH] fix: examples --- .gitignore | 2 +- examples/hashmap/hm1.c | 12 ++++++------ examples/queue/q1.c | 13 +++++++------ examples/string/str1.c | 13 +++++++------ examples/string/str2.c | 20 -------------------- hashmap/myhashmap.c | 2 +- hashmap/myhashmap.h | 20 ++++++++++---------- meson.build | 27 +++++++++++++++++++++++++++ native_cl.ini | 4 ++++ queue/myqueue.c | 10 +++++----- string/mystring.c | 2 +- 11 files changed, 69 insertions(+), 56 deletions(-) delete mode 100644 examples/string/str2.c create mode 100644 meson.build create mode 100644 native_cl.ini diff --git a/.gitignore b/.gitignore index cd03bd2..5d55158 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -test/ +.cache/ # Prerequisites *.d diff --git a/examples/hashmap/hm1.c b/examples/hashmap/hm1.c index 36eb0e6..26d02b8 100644 --- a/examples/hashmap/hm1.c +++ b/examples/hashmap/hm1.c @@ -7,7 +7,7 @@ #define MAX_STR_LEN 64 /* My custom data type stored as value */ -struct my_custom_type_t { +struct my_custom_type { int age; char favourite_brand[MAX_STR_LEN]; }; @@ -41,7 +41,7 @@ bool my_equal_fun(const void *key_a, const void *key_b) { void my_free_key(void *key) { free(key); } void my_free_value(void *value) { - struct my_custom_type_t *mct = (struct my_custom_type_t *)value; + struct my_custom_type *mct = (struct my_custom_type *)value; free(mct); } @@ -51,10 +51,10 @@ int main(void) { /* This hashmap will contain names as keys and a custom type as value */ size_t key_size = sizeof(char) * MAX_STR_LEN; size_t value_size = sizeof(int) + sizeof(char) * MAX_STR_LEN; - mcl_hashmap *map = mcl_hm_init(my_hash_func, my_equal_fun, my_free_key, my_free_value, key_size, value_size); + mcl_hashmap_s *map = mcl_hm_init(my_hash_func, my_equal_fun, my_free_key, my_free_value, key_size, value_size); /* Set a new value */ - struct my_custom_type_t p1 = { + struct my_custom_type p1 = { .age = 21, }; strncpy(p1.favourite_brand, "Ferrari", sizeof(p1.favourite_brand)); @@ -62,9 +62,9 @@ int main(void) { mcl_hm_set(map, "John", &p1); /* Retrieve the data */ - mcl_bucket *john = mcl_hm_get(map, "John"); + mcl_bucket_s *john = mcl_hm_get(map, "John"); char *name = (char *)john->key; - struct my_custom_type_t *john_v = (struct my_custom_type_t *)john->value; + struct my_custom_type *john_v = (struct my_custom_type *)john->value; int age = john_v->age; char *fav_brand = john_v->favourite_brand; fprintf(stdout, "Name: %s\nAge: %d\nFavourite brand: %s\n", name, age, fav_brand); diff --git a/examples/queue/q1.c b/examples/queue/q1.c index 4031ae7..7027027 100644 --- a/examples/queue/q1.c +++ b/examples/queue/q1.c @@ -5,7 +5,7 @@ int main(void) { /* Allocate a new queue */ /* Always remember to check return values */ - mcl_queue *queue = mcl_queue_init(3, sizeof(int)); + mcl_queue_s *queue = mcl_queue_init(3, sizeof(int)); int val, out; @@ -17,25 +17,26 @@ int main(void) { mcl_queue_push(queue, &val); /* Retrieve values */ - int front = *((int *)mcl_queue_get_front(queue)); - int rear = *((int *)mcl_queue_get_rear(queue)); + int front, rear; + mcl_queue_get_front(queue, &front); + mcl_queue_get_rear(queue, &rear); printf("Front: %d, Rear: %d\n", front, rear); /* Remove an element from the buffer */ mcl_queue_pop(queue, &out); printf("Pop: %d\n", out); - front = *((int *)mcl_queue_get_front(queue)); + mcl_queue_get_front(queue, &front); printf("Front after pop: %d\n", front); val = 3; mcl_queue_push(queue, &val); - rear = *((int *)mcl_queue_get_rear(queue)); + mcl_queue_get_rear(queue, &rear); printf("Rear after push 3: %d\n", rear); val = 4; - int res = mcl_queue_push(queue, &val); + mcl_queue_push(queue, &val); /* Clear queue */ while (mcl_queue_pop(queue, &out) == 0) { diff --git a/examples/string/str1.c b/examples/string/str1.c index 19a55f1..51cd3f9 100644 --- a/examples/string/str1.c +++ b/examples/string/str1.c @@ -9,15 +9,17 @@ int main(void) { char *c_str; /* Allocate a new dynamic string with an initial capacity */ - /* Always remember to check return values */ - mcl_string *str = mcl_string_new("Hello, world!", 512); + mcl_string_s *str = mcl_string_new("Hello, world!", 512); + if (str == NULL) { + printf("Failed to initialize string"); + exit(EXIT_FAILURE); + } /* Retrieve a C str from mcl_string with mcl_string_cstr() */ c_str = mcl_string_cstr(str); length = mcl_string_length(str); capacity = mcl_string_capacity(str); - printf("%s\nlength: %ld, capacity: %ld\n", c_str, length, capacity); - free(c_str); + printf("%s\nlength: %lld, capacity: %lld\n", c_str, length, capacity); /* Append text to a mcl_string */ mcl_string_append(str, " How are you?"); @@ -26,8 +28,7 @@ int main(void) { c_str = mcl_string_cstr(str); length = mcl_string_length(str); capacity = mcl_string_capacity(str); - printf("%s\nsize: %ld, cap: %ld\n", c_str, length, capacity); - free(c_str); + printf("%s\nsize: %lld, cap: %lld\n", c_str, length, capacity); /* Always deallocate memory */ mcl_string_free(str); diff --git a/examples/string/str2.c b/examples/string/str2.c deleted file mode 100644 index 0ea3411..0000000 --- a/examples/string/str2.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include - -#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; -} diff --git a/hashmap/myhashmap.c b/hashmap/myhashmap.c index c2a73c1..49bad88 100644 --- a/hashmap/myhashmap.c +++ b/hashmap/myhashmap.c @@ -73,7 +73,7 @@ mcl_hashmap_s *mcl_hm_init(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_ int ret; for (size_t i = 0; i < hashmap->num_locks; ++i) { - ret = mtx_init(&(hashmap->locks[i]), NULL); + ret = mtx_init(&(hashmap->locks[i]), mtx_plain); if (ret != thrd_success) { /* Mutex failed */ for (size_t j = 0; j < i; ++j) { diff --git a/hashmap/myhashmap.h b/hashmap/myhashmap.h index 53b362b..a14317b 100644 --- a/hashmap/myhashmap.h +++ b/hashmap/myhashmap.h @@ -57,15 +57,15 @@ typedef void free_value_f(void *value); * and memory management, along with the bucket array. */ typedef struct mcl_hashmap { - hash_f *hash_fn; /**< Hash function */ - equal_f *equal_fn; /**< Equality comparison function */ - free_key_f *free_key_fn; /**< Key deallocation function (optional) */ - free_value_f *free_value_fn; /**< Value deallocation function (optional) */ - size_t key_size; /**< Size in bytes of the key */ - size_t value_size; /**< Size in bytes of the value */ - mcl_bucket map[MYCLIB_HASHMAP_SIZE]; /**< Array of bucket chains */ - mtx_t *locks; /**< Mutex array */ - size_t num_locks; /**< Number of mutex */ + hash_f *hash_fn; /**< Hash function */ + equal_f *equal_fn; /**< Equality comparison function */ + free_key_f *free_key_fn; /**< Key deallocation function (optional) */ + free_value_f *free_value_fn; /**< Value deallocation function (optional) */ + size_t key_size; /**< Size in bytes of the key */ + size_t value_size; /**< Size in bytes of the value */ + mcl_bucket_s map[MYCLIB_HASHMAP_SIZE]; /**< Array of bucket chains */ + mtx_t *locks; /**< Mutex array */ + size_t num_locks; /**< Number of mutex */ } mcl_hashmap_s; /** @@ -83,7 +83,7 @@ typedef struct mcl_hashmap { * @param[in] value_size Size in bytes of each value to be stored * @return A pointer to the newly initialized hash map, or NULL on failure */ -mcl_hashmap *mcl_hm_init(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, free_value_f *free_value_fn, size_t key_size, size_t value_size); +mcl_hashmap_s *mcl_hm_init(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, free_value_f *free_value_fn, size_t key_size, size_t value_size); /** * @brief Free all resources used by the hash map diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..f005a9c --- /dev/null +++ b/meson.build @@ -0,0 +1,27 @@ +project( + 'examples', + 'c', + version: '0.1', + default_options: ['c_std=c17'], +) + +string = files( + 'examples/string/str1.c', + 'string/mystring.c', +) + +queue = files( + 'examples/queue/q1.c', + 'queue/myqueue.c', +) + +hashmap = files( + 'examples/hashmap/hm1.c', + 'hashmap/myhashmap.c', +) + +inc_dir = include_directories('string', 'queue', 'hashmap') + +executable('string', string, include_directories: inc_dir) +executable('queue', queue, include_directories: inc_dir) +executable('hashmap', hashmap, include_directories: inc_dir) diff --git a/native_cl.ini b/native_cl.ini new file mode 100644 index 0000000..d9e3a03 --- /dev/null +++ b/native_cl.ini @@ -0,0 +1,4 @@ +[binaries] +c = 'cl' +cpp = 'cl' +ar = 'lib' diff --git a/queue/myqueue.c b/queue/myqueue.c index 82bee7d..16951e3 100644 --- a/queue/myqueue.c +++ b/queue/myqueue.c @@ -16,7 +16,7 @@ mcl_queue_s *mcl_queue_init(size_t queue_size, size_t elem_size) { return NULL; } - int ret = mtx_init(&queue->lock, NULL); + int ret = mtx_init(&queue->lock, mtx_plain); if (ret != thrd_success) { free(queue->buffer); free(queue); @@ -47,7 +47,7 @@ int mcl_queue_push(mcl_queue_s *queue, const void *elem) { } /* Copy the elem in the buffer */ - void *dest = (void *)queue->buffer + (queue->rear * queue->elem_size); + void *dest = (char *)queue->buffer + (queue->rear * queue->elem_size); memcpy(dest, elem, queue->elem_size); queue->size++; @@ -71,7 +71,7 @@ int mcl_queue_pop(mcl_queue_s *queue, void *out_elem) { return -1; } - void *src = (void *)queue->buffer + (queue->front * queue->elem_size); + void *src = (char *)queue->buffer + (queue->front * queue->elem_size); memcpy(out_elem, src, queue->elem_size); queue->front = (queue->front + 1) % queue->capacity; @@ -94,7 +94,7 @@ int mcl_queue_get_front(mcl_queue_s *queue, void *out) { return -1; } - void *front = (void *)queue->buffer + (queue->front * queue->elem_size); + void *front = (char *)queue->buffer + (queue->front * queue->elem_size); memcpy(out, front, queue->elem_size); mtx_unlock(&queue->lock); @@ -121,7 +121,7 @@ int mcl_queue_get_rear(mcl_queue_s *queue, void *out) { rear_index = queue->rear - 1; } - void *rear = (void *)queue->buffer + (rear_index * queue->elem_size); + void *rear = (char *)queue->buffer + (rear_index * queue->elem_size); memcpy(out, rear, queue->elem_size); mtx_unlock(&queue->lock); diff --git a/string/mystring.c b/string/mystring.c index 5e73931..e9c4a0f 100644 --- a/string/mystring.c +++ b/string/mystring.c @@ -78,7 +78,7 @@ mcl_string_s *mcl_string_new(const char *text, size_t initial_capacity) { str->data[str->size] = '\0'; /* Init pthread mutex */ - if (mtx_init(&str->lock, NULL) != thrd_success) { + if (mtx_init(&str->lock, mtx_plain) != thrd_success) { free(str->data); free(str);