00001 /***************************************************************************** 00002 * Copyright (C) 2002, F. Hoffmann-La Roche & Co., AG, Basel, Switzerland. * 00003 * * 00004 * This file is part of "Roche Bioinformatics Software Objects and Services" * 00005 * * 00006 * This file is free software; you can redistribute it and/or * 00007 * modify it under the terms of the GNU Lesser General Public * 00008 * License as published by the Free Software Foundation; either * 00009 * version 2.1 of the License, or (at your option) any later version. * 00010 * * 00011 * This file is distributed in the hope that it will be useful, * 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00014 * Lesser General Public License for more details. * 00015 * * 00016 * To obtain a copy of the GNU Lesser General Public License * 00017 * please write to the Free Software * 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 00019 * or visit the WWW site http://www.gnu.org/copyleft/lesser.txt * 00020 * * 00021 * SCOPE: this licence applies to this file. Other files of the * 00022 * "Roche Bioinformatics Software Objects and Services" may be * 00023 * subject to other licences. * 00024 * * 00025 * CONTACT: clemens.broger@roche.com or detlef.wolf@roche.com * 00026 * * 00027 *****************************************************************************/ 00028 00036 #ifndef DEF_hlrmisc_H 00037 #define DEF_hlrmisc_H 00038 00039 00040 00041 /* miscellaneous functions not fitting in any other module */ 00042 00043 /* declarations for free, malloc, strdup, realloc */ 00044 #include <stdlib.h> 00045 #include <string.h> 00046 00047 /* ---- private stuff --- do not call or access from your progs ---- */ 00048 /* private */ extern char *hlr_strmcpyI(char *to, char *from, int toLength) ; 00049 /* private */ extern char *hlr_strcpysFunc(char *to, char *from, int toLengthMax) ; 00050 /* private */ extern int hlr_allocCnt ; 00051 /* safe versions of standard memory allocation routines 00052 performing die() (from log.c) if allocation fails */ 00053 /* private */ void *hlr_mallocs(size_t size) ; 00054 /* private */ void *hlr_callocs(size_t nelem, size_t elsize) ; 00055 /* private */ char *hlr_strdups(char *s1) ; 00056 /* ---- end of private stuff -------- */ 00057 00058 00059 /* macros for memory managment 00060 please use those instead of the plain C library 00061 routines if you want to find memory leaks and verify allocation success 00062 if you compile with MALLOC_NOCHECK defined, the plain C routines 00063 are used. 00064 hlr_mallocExtern is provided to keep track of memory 00065 allocated by external rouines like gdbm_fetch() 00066 */ 00067 #ifdef MALLOC_NOCHECK 00068 /* free only when allocated */ 00069 #define hlr_free(x) ((x) ? free(x), x=NULL, 1 : 0) 00070 #define hlr_strdup strdup 00071 #define hlr_malloc malloc 00072 #define hlr_calloc calloc 00073 #define hlr_mallocExtern() 00074 00075 #else 00076 /* count allocations and check for allocation success */ 00077 #define hlr_free(x) ((x) ? free(x), --hlr_allocCnt, x=0, 1 : 0) 00078 #define hlr_strdup(s) (++hlr_allocCnt, hlr_strdups(s)) 00079 #define hlr_malloc(n) (++hlr_allocCnt, hlr_mallocs(n)) 00080 #define hlr_mallocExtern() (++hlr_allocCnt) 00081 #define hlr_calloc(nelem,elsize) (++hlr_allocCnt, hlr_callocs(nelem,elsize)) 00082 #endif 00083 00084 #define hlr_realloc realloc 00085 00089 #define hlr_getAllocCnt() hlr_allocCnt 00090 00091 00092 /* copy string into a fixed sized array of char with truncation 00093 'strmcpy' mnemonic: 'string maximal copy' 00094 usage: 00095 char s[6] ; 00096 hlr_strmcpy(s, "hello bufffer!") ; 00097 note: 'to' must not be an expression with side-effects 00098 see also: hlr_strcpys() below 00099 */ 00100 #define hlr_strmcpy(to, from) hlr_strmcpyI(to, from, sizeof(to)) 00101 00102 00103 /* like strcpy, but save against overflow 00104 like hlr_strmcpy, but die() instead of silent truncation 00105 copy 'from' to 'to' while checking that 'from' fits into 'to' 00106 'to' must be of type char[] 00107 */ 00108 #define hlr_strcpys(to,from) hlr_strcpysFunc((to),(from),sizeof(to)-1) 00109 00110 00111 00112 /* be careful with all of the following macros: 00113 do not to use expressions with side effects as parameters, 00114 since they might be evaluated serveral times 00115 */ 00116 #define hlr_strdup0(s) ((s) ? hlr_strdup(s) : NULL) 00117 00118 00122 #define hlr_itoa(s,i) sprintf(s,"%d",i) 00123 #define HLR_ITOA_SIZE 21 00124 00125 /* usage: 00126 char str[HLR_ITOA_SIZE] ; 00127 int i=81062 ; 00128 hlr_itoa(str,i) ; 00129 The string must be at least 21 bytes long because 00130 the result can be up to 20 bytes 00131 (see ULONG_MAX in limits.h) 00132 */ 00133 00134 00135 00136 #ifndef MIN 00137 00140 #define MIN(a,b) ((a)>(b)?(b):(a)) 00141 #endif 00142 00143 00144 #ifndef MAX 00145 00148 #define MAX(a,b) ((a)<(b)?(b):(a)) 00149 #endif 00150 00151 00155 #define NUMELE(a) ((int)sizeof(a)/sizeof(a[0])) 00156 /* 00157 char *values[2] ; 00158 printf("%d", NUMELE(values) ; 00159 --> prints 2 00160 */ 00161 00162 00163 /* printf("%s", NULL) works on SGI, but not on WinNT or Solaris. 00164 Therefore to be safe, use 00165 printf("%s", s0f(s)) instead of printf("%s", s) 00166 If you are desparate for speed or s is an expression with side effects, 00167 you may replace s0f(s) with s0(s) 00168 which is a macro, trading the overhead of a function call 00169 for evaluating 's' twice. Beware of side-effects when using the macro! 00170 */ 00171 #define s0(s) ((s)?(s):"(null)") 00172 extern char *s0f(char *s) ; 00173 00174 /* a universal pointer to an illegal address; 00175 referencing it caused e.g. a BUS ERROR; 00176 use this to initialize local variable to enforce 00177 assigning a value before use 00178 */ 00179 #define ILLADR (void*)0xFFFFFFFF 00180 00181 /* safe starter for system() calls: */ 00182 extern int hlr_system(char *cmd, int nonZeroOK) ; 00183 00184 #endif