include "../ext/interrupt.pxi" # ctrl-c interrupt block support1include "../ext/stdsage.pxi"2include "../ext/python.pxi"3include "../ext/python_list.pxi"4include "../ext/python_number.pxi"5include "../ext/python_int.pxi"6include "../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 public 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 RichPyObject* o58o = <RichPyObject*>global_dummy5960# Make sure this never, ever gets collected61Py_INCREF(global_dummy)6263# By default every object created in Pyrex is garbage64# collected. This means it may have references to other objects65# the Garbage collector has to look out for. We remove this flag66# as the only reference an Integer has is to the global Integer67# ring. As this object is unique we don't need to garbage collect68# it as we always have a module level reference to it. If another69# attribute is added to the Integer class this flag removal so as70# the alloc and free functions may not be used anymore.71# This object will still be reference counted.72if not useGC:73flag = Py_TPFLAGS_HAVE_GC74o.ob_type.tp_flags = <long>(o.ob_type.tp_flags & (~flag))7576# Finally replace the functions called when an Integer needs77# to be constructed/destructed.78o.ob_type.tp_new = tp_new79o.ob_type.tp_dealloc = tp_dealloc808182