Ticket #10258: 10258_sage_malloc.patch

File 10258_sage_malloc.patch, 29.3 KB (added by jdemeyer, 11 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  
    126126# whitespace without the syntax clutter of lists of strings.
    127127includes = ['$SAGE_LOCAL/include/', '$SAGE_LOCAL/include/python2.6/',
    128128            '$SAGE_LOCAL/include/NTL/', 'include']
    129 cFiles = Split( "convert.c  interrupt.c  mpn_pylong.c  mpz_pylong.c") + \
     129cFiles = Split( "convert.c  interrupt.c  memory.c  mpn_pylong.c  mpz_pylong.c") + \
    130130         Split( "mpz_longlong.c stdsage.c  gmp_globals.c" )
    131131cppFiles = Split( "ZZ_pylong.cpp  ntl_wrap.cpp" )
    132132srcFiles = cFiles + cppFiles
  • new file c_lib/include/memory.h

    diff -r 37b3eef312da -r 2fa68ea15333 c_lib/include/memory.h
    - +  
     1/*
     2Wrappers for malloc(), calloc(), free(), realloc().
     3
     4
     5AUTHORS:
     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
     26extern "C" {
     27#endif
     28
     29static inline void* sage_malloc(size_t n)
     30{
     31    sig_block();
     32    void* ret = malloc(n);
     33    sig_unblock();
     34    return ret;
     35}
     36static 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}
     43static inline void sage_free(void* ptr)
     44{
     45    sig_block();
     46    free(ptr);
     47    sig_unblock();
     48}
     49static 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
     57void 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  
    3333#define STDSAGE_H
    3434
    3535#include "Python.h"
     36#include "memory.h"
    3637
    3738/* Building with this not commented out causes
    3839   serious problems on RHEL5 64-bit for Kiran Kedlaya... i.e., it doesn't work. */
     
    139140extern PyObject* global_empty_tuple;
    140141
    141142
    142 
    143 /*****************************************
    144           Memory management
    145 
    146 NOTE -- before changing these away from Python's keep the following in
    147 mind (from the Python C/API guide):
    148 
    149 "In most situations, however, it is recommended to allocate memory from
    150 the Python heap specifically because the latter is under control of
    151 the Python memory manager. For example, this is required when the
    152 interpreter is extended with new object types written in C. Another
    153 reason for using the Python heap is the desire to inform the Python
    154 memory manager about the memory needs of the extension module. Even
    155 when the requested memory is used exclusively for internal,
    156 highly-specific purposes, delegating all memory requests to the Python
    157 memory manager causes the interpreter to have a more accurate image of
    158 its memory footprint as a whole. Consequently, under certain
    159 circumstances, the Python memory manager may or may not trigger
    160 appropriate actions, like garbage collection, memory compaction or
    161 other preventive procedures. Note that by using the C library
    162 allocator as shown in the previous example, the allocated memory for
    163 the I/O buffer escapes completely the Python memory manager."
    164 
    165  *****************************************/
    166 
    167 #define sage_malloc  malloc
    168 #define sage_calloc  calloc
    169 #define sage_free    free
    170 #define sage_realloc realloc
    171 
    172143/**
    173144 * Initialisation of signal handlers, global variables, etc. Called
    174145 * exactly once at Sage start-up.
  • new file c_lib/src/memory.c

    diff -r 37b3eef312da -r 2fa68ea15333 c_lib/src/memory.c
    - +  
     1/*
     2Wrappers for malloc(), calloc(), free(), realloc().
     3
     4
     5AUTHORS:
     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 */
     24void* sage_mpir_malloc(size_t size)
     25{
     26    return sage_malloc(size);
     27}
     28
     29void* sage_mpir_realloc(void *ptr, size_t old_size, size_t new_size)
     30{
     31    return sage_realloc(ptr, new_size);
     32}
     33
     34void sage_mpir_free(void *ptr, size_t size)
     35{
     36    sage_free(ptr);
     37}
     38
     39void 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  
    4747 * Cygwin, this is also called from init_csage_module(). */
    4848void init_csage() {
    4949    init_global_empty_tuple();
     50    init_memory_functions();
    5051    setup_sage_signal_handler();
    5152    setup_NTL_error_callback(global_NTL_error_callback, NULL);
    5253}
  • module_list.py

    diff -r 37b3eef312da -r 2fa68ea15333 module_list.py
    a b  
    12281228    Extension('sage.rings.laurent_series_ring_element',
    12291229              sources = ['sage/rings/laurent_series_ring_element.pyx']),
    12301230
    1231     Extension('sage.rings.memory',
    1232               sources = ['sage/rings/memory.pyx'],
    1233               libraries=['gmp','stdc++']),
    1234 
    12351231    Extension('sage.rings.morphism',
    12361232              sources = ['sage/rings/morphism.pyx']),
    12371233
  • sage/all.py

    diff -r 37b3eef312da -r 2fa68ea15333 sage/all.py
    a b  
    5151
    5252###################################################################
    5353
    54 from time                import sleep
    55 
    56 from sage.rings.memory import pmem_malloc
    57 pmem_malloc()
    58 
    5954from sage.ext.c_lib import _init_csage, sig_on_count
    6055_init_csage()
    6156
     57from time                import sleep
     58
    6259from sage.misc.all       import *         # takes a while
    6360
    6461from sage.misc.sh import sh
     
    7168# This must come before Calculus -- it initializes the Pynac library.
    7269import sage.symbolic.pynac
    7370
    74 pmem_malloc()
    75 
    7671from sage.modules.all    import *
    7772from sage.monoids.all    import *
    7873from sage.algebras.all   import *
  • sage/combinat/combinat_cython.pyx

    diff -r 37b3eef312da -r 2fa68ea15333 sage/combinat/combinat_cython.pyx
    a b  
    1111
    1212include "../ext/stdsage.pxi"
    1313
    14 from stdlib cimport malloc, free
    1514
    1615from sage.libs.gmp.all cimport *
    1716from sage.rings.integer cimport Integer
     
    9493        mpz_init(t)
    9594        mpz_init(u)
    9695        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))
    9897        mpz_init_set_ui(bc[0], 1)
    9998        for j in range(1, max_bc+1):
    10099            mpz_init_set(bc[j], bc[j-1])
     
    116115                mpz_mul_2exp(u, u, n)
    117116        for j in range(max_bc+1):   # careful: 0 ... max_bc
    118117            mpz_clear(bc[j])
    119         free(bc)
     118        sage_free(bc)
    120119        mpz_fac_ui(t, k)
    121120        mpz_tdiv_q(s, s, t)
    122121        mpz_clear(t)
  • sage/ext/stdsage.pxi

    diff -r 37b3eef312da -r 2fa68ea15333 sage/ext/stdsage.pxi
    a b  
    4545    void* sage_malloc(size_t)
    4646    void  init_csage()
    4747    void  init_csage_module()
     48    void  init_memory_functions()
    4849
    4950
    5051# 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  
    326326    mpz_set_ui(i,n)
    327327    cdef char* s=mpz_get_str(NULL, 2, i)
    328328    t=str(s)
    329     free(s)
     329    sage_free(s)
    330330    mpz_clear(i)
    331331    return t
    332332
  • sage/gsl/ode.pyx

    diff -r 37b3eef312da -r 2fa68ea15333 sage/gsl/ode.pyx
    a b  
    1818
    1919include '../ext/cdefs.pxi'
    2020include '../ext/interrupt.pxi'
     21include '../ext/stdsage.pxi'
    2122include 'gsl.pxi'
    2223
    2324
     
    398399        cdef double * scale_abs_array
    399400        scale_abs_array=NULL
    400401
    401         y= <double*> malloc(sizeof(double)*(dim))
     402        y= <double*> sage_malloc(sizeof(double)*(dim))
    402403        if y==NULL:
    403404            raise MemoryError,"error allocating memory"
    404405        result=[]
     
    437438        cdef gsl_odeiv_step * s
    438439        s  = gsl_odeiv_step_alloc (T, dim)
    439440        if s==NULL:
    440             free(y)
     441            sage_free(y)
    441442            raise MemoryError, "error setting up solver"
    442443
    443444
     
    450451                c = gsl_odeiv_control_standard_new(self.error_abs,self.error_rel,self.a,self.a_dydt)
    451452            elif hasattr(self.scale_abs,'__len__'):
    452453                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))
    454455                    for i from 0 <=i<dim:
    455456                        scale_abs_array[i]=self.scale_abs[i]
    456457                    c = gsl_odeiv_control_scaled_new(self.error_abs,self.error_rel,self.a,self.a_dydt,scale_abs_array,dim)
     
    458459        if c == NULL:
    459460            gsl_odeiv_control_free (c)
    460461            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)
    464464            raise MemoryError, "error setting up solver"
    465465
    466466
     
    470470        if e == NULL:
    471471            gsl_odeiv_control_free (c)
    472472            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)
    476475            raise MemoryError, "error setting up solver"
    477476
    478477
     
    500499                gsl_odeiv_evolve_free (e)
    501500                gsl_odeiv_control_free (c)
    502501                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)
    506504                raise TypeError,"numpoints must be integer"
    507505            result.append( (self.t_span[0],self.y_0))
    508506            delta = (self.t_span[1]-self.t_span[0])/(1.0*num_points)
     
    518516                        gsl_odeiv_evolve_free (e)
    519517                        gsl_odeiv_control_free (c)
    520518                        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)
    524521                        raise ValueError,"error solving"
    525522
    526523
     
    528525                        gsl_odeiv_evolve_free (e)
    529526                        gsl_odeiv_control_free (c)
    530527                        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)
    534530                        raise ValueError,"error solving"
    535531
    536532                for j  from 0<=j<dim:
     
    554550                        gsl_odeiv_evolve_free (e)
    555551                        gsl_odeiv_control_free (c)
    556552                        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)
    560555                        raise ValueError,"error solving"
    561556
    562557
     
    564559                        gsl_odeiv_evolve_free (e)
    565560                        gsl_odeiv_control_free (c)
    566561                        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)
    570564                        raise ValueError,"error solving"
    571565
    572566
     
    582576        gsl_odeiv_evolve_free (e)
    583577        gsl_odeiv_control_free (c)
    584578        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)
    588581        self.solution = result
  • sage/gsl/probability_distribution.pyx

    diff -r 37b3eef312da -r 2fa68ea15333 sage/gsl/probability_distribution.pyx
    a b  
    3838
    3939import sage.plot.plot
    4040include '../ext/cdefs.pxi'
    41 #include '../ext/interrupt.pxi'
     41include '../ext/stdsage.pxi'
    4242include 'gsl.pxi'
    4343#cimport sage.rings.real_double
    4444import sage.rings.real_double
     
    203203            self.seed = random.randint(1, 2^32)
    204204        self.set_seed(self.seed)
    205205        self.dimension = dimension
    206         self.vec = <double *>malloc(self.dimension*(sizeof(double)))
     206        self.vec = <double *>sage_malloc(self.dimension*(sizeof(double)))
    207207
    208208    def set_seed(self, seed):
    209209        """
     
    246246    def __dealloc__(self):
    247247        if self.r != NULL:
    248248            gsl_rng_free(self.r)
    249         free(self.vec)
     249        sage_free(self.vec)
    250250
    251251    def get_random_element(self):
    252252        """
     
    518518        if self.r != NULL:
    519519            gsl_rng_free(self.r)
    520520        if self.parameters != NULL:
    521             free(self.parameters)
     521            sage_free(self.parameters)
    522522
    523523    def __str__(self):
    524524        """
     
    581581            sage: T.set_distribution('pareto', [0, 1])
    582582        """
    583583        if self.parameters != NULL:
    584             free(self.parameters)
     584            sage_free(self.parameters)
    585585 
    586586        if name == 'uniform':
    587587          self.distribution_type = uniform
     
    590590                  float(x)
    591591              except:
    592592                  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)
    594594          self.parameters[0] = parameters[0]
    595595          self.parameters[1] = parameters[1]
    596596        elif name == 'gaussian':
     
    598598                float(parameters)
    599599            except:
    600600                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))
    602602            self.parameters[0] = float(parameters)
    603603            self.distribution_type = gaussian
    604604        elif name == 'pareto':
     
    608608                map(float, parameters)
    609609            except:
    610610                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)
    612612            self.parameters[0] = float(parameters[0])
    613613            self.parameters[1] = float(parameters[1])
    614614            self.distribution_type = pareto
     
    618618                float(parameters)
    619619            except:
    620620                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))
    622622            self.parameters[0] = float(parameters)
    623623            self.distribution_type = rayleigh
    624624        elif name == 'lognormal':
     
    630630                except:
    631631                    raise TypeError, "Lognormal distributions requires real parameters"
    632632 
    633             self.parameters = <double*>malloc(sizeof(double)*2)
     633            self.parameters = <double*>sage_malloc(sizeof(double)*2)
    634634            self.parameters[0] = float(parameters[0])
    635635            self.parameters[1] = float(parameters[1])
    636636            self.distribution_type = lognormal
     
    639639                float(parameters)
    640640            except:
    641641                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))
    643643            self.parameters[0] = float(parameters)
    644644            self.distribution_type = t
    645645 
     
    648648                float(parameters)
    649649            except:
    650650                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))
    652652            self.parameters[0] = float(parameters)
    653653            self.distribution_type = chisquared
    654654        elif name == 'exppow':
     
    660660                except:
    661661                    raise TypeError, "exponential power distributions requires real parameters"
    662662 
    663             self.parameters = <double*>malloc(sizeof(double)*2)
     663            self.parameters = <double*>sage_malloc(sizeof(double)*2)
    664664            self.parameters[0] = float(parameters[0])
    665665            self.parameters[1] = float(parameters[1])
    666666            self.distribution_type = exppow
     
    672672            except:
    673673                raise TypeError, "weibull distribution requires real parameters"
    674674
    675             self.parameters = <double *>malloc(sizeof(double)*2)
     675            self.parameters = <double *>sage_malloc(sizeof(double)*2)
    676676            self.parameters[0] = float(parameters[0])
    677677            self.parameters[1] = float(parameters[1])
    678678            self.distribution_type = weibull
     
    685685            except:
    686686                raise TypeError, "beta distribution requires real parameters"
    687687
    688             self.parameters = <double *>malloc(sizeof(double)*2)
     688            self.parameters = <double *>sage_malloc(sizeof(double)*2)
    689689            self.parameters[0] = float(parameters[0])
    690690            self.parameters[1] = float(parameters[1])
    691691            self.distribution_type = beta
     
    919919        n = len(P)
    920920
    921921        cdef double *P_vec
    922         P_vec = <double *> malloc(n*(sizeof(double)))
     922        P_vec = <double *> sage_malloc(n*(sizeof(double)))
    923923
    924924        cdef int i
    925925        for i in range(n):
     
    927927
    928928        self.dist = gsl_ran_discrete_preproc(n, P_vec)
    929929
    930         free(P_vec)
     930        sage_free(P_vec)
    931931
    932932    def set_seed(self, seed):
    933933        """
  • sage/libs/all.py

    diff -r 37b3eef312da -r 2fa68ea15333 sage/libs/all.py
    a b  
    1 from sage.rings.memory import pmem_malloc
    2 
    31import sage.libs.ntl.all  as ntl
    42
    5 #import sage.libs.cf.cf as cf
    6 
    7 pmem_malloc()
    8 
    9 
    103from sage.libs.pari.all   import pari, pari_gen, allocatemem, PariError
    114
    125from 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  
    116116        """
    117117        cdef char* ss = fmpz_poly_to_string(self.poly)
    118118        cdef object s = ss
    119         free(ss)
     119        sage_free(ss)
    120120        return s
    121121       
    122122    def degree(self):
  • sage/libs/mwrank/mwrank.pyx

    diff -r 37b3eef312da -r 2fa68ea15333 sage/libs/mwrank/mwrank.pyx
    a b  
    2323import sys
    2424
    2525include '../../ext/interrupt.pxi'
     26include '../../ext/stdsage.pxi'
    2627
    2728# Need to permit tabs in order to doctest verbose output.
    2829"""
    2930SAGE_DOCTEST_ALLOW_TABS
    3031"""
    3132
    32 cdef extern from "stdlib.h":
    33     void free(void *ptr)
    34 
    3533cdef extern from "wrap.h":
    3634    ### misc functions ###
    3735    long mwrank_get_precision()
     
    9795    sig_off()
    9896    # Makes a python string and deletes what is pointed to by s.
    9997    t = str(s)
    100     free(s)
     98    sage_free(s)
    10199    return t
    102100
    103101class __init:
  • sage/libs/ntl/ntl_ZZX.pyx

    diff -r 37b3eef312da -r 2fa68ea15333 sage/libs/ntl/ntl_ZZX.pyx
    a b  
    11141114        F = []
    11151115        for i from 0 <= i < n:
    11161116            F.append((make_ZZX(v[i]), e[i]))
    1117         free(v)
    1118         free(e)
     1117        sage_free(v)
     1118        sage_free(e)
    11191119        return F
    11201120
    11211121
  • sage/libs/ntl/ntl_ZZ_pX.pyx

    diff -r 37b3eef312da -r 2fa68ea15333 sage/libs/ntl/ntl_ZZ_pX.pyx
    a b  
    163163        self.c.restore_c()
    164164        #cdef char* s = ZZ_pX_repr(&self.x)
    165165        #t = str(s)
    166         #free(s)
     166        #sage_free(s)
    167167        return ZZ_pX_to_PyString(&self.x)
    168168        #return t
    169169
     
    896896            #F.append((make_ZZ_pX(v[i], self.c), e[i]))
    897897        for i from 0 <= i < n:
    898898            ZZ_pX_delete(v[i])
    899         free(v)
    900         free(e)
     899        sage_free(v)
     900        sage_free(e)
    901901        return F
    902902
    903903    def linear_roots(self):
     
    941941            #F.append(make_ZZ_p(v[i], self.c))
    942942        for i from 0 <= i < n:
    943943            ZZ_p_delete(v[i])
    944         free(v)
     944        sage_free(v)
    945945        return F
    946946
    947947    def reverse(self, hi=None):
  • sage/libs/singular/singular.pyx

    diff -r 37b3eef312da -r 2fa68ea15333 sage/libs/singular/singular.pyx
    a b  
    634634
    635635# Our attempt at avoiding exponent overflows.
    636636cdef unsigned int max_exponent_size
    637 import sage.rings.memory
    638637
    639638cdef init_libsingular():
    640639    """
     
    668667    # load SINGULAR
    669668    siInit(lib)
    670669
    671     # steal memory manager back or weird things may happen
    672     sage.rings.memory.pmem_malloc()
    673 
    674670    dlclose(handle)
    675671
    676672    # 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  
    35103510        for i from 0 <= i < dim*self._ncols:
    35113511            mpz_init_set(M._entries[i], mp_N[i])
    35123512            mpz_clear(mp_N[i])
    3513         free(mp_N)
     3513        sage_free(mp_N)
    35143514
    35153515        verbose("finished computing null space", time)
    35163516        M._initialized = True
  • sage/modular/modform/eis_series_cython.pyx

    diff -r 37b3eef312da -r 2fa68ea15333 sage/modular/modform/eis_series_cython.pyx
    a b  
    2828        sage: eisenstein_series_poly(12, prec=5)
    2929        5  691 65520 134250480 11606736960 274945048560
    3030    """
    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))
    3232    cdef mpz_t one, mult, term, last, term_m1, last_m1
    3333    cdef unsigned long int expt
    3434    cdef long ind, ppow, int_p
     
    4242    if (prec == 0):
    4343        return PY_NEW(Fmpz_poly)
    4444
     45    sig_on()
     46
    4547    mpz_init(one)
    4648    mpz_init(term)
    4749    mpz_init(last)
     
    9395    fmpz_poly_scalar_mul_mpz(res.poly, res.poly, (<Integer>(a0.denominator())).value)
    9496    fmpz_poly_set_coeff_mpz(res.poly, 0, (<Integer>(a0.numerator())).value)
    9597
     98    sage_free(val)
     99
     100    sig_off()
     101
    96102    return res
  • sage/rings/integer.pyx

    diff -r 37b3eef312da -r 2fa68ea15333 sage/rings/integer.pyx
    a b  
    37833783       
    37843784        # we need (at most) log_2(#factors) concurrent sub-products
    37853785        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))
    37873787        if sub_prods == NULL:
    37883788            raise MemoryError
    37893789        for i from 0 <= i < prod_count:
     
    38143814       
    38153815        for i from 0 <= i < prod_count:
    38163816            mpz_clear(sub_prods[i])
    3817         free(sub_prods)
     3817        sage_free(sub_prods)
    38183818
    38193819        return z
    38203820
  • deleted file sage/rings/memory.pyx

    diff -r 37b3eef312da -r 2fa68ea15333 sage/rings/memory.pyx
    + -  
    1 # cython: profile=False
    2 
    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 evil
    28     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  
    10321032            ZZX_to_fmpz_poly(fac.__poly, v[i][0])
    10331033            F.append( (fac,e[i]) )
    10341034            ZZX_delete(v[i])
    1035         free(v)
    1036         free(e)
     1035        sage_free(v)
     1036        sage_free(e)
    10371037
    10381038        return Factorization(F, unit=z, sort=False)
    10391039
  • sage/rings/polynomial/polynomial_integer_dense_ntl.pyx

    diff -r 37b3eef312da -r 2fa68ea15333 sage/rings/polynomial/polynomial_integer_dense_ntl.pyx
    a b  
    855855            z.__poly = v[i][0]
    856856            F.append((z, e[i]))
    857857            ZZX_delete(v[i])
    858         free(v)
    859         free(e)
     858        sage_free(v)
     859        sage_free(e)
    860860        return Factorization(F, unit=c, sort=False)
    861861
    862862    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  
    181181            L1 = [e if isinstance(e, Rational) else Rational(e) for e in x]
    182182            n  = <unsigned long> len(x)
    183183            sig_on()
    184             L2 = <mpq_t *> malloc(n * sizeof(mpq_t))
     184            L2 = <mpq_t *> sage_malloc(n * sizeof(mpq_t))
    185185            for deg from 0 <= deg < n:
    186186                mpq_init(L2[deg])
    187187                mpq_set(L2[deg], (<Rational> L1[deg]).value)
    188188            _fmpq_poly_from_list(self.__poly, L2, n)
    189189            for deg from 0 <= deg < n:
    190190                mpq_clear(L2[deg])
    191             free(L2)
     191            sage_free(L2)
    192192            sig_off()
    193193           
    194194#           deg = 0