Pointer[] parray = getPointerArray(offset, sarray.length); for (int i=0;i < sarray.length;i++) { sarray[i] = Structure.updateStructureByReference((Class<Structure>) cls, sarray[i], parray[i]); Structure first = sarray[0]; if (first == null) { first = Structure.newInstance((Class<Structure>) cls, share(offset)); first.conditionalAutoRead(); sarray[0] = first; first.useMemory(this, (int)offset, true); first.read(); Structure[] tmp = first.toArray(sarray.length); for (int i=1;i < sarray.length;i++) { if (sarray[i] == null) { sarray[i].useMemory(this, (int)(offset + i * sarray[i].size()), true); sarray[i].read();
buf[i] = null; } else { buf[i] = sbuf[i].getPointer(); sbuf[i].write(); Structure first = sbuf[0]; if (first == null) { first = Structure.newInstance((Class<Structure>) cls, share(offset)); sbuf[0] = first; } else { first.useMemory(this, (int)offset, true); first.write(); Structure[] tmp = first.toArray(sbuf.length); for (int i=1;i < sbuf.length;i++) { if (sbuf[i] == null) { sbuf[i] = tmp[i]; } else { sbuf[i].useMemory(this, (int)(offset + i * sbuf[i].size()), true); sbuf[i].write();
private String toString(int indent, boolean showContents, boolean dumpMemory) { ensureAllocated(); String LS = System.getProperty("line.separator"); String name = format(getClass()) + "(" + getPointer() + ")"; if (!(getPointer() instanceof Memory)) { name += " (" + size() + " bytes)"; contents = "...}"; else for (Iterator<StructField> i = fields().values().iterator(); i.hasNext();) { StructField sf = i.next(); Object value = getFieldValue(sf.field); String type = format(sf.type); String index = ""; contents += prefix; if (sf.type.isArray() && value != null) { type = format(sf.type.getComponentType()); index = "[" + Array.getLength(value) + "]"; value = ((Structure)value).toString(indent + 1, !(value instanceof Structure.ByReference), dumpMemory); final int BYTES_PER_ROW = 4; contents += LS + "memory dump" + LS; byte[] buf = getPointer().getByteArray(0, size()); for (int i=0;i < buf.length;i++) { if ((i % BYTES_PER_ROW) == 0) contents += "[";
public void autoRead() { if (getAutoRead()) { read(); if (array != null) { for (int i=1;i < array.length;i++) { array[i].autoRead(); } } } }
public void autoWrite() { if (getAutoWrite()) { write(); if (array != null) { for (int i=1;i < array.length;i++) { array[i].autoWrite(); } } } }
private static void structureArrayCheck(Structure[] ss) { if (Structure.ByReference[].class.isAssignableFrom(ss.getClass())) { return; } Pointer base = ss[0].getPointer(); int size = ss[0].size(); for (int si=1;si < ss.length;si++) { if (ss[si].getPointer().peer != base.peer + size*si) { String msg = "Structure array elements must use" + " contiguous memory (bad backing address at Structure array index " + si + ")"; throw new IllegalArgumentException(msg); } } }
@Override public void read() { super.read(); Name = new char[NameLength / 2]; readField("Name"); } }
Structure s = Structure.newInstance((Class<? extends Structure>) dstType); byte[] buf = new byte[s.size()]; ((Pointer)value).read(0, buf, 0, buf.length); s.getPointer().write(0, buf, 0, buf.length); s.read(); value = s; } else { Structure s = Structure.newInstance((Class<? extends Structure>) dstType, (Pointer)value); s.conditionalAutoRead(); value = s;
/** Return whether the given Structure's backing data is identical to * this one, optionally clearing and re-writing native memory before checking. * @param s Structure to compare * @param clear whether to clear native memory * @return equality result */ public boolean dataEquals(Structure s, boolean clear) { if (clear) { s.getPointer().clear(s.size()); s.write(); getPointer().clear(size()); write(); } byte[] data = s.getPointer().getByteArray(0, s.size()); byte[] ref = getPointer().getByteArray(0, size()); if (data.length == ref.length) { for (int i=0;i < data.length;i++) { if (data[i] != ref[i]) { return false; } } return true; } return false; }
Structure s = (Structure)value; if (Structure.ByReference.class.isAssignableFrom(type)) { setPointer(offset, s == null ? null : s.getPointer()); if (s != null) { s.autoWrite(); s.useMemory(this, (int)offset, true); s.write();
struct.autoWrite(); if (struct instanceof Structure.ByValue) { return struct.getPointer(); } else if (arg instanceof Callback) { Structure.autoWrite(ss); Pointer[] pointers = new Pointer[ss.length + 1]; for (int i=0;i < ss.length;i++) { pointers[i] = ss[i] != null ? ss[i].getPointer() : null; throw new IllegalArgumentException("Structure array must have non-zero length"); } else if (ss[0] == null) { Structure.newInstance((Class<? extends Structure>) type).toArray(ss); return ss[0].getPointer(); } else { Structure.autoWrite(ss); return ss[0].getPointer();
private Structure copyBufferToStructure(Pointer buffer, Structure structure) { int byteCount = structure.size(); memcpy(structure.getPointer(), buffer, byteCount); structure.read(); return structure; }
/** Only keep the original structure if its native address is unchanged. * Otherwise replace it with a new object. * @param type Structure subclass * @param s Original Structure object * @param address the native <code>struct *</code> * @return Updated <code>Structure.ByReference</code> object */ static <T extends Structure> T updateStructureByReference(Class<T> type, T s, Pointer address) { if (address == null) { s = null; } else { if (s == null || !address.equals(s.getPointer())) { Structure s1 = reading().get(address); if (s1 != null && type.equals(s1.getClass())) { s = (T) s1; s.autoRead(); } else { s = newInstance(type, address); s.conditionalAutoRead(); } } else { s.autoRead(); } } return s; }
ensureAllocated(); if (this.memory instanceof AutoAllocated) { int requiredSize = array.length * size(); if (m.size() < requiredSize) { useMemory(autoAllocate(requiredSize)); int size = size(); for (int i=1;i < array.length;i++) { array[i] = newInstance(getClass(), memory.share(i*size, size)); array[i].conditionalAutoRead();
ensureAllocated(); if (busy().contains(this)) { return; busy().add(this); if (this instanceof Structure.ByReference) { reading().put(getPointer(), this); for (StructField structField : fields().values()) { readField(structField); busy().remove(this); if (reading().get(getPointer()) == this) { reading().remove(getPointer());
Structure s = Structure.newInstance(dstType); Pointer old = s.getPointer(); s.useMemory((Pointer)value); s.read(); s.write();
private static class MemberOrder { public int first; public int middle; public int last; }
@Override public void write() { UserLength = User == null ? 0 : User.length(); DomainLength = Domain == null ? 0 : Domain.length(); PasswordLength = Password == null ? 0 : Password.length(); super.write(); } }
/** * @return whether the given structure's type and pointer match. */ @Override public boolean equals(Object o) { return o instanceof Structure && o.getClass() == getClass() && ((Structure)o).getPointer().equals(getPointer()); }
Structure s = (Structure)currentValue; if (Structure.ByReference.class.isAssignableFrom(type)) { s = Structure.updateStructureByReference((Class<Structure>) type, s, getPointer(offset)); } else { s.useMemory(this, (int)offset, true); s.read();