From: <wme...@us...> - 2009-09-03 15:02:48
|
Revision: 6744 http://jython.svn.sourceforge.net/jython/?rev=6744&view=rev Author: wmeissner Date: 2009-09-03 15:02:40 +0000 (Thu, 03 Sep 2009) Log Message: ----------- Use a slightly more lightweight ByReference instance for byref() parameters. Add CData#getMemoryOp() instead of storing the memoryOp in CData Modified Paths: -------------- branches/ctypes-jffi/CoreExposed.includes branches/ctypes-jffi/src/org/python/modules/jffi/CData.java branches/ctypes-jffi/src/org/python/modules/jffi/MemoryOp.java branches/ctypes-jffi/src/org/python/modules/jffi/PointerCData.java branches/ctypes-jffi/src/org/python/modules/jffi/ScalarCData.java branches/ctypes-jffi/src/org/python/modules/jffi/Structure.java Added Paths: ----------- branches/ctypes-jffi/src/org/python/modules/jffi/ByReference.java Modified: branches/ctypes-jffi/CoreExposed.includes =================================================================== --- branches/ctypes-jffi/CoreExposed.includes 2009-09-02 07:20:36 UTC (rev 6743) +++ branches/ctypes-jffi/CoreExposed.includes 2009-09-03 15:02:40 UTC (rev 6744) @@ -52,6 +52,7 @@ org/python/modules/_csv/PyWriter.class org/python/modules/_functools/PyPartial.class org/python/modules/_hashlib$Hash.class +org/python/modules/jffi/ByReference.class org/python/modules/jffi/CData.class org/python/modules/jffi/CType.class org/python/modules/jffi/CType$Array.class Added: branches/ctypes-jffi/src/org/python/modules/jffi/ByReference.java =================================================================== --- branches/ctypes-jffi/src/org/python/modules/jffi/ByReference.java (rev 0) +++ branches/ctypes-jffi/src/org/python/modules/jffi/ByReference.java 2009-09-03 15:02:40 UTC (rev 6744) @@ -0,0 +1,26 @@ + +package org.python.modules.jffi; + +import org.python.core.PyObject; +import org.python.core.PyType; +import org.python.expose.ExposedType; + +@ExposedType(name = "jffi.ByReference", base = PyObject.class) +public final class ByReference extends PyObject implements Pointer { + public static final PyType TYPE = PyType.fromClass(ByReference.class); + + private final DirectMemory memory; + + ByReference(CType componentType, DirectMemory memory) { + super(TYPE); + this.memory = memory; + } + + public long getAddress() { + return getMemory().getAddress(); + } + + public final DirectMemory getMemory() { + return memory; + } +} Modified: branches/ctypes-jffi/src/org/python/modules/jffi/CData.java =================================================================== --- branches/ctypes-jffi/src/org/python/modules/jffi/CData.java 2009-09-02 07:20:36 UTC (rev 6743) +++ branches/ctypes-jffi/src/org/python/modules/jffi/CData.java 2009-09-03 15:02:40 UTC (rev 6744) @@ -10,22 +10,26 @@ @ExposedType(name = "jffi.CData", base = PyObject.class) public abstract class CData extends PyObject { public static final PyType TYPE = PyType.fromClass(CData.class); + + private final CType ctype; - final MemoryOp memoryOp; - final CType type; - private DirectMemory referenceMemory; - CData(PyType subtype, CType type, MemoryOp memoryOp) { + CData(PyType subtype, CType type) { super(subtype); - this.type = type; - this.memoryOp = memoryOp; + this.ctype = type; this.referenceMemory = null; } - + + /** + * Wraps up this object in a pointer that can be passed to native code. + * The byref() return value cannot be used as anything other than a parameter. + * + * @return A ByReference instance pointing to this object's native memory. + */ @ExposedMethod(names= { "byref" }) public PyObject byref() { - return new PointerCData(PointerCData.TYPE, type, getReferenceMemory(), memoryOp); + return new ByReference(ctype, getReferenceMemory()); } @ExposedMethod(names= { "pointer" }) @@ -34,9 +38,17 @@ throw Py.TypeError("expected type"); } - return new PointerCData((PyType) pytype, type, getReferenceMemory(), memoryOp); + return new PointerCData((PyType) pytype, CType.typeOf(pytype), getReferenceMemory(), getMemoryOp()); } + final CType getCType() { + return ctype; + } + + MemoryOp getMemoryOp() { + return getCType().getMemoryOp(); + } + final boolean hasReferenceMemory() { return referenceMemory != null; } @@ -63,7 +75,7 @@ } protected DirectMemory allocateReferenceMemory() { - DirectMemory m = AllocatedNativeMemory.allocate(type.size(), false); + DirectMemory m = AllocatedNativeMemory.allocate(getCType().size(), false); initReferenceMemory(m); this.referenceMemory = m; return m; Modified: branches/ctypes-jffi/src/org/python/modules/jffi/MemoryOp.java =================================================================== --- branches/ctypes-jffi/src/org/python/modules/jffi/MemoryOp.java 2009-09-02 07:20:36 UTC (rev 6743) +++ branches/ctypes-jffi/src/org/python/modules/jffi/MemoryOp.java 2009-09-03 15:02:40 UTC (rev 6744) @@ -188,8 +188,10 @@ public final void put(Memory mem, long offset, PyObject value) { if (value instanceof Pointer) { mem.putAddress(offset, ((Pointer) value).getAddress()); + } else if (value == Py.None) { + mem.putAddress(offset, 0); } else { - mem.putAddress(offset, Util.int64Value(value)); + throw Py.RuntimeError("invalid pointer"); } } Modified: branches/ctypes-jffi/src/org/python/modules/jffi/PointerCData.java =================================================================== --- branches/ctypes-jffi/src/org/python/modules/jffi/PointerCData.java 2009-09-02 07:20:36 UTC (rev 6743) +++ branches/ctypes-jffi/src/org/python/modules/jffi/PointerCData.java 2009-09-03 15:02:40 UTC (rev 6744) @@ -18,16 +18,11 @@ final MemoryOp componentMemoryOp; PointerCData(PyType subtype, CType type, DirectMemory memory, MemoryOp componentMemoryOp) { - super(subtype, type, type.getMemoryOp()); + super(subtype, type); this.memory = memory; this.componentMemoryOp = componentMemoryOp; } - - PointerCData(CType type, DirectMemory memory, MemoryOp componentMemoryOp) { - this(TYPE, type, memory, componentMemoryOp); - } - - + @ExposedNew public static PyObject PointerCData_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords) { @@ -38,18 +33,20 @@ throw Py.TypeError("invalid _jffi_type for " + subtype.getName()); } - CType.Pointer type = (CType.Pointer) jffi_type; + CType.Pointer pointerType = (CType.Pointer) jffi_type; + // No args == create NULL pointer if (args.length == 0) { - return new PointerCData(subtype, type, NullMemory.INSTANCE, type.componentMemoryOp); + return new PointerCData(subtype, pointerType, NullMemory.INSTANCE, pointerType.componentMemoryOp); } + PyObject value = args[0]; - if (value instanceof CData && value.getType().isSubType(type.pyComponentType)) { + if (value instanceof CData && value.getType().isSubType(pointerType.pyComponentType)) { - return new PointerCData(subtype, type, ((CData) value).getReferenceMemory(), type.componentMemoryOp); + return new PointerCData(subtype, pointerType, ((CData) value).getReferenceMemory(), pointerType.componentMemoryOp); } else { - throw Py.TypeError("expected " + type.pyComponentType.getName() + " instead of " + value.getType().getName()); + throw Py.TypeError("expected " + pointerType.pyComponentType.getName() + " instead of " + value.getType().getName()); } } Modified: branches/ctypes-jffi/src/org/python/modules/jffi/ScalarCData.java =================================================================== --- branches/ctypes-jffi/src/org/python/modules/jffi/ScalarCData.java 2009-09-02 07:20:36 UTC (rev 6743) +++ branches/ctypes-jffi/src/org/python/modules/jffi/ScalarCData.java 2009-09-03 15:02:40 UTC (rev 6744) @@ -37,19 +37,19 @@ return cdata; } - ScalarCData(PyType pyType, CType.Builtin type) { - super(pyType, type, type.getMemoryOp()); + ScalarCData(PyType pytype, CType.Builtin ctype) { + super(pytype, ctype); } protected final void initReferenceMemory(Memory m) { - memoryOp.put(m, 0, value); + getMemoryOp().put(m, 0, value); } @ExposedGet(name = "value") public PyObject getValue() { // If native memory has been allocated, read the value from there if (hasReferenceMemory()) { - return memoryOp.get(getReferenceMemory(), 0); + return getMemoryOp().get(getReferenceMemory(), 0); } return value; @@ -61,7 +61,7 @@ this.value = value; // If native memory has been allocated, sync the value to memory if (hasReferenceMemory()) { - memoryOp.put(getReferenceMemory(), 0, value); + getMemoryOp().put(getReferenceMemory(), 0, value); } } Modified: branches/ctypes-jffi/src/org/python/modules/jffi/Structure.java =================================================================== --- branches/ctypes-jffi/src/org/python/modules/jffi/Structure.java 2009-09-02 07:20:36 UTC (rev 6743) +++ branches/ctypes-jffi/src/org/python/modules/jffi/Structure.java 2009-09-03 15:02:40 UTC (rev 6744) @@ -13,14 +13,16 @@ public static final PyType TYPE = PyType.fromClass(Structure.class); private final StructLayout layout; + private final MemoryOp memoryOp; Structure(PyType pyType, StructLayout layout) { this(pyType, layout, AllocatedNativeMemory.allocate(layout.size(), true)); } Structure(PyType pyType, StructLayout layout, Memory m) { - super(pyType, layout, new MemoryOp.StructOp(pyType, layout)); + super(pyType, layout); this.layout = layout; + this.memoryOp = new MemoryOp.StructOp(pyType, layout); setReferenceMemory(m); } @@ -40,6 +42,11 @@ throw Py.RuntimeError("reference memory already initialized"); } + @Override + MemoryOp getMemoryOp() { + return memoryOp; + } + StructLayout.Field getField(PyObject key) { StructLayout.Field f = layout.getField(key); if (f == null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |