#ifndef APOS_UTILS_H
#define APOS_UTILS_H

#define MAX(a, b) ((a) >= (b) ? (a) : (b))
#define MAX3(a, b, c) (MAX(a, b) >= MAX(b, c) ? MAX(a, b) : MAX(b, c))
#define MAX4(a, b, c, d) (MAX3(a, b, c) >= MAX3(b, c, d) ? MAX3(a, b, c) : MAX3(b, c, d))
/* etc... */

#define MIN(a, b) ((a) <= (b) ? (a) : (b))
#define MIN3(a, b, c) (MIN(a, b) <= MIN(b, c) ? MIN(a, b) : MIN(b, c))
#define MIN4(a, b, c, d) (MIN3(a, b, c) <= MIN3(b, c, d) ? MIN3(a, b, c) : MIN3(b, c, d))
/* etc... */

#define GLUE2(x, y) x##y
#define GLUE(x, y) GLUE2(x, y)

#include <apos/builtin.h>

#if __has_builtin(__builtin_offsetof)
#define offsetof(type, member) __builtin_offsetof(type, member)
#else
#define offsetof(type, member) ((size_t)&((type *)0)->member)
#endif

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

#include <apos/types.h>

static inline size_t align_up(size_t val, size_t a)
{
	if(!a)
		return val;

	size_t rem = val % a;

	if (rem == 0)
		return val;

	return val + a - rem;
}

static inline size_t align_down(size_t val, size_t a)
{
	if(!a)
		return val;

	return val - (val % a);
}

#endif /* APOS_UTILS_H */
