rename hashmap
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#ifndef __HASHMAP_C__
|
||||
#define __HASHMAP_C__
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
@@ -8,11 +9,12 @@
|
||||
/* Each process on Linux can have a maximum of 1024 open file descriptors */
|
||||
#define HASHMAP_MAX_CLIENTS 1024
|
||||
|
||||
struct hashmap {
|
||||
int sockfd;
|
||||
struct sockaddr_storage sas;
|
||||
struct hashmap *next;
|
||||
};
|
||||
typedef struct bucket {
|
||||
int sockfd; /**< Client socket descriptor */
|
||||
struct sockaddr_storage sas; /**< Associated socket address */
|
||||
struct bucket *next; /**< Next node in case of collision */
|
||||
struct bucket *prev; /**< Previous node in case of collision */
|
||||
} bucket_t;
|
||||
|
||||
/**
|
||||
* @brief Calculates the hash code of a given file descriptor
|
||||
@@ -25,35 +27,60 @@ int hash(int sockfd);
|
||||
/**
|
||||
* @brief Initializes the hash map
|
||||
*
|
||||
* @param map[out] The hash map uninitialized
|
||||
* @param bucket[out] The hash map uninitialized
|
||||
*/
|
||||
void hm_init(struct hashmap *map);
|
||||
void hm_init(bucket_t *bucket);
|
||||
|
||||
/**
|
||||
* @brief Inserts a key in the hash map
|
||||
*
|
||||
* @param map[out] The hash map
|
||||
* @param bucket[out] The hash map
|
||||
* @param sockfd[in] The file descriptor (value)
|
||||
* @param sas[in] The sockaddr (value)
|
||||
*/
|
||||
void hm_push(struct hashmap *map, int sockfd, struct sockaddr_storage *sas);
|
||||
void hm_push(bucket_t *bucket, int sockfd, struct sockaddr_storage *sas);
|
||||
|
||||
/**
|
||||
* @brief Removes a key from the hash map
|
||||
*
|
||||
* @param bucket[out] The hash map
|
||||
* @param sockfd[in] The key
|
||||
*/
|
||||
void hm_remove(bucket_t *bucket, int sockfd);
|
||||
|
||||
/**
|
||||
* @brief Searches for a key in the hash map
|
||||
*
|
||||
* @param map[in] The hash map
|
||||
* @param bucket[in] The hash map
|
||||
* @param sockfd[in] The file descriptor (key)
|
||||
* @return struct hashmap* Returns NULL or the key pointer
|
||||
*/
|
||||
struct hashmap *hm_lookup(struct hashmap *map, int sockfd);
|
||||
bucket_t *hm_lookup(bucket_t *bucket, int sockfd);
|
||||
|
||||
/**
|
||||
* @brief Cleans the hash map
|
||||
*
|
||||
* @param map[out] The hash map
|
||||
* @param bucket[out] The hash map
|
||||
*/
|
||||
void hm_free(struct hashmap *map);
|
||||
void hm_free(bucket_t *bucket);
|
||||
|
||||
void hm_insert(struct hashmap *map, int sockfd, struct sockaddr_storage *sas);
|
||||
/**
|
||||
* @brief Checks if a file descriptor is in the bucket array (not linked list)
|
||||
*
|
||||
* @param bucket[in]
|
||||
* @param sockfd[in]
|
||||
* @return true If the file descriptor is in the bucket array
|
||||
* @return false If the file descriptor is not in the bucket array (check with hm_lookup())
|
||||
*/
|
||||
bool hm_is_in_bucket_array(bucket_t *bucket, int sockfd);
|
||||
|
||||
/**
|
||||
* @brief This function will add a key even if it exists (use hm_push() instead)
|
||||
*
|
||||
* @param bucket
|
||||
* @param sockfd
|
||||
* @param sas
|
||||
*/
|
||||
void hm_insert(bucket_t *bucket, int sockfd, struct sockaddr_storage *sas);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user