Bladeren bron

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 9 jaren geleden
bovenliggende
commit
d03f037a92
3 gewijzigde bestanden met toevoegingen van 11 en 30 verwijderingen
  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 Bestand weergeven

@@ -2643,7 +2643,8 @@ inline void gcode_G28() {
2643 2643
       #else // !DELTA
2644 2644
 
2645 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 2649
         mean /= abl2;
2649 2650
 

+ 8
- 28
Marlin/qr_solve.cpp Bestand weergeven

@@ -260,7 +260,7 @@ double r8mat_amax ( int m, int n, double a[] )
260 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,12 +294,9 @@ double *r8mat_copy_new ( int m, int n, double a1[] )
294 294
     Output, double R8MAT_COPY_NEW[M*N], the copy of A1.
295 295
 */
296 296
 {
297
-  double *a2;
298 297
   int i;
299 298
   int j;
300 299
 
301
-  a2 = ( double * ) malloc ( m * n * sizeof ( double ) );
302
-
303 300
   for ( j = 0; j < n; j++ )
304 301
   {
305 302
     for ( i = 0; i < m; i++ )
@@ -307,8 +304,6 @@ double *r8mat_copy_new ( int m, int n, double a1[] )
307 304
       a2[i+j*m] = a1[i+j*m];
308 305
     }
309 306
   }
310
-
311
-  return a2;
312 307
 }
313 308
 
314 309
 /******************************************************************************/
@@ -726,14 +721,13 @@ void dqrank ( double a[], int lda, int m, int n, double tol, int *kr,
726 721
   int j;
727 722
   int job;
728 723
   int k;
729
-  double *work;
724
+  double work[n];
730 725
 
731 726
   for ( i = 0; i < n; i++ )
732 727
   {
733 728
     jpvt[i] = 0;
734 729
   }
735 730
 
736
-  work = ( double * ) malloc ( n * sizeof ( double ) );
737 731
   job = 1;
738 732
 
739 733
   dqrdc ( a, lda, m, n, qraux, jpvt, work, job );
@@ -750,8 +744,6 @@ void dqrank ( double a[], int lda, int m, int n, double tol, int *kr,
750 744
     *kr = j + 1;
751 745
   }
752 746
 
753
-  free ( work );
754
-
755 747
   return;
756 748
 }
757 749
 /******************************************************************************/
@@ -1845,7 +1837,7 @@ void dswap ( int n, double x[], int incx, double y[], int incy )
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,34 +1887,22 @@ double *qr_solve ( int m, int n, double a[], double b[] )
1895 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 1891
   int ind;
1900 1892
   int itask;
1901
-  int *jpvt;
1893
+  int jpvt[n];
1902 1894
   int kr;
1903 1895
   int lda;
1904
-  double *qraux;
1905
-  double *r;
1896
+  double qraux[n];
1897
+  double r[m];
1906 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 1901
   lda = m;
1911 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 1903
   itask = 1;
1917 1904
 
1918 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 Bestand weergeven

@@ -17,6 +17,6 @@ int dqrsl ( double a[], int lda, int n, int k, double qraux[], double y[],
17 17
   double qy[], double qty[], double b[], double rsd[], double ab[], int job );
18 18
 void dscal ( int n, double sa, double x[], int incx );
19 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 22
 #endif

Laden…
Annuleren
Opslaan