Author: Antonio Cuni <[email protected]>
Branch: fast_cffi_list_init
Changeset: r67236:8a818cbdcdc0
Date: 2013-10-09 10:48 +0200
http://bitbucket.org/pypy/pypy/changeset/8a818cbdcdc0/
Log: add a function to memcpy the content of an rpython list into a raw
array
diff --git a/rpython/rlib/rarray.py b/rpython/rlib/rarray.py
new file mode 100644
--- /dev/null
+++ b/rpython/rlib/rarray.py
@@ -0,0 +1,31 @@
+from rpython.rtyper.lltypesystem import lltype, llmemory
+from rpython.rtyper.extregistry import ExtRegistryEntry
+
+def copy_list_to_raw_array(lst, array):
+ for i, item in enumerate(lst):
+ array[i] = item
+
+
+class Entry(ExtRegistryEntry):
+ _about_ = copy_list_to_raw_array
+
+ def compute_result_annotation(self, *s_args):
+ pass
+
+ def specialize_call(self, hop):
+ hop.exception_cannot_occur()
+ v_list, v_buf = hop.inputargs(*hop.args_r)
+ return hop.gendirectcall(ll_copy_list_to_raw_array, v_list, v_buf)
+
+
+def ll_copy_list_to_raw_array(ll_list, dst_ptr):
+ src_ptr = ll_list.ll_items()
+ src_adr = llmemory.cast_ptr_to_adr(src_ptr)
+ src_adr += llmemory.itemoffsetof(lltype.typeOf(src_ptr).TO, 0) # skip the
GC header
+ #
+ dst_adr = llmemory.cast_ptr_to_adr(dst_ptr)
+ dst_adr += llmemory.itemoffsetof(lltype.typeOf(dst_ptr).TO, 0)
+ #
+ ITEM = lltype.typeOf(dst_ptr).TO.OF
+ size = llmemory.sizeof(ITEM) * ll_list.ll_length()
+ llmemory.raw_memcopy(src_adr, dst_adr, size)
diff --git a/rpython/rlib/test/test_rarray.py b/rpython/rlib/test/test_rarray.py
new file mode 100644
--- /dev/null
+++ b/rpython/rlib/test/test_rarray.py
@@ -0,0 +1,38 @@
+from rpython.rlib.rarray import copy_list_to_raw_array
+from rpython.rtyper.lltypesystem import lltype, rffi
+from rpython.rtyper.test.tool import BaseRtypingTest
+
+
+
+class TestRArray(BaseRtypingTest):
+
+ def test_copy_list_to_raw_array(self):
+ ARRAY = rffi.CArray(lltype.Signed)
+ buf = lltype.malloc(ARRAY, 4, flavor='raw')
+ lst = [1, 2, 3, 4]
+ copy_list_to_raw_array(lst, buf)
+ for i in range(4):
+ assert buf[i] == i+1
+ lltype.free(buf, flavor='raw')
+
+
+ def test_copy_list_to_raw_array_rtyped(self):
+ INTARRAY = rffi.CArray(lltype.Signed)
+ FLOATARRAY = rffi.CArray(lltype.Float)
+ def fn():
+ buf = lltype.malloc(INTARRAY, 3, flavor='raw')
+ lst = [1, 2, 3]
+ copy_list_to_raw_array(lst, buf)
+ for i in range(3):
+ assert buf[i] == lst[i]
+ #
+ buf2 = lltype.malloc(FLOATARRAY, 3, flavor='raw')
+ lst = [1.1, 2.2, 3.3]
+ copy_list_to_raw_array(lst, buf2)
+ for i in range(3):
+ assert buf2[i] == lst[i]
+ #
+ lltype.free(buf, flavor='raw')
+ lltype.free(buf2, flavor='raw')
+ self.interpret(fn, [])
+
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit