Coverity: Matio module memory leaks fixed
[scilab.git] / scilab / modules / matio / src / c / CreateSparseVariable.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2008 - INRIA - Vincent COUVERT
4  * Copyright (C) 2010 - DIGITEO - Yann COLLETTE
5  *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  *
15  */
16
17 #include "CreateMatlabVariable.h"
18 #include "api_scilab.h"
19 #include "sci_malloc.h"
20 #include "localization.h"
21 #include "Scierror.h"
22 #include "sciprint.h"
23 #include "scisparse.h"
24
25 /* Defined in SCI/modules/sparse/src/fortran/spt.f */
26 extern int C2F(spt)(int *m, int *n, int *nel, int *it, int *workArray,
27                     double *A_R, double *A_I, int *A_mnel, int *A_icol,
28                     double *At_R, double *At_I, int *At_mnel, int *At_icol);
29
30 int CreateSparseVariable(void *pvApiCtx, int iVar, matvar_t *matVariable, int * parent, int item_position)
31 {
32     int K = 0;
33     mat_sparse_t *sparseData = NULL;
34     SciSparse *scilabSparse = NULL;
35     SciSparse *scilabSparseT = NULL; /* Transpose */
36     int *colIndexes = NULL;
37     int *rowIndexes = NULL;
38     int *workArray = NULL;
39     struct mat_complex_split_t *complexData = NULL;
40     SciErr sciErr;
41
42     sparseData = (mat_sparse_t*) matVariable->data;
43
44     scilabSparse = (SciSparse*) MALLOC(sizeof(SciSparse));
45     if (scilabSparse == NULL)
46     {
47         Scierror(999, _("%s: No more memory.\n"), "CreateSparseVariable");
48         return FALSE;
49     }
50
51     /* Computes column indexes from Matlab indexes */
52     if (sparseData->njc > 1)
53     {
54         colIndexes = (int*) MALLOC(sizeof(int) *  (sparseData->njc - 1));
55         if (colIndexes == NULL)
56         {
57             Scierror(999, _("%s: No more memory.\n"), "CreateSparseVariable");
58             FREE(scilabSparse);
59             return FALSE;
60         }
61
62         for (K = 0; K < sparseData->njc - 1; K++)
63         {
64             colIndexes[K] = sparseData->jc[K + 1] - sparseData->jc[K];
65         }
66     }
67
68     /* Computes row indexes from Matlab indexes */
69     rowIndexes = (int*) MALLOC(sizeof(int) * sparseData->nir);
70     if (rowIndexes == NULL)
71     {
72         Scierror(999, _("%s: No more memory.\n"), "CreateSparseVariable");
73         FREE(scilabSparse);
74         FREE(colIndexes);
75         return FALSE;
76     }
77
78     for (K = 0; K < sparseData->nir; K++)
79     {
80         rowIndexes[K] = sparseData->ir[K] + 1;
81     }
82
83     /* Create sparse matrix to be transposed */
84     scilabSparse->m    = (int)matVariable->dims[1];
85     scilabSparse->n    = (int)matVariable->dims[0];
86     scilabSparse->it   = matVariable->isComplex;
87     scilabSparse->nel  = sparseData->ndata;
88     scilabSparse->mnel = colIndexes;
89     scilabSparse->icol = rowIndexes;
90
91     if (scilabSparse->it == 0)
92     {
93         scilabSparse->R = (double*) sparseData->data;
94         scilabSparse->I = NULL;
95     }
96     else
97     {
98         complexData = (struct mat_complex_split_t *) sparseData->data;
99         scilabSparse->R = (double *) complexData->Re;
100         scilabSparse->I = (double *) complexData->Im;
101     }
102
103     /* Create transpose */
104     scilabSparseT = (SciSparse*) MALLOC(sizeof(SciSparse));
105     if (scilabSparseT == NULL)
106     {
107         Scierror(999, _("%s: No more memory.\n"), "CreateSparseVariable");
108         FREE(scilabSparse);
109         FREE(colIndexes);
110         FREE(rowIndexes);
111         return FALSE;
112     }
113
114     scilabSparseT->m   = scilabSparse->n;
115     scilabSparseT->n   = scilabSparse->m;
116     scilabSparseT->it  = scilabSparse->it;
117     scilabSparseT->nel = scilabSparse->nel;
118     scilabSparseT->mnel = NULL;
119     scilabSparseT->icol = NULL;
120
121     if (scilabSparseT->m == 0)
122     {
123         workArray = (int*) MALLOC(sizeof(int));
124     }
125     else
126     {
127         workArray = (int*) MALLOC(sizeof(int) * scilabSparseT->m);
128     }
129
130     if (workArray == NULL)
131     {
132         Scierror(999, _("%s: No more memory.\n"), "CreateSparseVariable");
133         FREE(scilabSparse);
134         FREE(scilabSparseT);
135         FREE(rowIndexes);
136         FREE(colIndexes);
137         return FALSE;
138     }
139
140     if (scilabSparseT->m != 0)
141     {
142         scilabSparseT->mnel = (int*) MALLOC(sizeof(int) * scilabSparseT->m);
143         if (scilabSparseT->mnel == NULL)
144         {
145             Scierror(999, _("%s: No more memory.\n"), "CreateSparseVariable");
146             FREE(scilabSparse);
147             FREE(scilabSparseT);
148             FREE(workArray);
149             FREE(colIndexes);
150             FREE(rowIndexes);
151             return FALSE;
152         }
153     }
154
155     if (scilabSparseT->nel != 0)
156     {
157         scilabSparseT->icol = (int*) MALLOC(sizeof(int) * scilabSparseT->nel);
158         if (scilabSparseT->icol == NULL)
159         {
160             Scierror(999, _("%s: No more memory.\n"), "CreateSparseVariable");
161             FREE(scilabSparse);
162             FREE(scilabSparseT);
163             FREE(scilabSparseT->mnel);
164             FREE(workArray);
165             FREE(colIndexes);
166             FREE(rowIndexes);
167             return FALSE;
168         }
169     }
170
171     if (scilabSparseT->nel != 0)
172     {
173         scilabSparseT->R = (double*) MALLOC(sizeof(double) * scilabSparseT->nel);
174         if (scilabSparseT->R == NULL)
175         {
176             Scierror(999, _("%s: No more memory.\n"), "CreateSparseVariable");
177             FREE(scilabSparse);
178             FREE(scilabSparseT);
179             FREE(scilabSparseT->icol);
180             FREE(scilabSparseT->mnel);
181             FREE(workArray);
182             FREE(colIndexes);
183             FREE(rowIndexes);
184             return FALSE;
185         }
186     }
187
188     if (scilabSparseT->it)
189     {
190         scilabSparseT->I = (double*) MALLOC(sizeof(double) * scilabSparseT->nel);
191         if (scilabSparseT->I == NULL)
192         {
193             Scierror(999, _("%s: No more memory.\n"), "CreateSparseVariable");
194             FREE(scilabSparse);
195             FREE(scilabSparseT);
196             FREE(scilabSparseT->icol);
197             FREE(scilabSparseT->mnel);
198             FREE(scilabSparseT->R);
199             FREE(workArray);
200             FREE(colIndexes);
201             FREE(rowIndexes);
202             return FALSE;
203         }
204     }
205
206     C2F(spt)(&scilabSparse->m, &scilabSparse->n, &scilabSparse->nel, &scilabSparse->it, workArray,
207              scilabSparse->R, scilabSparse->I, scilabSparse->mnel, scilabSparse->icol,
208              scilabSparseT->R, scilabSparseT->I, scilabSparseT->mnel, scilabSparseT->icol);
209
210     if (scilabSparse->it)
211     {
212         if (parent == NULL)
213         {
214             sciErr = createComplexSparseMatrix(pvApiCtx, iVar, scilabSparse->m, scilabSparse->n, scilabSparse->nel,
215                                                scilabSparseT->mnel, scilabSparseT->icol, scilabSparseT->R, scilabSparseT->I);
216             if (sciErr.iErr)
217             {
218                 printError(&sciErr, 0);
219                 FREE(scilabSparse);
220                 FREE(scilabSparseT);
221                 FREE(workArray);
222                 FREE(colIndexes);
223                 FREE(rowIndexes);
224                 FREE(scilabSparseT->icol);
225                 FREE(scilabSparseT->mnel);
226                 FREE(scilabSparseT->R);
227                 return 0;
228             }
229         }
230         else
231         {
232             sciErr = createComplexSparseMatrixInList(pvApiCtx, iVar, parent, item_position,
233                      scilabSparse->m, scilabSparse->n, scilabSparse->nel,
234                      scilabSparseT->mnel, scilabSparseT->icol, scilabSparseT->R, scilabSparseT->I);
235             if (sciErr.iErr)
236             {
237                 printError(&sciErr, 0);
238                 FREE(scilabSparse);
239                 FREE(scilabSparseT);
240                 FREE(workArray);
241                 FREE(colIndexes);
242                 FREE(rowIndexes);
243                 FREE(scilabSparseT->icol);
244                 FREE(scilabSparseT->mnel);
245                 FREE(scilabSparseT->R);
246                 return 0;
247             }
248         }
249     }
250     else
251     {
252         if (parent == NULL)
253         {
254             sciErr = createSparseMatrix(pvApiCtx, iVar, scilabSparseT->m, scilabSparseT->n, scilabSparseT->nel,
255                                         scilabSparseT->mnel, scilabSparseT->icol, scilabSparseT->R);
256             if (sciErr.iErr)
257             {
258                 printError(&sciErr, 0);
259                 FREE(scilabSparse);
260                 FREE(scilabSparseT);
261                 FREE(workArray);
262                 FREE(colIndexes);
263                 FREE(rowIndexes);
264                 FREE(scilabSparseT->icol);
265                 FREE(scilabSparseT->mnel);
266                 FREE(scilabSparseT->R);
267                 return 0;
268             }
269         }
270         else
271         {
272             sciErr = createSparseMatrixInList(pvApiCtx, iVar, parent, item_position,
273                                               scilabSparseT->m, scilabSparseT->n, scilabSparseT->nel,
274                                               scilabSparseT->mnel, scilabSparseT->icol, scilabSparseT->R);
275             if (sciErr.iErr)
276             {
277                 printError(&sciErr, 0);
278                 FREE(scilabSparse);
279                 FREE(scilabSparseT);
280                 FREE(workArray);
281                 FREE(colIndexes);
282                 FREE(rowIndexes);
283                 FREE(scilabSparseT->icol);
284                 FREE(scilabSparseT->mnel);
285                 FREE(scilabSparseT->R);
286                 return 0;
287             }
288         }
289     }
290
291     /* Free all arrays */
292     if (scilabSparse != NULL)
293     {
294         FREE(scilabSparse);
295     }
296     if (colIndexes != NULL)
297     {
298         FREE(colIndexes);
299     }
300     if (rowIndexes != NULL)
301     {
302         FREE(rowIndexes);
303     }
304     if (workArray != NULL)
305     {
306         FREE(workArray);
307     }
308     if (scilabSparseT->m != 0)
309     {
310         FREE(scilabSparseT->mnel);
311     }
312     if (scilabSparseT->nel != 0)
313     {
314         FREE(scilabSparseT->icol);
315     }
316     if (scilabSparseT->nel != 0)
317     {
318         FREE(scilabSparseT->R);
319     }
320     if ((scilabSparseT->nel != 0) && (scilabSparseT->it != 0))
321     {
322         FREE(scilabSparseT->I);
323     }
324     if (scilabSparseT != NULL)
325     {
326         FREE(scilabSparseT);
327     }
328
329     return TRUE;
330 }