c_utils.h

Go to the documentation of this file.
00001 /*
00002  *  This file is part of libc_utils.
00003  *
00004  *  libc_utils is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  libc_utils is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with libc_utils; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  */
00018 
00019 /*
00020  *  libc_utils is being developed at the Max-Planck-Institut fuer Astrophysik
00021  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00022  *  (DLR).
00023  */
00024 
00025 /*! \file c_utils.h
00026  *  Convenience functions
00027  *
00028  *  Copyright (C) 2008-2013 Max-Planck-Society
00029  *  \author Martin Reinecke
00030  *  \note This file should only be included from .c files, NOT from .h files.
00031  */
00032 
00033 #ifndef PLANCK_C_UTILS_H
00034 #define PLANCK_C_UTILS_H
00035 
00036 #include <math.h>
00037 #include <stdlib.h>
00038 #include <stddef.h>
00039 
00040 #ifdef __cplusplus
00041 extern "C" {
00042 #endif
00043 
00044 void util_fail_ (const char *file, int line, const char *func, const char *msg);
00045 void util_warn_ (const char *file, int line, const char *func, const char *msg);
00046 void *util_malloc_ (size_t sz);
00047 void util_free_ (void *ptr);
00048 
00049 #if defined (__GNUC__)
00050 #define UTIL_FUNC_NAME__ __func__
00051 #else
00052 #define UTIL_FUNC_NAME__ "unknown"
00053 #endif
00054 
00055 /*! \def UTIL_ASSERT(cond,msg)
00056     If \a cond is false, print an error message containing function name,
00057     source file name and line number of the call, as well as \a msg;
00058     then exit the program with an error status. */
00059 #define UTIL_ASSERT(cond,msg) \
00060   if(!(cond)) util_fail_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg)
00061 /*! \def UTIL_WARN(cond,msg)
00062     If \a cond is false, print an warning containing function name,
00063     source file name and line number of the call, as well as \a msg. */
00064 #define UTIL_WARN(cond,msg) \
00065   if(!(cond)) util_warn_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg)
00066 /*! \def UTIL_FAIL(msg)
00067     Print an error message containing function name,
00068     source file name and line number of the call, as well as \a msg;
00069     then exit the program with an error status. */
00070 #define UTIL_FAIL(msg) \
00071   util_fail_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg)
00072 
00073 /*! \def ALLOC(ptr,type,num)
00074     Allocate space for \a num objects of type \a type. Make sure that the
00075     allocation succeeded, else stop the program with an error. Return the
00076     resulting pointer in \a ptr. */
00077 #define ALLOC(ptr,type,num) \
00078   do { (ptr)=(type *)util_malloc_((num)*sizeof(type)); } while (0)
00079 /*! \def RALLOC(type,num)
00080     Allocate space for \a num objects of type \a type. Make sure that the
00081     allocation succeeded, else stop the program with an error. Cast the
00082     resulting pointer to \a (type*). */
00083 #define RALLOC(type,num) \
00084   ((type *)util_malloc_((num)*sizeof(type)))
00085 /*! \def DEALLOC(ptr)
00086     Deallocate \a ptr. It must have been allocated using \a ALLOC or
00087     \a RALLOC. */
00088 #define DEALLOC(ptr) \
00089   do { util_free_(ptr); (ptr)=NULL; } while(0)
00090 #define RESIZE(ptr,type,num) \
00091   do { util_free_(ptr); ALLOC(ptr,type,num); } while(0)
00092 #define GROW(ptr,type,sz_old,sz_new) \
00093   do { \
00094     if ((sz_new)>(sz_old)) \
00095       { RESIZE(ptr,type,2*(sz_new));sz_old=2*(sz_new); } \
00096   } while(0)
00097 /*! \def SET_ARRAY(ptr,i1,i2,val)
00098     Set the entries \a ptr[i1] ... \a ptr[i2-1] to \a val. */
00099 #define SET_ARRAY(ptr,i1,i2,val) \
00100   do { \
00101     ptrdiff_t cnt_; \
00102     for (cnt_=(i1);cnt_<(i2);++cnt_) (ptr)[cnt_]=(val); \
00103     } while(0)
00104 /*! \def COPY_ARRAY(src,dest,i1,i2)
00105     Copy the entries \a src[i1] ... \a src[i2-1] to
00106     \a dest[i1] ... \a dest[i2-1]. */
00107 #define COPY_ARRAY(src,dest,i1,i2) \
00108   do { \
00109     ptrdiff_t cnt_; \
00110     for (cnt_=(i1);cnt_<(i2);++cnt_) (dest)[cnt_]=(src)[cnt_]; \
00111     } while(0)
00112 
00113 #define ALLOC2D(ptr,type,num1,num2) \
00114   do { \
00115     size_t cnt_, num1_=(num1), num2_=(num2); \
00116     ALLOC((ptr),type *,num1_); \
00117     ALLOC((ptr)[0],type,num1_*num2_); \
00118     for (cnt_=1; cnt_<num1_; ++cnt_) \
00119       (ptr)[cnt_]=(ptr)[cnt_-1]+num2_; \
00120     } while(0)
00121 #define DEALLOC2D(ptr) \
00122   do { if(ptr) DEALLOC((ptr)[0]); DEALLOC(ptr); } while(0)
00123 
00124 #define FAPPROX(a,b,eps) \
00125   (fabs((a)-(b))<((eps)*fabs(b)))
00126 #define ABSAPPROX(a,b,eps) \
00127   (fabs((a)-(b))<(eps))
00128 #define IMAX(a,b) \
00129   (((a)>(b)) ? (a) : (b))
00130 #define IMIN(a,b) \
00131   (((a)<(b)) ? (a) : (b))
00132 
00133 #define SWAP(a,b,type) \
00134   do { type tmp_=(a); (a)=(b); (b)=tmp_; } while(0)
00135 
00136 #define CHECK_STACK_ALIGN(align) \
00137   do { \
00138     double foo; \
00139     UTIL_WARN((((size_t)(&foo))&(align-1))==0, \
00140       "WARNING: stack not sufficiently aligned!"); \
00141     } while(0)
00142 
00143 #ifdef __cplusplus
00144 }
00145 #endif
00146 
00147 #endif

Generated on Thu Oct 8 14:48:49 2015 for LevelS C support library