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