Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Peach_AudioChannelDividerAndCompensator
c_utils.h
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, 2009, 2010 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 #define UTIL_ASSERT(cond,msg) \ 00056 if(!(cond)) util_fail_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg) 00057 #define UTIL_WARN(cond,msg) \ 00058 if(!(cond)) util_warn_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg) 00059 #define UTIL_FAIL(msg) \ 00060 util_fail_(__FILE__,__LINE__,UTIL_FUNC_NAME__,msg) 00061 00062 #define ALLOC(ptr,type,num) \ 00063 do { (ptr)=(type *)util_malloc_((num)*sizeof(type)); } while (0) 00064 #define RALLOC(type,num) \ 00065 ((type *)util_malloc_((num)*sizeof(type))) 00066 #define DEALLOC(ptr) \ 00067 do { util_free_(ptr); (ptr)=NULL; } while(0) 00068 #define RESIZE(ptr,type,num) \ 00069 do { util_free_(ptr); ALLOC(ptr,type,num); } while(0) 00070 #define REALLOC(ptr,type,num) \ 00071 do { \ 00072 ptr = (type *)realloc(ptr,(num)*sizeof(type)); \ 00073 UTIL_ASSERT(ptr,"realloc() failed"); \ 00074 } while(0) 00075 #define GROW(ptr,type,sz_old,sz_new) \ 00076 do { \ 00077 if ((sz_new)>(sz_old)) \ 00078 { RESIZE(ptr,type,2*(sz_new));sz_old=2*(sz_new); } \ 00079 } while(0) 00080 #define SET_ARRAY(ptr,i1,i2,val) \ 00081 do { \ 00082 ptrdiff_t cnt_; \ 00083 for (cnt_=(i1);cnt_<(i2);++cnt_) (ptr)[cnt_]=(val); \ 00084 } while(0) 00085 #define COPY_ARRAY(src,dest,i1,i2) \ 00086 do { \ 00087 ptrdiff_t cnt_; \ 00088 for (cnt_=(i1);cnt_<(i2);++cnt_) (dest)[cnt_]=(src)[cnt_]; \ 00089 } while(0) 00090 00091 #define ALLOC2D(ptr,type,num1,num2) \ 00092 do { \ 00093 size_t cnt_, num1_=(num1), num2_=(num2); \ 00094 ALLOC(ptr,type *,num1_); \ 00095 ALLOC(ptr[0],type,num1_*num2_); \ 00096 for (cnt_=1; cnt_<num1_; ++cnt_) \ 00097 ptr[cnt_]=ptr[cnt_-1]+num2_; \ 00098 } while(0) 00099 #define DEALLOC2D(ptr) \ 00100 do { if(ptr) DEALLOC((ptr)[0]); DEALLOC(ptr); } while(0) 00101 00102 #define FAPPROX(a,b,eps) \ 00103 (fabs((a)-(b))<((eps)*fabs(b))) 00104 #define ABSAPPROX(a,b,eps) \ 00105 (fabs((a)-(b))<(eps)) 00106 #define IMAX(a,b) \ 00107 (((a)>(b)) ? (a) : (b)) 00108 #define IMIN(a,b) \ 00109 (((a)<(b)) ? (a) : (b)) 00110 00111 #define SWAP(a,b,type) \ 00112 do { type tmp_=(a); (a)=(b); (b)=tmp_; } while(0) 00113 00114 #define CHECK_STACK_ALIGN(align) \ 00115 do { \ 00116 FLOAT foo; \ 00117 UTIL_WARN((((size_t)(&foo))&(align-1))==0, \ 00118 "WARNING: stack not sufficiently aligned!"); \ 00119 } while(0) 00120 00121 #ifdef __cplusplus 00122 } 00123 #endif 00124 00125 #endif
Generated on Mon Jul 18 2022 23:30:05 by
1.7.2