1 /*
2 
3  *****************************************************************************
4  * Author:                                                                   *
5  * ------                                                                    *
6  *  Anton Kokalj                                  Email: Tone.Kokalj@ijs.si  *
7  *  Department of Physical and Organic Chemistry  Phone: x 386 1 477 3523    *
8  *  Jozef Stefan Institute                          Fax: x 386 1 477 3811    *
9  *  Jamova 39, SI-1000 Ljubljana                                             *
10  *  SLOVENIA                                                                 *
11  *                                                                           *
12  * Source: $XCRYSDEN_TOPDIR/C/memory.c
13  * ------                                                                    *
14  * Copyright (c) 1996-2003 by Anton Kokalj                                   *
15  *****************************************************************************
16 
17 */
18 
19 #include <math.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "memory.h"
23 
xcMalloc(size_t size)24 void *xcMalloc(size_t size) {
25   void *ptr;
26   if (size<=0) {
27     xcError("can't locate zero memory in xcMalloc");
28   }
29   ptr = (void *) malloc(size);
30   if (!ptr) xcError("out of memory in xcMalloc");
31   return ptr;
32 }
33 
34 
xcCalloc(size_t nmemb,size_t size)35 void *xcCalloc(size_t nmemb, size_t size) {
36   void *ptr;
37   if (size<=0 || nmemb<=0) {
38     xcError("can't locate zero memory in xcCalloc");
39   }
40   ptr = (void *) calloc( nmemb, size );
41   if (!ptr) xcError("out of memory in xcCalloc");
42   return ptr;
43 }
44 
45 
xcRealloc(void * ptr,size_t size)46 void *xcRealloc(void *ptr, size_t size) {
47   ptr = (void *) realloc( ptr, size );
48   if (!ptr && size) xcError("out of memory in xcRealloc");
49   return ptr;
50 }
51 
52 
xcFree(FREE_ARG ptr)53 void xcFree(FREE_ARG ptr) {
54   if (ptr) free (ptr);
55   /*fprintf(stderr,"xcFree();");*/
56 }
57 
xcNullFree(FREE_ARG ptr)58 void *xcNullFree(FREE_ARG ptr) {
59   if (ptr) free (ptr);
60   return (FREE_ARG)NULL;
61 }
62 
63 void
xcError(char error_text[])64 xcError(char error_text[])
65 {
66   /* XCrySDen standard error handler */
67   fprintf(stderr,"XCrySDen run-time error:\n");
68   fprintf(stderr,"%s\n",error_text);
69   fprintf(stderr,"Exit !!!\n");
70   exit(1);
71 }
72 
73 void
xcFree_Vectorf(float * v)74 xcFree_Vectorf(float *v)
75 {
76   free( (FREE_ARG) v );
77 }
78 
79 void
xcFree_Matrixf(float ** m)80 xcFree_Matrixf(float **m)
81 {
82   free( (FREE_ARG) m[0] );
83   free( (FREE_ARG) m );
84 }
85 
86 void
xcFree_Tensor3f(float *** t)87 xcFree_Tensor3f(float ***t)
88 {
89   free( (FREE_ARG) t[0][0] );
90   free( (FREE_ARG) t[0] );
91   free( (FREE_ARG) t );
92 }
93 
94 
95 void
xcFree_Tensor3i(int *** t)96 xcFree_Tensor3i(int ***t)
97 {
98   free( (FREE_ARG) t[0][0] );
99   free( (FREE_ARG) t[0] );
100   free( (FREE_ARG) t );
101 }
102 
103 
104 void
xcFree_ReallocatedTensor3f_00to11(float *** rt)105 xcFree_ReallocatedTensor3f_00to11( float ***rt )
106 {
107   free( (FREE_ARG) (rt[1] + 1) );
108   free( (FREE_ARG) (rt + 1) );
109 }
110 
111 
xcMallocVectorf(long n)112 float *xcMallocVectorf(long n)
113 {
114   float *v;
115   v=(float *) malloc( (size_t) n*sizeof(float) );
116   if (!v) xcError("allocation failure in xcMallocVectorf");
117   return v;
118 }
119 
120 
xcMallocMatrixf(long nr,long nc)121 float **xcMallocMatrixf(long nr, long nc)
122 {
123   long i;
124   float **m;
125   /* allocate pointers to pointers to rows */
126   m=(float **) malloc( (size_t) nr*sizeof(float *) );
127   if (!m) xcError("allocation failure 1 in xcMallocMatrixf");
128 
129   /* allocate pointers to rows and set pointers to them */
130   m[0]=(float *) malloc( (size_t) nr*nc*sizeof(float) );
131   if (!m[0]) xcError("allocation failure 2 in xcMallocMatrixf");
132 
133   for(i=1; i<nr; i++)
134     m[i] = m[i-1] + nc;
135 
136   return m;
137 }
138 
139 
xcMallocTensor3f(long nr,long nc,long nd)140 float ***xcMallocTensor3f(long nr, long nc, long nd)
141 {
142   /* allocate a float 3tensor with range t[0..nr-1][0..nc-1][0..nd-1] */
143   long i, j;
144   float ***t;
145   /* allocate pointers to pointers to rows */
146   t=(float ***) malloc( (size_t) nr*sizeof(float**) );
147   if (!t) xcError("allocation failure 1 in xcMallocTensor3f");
148 
149   /* allocate pointers to rows and set pointers to them */
150   t[0]=(float **) malloc( (size_t) nr*nc*sizeof(float*) );
151   if (!t[0]) xcError("allocation failure 2 in xcMallocTensor3f");
152 
153   /* allocate rows and set pointers to them */
154   t[0][0]=(float *) malloc( (size_t) nr*nc*nd*sizeof(float) );
155   if (!t[0][0]) xcError("allocation failure 3 in xcMallocTensor3f");
156 
157   for(i=0; i<nr; i++) {
158     t[i] = t[0] + i*nc;
159     for(j=0; j<nc; j++)
160       t[i][j] = t[0][0] + nd*(i*nc + j);
161   }
162   return t;
163 }
164 
xcMallocTensor3i(long nr,long nc,long nd)165 int ***xcMallocTensor3i(long nr, long nc, long nd)
166 {
167   /* allocate a int 3tensor with range t[0..nr-1][0..nc-1][0..nd-1] */
168   long i, j;
169   int ***t;
170   /* allocate pointers to pointers to rows */
171   t=(int ***) malloc( (size_t) nr*sizeof(int**) );
172   if (!t) xcError("allocation failure 1 in xcMallocTensor3i");
173 
174   /* allocate pointers to rows and set pointers to them */
175   t[0]=(int **) malloc( (size_t) nr*nc*sizeof(int*) );
176   if (!t[0]) xcError("allocation failure 2 in xcMallocTensor3i");
177 
178   /* allocate rows and set pointers to them */
179   t[0][0]=(int *) malloc( (size_t) nr*nc*nd*sizeof(int) );
180   if (!t[0][0]) xcError("allocation failure 3 in xcMallocTensor3i");
181 
182   for(i=0; i<nr; i++) {
183     t[i] = t[0] + i*nc;
184     for(j=0; j<nc; j++)
185       t[i][j] = t[0][0] + nd*(i*nc + j);
186   }
187   return t;
188 }
189 
xcMallocTensor3si(long nr,long nc,long nd)190 short int ***xcMallocTensor3si(long nr, long nc, long nd)
191 {
192   /* allocate a int 3tensor with range t[0..nr-1][0..nc-1][0..nd-1] */
193   long i, j;
194   short int ***t;
195   /* allocate pointers to pointers to rows */
196   t=(short int ***) malloc( (size_t) nr*sizeof(short int**) );
197   if (!t) xcError("allocation failure 1 in xcMallocTensor3i");
198 
199   /* allocate pointers to rows and set pointers to them */
200   t[0]=(short int **) malloc( (size_t) nr*nc*sizeof(short int*) );
201   if (!t[0]) xcError("allocation failure 2 in xcMallocTensor3i");
202 
203   /* allocate rows and set pointers to them */
204   t[0][0]=(short int *) malloc( (size_t) nr*nc*nd*sizeof(short int) );
205   if (!t[0][0]) xcError("allocation failure 3 in xcMallocTensor3i");
206 
207   for(i=0; i<nr; i++) {
208     t[i] = t[0] + i*nc;
209     for(j=0; j<nc; j++)
210       t[i][j] = t[0][0] + nd*(i*nc + j);
211   }
212   return t;
213 }
214 
xcReallocTensor3f_00to11(float *** t,long nr,long nc,long nd)215 float ***xcReallocTensor3f_00to11(float ***t, long nr, long nc, long nd)
216 {
217   /* convert from t[0..nr-1][0..nc-1][0..nd-1] to m[1..nr][1..nc][1..nd] */
218   long i, j;
219   float ***m;
220 
221   /* allocate pointers to pointers to rows */
222   m=(float ***) malloc((size_t) (nr * sizeof(float **)));
223   if (!m) xcError("allocation failure #1 in ***xcReallocTensor3f_00to11");
224 
225   m -= 1; /* now m[0] is undefined & m[1] == previous_m[0]; */
226   m[1]=(float **) malloc((size_t) (nr * nc * sizeof(float *)));
227   if (!m[1]) xcError("allocation failure #2 in ***xcReallocTensor3f_00to11");
228   m[1] -= 1;  /* &(m[i][0]) -= 1 */
229   for(i=1; i<=nr; i++) {
230     m[i] = m[1] + (i-1)*nc;
231     for(j=1; j<=nc; j++) {
232       /* m[i][j] -= 1; */
233       m[i][j] = t[i-1][j-1] - 1;
234     }
235   }
236   return m;
237 }
238 
239 
cryMem_Malloc_W(size_t size,char * where)240 void *cryMem_Malloc_W( size_t size, char *where ) {
241   void *ptr = (void *) malloc( size );
242   if (!ptr) {
243     fprintf( stderr,
244 	     "\n***\nMemory Error: can't allocate %ld bytes in %s\n",
245 	     size, where );
246     abort();
247   }
248   return ptr;
249 }
250