create a Tag struct type and use it
[awesome.git] / util.h
blobea0b3750a67926c91a607a6a09ae0cda7b6c38cd
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 "common.h"
30 /** \brief replace \c NULL strings with emtpy strings */
31 #define NONULL(x) (x ? x : "")
33 #define ssizeof(foo) (ssize_t)sizeof(foo)
34 #define countof(foo) (ssizeof(foo) / ssizeof(foo[0]))
36 #define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count)))
37 #define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count)))
38 #define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count))
39 #define p_dup(p, count) xmemdup((p), sizeof(*(p)) * (count))
41 #ifdef __GNUC__
43 #define p_delete(mem_pp) \
44 do { \
45 typeof(**(mem_pp)) **__ptr = (mem_pp); \
46 free(*__ptr); \
47 *__ptr = NULL; \
48 } while(0)
50 #else
52 #define p_delete(mem_p) \
53 do { \
54 void *__ptr = (mem_p); \
55 free(*__ptr); \
56 *(void **)__ptr = NULL; \
57 } while (0)
59 #endif
61 static inline void * __attribute__ ((malloc)) xmalloc(ssize_t size)
63 void *ptr;
65 if(size <= 0)
66 return NULL;
68 ptr = calloc(1, size);
70 if(!ptr)
71 abort();
73 return ptr;
76 static inline void
77 xrealloc(void **ptr, ssize_t newsize)
79 if(newsize <= 0)
80 p_delete(ptr);
81 else
83 *ptr = realloc(*ptr, newsize);
84 if(!*ptr)
85 abort();
89 static inline void *xmemdup(const void *src, ssize_t size)
91 return memcpy(xmalloc(size), src, size);
94 /** \brief \c NULL resistant strlen.
96 * Unlinke it's libc sibling, a_strlen returns a ssize_t, and supports its
97 * argument beeing NULL.
99 * \param[in] s the string.
100 * \return the string length (or 0 if \c s is \c NULL).
102 static inline ssize_t a_strlen(const char *s)
104 return s ? strlen(s) : 0;
108 /** \brief \c NULL resistant strdup.
110 * the a_strdup() function returns a pointer to a new string, which is a
111 * duplicate of \c s. Memory should be freed using p_delete().
113 * \warning when s is \c "", it returns NULL !
115 * \param[in] s the string to duplicate.
116 * \return a pointer to the duplicated string.
118 static inline char *a_strdup(const char *s)
120 ssize_t len = a_strlen(s);
121 return len ? p_dup(s, len + 1) : NULL;
124 /** \brief \c NULL resistant strcmp.
125 * \param[in] a the first string.
126 * \param[in] b the second string.
127 * \return <tt>strcmp(a, b)</tt>, and treats \c NULL strings like \c ""
128 * ones.
130 static inline int a_strcmp(const char *a, const char *b)
132 return strcmp(NONULL(a), NONULL(b));
135 void die(const char *, ...) __attribute__ ((noreturn)) __attribute__ ((format(printf, 1, 2)));
136 void eprint(const char *, ...) __attribute__ ((noreturn)) __attribute__ ((format(printf, 1, 2)));
137 Bool xgettextprop(Display *, Window, Atom, char *, unsigned int);
138 double compute_new_value_from_arg(const char *, double);
140 UICB_PROTO(uicb_spawn);
141 #endif