summaryrefslogtreecommitdiffstats
path: root/include/libtf/defines.h
blob: b1d2aa978cfacc16700f3aa62d449b5bb9dcf535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* tf.h - libtf helper defines
 *
 * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 or later as
 * published by the Free Software Foundation.
 *
 * See http://www.gnu.org/ for details.
 */

#ifndef TF_DEFINES_H
#define TF_DEFINES_H

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

#ifndef NULL
#define NULL 0L
#endif

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#ifndef array_size
#define array_size(array) (sizeof(array) / sizeof((array)[0]))
#endif

#ifndef offsetof
#define offsetof(type, member)  __builtin_offsetof (type, member)
#endif

#ifndef container_of
#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})
#endif

#ifndef likely
#define likely(x)			__builtin_expect(!!(x), 1)
#endif

#ifndef unlikely
#define unlikely(x)			__builtin_expect(!!(x), 0)
#endif

#define attribute_never_inline		__attribute__((noinline))
#define attribute_weak_function		__attribute__((weak))

#define TF_BUG_ON(cond) if (unlikely(cond)) { \
	fprintf(stderr, "BUG: failure at %s:%d/%s(): %s!\n",	\
		__FILE__, __LINE__, __func__, #cond);		\
	abort();						\
}

#define TF_ALIGN(size,align)	((((size_t)(size)) + (align)-1) & ~((align)-1))

#define TF_EMPTY_ARRAY 0

#ifndef TF_STACK_SIZE
#define TF_STACK_SIZE		4096
#endif

/* Monotonic time */
typedef uint32_t tf_mtime_t;
typedef int32_t tf_mtime_diff_t;

static inline
tf_mtime_diff_t tf_mtime_diff(tf_mtime_t a, tf_mtime_t b)
{
	return (tf_mtime_diff_t)(a - b);
}

#endif