From: <wme...@us...> - 2009-09-11 11:39:11
|
Revision: 6786 http://jython.svn.sourceforge.net/jython/?rev=6786&view=rev Author: wmeissner Date: 2009-09-11 11:39:00 +0000 (Fri, 11 Sep 2009) Log Message: ----------- Implement memset & memmove Modified Paths: -------------- branches/ctypes-jffi/Lib/ctypes/__init__.py branches/ctypes-jffi/src/org/python/modules/jffi/CData.java branches/ctypes-jffi/src/org/python/modules/jffi/jffi.java Modified: branches/ctypes-jffi/Lib/ctypes/__init__.py =================================================================== --- branches/ctypes-jffi/Lib/ctypes/__init__.py 2009-09-11 06:55:34 UTC (rev 6785) +++ branches/ctypes-jffi/Lib/ctypes/__init__.py 2009-09-11 11:39:00 UTC (rev 6786) @@ -137,12 +137,18 @@ def alignment(type): return type._jffi_type.alignment -def byref(cdata): - return cdata.byref() +def addressof(cdata): + return cdata.address() +def byref(cdata, offset = 0): + return cdata.byref(offset) + def pointer(cdata): return cdata.pointer(POINTER(cdata.__class__)) +memmove = jffi.memmove +memset = jffi.memset + _pointer_type_cache = {} def POINTER(ctype): # If a pointer class for the C type has been created, re-use it Modified: branches/ctypes-jffi/src/org/python/modules/jffi/CData.java =================================================================== --- branches/ctypes-jffi/src/org/python/modules/jffi/CData.java 2009-09-11 06:55:34 UTC (rev 6785) +++ branches/ctypes-jffi/src/org/python/modules/jffi/CData.java 2009-09-11 11:39:00 UTC (rev 6786) @@ -2,8 +2,6 @@ package org.python.modules.jffi; import org.python.core.Py; -import org.python.core.PyInteger; -import org.python.core.PyLong; import org.python.core.PyObject; import org.python.core.PyType; import org.python.expose.ExposedMethod; @@ -34,8 +32,8 @@ * @return A ByReference instance pointing to this object's native memory. */ @ExposedMethod(names= { "byref" }) - public PyObject byref() { - return new ByReference(ctype, getReferenceMemory()); + public PyObject byref(PyObject offset) { + return new ByReference(ctype, (DirectMemory) getReferenceMemory().slice(offset.asInt())); } @ExposedMethod(names= { "pointer" }) @@ -47,6 +45,11 @@ return new PointerCData((PyType) pytype, CType.typeOf(pytype), getReferenceMemory(), getMemoryOp()); } + @ExposedMethod(names = { "address" }) + public PyObject address() { + return Py.newInteger(getReferenceMemory().getAddress()); + } + final CType getCType() { return ctype; } Modified: branches/ctypes-jffi/src/org/python/modules/jffi/jffi.java =================================================================== --- branches/ctypes-jffi/src/org/python/modules/jffi/jffi.java 2009-09-11 06:55:34 UTC (rev 6785) +++ branches/ctypes-jffi/src/org/python/modules/jffi/jffi.java 2009-09-11 11:39:00 UTC (rev 6786) @@ -4,6 +4,8 @@ import com.kenai.jffi.Library; import org.python.core.ClassDictInit; import org.python.core.Py; +import org.python.core.PyInteger; +import org.python.core.PyLong; import org.python.core.PyObject; @@ -54,4 +56,30 @@ public static PyObject POINTER(PyObject type) { return type; } + + private static long getMemoryAddress(PyObject obj) { + if (obj instanceof Pointer) { + return ((Pointer) obj).getMemory().getAddress(); + } else if (obj instanceof CData) { + return ((CData) obj).getReferenceMemory().getAddress(); + } else if (obj instanceof PyInteger) { + return obj.asInt(); + } else if (obj instanceof PyLong) { + return ((PyLong) obj).asLong(0); + } else { + throw Py.TypeError("invalid memory address"); + } + } + + public static PyObject memmove(PyObject dst, PyObject src, PyObject length) { + com.kenai.jffi.MemoryIO.getInstance().copyMemory(getMemoryAddress(src), + getMemoryAddress(dst), length.asInt()); + + return Py.None; + } + + public static PyObject memset(PyObject dst, PyObject value, PyObject length) { + com.kenai.jffi.MemoryIO.getInstance().setMemory(getMemoryAddress(dst), length.asInt(), (byte) value.asInt()); + return Py.None; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |