summaryrefslogtreecommitdiff
path: root/libspectre/spectre-utils.h
blob: 3ed2da88aeeecebd7be60b79d60fc6e2714ab1c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* This file is part of Libspectre.
 * 
 * Copyright (C) 2007 Albert Astals Cid <aacid@kde.org>
 * Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
 *
 * Libspectre is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * Libspectre is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef SPECTRE_UTILS_H
#define SPECTRE_UTILS_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdarg.h>

#include <libspectre/spectre-macros.h>

SPECTRE_BEGIN_DECLS

/* Checks. Based on dbus-internals */
void _spectre_warn               (const char *format,
				  ...);
void _spectre_warn_check_failed  (const char *format,
				  ...);

#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
#define _SPECTRE_FUNCTION_NAME __func__
#elif defined(__GNUC__) || defined(_MSC_VER)
#define _SPECTRE_FUNCTION_NAME __FUNCTION__
#else
#define _SPECTRE_FUNCTION_NAME "unknown function"
#endif

/* Define SPECTRE_VA_COPY() to do the right thing for copying va_list variables. 
 * config.h may have already defined SPECTRE_VA_COPY as va_copy or __va_copy. 
 */
#if !defined (SPECTRE_VA_COPY)
#  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
#    define SPECTRE_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
#  elif defined (SPECTRE_VA_COPY_AS_ARRAY)
#    define SPECTRE_VA_COPY(ap1, ap2)   memcpy ((ap1), (ap2), sizeof (va_list))
#  else /* va_list is a pointer */
#    define SPECTRE_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
#  endif /* va_list is a pointer */
#endif /* !SPECTRE_VA_COPY */


/*
 * (code from GLib)
 * 
 * The _SPECTRE_LIKELY and _SPECTRE_UNLIKELY macros let the programmer give hints to 
 * the compiler about the expected result of an expression. Some compilers
 * can use this information for optimizations.
 *
 * The _SPECTRE_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
 * putting assignments in the macro arg
 */
#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
#define _SPECTRE_BOOLEAN_EXPR(expr)                \
	__extension__ ({			   \
		int _spectre_boolean_var_;	   \
		if (expr)		           \
			_spectre_boolean_var_ = 1; \
		else				   \
			_spectre_boolean_var_ = 0; \
		_spectre_boolean_var_;		   \
	})
#define _SPECTRE_LIKELY(expr) (__builtin_expect (_SPECTRE_BOOLEAN_EXPR(expr), 1))
#define _SPECTRE_UNLIKELY(expr) (__builtin_expect (_SPECTRE_BOOLEAN_EXPR(expr), 0))
#else
#define _SPECTRE_LIKELY(expr) (expr)
#define _SPECTRE_UNLIKELY(expr) (expr)
#endif


#ifdef SPECTRE_DISABLE_ASSERT
#define _spectre_assert(condition)
#else
void _spectre_real_assert (int          condition,
			   const char  *condition_text,
			   const char  *file,
			   int          line,
			   const char  *func);
#define _spectre_assert(condition)                                         \
	_spectre_real_assert ((condition) != 0, #condition, __FILE__, __LINE__, _SPECTRE_FUNCTION_NAME)
#endif /* SPECTRE_DISABLE_ASSERT */

#ifdef SPECTRE_DISABLE_CHECKS
#define _spectre_return_if_fail(condition)
#define _spectre_return_val_if_fail(condition, val)
#else /* SPECTRE_DISABLE_CHECKS */
#define _spectre_return_if_fail(condition) do {				                             \
	_spectre_assert ((*(const char*)_SPECTRE_FUNCTION_NAME) != '_');                             \
	if (!(condition)) {                                                                          \
		_spectre_warn_check_failed ("%s: assertion `%s' failed (%s:%d)\n",                   \
					    _SPECTRE_FUNCTION_NAME, #condition, __FILE__, __LINE__); \
		return;                                                                              \
	} } while (0)

#define _spectre_return_val_if_fail(condition, val) do {                                             \
	_spectre_assert ((*(const char*)_SPECTRE_FUNCTION_NAME) != '_');                             \
	if (!(condition)) {                                                                          \
		_spectre_warn_check_failed ("%s: assertion `%s' failed (%s:%d)\n",                   \
					    _SPECTRE_FUNCTION_NAME, #condition, __FILE__, __LINE__); \
		return (val);                                                                        \
	} } while (0)
#endif /* SPECTRE_DISABLE_CHECKS */

/* String handling helpers */
char  *_spectre_strdup_printf (const char *format,
			       ...);
char  *_spectre_strdup        (const char *str);
int    _spectre_strncasecmp   (const char *s1,
			       const char *s2,
			       size_t      n);
int    _spectre_strcasecmp    (const char *s1,
			       const char *s2);
double _spectre_strtod        (const char *nptr,
			       char      **endptr);

SPECTRE_END_DECLS

#endif /* SPECTRE_UTILS_H */