Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/misc/allocator.pyx
8814 views
1
include "sage/ext/interrupt.pxi" # ctrl-c interrupt block support
2
include "sage/ext/stdsage.pxi"
3
include "sage/ext/python.pxi"
4
from cpython.list cimport *
5
from cpython.number cimport *
6
from cpython.int cimport *
7
include "sage/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 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 PyObject* o = <PyObject*>global_dummy
59
cdef RichPyTypeObject* t = <RichPyTypeObject*>o.ob_type
60
61
# Make sure this never, ever gets collected.
62
# This is not necessary for cdef'ed variables as the global
63
# dummy integer, as such objects do not get automatically collected.
64
# In fact there is no obvious reason to prevent collection when Sage quits
65
# and we are certain no further call to the allocation function will be
66
# made; so this could be removed when the code is clean enough.
67
Py_INCREF(global_dummy)
68
69
# By default every object created in Pyrex is garbage
70
# collected. This means it may have references to other objects
71
# the Garbage collector has to look out for. We remove this flag
72
# as the only reference an Integer has is to the global Integer
73
# ring. As this object is unique we don't need to garbage collect
74
# it as we always have a module level reference to it. If another
75
# attribute is added to the Integer class this flag removal so as
76
# the alloc and free functions may not be used anymore.
77
# This object will still be reference counted.
78
if not useGC:
79
flag = Py_TPFLAGS_HAVE_GC
80
t.tp_flags = <long>(t.tp_flags & (~flag))
81
82
# Finally replace the functions called when an Integer needs
83
# to be constructed/destructed.
84
t.tp_new = tp_new
85
t.tp_dealloc = tp_dealloc
86
87