1 /* atlc - arbitrary transmission line calculator, for the analysis of
2 transmission lines are directional couplers.
4 Copyright (C) 2002. Dr. David Kirkby, PhD (G8WRB).
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either package_version 2
9 of the License, or (at your option) any later package_version.
11 This program 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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 Dr. David Kirkby, e-mail drkirkby at gmail.com
23 Some of these memory allocation routies are hacked versions of those
24 from the book 'Numerical Recipes in C' by Press et al. */
33 #include <sys/types.h>
36 #include "definitions.h"
37 #include "exit_codes.h"
41 char *string(long nl,long nh)
45 v=(char *)malloc((unsigned) (nh-nl+1));
47 exit_with_msg_and_exit_code("Memory allocation failure in string()",MEMORY_ALLOCATION_ERROR_IN_STRING);
52 unsigned char *ustring(long nl,long nh)
56 v=(unsigned char *)malloc((size_t) (sizeof(unsigned char)* (nh-nl+1)));
58 fprintf(stderr,"Can't allocate %ld bytes\n",nh-nl+1);
62 exit_with_msg_and_exit_code("Memory allocation failure in ustring()",MEMORY_ALLOCATION_ERROR_IN_USTRING);
67 void free_string(char *v, long nl, long nh)
68 /* free a string allocated with string() */
71 exit_with_msg_and_exit_code("nh <= nl in free_string()",SILLY_ARGUMENTS_IN_FREE_STRING);
72 free((FREE_ARG) (v+nl));
75 void free_ustring(unsigned char *v, long nl, long nh)
76 /* free a string allocated with ustring() */
79 exit_with_msg_and_exit_code("nh <= nl in free_ustring()",SILLY_ARGUMENTS_IN_FREE_USTRING);
80 free((FREE_ARG) (v+nl));
84 /* allocate a signed char matrix with subscript range m[nrl..nrh][ncl..nch] */
85 signed char **scmatrix(long nrl, long nrh, long ncl, long nch)
87 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
89 /* allocate pointers to rows */
90 m=(signed char **) malloc((size_t)((nrow+NR_END)*sizeof(signed char*)));
92 exit_with_msg_and_exit_code("Memory allocation failure #1 in scmatrix()",MEMORY_ALLOCATION_ERROR_IN_SCMATRIX);
96 /* allocate rows and set pointers to them */
97 m[nrl]=(signed char *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(signed char)));
99 exit_with_msg_and_exit_code("Memory allocation failure #2 in ucmatrix()",MEMORY_ALLOCATION_ERROR_IN_UCMATRIX);
104 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
106 /* return pointer to array of pointers to rows */
111 /* allocate a unsigned char matrix with subscript range m[nrl..nrh][ncl..nch] */
112 unsigned char **ucmatrix(long nrl, long nrh, long ncl, long nch)
114 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
116 /* allocate pointers to rows */
117 m=(unsigned char **) malloc((size_t)((nrow+NR_END)*sizeof(unsigned char*)));
119 exit_with_msg_and_exit_code("Memory allocation failure #1 in scmatrix()",MEMORY_ALLOCATION_ERROR_IN_UCMATRIX);
123 /* allocate rows and set pointers to them */
124 m[nrl]=(unsigned char *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(unsigned char)));
126 exit_with_msg_and_exit_code("Memory allocation failure #2 in ucmatrix()",MEMORY_ALLOCATION_ERROR_IN_UCMATRIX);
131 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
133 /* return pointer to array of pointers to rows */
138 /* free a signed char matrix allocated by cmatrix() */
139 void free_scmatrix(signed char **m, long nrl, long nrh, long ncl, long nch)
142 exit_with_msg_and_exit_code("nrh <= nrl in free_scmatrix()",SILLY_ARGUMENTS_IN_FREE_SCMATRIX);
144 exit_with_msg_and_exit_code("nch <= ncl in free_scmatrix()",SILLY_ARGUMENTS_IN_FREE_SCMATRIX);
145 free((FREE_ARG) (m[nrl]+ncl-NR_END));
146 free((FREE_ARG) (m+nrl-NR_END));
150 /* free a unsigned char matrix allocated by ucmatrix() */
151 void free_ucmatrix(unsigned char **m, long nrl, long nrh, long ncl, long nch)
154 exit_with_msg_and_exit_code("nrh <= nrl in free_ucmatrix()",SILLY_ARGUMENTS_IN_FREE_UCMATRIX);
156 exit_with_msg_and_exit_code("nch <= ncl in free_ucmatrix()",SILLY_ARGUMENTS_IN_FREE_UCMATRIX);
157 free((FREE_ARG) (m[nrl]+ncl-NR_END));
158 free((FREE_ARG) (m+nrl-NR_END));
161 double **dmatrix(long nrl, long nrh, long ncl, long nch)
162 /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
164 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
167 /* allocate pointers to rows */
168 m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
170 exit_with_msg_and_exit_code("Memory allocation error #1 in dmatrix()",MEMORY_ALLOCATION_ERROR_IN_DMATRIX);
174 /* allocate rows and set pointers to them */
175 m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
177 exit_with_msg_and_exit_code("Memory allocation error #2 in dmatrix()",MEMORY_ALLOCATION_ERROR_IN_DMATRIX);
181 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
183 /* return pointer to array of pointers to rows */
188 void free_dvector(double *v, long nl, long nh)
189 /* free a double vector allocated with dvector() */
192 exit_with_msg_and_exit_code("nh is <= nl in call to free_dvector",SILLY_ARGUMENTS_IN_FREE_DVECTOR);
193 free((FREE_ARG) (v+nl-NR_END));
196 void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
197 /* free a double matrix allocated by dmatrix() */
200 exit_with_msg_and_exit_code("nhh is <= nl in call to free_dmatrix",SILLY_ARGUMENTS_IN_FREE_DMATRIX);
202 exit_with_msg_and_exit_code("nch is <= nl in call to free_dmatrix",SILLY_ARGUMENTS_IN_FREE_DMATRIX);
203 free((FREE_ARG) (m[nrl]+ncl-NR_END));
204 free((FREE_ARG) (m+nrl-NR_END));
207 void free_cx(char **m, long nrl, long nrh, long ncl, long nch)
208 /* free a char matrix allocated by cmatrix() */
211 exit_with_msg_and_exit_code("nrh is <= nrl in call to free_charmatrix",SILLY_ARGUMENTS_IN_FREE_CMATRIX);
213 exit_with_msg_and_exit_code("nch is <= ncl in call to free_charmatrix",SILLY_ARGUMENTS_IN_FREE_CMATRIX);
214 free((FREE_ARG) (m[nrl]+ncl-NR_END));
215 free((FREE_ARG) (m+nrl-NR_END));
218 double *dvector(long nl, long nh)
219 /* allocate a double vector with subscript range v[nl..nh] */
222 v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double)));
224 exit_with_msg_and_exit_code("Memory allocation error in dvector()",MEMORY_ALLOCATION_ERROR_IN_DVECTOR);