Debian Codegraph @

rpython/rlib/rrawarray.py

raw symbols SCIP
1 from rpython.rtyper.llannotation import lltype_to_annotation
2 from rpython.rlib.objectmodel import specialize
3 from rpython.rlib import jit, rgc
4 from rpython.rtyper.lltypesystem import lltype, llmemory
5 from rpython.rtyper.extregistry import ExtRegistryEntry
6 from rpython.tool.pairtype import pair
7
8 def copy_list_to_raw_array(lst, array):
9 for i, item in enumerate(lst):
10 array[i] = item
11
12 def populate_list_from_raw_array(lst, array, length):
13 lst[:] = [array[i] for i in range(length)]
14
15
16
17 class Entry(ExtRegistryEntry):
18 _about_ = copy_list_to_raw_array
19
20 def compute_result_annotation(self, *s_args):
21 pass
22
23 def specialize_call(self, hop):
24 hop.exception_cannot_occur()
25 v_list, v_buf = hop.inputargs(*hop.args_r)
26 return hop.gendirectcall(ll_copy_list_to_raw_array, v_list, v_buf)
27
28
29 class Entry(ExtRegistryEntry):
30 _about_ = populate_list_from_raw_array
31
32 def compute_result_annotation(self, s_list, s_array, s_length):
33 s_item = lltype_to_annotation(s_array.ll_ptrtype.TO.OF)
34 s_newlist = self.bookkeeper.newlist(s_item)
35 s_newlist.listdef.resize()
36 pair(s_list, s_newlist).union()
37
38 def specialize_call(self, hop):
39 v_list, v_buf, v_length = hop.inputargs(*hop.args_r)
40 hop.exception_is_here()
41 return hop.gendirectcall(ll_populate_list_from_raw_array, v_list, v_buf, v_length)
42
43
44 @specialize.ll()
45 def get_raw_buf(ptr):
46 ofs = llmemory.itemoffsetof(lltype.typeOf(ptr).TO, 0)
47 return llmemory.cast_ptr_to_adr(ptr) + ofs
48 get_raw_buf._always_inline_ = True
49
50
51 @jit.dont_look_inside
52 def ll_copy_list_to_raw_array(ll_list, dst_ptr):
53 if rgc.must_split_gc_address_space():
54 for i in range(ll_list.ll_length()):
55 dst_ptr[i] = ll_list.ll_getitem_fast(i)
56 return
57 # this code is delicate: we must ensure that there are no GC operations
58 # around the call to raw_memcopy
59 #
60 ITEM = lltype.typeOf(dst_ptr).TO.OF
61 size = llmemory.sizeof(ITEM) * ll_list.ll_length()
62 # start of no-GC section
63 src_adr = get_raw_buf(ll_list.ll_items())
64 dst_adr = get_raw_buf(dst_ptr)
65 llmemory.raw_memcopy(src_adr, dst_adr, size)
66 # end of no-GC section
67
68
69 @jit.dont_look_inside
70 def ll_populate_list_from_raw_array(ll_list, src_ptr, length):
71 ll_list._ll_resize(length)
72 if rgc.must_split_gc_address_space():
73 for i in range(length):
74 ll_list.ll_setitem_fast(i, src_ptr[i])
75 return
76 ITEM = lltype.typeOf(src_ptr).TO.OF
77 size = llmemory.sizeof(ITEM) * length
78 # start of no-GC section
79 src_adr = get_raw_buf(src_ptr)
80 dst_adr = get_raw_buf(ll_list.ll_items())
81 llmemory.raw_memcopy(src_adr, dst_adr, size)
82 # end of no-GC section