]> git.sur5r.net Git - i3/i3/blob - i3bar/include/util.h
Merge branch 'next'
[i3/i3] / i3bar / include / util.h
1 /*
2  * i3bar - an xcb-based status- and ws-bar for i3
3  *
4  * © 2010-2011 Axel Wagner and contributors
5  *
6  * See file LICNSE for license information
7  *
8  */
9 #ifndef UTIL_H_
10 #define UTIL_H_
11
12 #include "queue.h"
13
14 /* Get the maximum/minimum of x and y */
15 #define MAX(x,y) ((x) > (y) ? (x) : (y))
16 #define MIN(x,y) ((x) < (y) ? (x) : (y))
17
18 /* Securely free p */
19 #define FREE(p) do { \
20     if (p != NULL) { \
21         free(p); \
22         p = NULL; \
23     } \
24 } while (0)
25
26 /* Securely fee single-linked list */
27 #define FREE_SLIST(l, type) do { \
28     type *walk = SLIST_FIRST(l); \
29     while (!SLIST_EMPTY(l)) { \
30         SLIST_REMOVE_HEAD(l, slist); \
31         FREE(walk); \
32         walk = SLIST_FIRST(l); \
33     } \
34 } while (0)
35
36 #endif
37
38 /* Securely fee tail-queues */
39 #define FREE_TAILQ(l, type) do { \
40     type *walk = TAILQ_FIRST(l); \
41     while (!TAILQ_EMPTY(l)) { \
42         TAILQ_REMOVE(l, TAILQ_FIRST(l), tailq); \
43         FREE(walk); \
44         walk = TAILQ_FIRST(l); \
45     } \
46 } while (0)
47
48 /* Use cool logging-macros */
49 #define DLOG(fmt, ...) do { \
50     if (config.verbose) { \
51         printf("[%s:%d] " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
52     } \
53 } while(0)
54
55 #define ELOG(fmt, ...) do { \
56     fprintf(stderr, "[%s:%d] ERROR: " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
57 } while(0)