fix: examples

This commit is contained in:
2025-09-05 00:18:08 +02:00
parent 24f6ed0f84
commit 4f71eed36f
11 changed files with 69 additions and 56 deletions

2
.gitignore vendored
View File

@@ -1,4 +1,4 @@
test/
.cache/
# Prerequisites
*.d

View File

@@ -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);

View File

@@ -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) {

View File

@@ -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);

View File

@@ -1,20 +0,0 @@
#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

@@ -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) {

View File

@@ -63,7 +63,7 @@ typedef struct mcl_hashmap {
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 */
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

27
meson.build Normal file
View File

@@ -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)

4
native_cl.ini Normal file
View File

@@ -0,0 +1,4 @@
[binaries]
c = 'cl'
cpp = 'cl'
ar = 'lib'

View File

@@ -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);

View File

@@ -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);