/** * Writes an array of signed 8 bit integer values to the memory area. * * @param offset The offset from the start of the memory area to write the values. * @param length The number of values to be written to memory. * @return <tt>this</tt> object. */ @JRubyMethod(name = { "put_array_of_int8", "put_array_of_char" }, required = 2) public IRubyObject put_array_of_int8(ThreadContext context, IRubyObject offset, IRubyObject arrParam) { MemoryUtil.putArrayOfSigned8(context.runtime, getMemoryIO(), getOffset(offset), checkArray(arrParam)); return this; }
@Override public final boolean eql(IRubyObject other) { return this.equals(other); } /**
@JRubyMethod(name = "get_pointer", required = 1) public IRubyObject get_pointer(ThreadContext context, IRubyObject offset) { return getPointer(context.runtime, getOffset(offset)); }
/** * Writes an array of unsigned 32 bit integer values to the memory area. * * @param ary The array of values to write to the memory area. * @return <tt>this</tt> object. */ @JRubyMethod(name = { "write_array_of_uint32", "write_array_of_uint" }, required = 1) public IRubyObject write_array_of_uint32(ThreadContext context, IRubyObject ary) { MemoryUtil.putArrayOfUnsigned32(context.runtime, getMemoryIO(), 0, checkArray(ary)); return this; }
/** * Reads an 8 bit unsigned integer value from the memory address. * * @param offset The offset from the base pointer address to read the value. * @return The value read from the address. */ @JRubyMethod(name = { "get_uint8", "get_uchar" }, required = 1) public IRubyObject get_uint8(ThreadContext context, IRubyObject offset) { return Util.newUnsigned8(context.runtime, getMemoryIO().getByte(getOffset(offset))); }
@JRubyMethod(name = { "read_array_of_type" }, required = 3) public IRubyObject read_array_of_type(ThreadContext context, IRubyObject typeArg, IRubyObject reader, IRubyObject lenArg) { Type type = context.runtime.getFFI().getTypeResolver().findType(context.runtime, typeArg); DynamicMethod method = getMetaClass().searchMethod(reader.asJavaString()); int len = checkArrayLength(lenArg); RubyArray arr = RubyArray.newArray(context.runtime, len); for (int i = 0, off = 0; i < len; i++, off += type.size) { arr.add(method.call(context, this.slice(context.runtime, off, type.size), this.getMetaClass(), reader.asJavaString())); } return arr; }
@JRubyMethod(name = "initialize_copy", visibility = PRIVATE) public IRubyObject initialize_copy(ThreadContext context, IRubyObject other) { if (other == this) { return this; } if (!(other instanceof Struct)) { throw context.runtime.newTypeError("not an instance of Struct"); } Struct orig = (Struct) other; memory = (AbstractMemory) orig.getMemory().slice(context.runtime, 0, layout.getSize()).dup(); if (orig.referenceCache != null) { referenceCache = new Object[layout.getReferenceFieldCount()]; System.arraycopy(orig.referenceCache, 0, referenceCache, 0, referenceCache.length); } setMemoryIO(memory.getMemoryIO()); return this; }
@JRubyMethod(name = { "write_array_of_type" }, required = 3) public IRubyObject write_array_of_type(ThreadContext context, IRubyObject typeArg, IRubyObject writer, IRubyObject aryArg) { Type type = context.runtime.getFFI().getTypeResolver().findType(context.runtime, typeArg); DynamicMethod method = getMetaClass().searchMethod(writer.asJavaString()); RubyArray arr = aryArg.convertToArray(); int len = arr.size(); for (int i = 0, off = 0; i < len; i++, off += type.size) { method.call(context, this.slice(context.runtime, off, type.size), this.getMetaClass(), writer.asJavaString(), arr.entry(i)); } return this; }
@JRubyMethod(name = { "read_array_of_type" }, required = 2) public IRubyObject read_array_of_type(ThreadContext context, IRubyObject typeArg, IRubyObject lenArg) { Type type = context.runtime.getFFI().getTypeResolver().findType(context.runtime, typeArg); MemoryOp op = MemoryOp.getMemoryOp(type); if (op == null) { throw context.runtime.newTypeError("cannot get memory reader for type " + type); } int len = checkArrayLength(lenArg); RubyArray arr = RubyArray.newArray(context.runtime, len); for (int i = 0, off = 0; i < len; i++, off += type.size) { arr.add(op.get(context, getMemoryIO(), off)); } return arr; }
@JRubyMethod(name = { "read_pointer" }) public IRubyObject read_pointer(ThreadContext context) { return getPointer(context.runtime, 0); }
@Override protected MemoryIO allocateMemoryIO() { throw getRuntime().newRuntimeError("allocateMemoryIO should not be called"); }
AbstractMemory.createAbstractMemoryClass(runtime, ffi);
/** * Writes a 32 bit signed integer value to the memory address. * * @param offset The offset from the base pointer address to write the value. * @param value The value to write. * @return The value written. */ @JRubyMethod(name = { "put_int32", "put_int" }, required = 2) public IRubyObject put_int32(ThreadContext context, IRubyObject offset, IRubyObject value) { getMemoryIO().putInt(getOffset(offset), Util.int32Value(value)); return this; }
/** * Writes an array of signed 64 bit integer values to the memory area. * * @param ary The array of values to write to the memory area. * @return <tt>this</tt> object. */ @JRubyMethod(name = { "write_array_of_int64", "write_array_of_long_long" }, required = 1) public IRubyObject write_array_of_int64(ThreadContext context, IRubyObject ary) { MemoryUtil.putArrayOfSigned64(context.runtime, getMemoryIO(), 0, checkArray(ary)); return this; }
@JRubyMethod(name = { "read_array_of_type" }, required = 3) public IRubyObject read_array_of_type(ThreadContext context, IRubyObject typeArg, IRubyObject reader, IRubyObject lenArg) { Type type = context.runtime.getFFI().getTypeResolver().findType(context.runtime, typeArg); DynamicMethod method = getMetaClass().searchMethod(reader.asJavaString()); int len = checkArrayLength(lenArg); RubyArray arr = RubyArray.newArray(context.runtime, len); for (int i = 0, off = 0; i < len; i++, off += type.size) { arr.add(method.call(context, this.slice(context.runtime, off, type.size), this.getMetaClass(), reader.asJavaString())); } return arr; }
@JRubyMethod(name = "initialize_copy", visibility = PRIVATE) public IRubyObject initialize_copy(ThreadContext context, IRubyObject other) { if (other == this) { return this; } if (!(other instanceof Struct)) { throw context.runtime.newTypeError("not an instance of Struct"); } Struct orig = (Struct) other; memory = (AbstractMemory) orig.getMemory().slice(context.runtime, 0, layout.getSize()).dup(); if (orig.referenceCache != null) { referenceCache = new Object[layout.getReferenceFieldCount()]; System.arraycopy(orig.referenceCache, 0, referenceCache, 0, referenceCache.length); } setMemoryIO(memory.getMemoryIO()); return this; }
@JRubyMethod(name = { "write_array_of_type" }, required = 3) public IRubyObject write_array_of_type(ThreadContext context, IRubyObject typeArg, IRubyObject writer, IRubyObject aryArg) { Type type = context.runtime.getFFI().getTypeResolver().findType(context.runtime, typeArg); DynamicMethod method = getMetaClass().searchMethod(writer.asJavaString()); RubyArray arr = aryArg.convertToArray(); int len = arr.size(); for (int i = 0, off = 0; i < len; i++, off += type.size) { method.call(context, this.slice(context.runtime, off, type.size), this.getMetaClass(), writer.asJavaString(), arr.entry(i)); } return this; }
@JRubyMethod(name = { "read_array_of_type" }, required = 2) public IRubyObject read_array_of_type(ThreadContext context, IRubyObject typeArg, IRubyObject lenArg) { Type type = context.runtime.getFFI().getTypeResolver().findType(context.runtime, typeArg); MemoryOp op = MemoryOp.getMemoryOp(type); if (op == null) { throw context.runtime.newTypeError("cannot get memory reader for type " + type); } int len = checkArrayLength(lenArg); RubyArray arr = RubyArray.newArray(context.runtime, len); for (int i = 0, off = 0; i < len; i++, off += type.size) { arr.add(op.get(context, getMemoryIO(), off)); } return arr; }
@JRubyMethod(name = { "get_pointer" }) public IRubyObject get_pointer(ThreadContext context) { return getPointer(context.runtime, 0); }
@Override protected MemoryIO allocateMemoryIO() { throw getRuntime().newRuntimeError("allocateMemoryIO should not be called"); }