diff --git a/src/matches.c b/src/matches.c new file mode 100644 index 0000000..b1fae2a --- /dev/null +++ b/src/matches.c @@ -0,0 +1,51 @@ +#include "common.h" +#include "matches.h" + +bool +f2b_matches_create(f2b_matches_t *m, size_t max) { + assert(m != NULL); + assert(max != 0); + + if ((m->times = calloc(max, sizeof(time_t))) == NULL) + return false; + + m->used = 0; + m->max = max; + return true; +} + +void +f2b_matches_destroy(f2b_matches_t *m) { + assert(m != NULL); + + FREE(m->times); + m->used = 0; + m->max = 0; +} + +bool +f2b_matches_append(f2b_matches_t *m, time_t t) { + assert(m != NULL); + + if (m->used >= m->max) + return false; + + m->times[m->used] = t; + m->used++; + return true; +} + +void +f2b_matches_expire(f2b_matches_t *m, time_t t) { + assert(m != NULL); + + for (size_t i = 0; i < m->used; ) { + if (m->times[i] > t) { + i++; + continue; + } + m->used--; + m->times[i] = m->times[m->used]; + m->times[m->used] = 0; + } +} diff --git a/src/matches.h b/src/matches.h new file mode 100644 index 0000000..89f3de0 --- /dev/null +++ b/src/matches.h @@ -0,0 +1,16 @@ +#ifndef F2B_MATCHES_H_ +#define F2B_MATCHES_H_ + +typedef struct { + size_t max; + size_t used; + time_t *times; +} f2b_matches_t; + +bool f2b_matches_create (f2b_matches_t *m, size_t max); +void f2b_matches_destroy(f2b_matches_t *m); + +bool f2b_matches_append (f2b_matches_t *m, time_t t); +void f2b_matches_expire (f2b_matches_t *m, time_t t); + +#endif /* F2B_MATCHES_H_ */