~ubuntu-branches/ubuntu/vivid/libspectre/vivid-proposed

« back to all changes in this revision

Viewing changes to libspectre/spectre-utils.h

  • Committer: Bazaar Package Importer
  • Author(s): Matthew Rosewarne
  • Date: 2008-01-06 21:58:55 UTC
  • Revision ID: james.westby@ubuntu.com-20080106215855-zmmkfhf4m3190zsj
Tags: upstream-0.2.0.ds
ImportĀ upstreamĀ versionĀ 0.2.0.ds

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Libspectre.
 
2
 * 
 
3
 * Copyright (C) 2007 Albert Astals Cid <aacid@kde.org>
 
4
 * Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
 
5
 *
 
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)
 
9
 * any later version.
 
10
 *
 
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.
 
15
 *
 
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.
 
19
 */
 
20
 
 
21
#ifndef SPECTRE_UTILS_H
 
22
#define SPECTRE_UTILS_H
 
23
 
 
24
#include <config.h>
 
25
 
 
26
#include <stdarg.h>
 
27
 
 
28
#include <libspectre/spectre-macros.h>
 
29
 
 
30
SPECTRE_BEGIN_DECLS
 
31
 
 
32
/* Checks. Based on dbus-internals */
 
33
void _spectre_warn               (const char *format,
 
34
                                  ...);
 
35
void _spectre_warn_check_failed  (const char *format,
 
36
                                  ...);
 
37
 
 
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__
 
42
#else
 
43
#define _SPECTRE_FUNCTION_NAME "unknown function"
 
44
#endif
 
45
 
 
46
/*
 
47
 * (code from GLib)
 
48
 * 
 
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.
 
52
 *
 
53
 * The _SPECTRE_BOOLEAN_EXPR macro is intended to trigger a gcc warning when
 
54
 * putting assignments in the macro arg
 
55
 */
 
56
#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
 
57
#define _SPECTRE_BOOLEAN_EXPR(expr)                \
 
58
        __extension__ ({                           \
 
59
                int _spectre_boolean_var_;         \
 
60
                if (expr)                          \
 
61
                        _spectre_boolean_var_ = 1; \
 
62
                else                               \
 
63
                        _spectre_boolean_var_ = 0; \
 
64
                _spectre_boolean_var_;             \
 
65
        })
 
66
#define _SPECTRE_LIKELY(expr) (__builtin_expect (_SPECTRE_BOOLEAN_EXPR(expr), 1))
 
67
#define _SPECTRE_UNLIKELY(expr) (__builtin_expect (_SPECTRE_BOOLEAN_EXPR(expr), 0))
 
68
#else
 
69
#define _SPECTRE_LIKELY(expr) (expr)
 
70
#define _SPECTRE_UNLIKELY(expr) (expr)
 
71
#endif
 
72
 
 
73
 
 
74
#ifdef SPECTRE_DISABLE_ASSERT
 
75
#define _spectre_assert(condition)
 
76
#else
 
77
void _spectre_real_assert (int          condition,
 
78
                           const char  *condition_text,
 
79
                           const char  *file,
 
80
                           int          line,
 
81
                           const char  *func);
 
82
#define _spectre_assert(condition)                                         \
 
83
        _spectre_real_assert ((condition) != 0, #condition, __FILE__, __LINE__, _SPECTRE_FUNCTION_NAME)
 
84
#endif /* SPECTRE_DISABLE_ASSERT */
 
85
 
 
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) != '_');                             \
 
92
        if (!(condition)) {                                                                          \
 
93
                _spectre_warn_check_failed ("%s: assertion `%s' failed (%s:%d)\n",                   \
 
94
                                            _SPECTRE_FUNCTION_NAME, #condition, __FILE__, __LINE__); \
 
95
                return;                                                                              \
 
96
        } } while (0)
 
97
 
 
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__); \
 
103
                return (val);                                                                        \
 
104
        } } while (0)
 
105
#endif /* SPECTRE_DISABLE_CHECKS */
 
106
 
 
107
/* String handling helpers */
 
108
char *_spectre_strdup_printf (const char *format,
 
109
                              ...);
 
110
char *_spectre_strdup        (const char *str);
 
111
int   _spectre_strncasecmp   (const char *s1,
 
112
                              const char *s2,
 
113
                              size_t      n);
 
114
int   _spectre_strcasecmp    (const char *s1,
 
115
                              const char *s2);
 
116
 
 
117
SPECTRE_END_DECLS
 
118
 
 
119
#endif /* SPECTRE_UTILS_H */
 
120