Browse Source

+ src/matches.[ch]

master
Alex 'AdUser' Z 9 years ago
parent
commit
ca21b66ddb
  1. 51
      src/matches.c
  2. 16
      src/matches.h

51
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;
}
}

16
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_ */
Loading…
Cancel
Save