l+og: add log_ERR() helpers

These helpers are exactly like log_ERRNO() but take the error code via
negative integer parameter instead of errno. They set errno and print the
error-message via %m as usual.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
ring: use "size" instead of "len" and uint8_t

We deal with raw buffers, not with characters. So use "size" instead of
"length" and uint8_t/void instead of char.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
ring: rewrite to use start+used

Don't use start+end as we don't have independent readers and writers,
anyway. This simplifies the ring-buffer significantly and also speeds up
some computations.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
macro: add header with miscellaneous helper macros

Things like min/max seem to be simple to implement, but people often make
mistakes. We provide a bunch of simple helpers which implement strict
type-checking and other sanity checks to avoid common errors.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
macro: add shl_assert_cc() compile-time asserts

We fall back to static_assert() from assert.h, based on _Static_assert()
defined by C11. We provide a default error message to simplify its usage.
Use static_assert() directly if you want more verbose messages.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
macro: add safe-multiplication helpers

Checking for overflows on additions is simple, however, for
multiplications it's non-trivial. In case we have a bigger integer type,
we can simply run the multiplication one class higher and check for an
overflow. However, for "unsigned long long" / "uint64_t" we need to use
a division to test for overflows beforehand.

It would be nice if the C language provided us with overflow register
flags. Unfortunately, there's no such thing so we're left with expensive
checks. Note that the division is horribly slow, so you should always
reconsider whether you really need this big space or whether other
heuristics could avoid most of the checks.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
util: add shl_util with initial strict atoi()

shl_util will contain miscellaneous helpers we need quite often. This
commit adds strict atoi() helpers to work around common problems with
atoi() and strtol().

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
util: add shl_greedy_realloc?()

The greedy_realloc helpers wrap calls to realloc() with an additional
size-storage. Instead of resizing to the requested value, it resizes to
the next power-of-2. This is a common technique to avoid many small
resizes but instead keep some buffer for new values.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
bug: modernize

Use the new macro helpers to simplify the buffer code and use
uint8_t+void* instead of char*.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>