fix the ultimate bug when restarting awesome, client misdisplayed
[awesome.git] / util.h
blobc56d75631bf5249784c2d593dd8e61cf364f58f4
1 /*
2 * util.c - useful functions header
3 *
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
5 * Copyright © 2006 Pierre Habouzit <madcoder@debian.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef AWESOME_MEM_H
24 #define AWESOME_MEM_H
26 #include <string.h>
27 #include <stdlib.h>
28 #include "config.h"
30 #define ssizeof(foo) (ssize_t)sizeof(foo)
31 #define countof(foo) (ssizeof(foo) / ssizeof(foo[0]))
33 #define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count)))
34 #define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
35 #define p_realloc(pp, count) xrealloc((void*)(pp) sizeof(**(pp) * (count)))
36 #define p_dup(p, count) xmemdup((p), sizeof(*(p)) * (count))
38 #ifdef __GNUC__
40 #define p_delete(mem_pp) \
41 do { \
42 typeof(**(mem_pp)) **__ptr = (mem_pp); \
43 free(*__ptr); \
44 *__ptr = NULL; \
45 } while(0)
47 #else
49 #define p_delete(mem_p) \
50 do { \
51 void *__ptr = (mem_p); \
52 free(*__ptr); \
53 *(void **)__ptr = NULL; \
54 } while (0)
56 #endif
58 static inline void * __attribute__ ((malloc)) xmalloc(ssize_t size)
60 void *ptr;
62 if(size <= 0)
63 return NULL;
65 ptr = calloc(1, size);
67 if(!ptr)
68 abort();
70 return ptr;
73 static inline void
74 xrealloc(void **ptr, ssize_t newsize)
76 if(newsize <= 0)
77 p_delete(ptr);
78 else
80 *ptr = realloc(*ptr, newsize);
81 if(!*ptr)
82 abort();
86 static inline void *xmemdup(const void *src, ssize_t size)
88 return memcpy(xmalloc(size), src, size);
91 /** \brief \c NULL resistant strlen.
93 * Unlinke it's libc sibling, a_strlen returns a ssize_t, and supports its
94 * argument beeing NULL.
96 * \param[in] s the string.
97 * \return the string length (or 0 if \c s is \c NULL).
99 static inline ssize_t a_strlen(const char *s)
101 return s ? strlen(s) : 0;
105 /** \brief \c NULL resistant strdup.
107 * the a_strdup() function returns a pointer to a new string, which is a
108 * duplicate of \c s. Memory should be freed using p_delete().
110 * \warning when s is \c "", it returns NULL !
112 * \param[in] s the string to duplicate.
113 * \return a pointer to the duplicated string.
115 static inline char *a_strdup(const char *s)
117 ssize_t len = a_strlen(s);
118 return len ? p_dup(s, len + 1) : NULL;
121 void die(const char *, ...) __attribute__ ((noreturn)) __attribute__ ((format(printf, 1, 2)));
122 void eprint(const char *, ...) __attribute__ ((noreturn)) __attribute__ ((format(printf, 1, 2)));
123 void uicb_spawn(Display *, DC *, awesome_config *, const char *);
124 Bool xgettextprop(Display *, Window, Atom, char *, unsigned int);
125 double compute_new_value_from_arg(const char *, double);
127 #endif