include "sage/ext/interrupt.pxi" # ctrl-c interrupt block support1include "sage/ext/stdsage.pxi"2include "sage/ext/python.pxi"3from cpython.list cimport *4from cpython.number cimport *5from cpython.int cimport *6include "sage/ext/python_rich_object.pxi"78#def time_alloc_list(n):9#"""10#Allocate n a list of n Sage integers using PY_NEW.11#(Used for timing purposes.)1213#EXAMPLES:14#sage: from sage.rings.integer import time_alloc_list15#sage: v = time_alloc_list(100)16#"""17#cdef int i18#l = []19#for i from 0 <= i < n:20#l.append(PY_NEW(Integer))2122#return l2324#def time_alloc(n):25#"""26#Time allocating n integers using PY_NEW.27#Used for timing purposes.2829#EXAMPLES:30#sage: from sage.rings.integer import time_alloc31#sage: time_alloc(100)32#"""33#cdef int i34#for i from 0 <= i < n:35#z = PY_NEW(Integer)3637#def pool_stats():38#"""39#Returns information about the Integer object pool.4041#EXAMPLES:42#sage: from sage.rings.integer import pool_stats43#sage: pool_stats() # random-ish output44#Used pool 0 / 0 times45#Pool contains 3 / 100 items46#"""47#return ["Used pool %s / %s times" % (use_pool, total_alloc),48#"Pool contains %s / %s items" % (integer_pool_count, integer_pool_size)]4950cdef hook_tp_functions(object global_dummy, allocfunc tp_alloc, newfunc tp_new, freefunc tp_free, destructor tp_dealloc, bint useGC):51"""52Initialize the fast integer creation functions.53"""5455cdef long flag5657cdef PyObject* o = <PyObject*>global_dummy58cdef RichPyTypeObject* t = <RichPyTypeObject*>o.ob_type5960# Make sure this never, ever gets collected.61# This is not necessary for cdef'ed variables as the global62# dummy integer, as such objects do not get automatically collected.63# In fact there is no obvious reason to prevent collection when Sage quits64# and we are certain no further call to the allocation function will be65# made; so this could be removed when the code is clean enough.66Py_INCREF(global_dummy)6768# By default every object created in Pyrex is garbage69# collected. This means it may have references to other objects70# the Garbage collector has to look out for. We remove this flag71# as the only reference an Integer has is to the global Integer72# ring. As this object is unique we don't need to garbage collect73# it as we always have a module level reference to it. If another74# attribute is added to the Integer class this flag removal so as75# the alloc and free functions may not be used anymore.76# This object will still be reference counted.77if not useGC:78flag = Py_TPFLAGS_HAVE_GC79t.tp_flags = <long>(t.tp_flags & (~flag))8081# Finally replace the functions called when an Integer needs82# to be constructed/destructed.83t.tp_new = tp_new84t.tp_dealloc = tp_dealloc858687