Imported Upstream version 4.6.0
[debian/atlc] / src / non_gui / memory.c
1 /* atlc - arbitrary transmission line calculator, for the analysis of
2 transmission lines are directional couplers. 
3
4 Copyright (C) 2002. Dr. David Kirkby, PhD (G8WRB).
5
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.
10
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.
15
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,
19 USA.
20
21 Dr. David Kirkby, e-mail drkirkby at ntlworld.com 
22
23 Some of these memory allocation routies are hacked versions of those
24 from the book 'Numerical Recipes in C' by Press et al. */
25
26 #include "config.h"
27
28 #ifdef HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31
32 #ifdef HAVE_SYS_TYPES
33 #include <sys/types.h>
34 #endif
35
36 #include "definitions.h"
37 #include "exit_codes.h"
38
39 #define NR_END 1
40
41 char *string(long nl,long nh)
42 {
43   char *v;
44
45   v=(char *)malloc((unsigned) (nh-nl+1));
46   if (!v)
47     exit_with_msg_and_exit_code("Memory allocation failure in string()",MEMORY_ALLOCATION_ERROR_IN_STRING);
48   
49   return v-nl;
50 }
51
52 unsigned char *ustring(long nl,long nh)
53 {
54   unsigned char *v;
55
56   v=(unsigned char *)malloc((size_t) (sizeof(unsigned char)* (nh-nl+1)));
57   if (!v) {
58     fprintf(stderr,"Can't allocate %ld bytes\n",nh-nl+1); 
59     while(1)
60     {
61     } 
62     exit_with_msg_and_exit_code("Memory allocation failure in ustring()",MEMORY_ALLOCATION_ERROR_IN_USTRING);
63   }
64   return v-nl;
65 }
66
67 void free_string(char *v, long nl, long nh)
68 /* free a string allocated with string() */
69 {
70   if(nh <= nl)
71     exit_with_msg_and_exit_code("nh <= nl in free_string()",SILLY_ARGUMENTS_IN_FREE_STRING);
72   free((FREE_ARG) (v+nl));
73 }
74
75 void free_ustring(unsigned char *v, long nl, long nh)
76 /* free a string allocated with ustring() */
77 {
78   if(nh <= nl)
79     exit_with_msg_and_exit_code("nh <= nl in free_ustring()",SILLY_ARGUMENTS_IN_FREE_USTRING);
80   free((FREE_ARG) (v+nl));
81 }
82
83
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)
86 {
87         long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
88         signed char **m;
89         /* allocate pointers to rows */
90         m=(signed char **) malloc((size_t)((nrow+NR_END)*sizeof(signed char*)));
91         if (!m) 
92           exit_with_msg_and_exit_code("Memory allocation failure #1 in scmatrix()",MEMORY_ALLOCATION_ERROR_IN_SCMATRIX);
93         m += NR_END;
94         m -= nrl;
95
96         /* allocate rows and set pointers to them */
97         m[nrl]=(signed char *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(signed char)));
98         if (!m[nrl]) 
99           exit_with_msg_and_exit_code("Memory allocation failure #2 in ucmatrix()",MEMORY_ALLOCATION_ERROR_IN_UCMATRIX);
100
101         m[nrl] += NR_END;
102         m[nrl] -= ncl;
103
104         for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
105
106         /* return pointer to array of pointers to rows */
107         return m;
108 }
109
110
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)
113 {
114         long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
115         unsigned char **m;
116         /* allocate pointers to rows */
117         m=(unsigned char **) malloc((size_t)((nrow+NR_END)*sizeof(unsigned char*)));
118         if (!m) 
119           exit_with_msg_and_exit_code("Memory allocation failure #1 in scmatrix()",MEMORY_ALLOCATION_ERROR_IN_UCMATRIX);
120         m += NR_END;
121         m -= nrl;
122
123         /* allocate rows and set pointers to them */
124         m[nrl]=(unsigned char *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(unsigned char)));
125         if (!m[nrl]) 
126           exit_with_msg_and_exit_code("Memory allocation failure #2 in ucmatrix()",MEMORY_ALLOCATION_ERROR_IN_UCMATRIX);
127
128         m[nrl] += NR_END;
129         m[nrl] -= ncl;
130
131         for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
132
133         /* return pointer to array of pointers to rows */
134         return m;
135 }
136
137
138 /* free a signed char matrix allocated by cmatrix() */
139 void free_scmatrix(signed char **m, long nrl, long nrh, long ncl, long nch)
140 {
141   if(nrh <= nrl)
142       exit_with_msg_and_exit_code("nrh <= nrl in free_scmatrix()",SILLY_ARGUMENTS_IN_FREE_SCMATRIX);
143   if(nch <= ncl)
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));
147 }
148
149
150 /* free a unsigned char matrix allocated by ucmatrix() */
151 void free_ucmatrix(unsigned char **m, long nrl, long nrh, long ncl, long nch)
152 {
153   if(nrh <= nrl)
154       exit_with_msg_and_exit_code("nrh <= nrl in free_ucmatrix()",SILLY_ARGUMENTS_IN_FREE_UCMATRIX);
155   if(nch <= ncl)
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));
159 }
160
161 double **dmatrix(long nrl, long nrh, long ncl, long nch)
162 /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
163 {
164         long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
165         double **m;
166
167         /* allocate pointers to rows */
168         m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
169         if (!m) 
170           exit_with_msg_and_exit_code("Memory allocation error #1 in dmatrix()",MEMORY_ALLOCATION_ERROR_IN_DMATRIX);
171         m += NR_END;
172         m -= nrl;
173
174         /* allocate rows and set pointers to them */
175         m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
176         if (!m[nrl]) 
177           exit_with_msg_and_exit_code("Memory allocation error #2 in dmatrix()",MEMORY_ALLOCATION_ERROR_IN_DMATRIX);
178         m[nrl] += NR_END;
179         m[nrl] -= ncl;
180
181         for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
182
183         /* return pointer to array of pointers to rows */
184         return m;
185 }
186
187
188 void free_dvector(double *v, long nl, long nh)
189 /* free a double vector allocated with dvector() */
190 {
191   if(nh <= nl)
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));
194 }
195
196 void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
197 /* free a double matrix allocated by dmatrix() */
198 {
199   if(nrh <= nrl)
200     exit_with_msg_and_exit_code("nhh is <= nl in call to free_dmatrix",SILLY_ARGUMENTS_IN_FREE_DMATRIX);
201   if(nch <= ncl)
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));
205 }
206
207 void free_cx(char **m, long nrl, long nrh, long ncl, long nch)
208 /* free a char matrix allocated by cmatrix() */
209 {
210   if(nrh <= nrl)
211     exit_with_msg_and_exit_code("nrh is <= nrl in call to free_charmatrix",SILLY_ARGUMENTS_IN_FREE_CMATRIX);
212   if(nch <= ncl)
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));
216 }
217
218 double *dvector(long nl, long nh)
219 /* allocate a double vector with subscript range v[nl..nh] */
220 {
221         double *v;
222         v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double)));
223         if (!v)
224           exit_with_msg_and_exit_code("Memory allocation error in dvector()",MEMORY_ALLOCATION_ERROR_IN_DVECTOR);
225         return v-nl+NR_END;
226 }
227