Browse Source

Get rid of malloc() and free() (PR#2549)

Just the minimal changes to qr_solve.cpp and .h to get rid of malloc() and free().

Saves about 656 bytes of progmem (library-code)
and 22 bytes for static variables.

Should use exactly the same amount of stack as it did before on the heap.
AnHardt 10 years ago
parent
commit
d03f037a92
3 changed files with 11 additions and 30 deletions
  1. 2
    1
      Marlin/Marlin_main.cpp
  2. 8
    28
      Marlin/qr_solve.cpp
  3. 1
    1
      Marlin/qr_solve.h

+ 2
- 1
Marlin/Marlin_main.cpp View File

2643
       #else // !DELTA
2643
       #else // !DELTA
2644
 
2644
 
2645
         // solve lsq problem
2645
         // solve lsq problem
2646
-        double *plane_equation_coefficients = qr_solve(abl2, 3, eqnAMatrix, eqnBVector);
2646
+        double plane_equation_coefficients[3];
2647
+        qr_solve(plane_equation_coefficients, abl2, 3, eqnAMatrix, eqnBVector);
2647
 
2648
 
2648
         mean /= abl2;
2649
         mean /= abl2;
2649
 
2650
 

+ 8
- 28
Marlin/qr_solve.cpp View File

260
   return value;
260
   return value;
261
 }
261
 }
262
 
262
 
263
-double *r8mat_copy_new ( int m, int n, double a1[] )
263
+void r8mat_copy( double a2[], int m, int n, double a1[] )
264
 
264
 
265
 /******************************************************************************/
265
 /******************************************************************************/
266
 /*
266
 /*
294
     Output, double R8MAT_COPY_NEW[M*N], the copy of A1.
294
     Output, double R8MAT_COPY_NEW[M*N], the copy of A1.
295
 */
295
 */
296
 {
296
 {
297
-  double *a2;
298
   int i;
297
   int i;
299
   int j;
298
   int j;
300
 
299
 
301
-  a2 = ( double * ) malloc ( m * n * sizeof ( double ) );
302
-
303
   for ( j = 0; j < n; j++ )
300
   for ( j = 0; j < n; j++ )
304
   {
301
   {
305
     for ( i = 0; i < m; i++ )
302
     for ( i = 0; i < m; i++ )
307
       a2[i+j*m] = a1[i+j*m];
304
       a2[i+j*m] = a1[i+j*m];
308
     }
305
     }
309
   }
306
   }
310
-
311
-  return a2;
312
 }
307
 }
313
 
308
 
314
 /******************************************************************************/
309
 /******************************************************************************/
726
   int j;
721
   int j;
727
   int job;
722
   int job;
728
   int k;
723
   int k;
729
-  double *work;
724
+  double work[n];
730
 
725
 
731
   for ( i = 0; i < n; i++ )
726
   for ( i = 0; i < n; i++ )
732
   {
727
   {
733
     jpvt[i] = 0;
728
     jpvt[i] = 0;
734
   }
729
   }
735
 
730
 
736
-  work = ( double * ) malloc ( n * sizeof ( double ) );
737
   job = 1;
731
   job = 1;
738
 
732
 
739
   dqrdc ( a, lda, m, n, qraux, jpvt, work, job );
733
   dqrdc ( a, lda, m, n, qraux, jpvt, work, job );
750
     *kr = j + 1;
744
     *kr = j + 1;
751
   }
745
   }
752
 
746
 
753
-  free ( work );
754
-
755
   return;
747
   return;
756
 }
748
 }
757
 /******************************************************************************/
749
 /******************************************************************************/
1845
 
1837
 
1846
 /******************************************************************************/
1838
 /******************************************************************************/
1847
 
1839
 
1848
-double *qr_solve ( int m, int n, double a[], double b[] )
1840
+void qr_solve ( double x[], int m, int n, double a[], double b[] )
1849
 
1841
 
1850
 /******************************************************************************/
1842
 /******************************************************************************/
1851
 /*
1843
 /*
1895
     Output, double QR_SOLVE[N], the least squares solution.
1887
     Output, double QR_SOLVE[N], the least squares solution.
1896
 */
1888
 */
1897
 {
1889
 {
1898
-  double *a_qr;
1890
+  double a_qr[n*m];
1899
   int ind;
1891
   int ind;
1900
   int itask;
1892
   int itask;
1901
-  int *jpvt;
1893
+  int jpvt[n];
1902
   int kr;
1894
   int kr;
1903
   int lda;
1895
   int lda;
1904
-  double *qraux;
1905
-  double *r;
1896
+  double qraux[n];
1897
+  double r[m];
1906
   double tol;
1898
   double tol;
1907
-  double *x;
1908
 
1899
 
1909
-  a_qr = r8mat_copy_new ( m, n, a );
1900
+  r8mat_copy( a_qr, m, n, a );
1910
   lda = m;
1901
   lda = m;
1911
   tol = r8_epsilon ( ) / r8mat_amax ( m, n, a_qr );
1902
   tol = r8_epsilon ( ) / r8mat_amax ( m, n, a_qr );
1912
-  x = ( double * ) malloc ( n * sizeof ( double ) );
1913
-  jpvt = ( int * ) malloc ( n * sizeof ( int ) );
1914
-  qraux = ( double * ) malloc ( n * sizeof ( double ) );
1915
-  r = ( double * ) malloc ( m * sizeof ( double ) );
1916
   itask = 1;
1903
   itask = 1;
1917
 
1904
 
1918
   ind = dqrls ( a_qr, lda, m, n, tol, &kr, b, x, r, jpvt, qraux, itask );
1905
   ind = dqrls ( a_qr, lda, m, n, tol, &kr, b, x, r, jpvt, qraux, itask );
1919
-
1920
-  free ( a_qr );
1921
-  free ( jpvt );
1922
-  free ( qraux ); 
1923
-  free ( r );
1924
-
1925
-  return x;
1926
 }
1906
 }
1927
 /******************************************************************************/
1907
 /******************************************************************************/
1928
 
1908
 

+ 1
- 1
Marlin/qr_solve.h View File

17
   double qy[], double qty[], double b[], double rsd[], double ab[], int job );
17
   double qy[], double qty[], double b[], double rsd[], double ab[], int job );
18
 void dscal ( int n, double sa, double x[], int incx );
18
 void dscal ( int n, double sa, double x[], int incx );
19
 void dswap ( int n, double x[], int incx, double y[], int incy );
19
 void dswap ( int n, double x[], int incx, double y[], int incy );
20
-double *qr_solve ( int m, int n, double a[], double b[] );
20
+void qr_solve ( double x[], int m, int n, double a[], double b[] );
21
 
21
 
22
 #endif
22
 #endif

Loading…
Cancel
Save