Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/misc/allocator.pyx
4036 views
1
include "../ext/interrupt.pxi" # ctrl-c interrupt block support
2
include "../ext/stdsage.pxi"
3
include "../ext/python.pxi"
4
include "../ext/python_list.pxi"
5
include "../ext/python_number.pxi"
6
include "../ext/python_int.pxi"
7
include "../ext/python_rich_object.pxi"
8
9
#def time_alloc_list(n):
10
#"""
11
#Allocate n a list of n Sage integers using PY_NEW.
12
#(Used for timing purposes.)
13
14
#EXAMPLES:
15
#sage: from sage.rings.integer import time_alloc_list
16
#sage: v = time_alloc_list(100)
17
#"""
18
#cdef int i
19
#l = []
20
#for i from 0 <= i < n:
21
#l.append(PY_NEW(Integer))
22
23
#return l
24
25
#def time_alloc(n):
26
#"""
27
#Time allocating n integers using PY_NEW.
28
#Used for timing purposes.
29
30
#EXAMPLES:
31
#sage: from sage.rings.integer import time_alloc
32
#sage: time_alloc(100)
33
#"""
34
#cdef int i
35
#for i from 0 <= i < n:
36
#z = PY_NEW(Integer)
37
38
#def pool_stats():
39
#"""
40
#Returns information about the Integer object pool.
41
42
#EXAMPLES:
43
#sage: from sage.rings.integer import pool_stats
44
#sage: pool_stats() # random-ish output
45
#Used pool 0 / 0 times
46
#Pool contains 3 / 100 items
47
#"""
48
#return ["Used pool %s / %s times" % (use_pool, total_alloc),
49
#"Pool contains %s / %s items" % (integer_pool_count, integer_pool_size)]
50
51
cdef public hook_tp_functions(object global_dummy, allocfunc tp_alloc, newfunc tp_new, freefunc tp_free, destructor tp_dealloc, bint useGC):
52
"""
53
Initialize the fast integer creation functions.
54
"""
55
56
cdef long flag
57
58
cdef RichPyObject* o
59
o = <RichPyObject*>global_dummy
60
61
# Make sure this never, ever gets collected
62
Py_INCREF(global_dummy)
63
64
# By default every object created in Pyrex is garbage
65
# collected. This means it may have references to other objects
66
# the Garbage collector has to look out for. We remove this flag
67
# as the only reference an Integer has is to the global Integer
68
# ring. As this object is unique we don't need to garbage collect
69
# it as we always have a module level reference to it. If another
70
# attribute is added to the Integer class this flag removal so as
71
# the alloc and free functions may not be used anymore.
72
# This object will still be reference counted.
73
if not useGC:
74
flag = Py_TPFLAGS_HAVE_GC
75
o.ob_type.tp_flags = <long>(o.ob_type.tp_flags & (~flag))
76
77
# Finally replace the functions called when an Integer needs
78
# to be constructed/destructed.
79
o.ob_type.tp_new = tp_new
80
o.ob_type.tp_dealloc = tp_dealloc
81
82