]> git.sur5r.net Git - i3/i3/blob - i3bar/include/util.h
Merge pull request #3022 from orestisf1993/i3bar-leaks
[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 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  */
8 #pragma once
9
10 #include <config.h>
11
12 #include "queue.h"
13
14 /* Get the maximum/minimum of x and y */
15 #undef MAX
16 #define MAX(x, y) ((x) > (y) ? (x) : (y))
17 #undef MIN
18 #define MIN(x, y) ((x) < (y) ? (x) : (y))
19
20 #define STARTS_WITH(string, len, needle) (((len) >= strlen((needle))) && strncasecmp((string), (needle), strlen((needle))) == 0)
21
22 /* Securely free p */
23 #define FREE(p)   \
24     do {          \
25         free(p);  \
26         p = NULL; \
27     } while (0)
28
29 /* Securely free single-linked list */
30 #define FREE_SLIST(l, type)              \
31     do {                                 \
32         type *walk = SLIST_FIRST(l);     \
33         while (!SLIST_EMPTY(l)) {        \
34             SLIST_REMOVE_HEAD(l, slist); \
35             FREE(walk);                  \
36             walk = SLIST_FIRST(l);       \
37         }                                \
38     } while (0)
39
40 /* Securely free tail queue */
41 #define FREE_TAILQ(l, type)                         \
42     do {                                            \
43         type *walk = TAILQ_FIRST(l);                \
44         while (!TAILQ_EMPTY(l)) {                   \
45             TAILQ_REMOVE(l, TAILQ_FIRST(l), tailq); \
46             FREE(walk);                             \
47             walk = TAILQ_FIRST(l);                  \
48         }                                           \
49     } while (0)
50
51 #if defined(DLOG)
52 #undef DLOG
53 #endif
54 /* Use cool logging macros */
55 #define DLOG(fmt, ...)                                                 \
56     do {                                                               \
57         if (config.verbose) {                                          \
58             printf("[%s:%d] " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
59         }                                                              \
60     } while (0)
61
62 /* We will include libi3.h which define its own version of ELOG.
63  * We want *our* version, so we undef the libi3 one. */
64 #if defined(ELOG)
65 #undef ELOG
66 #endif
67 #define ELOG(fmt, ...)                                                             \
68     do {                                                                           \
69         fprintf(stderr, "[%s:%d] ERROR: " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
70     } while (0)