librfn
An ad-hoc utility library
util.c
Go to the documentation of this file.
1 /*
2  * util.c
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * Copyright (C) 2012, 2014 Daniel Thompson <daniel@redfelineninja.org.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #include <stdlib.h>
15 #include <stdio.h>
16 
17 #include "librfn.h"
18 
19 int32_t cyclecmp32(uint32_t a, uint32_t b)
20 {
21  return (int32_t) (a - b);
22 }
23 
24 bool ratelimit_check(ratelimit_state_t *rs, uint32_t n, uint32_t window)
25 {
26  int32_t delta = (int32_t) (rs->time - time_now());
27  if (delta < 0 || delta > (signed) (window * 1000000)) {
28  rs->time = time_now() + (window * 1000000);
29  rs->count = 1;
30  return true;
31  }
32 
33  return (rs->count++) < n;
34 }
35 
41 {
42  abort();
43 }
44 
45 void *xmalloc(size_t sz)
46 {
47  void *p = malloc(sz);
48  if (!p)
50  return p;
51 }
struct alpha a
uint32_t time
Definition: util.h:77
void * xmalloc(size_t sz)
Definition: util.c:45
struct bravo b
bool ratelimit_check(ratelimit_state_t *rs, uint32_t n, uint32_t window)
Check that the rate limiter has not triggered.
Definition: util.c:24
int32_t cyclecmp32(uint32_t a, uint32_t b)
Compares values that may be subject to overflow.
Definition: util.c:19
Control structure used for rate limiting.
Definition: util.h:76
uint32_t time_now(void)
void rf_internal_out_of_memory(void)
Definition: util.c:40
uint32_t count
Definition: util.h:78