Ignore:
Timestamp:
07/31/07 19:52:12 (6 years ago)
Author:
Jonathan Bober <bober@…>
Branch:
default
Message:

Some minor changes to fix memory leaks. Hopefully there aren't any now.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sage/combinat/partitions_c.cc

    r5529 r5538  
    171171mp_rnd_t round_mode = GMP_RNDN; 
    172172 
    173 mpfr_t tempa1, tempa2, tempf1, tempf2, temps1, temps2, tempt1, tempt2;  // temp variables for different functions, with precision set and cleared by mp_set_precision 
     173mpfr_t tempa1, tempa2, tempf1, tempf2, temps1, temps2, tempt1, tempt2;  // temp variables for different functions, with precision set and cleared by initialize_mpfr_variables 
    174174mpfr_t tempc1, tempc2; // temp variable used by cospi() 
    175175 
     
    330330 
    331331 
    332 void mp_set_precision(unsigned int prec) { 
     332void initialize_mpfr_variables(unsigned int prec) { 
    333333    // 
    334334    // Clear and initialize some "temp" variables that are used in the computation of various functions. 
     
    347347    // 
    348348    // -etc... 
    349  
    350     static bool init = false; 
    351     if(init) { 
    352         mpfr_clear(tempa1); 
    353         mpfr_clear(tempa2); 
    354         mpfr_clear(tempf1); 
    355         mpfr_clear(tempf2); 
    356         mpfr_clear(tempt1); 
    357         mpfr_clear(tempt2); 
    358         mpfr_clear(temps1); 
    359         mpfr_clear(temps2); 
    360  
    361         mpfr_clear(tempc1); 
    362         mpfr_clear(tempc2); 
    363     } 
     349    // 
     350    // NOTE: Calls to this function must be paired with calls to clear_mpfr_variables() 
     351 
    364352    mpfr_init2(tempa1, prec); 
    365353    mpfr_init2(tempa2, prec); 
     
    372360    mpfr_init2(tempc1, prec); 
    373361    mpfr_init2(tempc2, prec); 
    374  
    375  
    376     init = true; 
     362} 
     363 
     364void clear_mpfr_variables() { 
     365    mpfr_clear(tempa1); 
     366    mpfr_clear(tempa2); 
     367    mpfr_clear(tempf1); 
     368    mpfr_clear(tempf2); 
     369    mpfr_clear(tempt1); 
     370    mpfr_clear(tempt2); 
     371    mpfr_clear(temps1); 
     372    mpfr_clear(temps2); 
     373 
     374    mpfr_clear(tempc1); 
     375    mpfr_clear(tempc2); 
    377376} 
    378377 
     
    387386    // Also, we precompute some extra constants that we use a lot, such as 
    388387    // sqrt2, sqrt3, pi, 1/24, 1/12, etc. 
     388    // 
     389    // NOTE: Calls to this function must be paired with calls to clear_constants() 
    389390    static bool init = false; 
    390391    mp_precision = prec; 
    391392    mp_prec_t p = mp_precision; 
    392393     
    393     if(init) { 
    394         mpfr_clear(mp_one_over_12); mpfr_clear(mp_one_over_24); mpfr_clear(mp_sqrt2); mpfr_clear(mp_sqrt3); mpfr_clear(mp_pi); 
    395         mpfr_clear(mp_A); mpfr_clear(mp_B); mpfr_clear(mp_C); mpfr_clear(mp_D); mpfr_clear(half); mpfr_clear(fourth); 
    396  
    397         mpz_clear(ztemp1); 
    398         mpz_clear(ztemp2); 
    399         mpz_clear(ztemp3); 
    400          
    401         mpq_clear(qtemp1); 
    402         mpq_clear(qtemp2); 
    403         mpq_clear(qtemp3); 
    404     } 
    405394    mpfr_init2(mp_one_over_12,p); mpfr_init2(mp_one_over_24,p); mpfr_init2(mp_sqrt2,p); mpfr_init2(mp_sqrt3,p); mpfr_init2(mp_pi,p); 
    406395    mpfr_init2(mp_A,p); mpfr_init2(mp_B,p); mpfr_init2(mp_C,p); mpfr_init2(mp_D,p); mpfr_init2(fourth, p); mpfr_init2(half, p); 
    407     mpz_init(ztemp1); 
    408     mpz_init(ztemp2); 
    409     mpz_init(ztemp3); 
    410  
    411     mpq_init(qtemp1); 
    412     mpq_init(qtemp2); 
    413     mpq_init(qtemp3); 
    414396     
    415397    init = true; 
     
    477459    d_D = 2.0 * (n - 1.0/24.0) * sqrt(n - 1.0/24.0); 
    478460 
     461} 
     462 
     463void clear_constants() { 
     464        mpfr_clear(mp_one_over_12); mpfr_clear(mp_one_over_24); mpfr_clear(mp_sqrt2); mpfr_clear(mp_sqrt3); mpfr_clear(mp_pi); 
     465        mpfr_clear(mp_A); mpfr_clear(mp_B); mpfr_clear(mp_C); mpfr_clear(mp_D); mpfr_clear(half); mpfr_clear(fourth); 
     466} 
     467 
     468void initialize_mpz_and_mpq_variables() { 
     469    /*  
     470     * We use a few mpz_t and mpq_t variables which need to be initialized 
     471     * before they can be used. Initialization and clearing take some 
     472     * time, so we initialize just once in this function, and clear in another. 
     473     */ 
     474    mpz_init(ztemp1); 
     475    mpz_init(ztemp2); 
     476    mpz_init(ztemp3); 
     477 
     478    mpq_init(qtemp1); 
     479    mpq_init(qtemp2); 
     480    mpq_init(qtemp3); 
     481} 
     482 
     483void clear_mpz_and_mpq_variables() { 
     484    mpz_clear(ztemp1); 
     485    mpz_clear(ztemp2); 
     486    mpz_clear(ztemp3); 
     487 
     488    mpq_clear(qtemp1); 
     489    mpq_clear(qtemp2); 
     490    mpq_clear(qtemp3); 
    479491} 
    480492 
     
    890902    mpfr_set_ui(result, 0, round_mode);                             // 
    891903                                                                     
     904    initialize_mpz_and_mpq_variables(); 
    892905    initialize_constants(initial_precision, n);                     // Now that we have the precision information, we initialize some constants 
    893906                                                                    // that will be used throughout, and also 
    894907                                                                    // 
    895     mp_set_precision(initial_precision);                            // set the precision of the "temp" variables that are used in individual functions. 
     908    initialize_mpfr_variables(initial_precision);                            // set the precision of the "temp" variables that are used in individual functions. 
    896909 
    897910    unsigned int current_precision = initial_precision; 
     
    967980        if(new_precision != current_precision) {                    // If the precision changes, we need to clear 
    968981            current_precision = new_precision;                      // and reinitialize all "temp" variables to 
    969             mp_set_precision(current_precision);                    // use lower precision. 
     982            clear_mpfr_variables();                                 // use lower precision. 
     983            initialize_mpfr_variables(current_precision);           // 
    970984            mpfr_clear(t1); mpfr_clear(t2);                         // 
    971985            mpfr_init2(t1, current_precision);                      // 
     
    9881002    mpfr_div(result, result, mp_pi, round_mode);                    // The actual result is the sum that we have computed 
    9891003    mpfr_div(result, result, mp_sqrt2, round_mode);                 // divided by pi*sqrt(2). 
     1004 
     1005    clear_constants(); 
     1006    clear_mpz_and_mpq_variables(); 
     1007    clear_mpfr_variables(); 
     1008    mpfr_clear(t1); 
     1009    mpfr_clear(t2);                         // 
    9901010} 
    9911011 
     
    12991319    mpfr_get_z(answer, result, round_mode); 
    13001320 
     1321    mpfr_clear(result); 
    13011322    return 0; 
    13021323} 
Note: See TracChangeset for help on using the changeset viewer.