1
/* This file is part of Libspectre.
3
* Copyright (C) 2007 Albert Astals Cid <aacid@kde.org>
4
* Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
6
* Libspectre is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2, or (at your option)
11
* Libspectre is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#ifndef SPECTRE_UTILS_H
22
#define SPECTRE_UTILS_H
28
#include <libspectre/spectre-macros.h>
32
/* Checks. Based on dbus-internals */
33
void _spectre_warn (const char *format,
35
void _spectre_warn_check_failed (const char *format,
38
#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
39
#define _SPECTRE_FUNCTION_NAME __func__
40
#elif defined(__GNUC__) || defined(_MSC_VER)
41
#define _SPECTRE_FUNCTION_NAME __FUNCTION__
43
#define _SPECTRE_FUNCTION_NAME "unknown function"
49
* The _SPECTRE_LIKELY and _SPECTRE_UNLIKELY macros let the programmer give hints to
50
* the compiler about the expected result of an expression. Some compilers
51
* can use this information for optimizations.
53
* The _SPECTRE_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
54
* putting assignments in the macro arg
56
#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
57
#define _SPECTRE_BOOLEAN_EXPR(expr) \
59
int _spectre_boolean_var_; \
61
_spectre_boolean_var_ = 1; \
63
_spectre_boolean_var_ = 0; \
64
_spectre_boolean_var_; \
66
#define _SPECTRE_LIKELY(expr) (__builtin_expect (_SPECTRE_BOOLEAN_EXPR(expr), 1))
67
#define _SPECTRE_UNLIKELY(expr) (__builtin_expect (_SPECTRE_BOOLEAN_EXPR(expr), 0))
69
#define _SPECTRE_LIKELY(expr) (expr)
70
#define _SPECTRE_UNLIKELY(expr) (expr)
74
#ifdef SPECTRE_DISABLE_ASSERT
75
#define _spectre_assert(condition)
77
void _spectre_real_assert (int condition,
78
const char *condition_text,
82
#define _spectre_assert(condition) \
83
_spectre_real_assert ((condition) != 0, #condition, __FILE__, __LINE__, _SPECTRE_FUNCTION_NAME)
84
#endif /* SPECTRE_DISABLE_ASSERT */
86
#ifdef SPECTRE_DISABLE_CHECKS
87
#define _spectre_return_if_fail(condition)
88
#define _spectre_return_val_if_fail(condition, val)
89
#else /* SPECTRE_DISABLE_CHECKS */
90
#define _spectre_return_if_fail(condition) do { \
91
_spectre_assert ((*(const char*)_SPECTRE_FUNCTION_NAME) != '_'); \
93
_spectre_warn_check_failed ("%s: assertion `%s' failed (%s:%d)\n", \
94
_SPECTRE_FUNCTION_NAME, #condition, __FILE__, __LINE__); \
98
#define _spectre_return_val_if_fail(condition, val) do { \
99
_spectre_assert ((*(const char*)_SPECTRE_FUNCTION_NAME) != '_'); \
100
if (!(condition)) { \
101
_spectre_warn_check_failed ("%s: assertion `%s' failed (%s:%d)\n", \
102
_SPECTRE_FUNCTION_NAME, #condition, __FILE__, __LINE__); \
105
#endif /* SPECTRE_DISABLE_CHECKS */
107
/* String handling helpers */
108
char *_spectre_strdup_printf (const char *format,
110
char *_spectre_strdup (const char *str);
111
int _spectre_strncasecmp (const char *s1,
114
int _spectre_strcasecmp (const char *s1,
119
#endif /* SPECTRE_UTILS_H */