LevelS C support library
3.50
c_utils
c_utils.h
Go to the documentation of this file.
1
/*
2
* This file is part of libc_utils.
3
*
4
* libc_utils is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
8
*
9
* libc_utils is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with libc_utils; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
*/
18
19
/*
20
* libc_utils is being developed at the Max-Planck-Institut fuer Astrophysik
21
* and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
22
* (DLR).
23
*/
24
25
/*! \file c_utils.h
26
* Convenience functions
27
*
28
* Copyright (C) 2008-2017 Max-Planck-Society
29
* \author Martin Reinecke
30
* \note This file should only be included from .c files, NOT from .h files.
31
*/
32
33
#ifndef PLANCK_C_UTILS_H
34
#define PLANCK_C_UTILS_H
35
36
#include <math.h>
37
#include <stdlib.h>
38
#include <stddef.h>
39
40
#ifdef __cplusplus
41
extern
"C"
{
42
#endif
43
44
void
util_fail_ (
const
char
*file,
int
line,
const
char
*func,
const
char
*msg);
45
void
util_warn_ (
const
char
*file,
int
line,
const
char
*func,
const
char
*msg);
46
void
*util_malloc_ (
size_t
sz);
47
void
util_free_ (
void
*ptr);
48
49
#if defined (__GNUC__)
50
#define UTIL_FUNC_NAME__ __func__
51
#else
52
#define UTIL_FUNC_NAME__ "unknown"
53
#endif
54
55
/*! \def UTIL_ASSERT(cond,msg)
56
If \a cond is false, print an error message containing function name,
57
source file name and line number of the call, as well as \a msg;
58
then exit the program with an error status. */
59
#define UTIL_ASSERT(cond,msg) \
60
if(!(cond)) util_fail_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg)
61
/*! \def UTIL_WARN(cond,msg)
62
If \a cond is false, print an warning containing function name,
63
source file name and line number of the call, as well as \a msg. */
64
#define UTIL_WARN(cond,msg) \
65
if(!(cond)) util_warn_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg)
66
/*! \def UTIL_FAIL(msg)
67
Print an error message containing function name,
68
source file name and line number of the call, as well as \a msg;
69
then exit the program with an error status. */
70
#define UTIL_FAIL(msg) \
71
util_fail_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg)
72
73
/*! \def ALLOC(ptr,type,num)
74
Allocate space for \a num objects of type \a type. Make sure that the
75
allocation succeeded, else stop the program with an error. Return the
76
resulting pointer in \a ptr. */
77
#define ALLOC(ptr,type,num) \
78
do { (ptr)=(type *)util_malloc_((num)*sizeof(type)); } while (0)
79
/*! \def RALLOC(type,num)
80
Allocate space for \a num objects of type \a type. Make sure that the
81
allocation succeeded, else stop the program with an error. Cast the
82
resulting pointer to \a (type*). */
83
#define RALLOC(type,num) \
84
((type *)util_malloc_((num)*sizeof(type)))
85
/*! \def DEALLOC(ptr)
86
Deallocate \a ptr. It must have been allocated using \a ALLOC or
87
\a RALLOC. */
88
#define DEALLOC(ptr) \
89
do { util_free_(ptr); (ptr)=NULL; } while(0)
90
#define RESIZE(ptr,type,num) \
91
do { util_free_(ptr); ALLOC(ptr,type,num); } while(0)
92
#define GROW(ptr,type,sz_old,sz_new) \
93
do { \
94
if ((sz_new)>(sz_old)) \
95
{ RESIZE(ptr,type,2*(sz_new));sz_old=2*(sz_new); } \
96
} while(0)
97
/*! \def SET_ARRAY(ptr,i1,i2,val)
98
Set the entries \a ptr[i1] ... \a ptr[i2-1] to \a val. */
99
#define SET_ARRAY(ptr,i1,i2,val) \
100
do { \
101
ptrdiff_t cnt_; \
102
for (cnt_=(i1);cnt_<(i2);++cnt_) (ptr)[cnt_]=(val); \
103
} while(0)
104
/*! \def COPY_ARRAY(src,dest,i1,i2)
105
Copy the entries \a src[i1] ... \a src[i2-1] to
106
\a dest[i1] ... \a dest[i2-1]. */
107
#define COPY_ARRAY(src,dest,i1,i2) \
108
do { \
109
ptrdiff_t cnt_; \
110
for (cnt_=(i1);cnt_<(i2);++cnt_) (dest)[cnt_]=(src)[cnt_]; \
111
} while(0)
112
113
#define ALLOC2D(ptr,type,num1,num2) \
114
do { \
115
size_t cnt_, num1_=(num1), num2_=(num2); \
116
ALLOC((ptr),type *,num1_); \
117
ALLOC((ptr)[0],type,num1_*num2_); \
118
for (cnt_=1; cnt_<num1_; ++cnt_) \
119
(ptr)[cnt_]=(ptr)[cnt_-1]+num2_; \
120
} while(0)
121
#define DEALLOC2D(ptr) \
122
do { if(ptr) DEALLOC((ptr)[0]); DEALLOC(ptr); } while(0)
123
124
#define FAPPROX(a,b,eps) \
125
(fabs((a)-(b))<((eps)*fabs(b)))
126
#define ABSAPPROX(a,b,eps) \
127
(fabs((a)-(b))<(eps))
128
#define IMAX(a,b) \
129
(((a)>(b)) ? (a) : (b))
130
#define IMIN(a,b) \
131
(((a)<(b)) ? (a) : (b))
132
133
#define SWAP(a,b,type) \
134
do { type tmp_=(a); (a)=(b); (b)=tmp_; } while(0)
135
136
#define CHECK_STACK_ALIGN(align) \
137
do { \
138
double foo; \
139
UTIL_WARN((((size_t)(&foo))&(align-1))==0, \
140
"WARNING: stack not sufficiently aligned!"); \
141
} while(0)
142
143
#ifdef __cplusplus
144
}
145
#endif
146
147
#ifdef __GNUC__
148
#define NOINLINE __attribute__((noinline))
149
#else
150
#define NOINLINE
151
#endif
152
153
#endif
Generated on Mon Dec 10 2018 10:24:19 for LevelS C support library