Debian Codegraph @

rpython/jit/metainterp/test/test_rawmem.py

raw symbols SCIP
1 from rpython.jit.metainterp.test.support import LLJitMixin
2 from rpython.rtyper.lltypesystem import lltype, rffi
3 from rpython.rlib.rawstorage import (alloc_raw_storage, raw_storage_setitem,
4 free_raw_storage, raw_storage_getitem)
5
6
7 class RawMemTests(object):
8 def test_cast_void_ptr(self):
9 TP = lltype.Array(lltype.Float, hints={"nolength": True})
10 VOID_TP = lltype.Array(lltype.Void, hints={"nolength": True, "uncast_on_llgraph": True})
11 class A(object):
12 def __init__(self, x):
13 self.storage = rffi.cast(lltype.Ptr(VOID_TP), x)
14
15 def f(n):
16 x = lltype.malloc(TP, n, flavor="raw", zero=True)
17 a = A(x)
18 s = 0.0
19 rffi.cast(lltype.Ptr(TP), a.storage)[0] = 1.0
20 s += rffi.cast(lltype.Ptr(TP), a.storage)[0]
21 lltype.free(x, flavor="raw")
22 return s
23 self.interp_operations(f, [10])
24
25 def test_fixed_size_malloc(self):
26 TIMEVAL = lltype.Struct('dummy', ('tv_sec', rffi.LONG), ('tv_usec', rffi.LONG))
27 def f():
28 p = lltype.malloc(TIMEVAL, flavor='raw')
29 lltype.free(p, flavor='raw')
30 return 42
31 res = self.interp_operations(f, [])
32 assert res == 42
33 self.check_operations_history({'call_i': 1,
34 'call_n': 1,
35 'guard_no_exception': 1,
36 'finish': 1})
37
38 def test_raw_storage_int(self):
39 def f():
40 p = alloc_raw_storage(15)
41 raw_storage_setitem(p, 3, 24)
42 res = raw_storage_getitem(lltype.Signed, p, 3)
43 free_raw_storage(p)
44 return res
45 res = self.interp_operations(f, [])
46 assert res == 24
47 self.check_operations_history({'call_i': 1, 'guard_no_exception': 1,
48 'call_n': 1,
49 'raw_store': 1, 'raw_load_i': 1,
50 'finish': 1})
51 self.metainterp.staticdata.stats.check_resops({'finish': 1}, omit_finish=False)
52
53 def test_raw_storage_float(self):
54 def f():
55 p = alloc_raw_storage(15)
56 raw_storage_setitem(p, 4, 2.4e15)
57 res = raw_storage_getitem(lltype.Float, p, 4)
58 free_raw_storage(p)
59 return res
60 res = self.interp_operations(f, [])
61 assert res == 2.4e15
62 self.check_operations_history({'call_i': 1, 'guard_no_exception': 1,
63 'call_n': 1,
64 'raw_store': 1, 'raw_load_f': 1,
65 'finish': 1})
66 self.metainterp.staticdata.stats.check_resops({'finish': 1}, omit_finish=False)
67
68 def test_raw_storage_byte(self):
69 def f():
70 p = alloc_raw_storage(15)
71 raw_storage_setitem(p, 5, rffi.cast(rffi.UCHAR, 254))
72 res = raw_storage_getitem(rffi.UCHAR, p, 5)
73 free_raw_storage(p)
74 return rffi.cast(lltype.Signed, res)
75 res = self.interp_operations(f, [])
76 assert res == 254
77 self.check_operations_history({'call_n': 1, 'guard_no_exception': 1,
78 'call_i': 1,
79 'raw_store': 1, 'raw_load_i': 1,
80 'finish': 1})
81 self.metainterp.staticdata.stats.check_resops({'finish': 1}, omit_finish=False)
82
83 def test_raw_storage_options(self):
84 def f():
85 p = alloc_raw_storage(15, track_allocation=False, zero=True)
86 raw_storage_setitem(p, 3, 24)
87 res = raw_storage_getitem(lltype.Signed, p, 3)
88 free_raw_storage(p, track_allocation=False)
89 return res
90 res = self.interp_operations(f, [])
91 assert res == 24
92 self.check_operations_history({'call_n': 1, 'guard_no_exception': 1,
93 'call_i': 1,
94 'raw_store': 1, 'raw_load_i': 1,
95 'finish': 1})
96 self.metainterp.staticdata.stats.check_resops({'finish': 1}, omit_finish=False)
97
98 def test_scoped_alloc_buffer(self):
99 def f():
100 with rffi.scoped_alloc_buffer(42) as p:
101 p.raw[0] = 'X'
102 s = p.str(1)
103 return ord(s[0])
104
105 res = self.interp_operations(f, [])
106 assert res == ord('X')
107
108
109 class TestRawMem(RawMemTests, LLJitMixin):
110
111 def test_getarraysubstruct(self):
112 # NOTE: not for backend/*/test
113 A2 = lltype.Array(('a', lltype.Signed), ('b', lltype.Signed),
114 hints={'nolength': True})
115 p = lltype.malloc(A2, 10, flavor='raw', immortal=True, zero=True)
116 p[2].b = 689
117 def f(n, m):
118 p[n].a = 55
119 p[n].b = 44
120 p[4].b = 66
121 return p[m].b
122
123 # run with 'disable_optimizations' to prevent an error
124 # 'Symbolics cannot be compared!' in the optimizer for int_mul
125 res = self.interp_operations(f, [7, 2], disable_optimizations=True)
126 assert res == 689
127 res = self.interp_operations(f, [7, 4], disable_optimizations=True)
128 assert res == 66
129 res = self.interp_operations(f, [2, 2], disable_optimizations=True)
130 assert res == 44