librfn
An ad-hoc utility library
Data Structures | Macros | Functions
Miscellaneous utilities

Utility functions that did not fit into other modules. More...

Data Structures

struct  ratelimit_state_t
 Control structure used for rate limiting. More...
 

Macros

#define containerof(ptr, type, member)   ((type *) (((char *) ptr) - offsetof(type, member)))
 
#define lengthof(x)   ((sizeof(x) / sizeof(*(x))))
 
#define verify(x)   assert(x)
 
#define RATELIMIT_TO(n, window, fn)
 Ratelimit a single expression. More...
 
#define RATELIMIT(fn)   RATELIMIT_TO(3, 10, fn)
 Ratelimit a single expression using default ratelimiter values. More...
 

Functions

int32_t cyclecmp32 (uint32_t a, uint32_t b)
 Compares values that may be subject to overflow. More...
 
bool ratelimit_check (ratelimit_state_t *rs, uint32_t n, uint32_t window)
 Check that the rate limiter has not triggered. More...
 
void rf_internal_out_of_memory (void)
 
void * xmalloc (size_t sz)
 

Detailed Description

Utility functions that did not fit into other modules.

Macro Definition Documentation

#define containerof (   ptr,
  type,
  member 
)    ((type *) (((char *) ptr) - offsetof(type, member)))

This macro is similar to the Linux kernel's container_of() macro but doesn't use the GNU extension needed to assure type safety.

Definition at line 35 of file util.h.

#define lengthof (   x)    ((sizeof(x) / sizeof(*(x))))

Determine the number of elements in a statically allocated array.

There's no compile-time checks to this macro. Abuse it and you'll just get the wrong result!

Definition at line 44 of file util.h.

#define RATELIMIT (   fn)    RATELIMIT_TO(3, 10, fn)

Ratelimit a single expression using default ratelimiter values.

Definition at line 106 of file util.h.

#define RATELIMIT_TO (   n,
  window,
  fn 
)
Value:
{ \
static ratelimit_state_t rs_; \
if (ratelimit_check(&rs_, n, window)) \
(fn); \
}
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
Control structure used for rate limiting.
Definition: util.h:76

Ratelimit a single expression.

The expression will automatically be provides a private ratelimit_state_t;

Definition at line 96 of file util.h.

#define verify (   x)    assert(x)

Definition at line 55 of file util.h.

Function Documentation

int32_t cyclecmp32 ( uint32_t  a,
uint32_t  b 
)

Compares values that may be subject to overflow.

This comparision assumes that 0 > 0xffffffff because, in 32-bit unsigned maths, 0 == 0xffffffff + 1. This is particular useful for working with 32-bit representations of time.

Returns
>0 if a > b
0 if a == b
<0 if a < b

Definition at line 19 of file util.c.

bool ratelimit_check ( ratelimit_state_t rs,
uint32_t  n,
uint32_t  window 
)

Check that the rate limiter has not triggered.

The rate limiter will trigger if there are more than n events each window.

Returns
true, if the ratelimited activity should be performed; false otherwise.

Definition at line 24 of file util.c.

void rf_internal_out_of_memory ( void  )

Out of memory hook.

Todo:
Currently out_of_memory cannot actually be hooked

Definition at line 40 of file util.c.

void* xmalloc ( size_t  sz)

Definition at line 45 of file util.c.