Ticket #10258: 10258_sage_malloc.patch
| File 10258_sage_malloc.patch, 29.3 KB (added by jdemeyer, 2 years ago) |
|---|
-
c_lib/SConstruct
# HG changeset patch # User Jeroen Demeyer <jdemeyer@cage.ugent.be> # Date 1295598971 -3600 # Node ID 2fa68ea15333fba1ac0859c15a0073d1b30d6442 # Parent 37b3eef312daede262510475697cfbdb7ac33d52 #10258: clean up sage memory handling functions New files c_lib/include/memory.h and c_lib/src/memory.c defining sage_malloc() and friends and init_memory_functions(), moved from sage/rings/memory.pyx. Also block interrupts during allocation functions. diff -r 37b3eef312da -r 2fa68ea15333 c_lib/SConstruct
a b 126 126 # whitespace without the syntax clutter of lists of strings. 127 127 includes = ['$SAGE_LOCAL/include/', '$SAGE_LOCAL/include/python2.6/', 128 128 '$SAGE_LOCAL/include/NTL/', 'include'] 129 cFiles = Split( "convert.c interrupt.c m pn_pylong.c mpz_pylong.c") + \129 cFiles = Split( "convert.c interrupt.c memory.c mpn_pylong.c mpz_pylong.c") + \ 130 130 Split( "mpz_longlong.c stdsage.c gmp_globals.c" ) 131 131 cppFiles = Split( "ZZ_pylong.cpp ntl_wrap.cpp" ) 132 132 srcFiles = cFiles + cppFiles -
new file c_lib/include/memory.h
diff -r 37b3eef312da -r 2fa68ea15333 c_lib/include/memory.h
- + 1 /* 2 Wrappers for malloc(), calloc(), free(), realloc(). 3 4 5 AUTHORS: 6 7 - Jeroen Demeyer (2011-01-13): initial version (#10258) 8 9 */ 10 11 /***************************************************************************** 12 * Copyright (C) 2011 Jeroen Demeyer <jdemeyer@cage.ugent.be> 13 * 14 * Distributed under the terms of the GNU General Public License (GPL) 15 * as published by the Free Software Foundation; either version 2 of 16 * the License, or (at your option) any later version. 17 * http://www.gnu.org/licenses/ 18 ****************************************************************************/ 19 20 #ifndef C_LIB_INCLUDE_MEMORY_H 21 #define C_LIB_INCLUDE_MEMORY_H 22 23 #include "interrupt.h" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 29 static inline void* sage_malloc(size_t n) 30 { 31 sig_block(); 32 void* ret = malloc(n); 33 sig_unblock(); 34 return ret; 35 } 36 static inline void* sage_calloc(size_t nmemb, size_t size) 37 { 38 sig_block(); 39 void* ret = calloc(nmemb, size); 40 sig_unblock(); 41 return ret; 42 } 43 static inline void sage_free(void* ptr) 44 { 45 sig_block(); 46 free(ptr); 47 sig_unblock(); 48 } 49 static inline void* sage_realloc(void *ptr, size_t size) 50 { 51 sig_block(); 52 void* ret = realloc(ptr, size); 53 sig_unblock(); 54 return ret; 55 } 56 57 void init_memory_functions(); 58 59 60 #ifdef __cplusplus 61 } /* extern "C" */ 62 #endif 63 #endif /* C_LIB_INCLUDE_MEMORY_H */ -
c_lib/include/stdsage.h
diff -r 37b3eef312da -r 2fa68ea15333 c_lib/include/stdsage.h
a b 33 33 #define STDSAGE_H 34 34 35 35 #include "Python.h" 36 #include "memory.h" 36 37 37 38 /* Building with this not commented out causes 38 39 serious problems on RHEL5 64-bit for Kiran Kedlaya... i.e., it doesn't work. */ … … 139 140 extern PyObject* global_empty_tuple; 140 141 141 142 142 143 /*****************************************144 Memory management145 146 NOTE -- before changing these away from Python's keep the following in147 mind (from the Python C/API guide):148 149 "In most situations, however, it is recommended to allocate memory from150 the Python heap specifically because the latter is under control of151 the Python memory manager. For example, this is required when the152 interpreter is extended with new object types written in C. Another153 reason for using the Python heap is the desire to inform the Python154 memory manager about the memory needs of the extension module. Even155 when the requested memory is used exclusively for internal,156 highly-specific purposes, delegating all memory requests to the Python157 memory manager causes the interpreter to have a more accurate image of158 its memory footprint as a whole. Consequently, under certain159 circumstances, the Python memory manager may or may not trigger160 appropriate actions, like garbage collection, memory compaction or161 other preventive procedures. Note that by using the C library162 allocator as shown in the previous example, the allocated memory for163 the I/O buffer escapes completely the Python memory manager."164 165 *****************************************/166 167 #define sage_malloc malloc168 #define sage_calloc calloc169 #define sage_free free170 #define sage_realloc realloc171 172 143 /** 173 144 * Initialisation of signal handlers, global variables, etc. Called 174 145 * exactly once at Sage start-up. -
new file c_lib/src/memory.c
diff -r 37b3eef312da -r 2fa68ea15333 c_lib/src/memory.c
- + 1 /* 2 Wrappers for malloc(), calloc(), free(), realloc(). 3 4 5 AUTHORS: 6 7 - Jeroen Demeyer (2011-01-13): initial version (#10258) 8 9 */ 10 11 /***************************************************************************** 12 * Copyright (C) 2011 Jeroen Demeyer <jdemeyer@cage.ugent.be> 13 * 14 * Distributed under the terms of the GNU General Public License (GPL) 15 * as published by the Free Software Foundation; either version 2 of 16 * the License, or (at your option) any later version. 17 * http://www.gnu.org/licenses/ 18 ****************************************************************************/ 19 20 #include <mpir.h> 21 #include "memory.h" 22 23 /* mpir memory functions */ 24 void* sage_mpir_malloc(size_t size) 25 { 26 return sage_malloc(size); 27 } 28 29 void* sage_mpir_realloc(void *ptr, size_t old_size, size_t new_size) 30 { 31 return sage_realloc(ptr, new_size); 32 } 33 34 void sage_mpir_free(void *ptr, size_t size) 35 { 36 sage_free(ptr); 37 } 38 39 void init_memory_functions() 40 { 41 #if 0 42 void* (*mpir_malloc)(size_t); 43 void* (*mpir_realloc)(void*, size_t, size_t); 44 void (*mpir_free)(void*, size_t); 45 mp_get_memory_functions(&mpir_malloc, &mpir_realloc, &mpir_free); 46 printf("MPIR memory functions BEFORE: %p %p %p\n", mpir_malloc, mpir_realloc, mpir_free); 47 #endif 48 mp_set_memory_functions(sage_mpir_malloc, sage_mpir_realloc, sage_mpir_free); 49 #if 0 50 mp_get_memory_functions(&mpir_malloc, &mpir_realloc, &mpir_free); 51 printf("MPIR memory functions AFTER: %p %p %p\n", mpir_malloc, mpir_realloc, mpir_free); 52 #endif 53 } -
c_lib/src/stdsage.c
diff -r 37b3eef312da -r 2fa68ea15333 c_lib/src/stdsage.c
a b 47 47 * Cygwin, this is also called from init_csage_module(). */ 48 48 void init_csage() { 49 49 init_global_empty_tuple(); 50 init_memory_functions(); 50 51 setup_sage_signal_handler(); 51 52 setup_NTL_error_callback(global_NTL_error_callback, NULL); 52 53 } -
module_list.py
diff -r 37b3eef312da -r 2fa68ea15333 module_list.py
a b 1228 1228 Extension('sage.rings.laurent_series_ring_element', 1229 1229 sources = ['sage/rings/laurent_series_ring_element.pyx']), 1230 1230 1231 Extension('sage.rings.memory',1232 sources = ['sage/rings/memory.pyx'],1233 libraries=['gmp','stdc++']),1234 1235 1231 Extension('sage.rings.morphism', 1236 1232 sources = ['sage/rings/morphism.pyx']), 1237 1233 -
sage/all.py
diff -r 37b3eef312da -r 2fa68ea15333 sage/all.py
a b 51 51 52 52 ################################################################### 53 53 54 from time import sleep55 56 from sage.rings.memory import pmem_malloc57 pmem_malloc()58 59 54 from sage.ext.c_lib import _init_csage, sig_on_count 60 55 _init_csage() 61 56 57 from time import sleep 58 62 59 from sage.misc.all import * # takes a while 63 60 64 61 from sage.misc.sh import sh … … 71 68 # This must come before Calculus -- it initializes the Pynac library. 72 69 import sage.symbolic.pynac 73 70 74 pmem_malloc()75 76 71 from sage.modules.all import * 77 72 from sage.monoids.all import * 78 73 from sage.algebras.all import * -
sage/combinat/combinat_cython.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/combinat/combinat_cython.pyx
a b 11 11 12 12 include "../ext/stdsage.pxi" 13 13 14 from stdlib cimport malloc, free15 14 16 15 from sage.libs.gmp.all cimport * 17 16 from sage.rings.integer cimport Integer … … 94 93 mpz_init(t) 95 94 mpz_init(u) 96 95 max_bc = (k+1)//2 97 bc = <mpz_t*> malloc((max_bc+1) * sizeof(mpz_t))96 bc = <mpz_t*> sage_malloc((max_bc+1) * sizeof(mpz_t)) 98 97 mpz_init_set_ui(bc[0], 1) 99 98 for j in range(1, max_bc+1): 100 99 mpz_init_set(bc[j], bc[j-1]) … … 116 115 mpz_mul_2exp(u, u, n) 117 116 for j in range(max_bc+1): # careful: 0 ... max_bc 118 117 mpz_clear(bc[j]) 119 free(bc)118 sage_free(bc) 120 119 mpz_fac_ui(t, k) 121 120 mpz_tdiv_q(s, s, t) 122 121 mpz_clear(t) -
sage/ext/stdsage.pxi
diff -r 37b3eef312da -r 2fa68ea15333 sage/ext/stdsage.pxi
a b 45 45 void* sage_malloc(size_t) 46 46 void init_csage() 47 47 void init_csage_module() 48 void init_memory_functions() 48 49 49 50 50 51 # Do this for every single module that links in stdsage. -
sage/graphs/generic_graph_pyx.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/graphs/generic_graph_pyx.pyx
a b 326 326 mpz_set_ui(i,n) 327 327 cdef char* s=mpz_get_str(NULL, 2, i) 328 328 t=str(s) 329 free(s)329 sage_free(s) 330 330 mpz_clear(i) 331 331 return t 332 332 -
sage/gsl/ode.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/gsl/ode.pyx
a b 18 18 19 19 include '../ext/cdefs.pxi' 20 20 include '../ext/interrupt.pxi' 21 include '../ext/stdsage.pxi' 21 22 include 'gsl.pxi' 22 23 23 24 … … 398 399 cdef double * scale_abs_array 399 400 scale_abs_array=NULL 400 401 401 y= <double*> malloc(sizeof(double)*(dim))402 y= <double*> sage_malloc(sizeof(double)*(dim)) 402 403 if y==NULL: 403 404 raise MemoryError,"error allocating memory" 404 405 result=[] … … 437 438 cdef gsl_odeiv_step * s 438 439 s = gsl_odeiv_step_alloc (T, dim) 439 440 if s==NULL: 440 free(y)441 sage_free(y) 441 442 raise MemoryError, "error setting up solver" 442 443 443 444 … … 450 451 c = gsl_odeiv_control_standard_new(self.error_abs,self.error_rel,self.a,self.a_dydt) 451 452 elif hasattr(self.scale_abs,'__len__'): 452 453 if len(self.scale_abs)==dim: 453 scale_abs_array =<double *> malloc(dim*sizeof(double))454 scale_abs_array =<double *> sage_malloc(dim*sizeof(double)) 454 455 for i from 0 <=i<dim: 455 456 scale_abs_array[i]=self.scale_abs[i] 456 457 c = gsl_odeiv_control_scaled_new(self.error_abs,self.error_rel,self.a,self.a_dydt,scale_abs_array,dim) … … 458 459 if c == NULL: 459 460 gsl_odeiv_control_free (c) 460 461 gsl_odeiv_step_free (s) 461 free(y) 462 if scale_abs_array!=NULL: 463 free(scale_abs_array) 462 sage_free(y) 463 sage_free(scale_abs_array) 464 464 raise MemoryError, "error setting up solver" 465 465 466 466 … … 470 470 if e == NULL: 471 471 gsl_odeiv_control_free (c) 472 472 gsl_odeiv_step_free (s) 473 free(y) 474 if scale_abs_array!=NULL: 475 free(scale_abs_array) 473 sage_free(y) 474 sage_free(scale_abs_array) 476 475 raise MemoryError, "error setting up solver" 477 476 478 477 … … 500 499 gsl_odeiv_evolve_free (e) 501 500 gsl_odeiv_control_free (c) 502 501 gsl_odeiv_step_free (s) 503 free(y) 504 if scale_abs_array!=NULL: 505 free(scale_abs_array) 502 sage_free(y) 503 sage_free(scale_abs_array) 506 504 raise TypeError,"numpoints must be integer" 507 505 result.append( (self.t_span[0],self.y_0)) 508 506 delta = (self.t_span[1]-self.t_span[0])/(1.0*num_points) … … 518 516 gsl_odeiv_evolve_free (e) 519 517 gsl_odeiv_control_free (c) 520 518 gsl_odeiv_step_free (s) 521 free(y) 522 if scale_abs_array!=NULL: 523 free(scale_abs_array) 519 sage_free(y) 520 sage_free(scale_abs_array) 524 521 raise ValueError,"error solving" 525 522 526 523 … … 528 525 gsl_odeiv_evolve_free (e) 529 526 gsl_odeiv_control_free (c) 530 527 gsl_odeiv_step_free (s) 531 free(y) 532 if scale_abs_array!=NULL: 533 free(scale_abs_array) 528 sage_free(y) 529 sage_free(scale_abs_array) 534 530 raise ValueError,"error solving" 535 531 536 532 for j from 0<=j<dim: … … 554 550 gsl_odeiv_evolve_free (e) 555 551 gsl_odeiv_control_free (c) 556 552 gsl_odeiv_step_free (s) 557 free(y) 558 if scale_abs_array!=NULL: 559 free(scale_abs_array) 553 sage_free(y) 554 sage_free(scale_abs_array) 560 555 raise ValueError,"error solving" 561 556 562 557 … … 564 559 gsl_odeiv_evolve_free (e) 565 560 gsl_odeiv_control_free (c) 566 561 gsl_odeiv_step_free (s) 567 free(y) 568 if scale_abs_array!=NULL: 569 free(scale_abs_array) 562 sage_free(y) 563 sage_free(scale_abs_array) 570 564 raise ValueError,"error solving" 571 565 572 566 … … 582 576 gsl_odeiv_evolve_free (e) 583 577 gsl_odeiv_control_free (c) 584 578 gsl_odeiv_step_free (s) 585 free(y) 586 if scale_abs_array!=NULL: 587 free(scale_abs_array) 579 sage_free(y) 580 sage_free(scale_abs_array) 588 581 self.solution = result -
sage/gsl/probability_distribution.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/gsl/probability_distribution.pyx
a b 38 38 39 39 import sage.plot.plot 40 40 include '../ext/cdefs.pxi' 41 #include '../ext/interrupt.pxi'41 include '../ext/stdsage.pxi' 42 42 include 'gsl.pxi' 43 43 #cimport sage.rings.real_double 44 44 import sage.rings.real_double … … 203 203 self.seed = random.randint(1, 2^32) 204 204 self.set_seed(self.seed) 205 205 self.dimension = dimension 206 self.vec = <double *> malloc(self.dimension*(sizeof(double)))206 self.vec = <double *>sage_malloc(self.dimension*(sizeof(double))) 207 207 208 208 def set_seed(self, seed): 209 209 """ … … 246 246 def __dealloc__(self): 247 247 if self.r != NULL: 248 248 gsl_rng_free(self.r) 249 free(self.vec)249 sage_free(self.vec) 250 250 251 251 def get_random_element(self): 252 252 """ … … 518 518 if self.r != NULL: 519 519 gsl_rng_free(self.r) 520 520 if self.parameters != NULL: 521 free(self.parameters)521 sage_free(self.parameters) 522 522 523 523 def __str__(self): 524 524 """ … … 581 581 sage: T.set_distribution('pareto', [0, 1]) 582 582 """ 583 583 if self.parameters != NULL: 584 free(self.parameters)584 sage_free(self.parameters) 585 585 586 586 if name == 'uniform': 587 587 self.distribution_type = uniform … … 590 590 float(x) 591 591 except: 592 592 raise TypeError, "Uniform distribution requires parameters coercible to float" 593 self.parameters = <double*> malloc(sizeof(double)*2)593 self.parameters = <double*>sage_malloc(sizeof(double)*2) 594 594 self.parameters[0] = parameters[0] 595 595 self.parameters[1] = parameters[1] 596 596 elif name == 'gaussian': … … 598 598 float(parameters) 599 599 except: 600 600 raise TypeError, "gaussian distribution requires parameter sigma coercible to float" 601 self.parameters = <double*> malloc(sizeof(double))601 self.parameters = <double*>sage_malloc(sizeof(double)) 602 602 self.parameters[0] = float(parameters) 603 603 self.distribution_type = gaussian 604 604 elif name == 'pareto': … … 608 608 map(float, parameters) 609 609 except: 610 610 raise TypeError, "parameters must be coercible to float" 611 self.parameters = <double*> malloc(sizeof(double)*2)611 self.parameters = <double*>sage_malloc(sizeof(double)*2) 612 612 self.parameters[0] = float(parameters[0]) 613 613 self.parameters[1] = float(parameters[1]) 614 614 self.distribution_type = pareto … … 618 618 float(parameters) 619 619 except: 620 620 raise TypeError, "rayleigh distribution requires parameter sigma coercible to float" 621 self.parameters = <double*> malloc(sizeof(double))621 self.parameters = <double*>sage_malloc(sizeof(double)) 622 622 self.parameters[0] = float(parameters) 623 623 self.distribution_type = rayleigh 624 624 elif name == 'lognormal': … … 630 630 except: 631 631 raise TypeError, "Lognormal distributions requires real parameters" 632 632 633 self.parameters = <double*> malloc(sizeof(double)*2)633 self.parameters = <double*>sage_malloc(sizeof(double)*2) 634 634 self.parameters[0] = float(parameters[0]) 635 635 self.parameters[1] = float(parameters[1]) 636 636 self.distribution_type = lognormal … … 639 639 float(parameters) 640 640 except: 641 641 raise TypeError, "parameter to t distribution must be coercible to float" 642 self.parameters = <double*> malloc(sizeof(double))642 self.parameters = <double*>sage_malloc(sizeof(double)) 643 643 self.parameters[0] = float(parameters) 644 644 self.distribution_type = t 645 645 … … 648 648 float(parameters) 649 649 except: 650 650 raise TypeError, "parameters to t distribution must be coercible to float" 651 self.parameters = <double *> malloc(sizeof(double))651 self.parameters = <double *>sage_malloc(sizeof(double)) 652 652 self.parameters[0] = float(parameters) 653 653 self.distribution_type = chisquared 654 654 elif name == 'exppow': … … 660 660 except: 661 661 raise TypeError, "exponential power distributions requires real parameters" 662 662 663 self.parameters = <double*> malloc(sizeof(double)*2)663 self.parameters = <double*>sage_malloc(sizeof(double)*2) 664 664 self.parameters[0] = float(parameters[0]) 665 665 self.parameters[1] = float(parameters[1]) 666 666 self.distribution_type = exppow … … 672 672 except: 673 673 raise TypeError, "weibull distribution requires real parameters" 674 674 675 self.parameters = <double *> malloc(sizeof(double)*2)675 self.parameters = <double *>sage_malloc(sizeof(double)*2) 676 676 self.parameters[0] = float(parameters[0]) 677 677 self.parameters[1] = float(parameters[1]) 678 678 self.distribution_type = weibull … … 685 685 except: 686 686 raise TypeError, "beta distribution requires real parameters" 687 687 688 self.parameters = <double *> malloc(sizeof(double)*2)688 self.parameters = <double *>sage_malloc(sizeof(double)*2) 689 689 self.parameters[0] = float(parameters[0]) 690 690 self.parameters[1] = float(parameters[1]) 691 691 self.distribution_type = beta … … 919 919 n = len(P) 920 920 921 921 cdef double *P_vec 922 P_vec = <double *> malloc(n*(sizeof(double)))922 P_vec = <double *> sage_malloc(n*(sizeof(double))) 923 923 924 924 cdef int i 925 925 for i in range(n): … … 927 927 928 928 self.dist = gsl_ran_discrete_preproc(n, P_vec) 929 929 930 free(P_vec)930 sage_free(P_vec) 931 931 932 932 def set_seed(self, seed): 933 933 """ -
sage/libs/all.py
diff -r 37b3eef312da -r 2fa68ea15333 sage/libs/all.py
a b 1 from sage.rings.memory import pmem_malloc2 3 1 import sage.libs.ntl.all as ntl 4 2 5 #import sage.libs.cf.cf as cf6 7 pmem_malloc()8 9 10 3 from sage.libs.pari.all import pari, pari_gen, allocatemem, PariError 11 4 12 5 from sage.libs.mwrank.all import (mwrank_EllipticCurve, mwrank_MordellWeil, -
sage/libs/flint/fmpz_poly.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/libs/flint/fmpz_poly.pyx
a b 116 116 """ 117 117 cdef char* ss = fmpz_poly_to_string(self.poly) 118 118 cdef object s = ss 119 free(ss)119 sage_free(ss) 120 120 return s 121 121 122 122 def degree(self): -
sage/libs/mwrank/mwrank.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/libs/mwrank/mwrank.pyx
a b 23 23 import sys 24 24 25 25 include '../../ext/interrupt.pxi' 26 include '../../ext/stdsage.pxi' 26 27 27 28 # Need to permit tabs in order to doctest verbose output. 28 29 """ 29 30 SAGE_DOCTEST_ALLOW_TABS 30 31 """ 31 32 32 cdef extern from "stdlib.h":33 void free(void *ptr)34 35 33 cdef extern from "wrap.h": 36 34 ### misc functions ### 37 35 long mwrank_get_precision() … … 97 95 sig_off() 98 96 # Makes a python string and deletes what is pointed to by s. 99 97 t = str(s) 100 free(s)98 sage_free(s) 101 99 return t 102 100 103 101 class __init: -
sage/libs/ntl/ntl_ZZX.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/libs/ntl/ntl_ZZX.pyx
a b 1114 1114 F = [] 1115 1115 for i from 0 <= i < n: 1116 1116 F.append((make_ZZX(v[i]), e[i])) 1117 free(v)1118 free(e)1117 sage_free(v) 1118 sage_free(e) 1119 1119 return F 1120 1120 1121 1121 -
sage/libs/ntl/ntl_ZZ_pX.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/libs/ntl/ntl_ZZ_pX.pyx
a b 163 163 self.c.restore_c() 164 164 #cdef char* s = ZZ_pX_repr(&self.x) 165 165 #t = str(s) 166 # free(s)166 #sage_free(s) 167 167 return ZZ_pX_to_PyString(&self.x) 168 168 #return t 169 169 … … 896 896 #F.append((make_ZZ_pX(v[i], self.c), e[i])) 897 897 for i from 0 <= i < n: 898 898 ZZ_pX_delete(v[i]) 899 free(v)900 free(e)899 sage_free(v) 900 sage_free(e) 901 901 return F 902 902 903 903 def linear_roots(self): … … 941 941 #F.append(make_ZZ_p(v[i], self.c)) 942 942 for i from 0 <= i < n: 943 943 ZZ_p_delete(v[i]) 944 free(v)944 sage_free(v) 945 945 return F 946 946 947 947 def reverse(self, hi=None): -
sage/libs/singular/singular.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/libs/singular/singular.pyx
a b 634 634 635 635 # Our attempt at avoiding exponent overflows. 636 636 cdef unsigned int max_exponent_size 637 import sage.rings.memory638 637 639 638 cdef init_libsingular(): 640 639 """ … … 668 667 # load SINGULAR 669 668 siInit(lib) 670 669 671 # steal memory manager back or weird things may happen672 sage.rings.memory.pmem_malloc()673 674 670 dlclose(handle) 675 671 676 672 # we set and save some global Singular options -
sage/matrix/matrix_integer_dense.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/matrix/matrix_integer_dense.pyx
a b 3510 3510 for i from 0 <= i < dim*self._ncols: 3511 3511 mpz_init_set(M._entries[i], mp_N[i]) 3512 3512 mpz_clear(mp_N[i]) 3513 free(mp_N)3513 sage_free(mp_N) 3514 3514 3515 3515 verbose("finished computing null space", time) 3516 3516 M._initialized = True -
sage/modular/modform/eis_series_cython.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/modular/modform/eis_series_cython.pyx
a b 28 28 sage: eisenstein_series_poly(12, prec=5) 29 29 5 691 65520 134250480 11606736960 274945048560 30 30 """ 31 cdef mpz_t *val = <mpz_t *> malloc(prec * sizeof(mpz_t))31 cdef mpz_t *val = <mpz_t *>sage_malloc(prec * sizeof(mpz_t)) 32 32 cdef mpz_t one, mult, term, last, term_m1, last_m1 33 33 cdef unsigned long int expt 34 34 cdef long ind, ppow, int_p … … 42 42 if (prec == 0): 43 43 return PY_NEW(Fmpz_poly) 44 44 45 sig_on() 46 45 47 mpz_init(one) 46 48 mpz_init(term) 47 49 mpz_init(last) … … 93 95 fmpz_poly_scalar_mul_mpz(res.poly, res.poly, (<Integer>(a0.denominator())).value) 94 96 fmpz_poly_set_coeff_mpz(res.poly, 0, (<Integer>(a0.numerator())).value) 95 97 98 sage_free(val) 99 100 sig_off() 101 96 102 return res -
sage/rings/integer.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/rings/integer.pyx
a b 3783 3783 3784 3784 # we need (at most) log_2(#factors) concurrent sub-products 3785 3785 cdef int prod_count = <int>ceil_c(log_c(n/k+1)/log_c(2)) 3786 cdef mpz_t* sub_prods = <mpz_t*> malloc(prod_count * sizeof(mpz_t))3786 cdef mpz_t* sub_prods = <mpz_t*>sage_malloc(prod_count * sizeof(mpz_t)) 3787 3787 if sub_prods == NULL: 3788 3788 raise MemoryError 3789 3789 for i from 0 <= i < prod_count: … … 3814 3814 3815 3815 for i from 0 <= i < prod_count: 3816 3816 mpz_clear(sub_prods[i]) 3817 free(sub_prods)3817 sage_free(sub_prods) 3818 3818 3819 3819 return z 3820 3820 -
deleted file sage/rings/memory.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/rings/memory.pyx
+ - 1 # cython: profile=False2 3 include "../ext/cdefs.pxi"4 include "../ext/gmp.pxi"5 include "../ext/stdsage.pxi"6 7 cdef void* pymem_realloc(void *ptr, size_t old_size, size_t new_size):8 return sage_realloc(ptr, new_size)9 10 cdef void pymem_free(void *ptr, size_t size):11 sage_free(ptr)12 13 cdef void* pymem_malloc(size_t size):14 return sage_malloc(size)15 16 cdef extern from "gmp.h":17 void mp_set_memory_functions (void *(*alloc_func_ptr) (size_t), \18 void *(*realloc_func_ptr) (void *, size_t, size_t), \19 void (*free_func_ptr) (void *, size_t))20 21 22 def pmem_malloc():23 """24 Use our own memory manager for for GMP memory management.25 26 27 WARNING: Call this before the integer initialization or evil28 things might happen.29 """30 mp_set_memory_functions(sage_malloc, pymem_realloc, pymem_free)31 #mp_set_memory_functions(PyMem_Malloc, pymem_realloc, pymem_free)32 #mp_set_memory_functions(pymem_malloc, pymem_realloc, pymem_free)33 34 pmem_malloc()35 -
sage/rings/polynomial/polynomial_integer_dense_flint.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/rings/polynomial/polynomial_integer_dense_flint.pyx
a b 1032 1032 ZZX_to_fmpz_poly(fac.__poly, v[i][0]) 1033 1033 F.append( (fac,e[i]) ) 1034 1034 ZZX_delete(v[i]) 1035 free(v)1036 free(e)1035 sage_free(v) 1036 sage_free(e) 1037 1037 1038 1038 return Factorization(F, unit=z, sort=False) 1039 1039 -
sage/rings/polynomial/polynomial_integer_dense_ntl.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/rings/polynomial/polynomial_integer_dense_ntl.pyx
a b 855 855 z.__poly = v[i][0] 856 856 F.append((z, e[i])) 857 857 ZZX_delete(v[i]) 858 free(v)859 free(e)858 sage_free(v) 859 sage_free(e) 860 860 return Factorization(F, unit=c, sort=False) 861 861 862 862 def _factor_pari(self): -
sage/rings/polynomial/polynomial_rational_flint.pyx
diff -r 37b3eef312da -r 2fa68ea15333 sage/rings/polynomial/polynomial_rational_flint.pyx
a b 181 181 L1 = [e if isinstance(e, Rational) else Rational(e) for e in x] 182 182 n = <unsigned long> len(x) 183 183 sig_on() 184 L2 = <mpq_t *> malloc(n * sizeof(mpq_t))184 L2 = <mpq_t *> sage_malloc(n * sizeof(mpq_t)) 185 185 for deg from 0 <= deg < n: 186 186 mpq_init(L2[deg]) 187 187 mpq_set(L2[deg], (<Rational> L1[deg]).value) 188 188 _fmpq_poly_from_list(self.__poly, L2, n) 189 189 for deg from 0 <= deg < n: 190 190 mpq_clear(L2[deg]) 191 free(L2)191 sage_free(L2) 192 192 sig_off() 193 193 194 194 # deg = 0
