2 * util.c - useful functions header
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
5 * Copyright © 2006 Pierre Habouzit <madcoder@debian.org>
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.
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))
40 #define p_delete(mem_pp) \
42 typeof(**(mem_pp)) **__ptr = (mem_pp); \
49 #define p_delete(mem_p) \
51 void *__ptr = (mem_p); \
53 *(void **)__ptr = NULL; \
58 static inline void * __attribute__ ((malloc
)) xmalloc(ssize_t size
)
65 ptr
= calloc(1, size
);
74 xrealloc(void **ptr
, ssize_t newsize
)
80 *ptr
= realloc(*ptr
, newsize
);
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 eprint(const char *, ...) __attribute__ ((noreturn
)) __attribute__ ((format(printf
, 1, 2)));
122 void uicb_spawn(Display
*, DC
*, awesome_config
*, const char *);
123 Bool
xgettextprop(Display
*, Window
, Atom
, char *, unsigned int);
124 double compute_new_value_from_arg(const char *, double);