update hashmap library

This commit is contained in:
2025-08-01 23:45:29 +02:00
parent aedd51fb99
commit c7ad5d7874
8 changed files with 261 additions and 219 deletions

View File

@@ -34,7 +34,7 @@ typedef struct cws_http_t {
char location[CWS_HTTP_LOCATION_LEN]; /**< Resource requested */
char location_path[CWS_HTTP_LOCATION_PATH_LEN]; /**< Full resource path */
char http_version[CWS_HTTP_VERSION_LEN]; /**< HTTP version */
cws_hashmap *headers; /**< Headers hash map */
mcl_hashmap *headers; /**< Headers hash map */
} cws_http;
/* Connection */
/* Accept-Encoding */

View File

@@ -22,14 +22,6 @@
/* Main server loop */
extern volatile sig_atomic_t cws_server_run;
/**
* @brief Runs the server
*
* @param[in] config The server's config
* @return 0 on success, -1 on error
*/
int cws_server_start(cws_config *config);
/**
* @brief Setups hints object
*
@@ -39,12 +31,20 @@ int cws_server_start(cws_config *config);
*/
void cws_server_setup_hints(struct addrinfo *hints, size_t len, const char *hostname);
/**
* @brief Runs the server
*
* @param[in] config The server's config
* @return 0 on success, -1 on error
*/
int cws_server_start(cws_config *config);
/**
* @brief Main server loop
*
* @param[in,out] sockfd Socket of the commincation endpoint
*/
void cws_server_loop(int sockfd, cws_config *config);
int cws_server_loop(int sockfd, cws_config *config);
/**
* @brief Adds a file descriptor to the interest list
@@ -53,7 +53,7 @@ void cws_server_loop(int sockfd, cws_config *config);
* @param[in] sockfd The file descriptor to watch
* @param[in] events The events to follow
*/
void cws_epoll_add(int epfd, int sockfd, uint32_t events);
int cws_epoll_add(int epfd, int sockfd, uint32_t events);
/**
* @brief Removes a file descriptor from the interest list
@@ -61,14 +61,14 @@ void cws_epoll_add(int epfd, int sockfd, uint32_t events);
* @param[in] epfd epoll file descriptor
* @param[in] sockfd The file descriptor to remove
*/
void cws_epoll_del(int epfd, int sockfd);
int cws_epoll_del(int epfd, int sockfd);
/**
* @brief Makes a file descriptor non-blocking
*
* @param[in] sockfd The file descriptor to make non-blocking
*/
void cws_fd_set_nonblocking(int sockfd);
int cws_fd_set_nonblocking(int sockfd);
/**
* @brief Handles the new client
@@ -87,10 +87,6 @@ int cws_server_accept_client(int sockfd, struct sockaddr_storage *their_sa, sock
* @param[in] client_fd Client file descriptor
* @param[in] hashmap Clients hash map
*/
void cws_server_close_client(int epfd, int client_fd, cws_hashmap *hashmap);
/* Undocumented functions */
SSL_CTX *cws_ssl_create_context();
bool cws_ssl_configure(SSL_CTX *context, cws_config *config);
void cws_server_close_client(int epfd, int client_fd, mcl_hashmap *hashmap);
#endif

View File

@@ -1,26 +1,30 @@
#ifndef CWS_HASHMAP_H
#define CWS_HASHMAP_H
#ifndef MYCLIB_HASHMAP_H
#define MYCLIB_HASHMAP_H
#include <stdbool.h>
#include <stddef.h>
#define CWS_HASHMAP_SIZE 1024 /**< Number of buckets in the hash map */
#define MYCLIB_HASHMAP_SIZE 1024 /**< Number of buckets in the hash map */
/**
* @brief A single bucket in the hash map.
* @brief A single bucket in the hash map
*
* Each bucket can hold one key-value pair and points to the next bucket
* in case of hash collisions (separate chaining).
*/
typedef struct cws_bucket_t {
typedef struct mcl_bucket_t {
void *key; /**< Pointer to the key */
void *value; /**< Pointer to the value */
struct cws_bucket_t *next; /**< Pointer to the next bucket in case of collision */
} cws_bucket;
struct mcl_bucket_t *next; /**< Pointer to the next bucket in case of collision */
} mcl_bucket;
/**
* @brief Function pointer type for a hash function
*
* @param[in] key Pointer to the key to hash
* @return The computed hash as an integer
* @return The computed hash as an unsigned integer
*/
typedef int cws_hash_fn(void *key);
typedef unsigned int mcl_hash_fn(const void *key);
/**
* @brief Function pointer type for a key comparison function
@@ -29,78 +33,95 @@ typedef int cws_hash_fn(void *key);
* @param[in] key_b Pointer to the second key
* @return true if the keys are considered equal, false otherwise
*/
typedef bool cws_equal_fn(void *key_a, void *key_b);
typedef bool mcl_equal_fn(const void *key_a, const void *key_b);
/**
* @brief Function pointer type for freeing a key
*
* @param[in] key Pointer to the key to free
*/
typedef void cws_free_key_fn(void *key);
typedef void mcl_free_key_fn(void *key);
/**
* @brief Function pointer type for freeing a value
*
* @param[in] value Pointer to the value to free
*/
typedef void cws_free_value_fn(void *value);
typedef void mcl_free_value_fn(void *value);
/**
* @brief Main structure representing the hash map
*/
typedef struct cws_hashmap_t {
cws_hash_fn *hash_fn; /**< Hash function */
cws_equal_fn *equal_fn; /**< Equality comparison function */
cws_free_key_fn *free_key_fn; /**< Key deallocation function */
cws_free_value_fn *free_value_fn; /**< Value deallocation function */
cws_bucket map[CWS_HASHMAP_SIZE]; /**< Array of bucket chains */
} cws_hashmap;
/**
* @brief Initializes a new hash map with user-defined behavior
*
* @param[in] hash_fn Function used to hash keys
* @param[in] equal_fn Function used to compare keys
* @param[in] free_key_fn Function used to free keys
* @param[in] free_value_fn Function used to free values
* @return A pointer to the newly initialized hash map
* Contains function pointers for hash computation, key comparison,
* and memory management, along with the bucket array.
*/
cws_hashmap *cws_hm_init(cws_hash_fn *hash_fn, cws_equal_fn *equal_fn, cws_free_key_fn *free_key_fn, cws_free_value_fn *free_value_fn);
typedef struct mcl_hashmap_t {
mcl_hash_fn *hash_fn; /**< Hash function */
mcl_equal_fn *equal_fn; /**< Equality comparison function */
mcl_free_key_fn *free_key_fn; /**< Key deallocation function (optional) */
mcl_free_value_fn *free_value_fn; /**< Value deallocation function (optional) */
mcl_bucket map[MYCLIB_HASHMAP_SIZE]; /**< Array of bucket chains */
} mcl_hashmap;
/**
* @brief Frees all resources used by the hash map
* @brief Initialize a new hash map with user-defined behavior functions
*
* Creates a new hash map and initializes it with the provided function pointers.
* The free functions can be NULL if no automatic memory management is needed.
*
* @param[in] hash_fn Function used to hash keys (required)
* @param[in] equal_fn Function used to compare keys (required)
* @param[in] free_key_fn Function used to free keys (optional, can be NULL)
* @param[in] free_value_fn Function used to free values (optional, can be NULL)
* @return A pointer to the newly initialized hash map, or NULL on failure
*/
mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_key_fn *free_key_fn, mcl_free_value_fn *free_value_fn);
/**
* @brief Free all resources used by the hash map
*
* Iterates through all buckets, frees keys and values using the provided
* free functions (if not NULL), and deallocates the hash map structure.
*
* @param[in] hashmap Pointer to the hash map to free
*/
void cws_hm_free(cws_hashmap *hashmap);
void mcl_hm_free(mcl_hashmap *hashmap);
/**
* @brief Inserts or updates a key-value pair in the hash map
* @brief Insert or update a key-value pair in the hash map
*
* If the key already exists, the old value is freed (if free_value_fn is provided)
* and replaced with the new value. If the key doesn't exist, a new entry is created.
*
* @param[in] hashmap Pointer to the hash map
* @param[in] key Pointer to the key to insert
* @param[in] value Pointer to the value to insert
* @return true if the operation succeeded, false otherwise
* @param[in] key Pointer to the key to insert (must not be NULL)
* @param[in] value Pointer to the value to insert (can be NULL)
* @return true if the operation succeeded, false on failure (NULL hashmap/key or memory allocation failure)
*/
bool cws_hm_set(cws_hashmap *hashmap, void *key, void *value);
bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value);
/**
* @brief Retrieves a bucket by key
* @brief Retrieve a bucket by key
*
* Searches for the given key in the hash map and returns the bucket containing it.
* The caller can then access both the key and value from the returned bucket.
*
* @param[in] hashmap Pointer to the hash map
* @param[in] key Pointer to the key to search for
* @return Pointer to the found bucket, or NULL if not found
* @return Pointer to the found bucket, or NULL if not found or on invalid input
*/
cws_bucket *cws_hm_get(cws_hashmap *hashmap, void *key);
mcl_bucket *mcl_hm_get(mcl_hashmap *hashmap, void *key);
/**
* @brief Removes a key-value pair from the hash map
* @brief Remove a key-value pair from the hash map
*
* Searches for the given key and removes it from the hash map. Both the key
* and value are freed using the provided free functions (if not NULL).
*
* @param[in] hashmap Pointer to the hash map
* @param[in] key Pointer to the key to remove, this pointer will be freed so pay attention
* @return False if the key is not found, otherwise true
* @param[in] key Pointer to the key to remove
* @return true if the key was found and removed, false if not found or on invalid input
*/
bool cws_hm_remove(cws_hashmap *hashmap, void *key);
bool mcl_hm_remove(mcl_hashmap *hashmap, void *key);
#endif
#endif /* MYCLIB_HASHMAP_H */

View File

@@ -33,12 +33,12 @@ void cws_utils_get_client_ip(struct sockaddr_storage *sa, char *ip);
char *cws_strip(char *str);
/* Functions used for hash maps */
int my_str_hash_fn(void *key);
bool my_str_equal_fn(void *a, void *b);
unsigned int my_str_hash_fn(const void *key);
bool my_str_equal_fn(const void *a, const void *b);
void my_str_free_fn(void *value);
int my_int_hash_fn(void *key);
bool my_int_equal_fn(void *a, void *b);
unsigned int my_int_hash_fn(const void *key);
bool my_int_equal_fn(const void *a, const void *b);
void my_int_free_fn(void *value);
#endif