trafilo 0.1.0
Streaming event-handler framework in C
Loading...
Searching...
No Matches
hashmap.h
Go to the documentation of this file.
1#ifndef HASHMAP_H
2#define HASHMAP_H
3
4#include <pthread.h>
5#include <time.h>
6#include <stdlib.h>
7#include <string.h>
8#include "window.h"
10
14typedef struct bucket_node {
15 char *key; /* Bucket owned key*/
16 void *state; /* User defined state */
17 sliding_window_t *window; /* An embedded sliding window */
18
20 struct timespec last_event_ts;
22
26typedef struct hashmap_t {
27 bucket_node **buckets; /* Array of chain buckets */
28 pthread_mutex_t *locks; /* Array of per bucket locks*/
29 size_t num_buckets; /* Number of buckets in hashmap */
30} hashmap_t;
31
37hashmap_t *hashmap_create(size_t num_buckets);
38
45bucket_node *hashmap_find_or_create(hashmap_t *hashmap, const char *key);
46
52void hashmap_destroy(hashmap_t *hashmap, trafilo_state_free_fn state_free);
53
60void hashmap_for_each(hashmap_t *hashmap,
61 void (*fn)(bucket_node *bucket, void *arg),
62 void *arg);
63
69void hashmap_unlock_bucket(hashmap_t *hashmap, const char *key);
70
71#endif
void hashmap_destroy(hashmap_t *hashmap, trafilo_state_free_fn state_free)
Graceful cleanup of the hashmap.
Definition hashmap.c:106
void hashmap_for_each(hashmap_t *hashmap, void(*fn)(bucket_node *bucket, void *arg), void *arg)
For each iteration for operations, calls passed function within lock protection.
Definition hashmap.c:138
hashmap_t * hashmap_create(size_t num_buckets)
Create a hashmap.
Definition hashmap.c:5
void hashmap_unlock_bucket(hashmap_t *hashmap, const char *key)
Helper to unlock a locked node.
Definition hashmap.c:154
bucket_node * hashmap_find_or_create(hashmap_t *hashmap, const char *key)
Find bucket for key; create if absent. Returns with bucket->bucket_lock LOCKED.
Definition hashmap.c:76
A bucket node of the hashtable.
Definition hashmap.h:14
void * state
Definition hashmap.h:16
struct timespec last_event_ts
Definition hashmap.h:20
sliding_window_t * window
Definition hashmap.h:17
char * key
Definition hashmap.h:15
struct bucket_node * next
Definition hashmap.h:19
The hashmap structure.
Definition hashmap.h:26
size_t num_buckets
Definition hashmap.h:29
pthread_mutex_t * locks
Definition hashmap.h:28
bucket_node ** buckets
Definition hashmap.h:27
A sliding sampling window represented as a linked list of timestamps.
Definition window.h:19
void(* trafilo_state_free_fn)(void *user_state)
Free the user defined state, when a bucket is evicted.
Definition trafilo.h:76