public void __init__(object value) { _memHolder = new MemoryHolder(Size); NativeType.SetValue(_memHolder, 0, value); if (IsString) { _memHolder.AddObject("str", value); } }
/// <summary> /// Creates a new MemoryHolder at the specified address which will keep alive the /// parent memory holder. /// </summary> public MemoryHolder(IntPtr data, int size, MemoryHolder parent) { GC.SuppressFinalize(this); _data = data; _parent = parent; _objects = parent._objects; _size = size; }
public void __init__(params object[] args) { CheckAbstract(); INativeType nativeType = NativeType; StructType st = (StructType)nativeType; _memHolder = new MemoryHolder(nativeType.Size); st.SetValueInternal(_memHolder, 0, args); }
public Pointer(CData value) { _object = value; // Keep alive the object, more to do here. _memHolder = new MemoryHolder(IntPtr.Size); _memHolder.WriteIntPtr(0, value._memHolder); _memHolder.AddObject("1", value); if (value._objects != null) { _memHolder.AddObject("0", value._objects); } }
public void __init__(params object[] args) { INativeType nativeType = NativeType; _memHolder = new MemoryHolder(nativeType.Size); if (args.Length > ((ArrayType)nativeType).Length) { throw PythonOps.IndexError("too many arguments"); } nativeType.SetValue(_memHolder, 0, args); }
public static void resize(CData obj, int newSize) { if (newSize < obj.NativeType.Size) { throw PythonOps.ValueError("minimum size is {0}", newSize); } MemoryHolder newMem = new MemoryHolder(newSize); obj._memHolder.CopyTo(newMem, 0, Math.Min(obj._memHolder.Size, newSize)); obj._memHolder = newMem; }
public void __init__(CodeContext/*!*/ context, [ParamDictionary]IAttributesCollection kwargs) { CheckAbstract(); INativeType nativeType = NativeType; _memHolder = new MemoryHolder(nativeType.Size); StructType st = (StructType)nativeType; foreach (var x in kwargs.SymbolAttributes) { PythonOps.SetAttr(context, this, x.Key, x.Value); } }
// __nonzero__ /// <summary> /// Creates a new CFuncPtr object from a tuple. The 1st element of the /// tuple is the ordinal or function name. The second is an object with /// a _handle property. The _handle property is the handle of the module /// from which the function will be loaded. /// </summary> public _CFuncPtr(PythonTuple args) { if (args == null) { throw PythonOps.TypeError("expected sequence, got None"); } else if (args.Count != 2) { throw PythonOps.TypeError($"argument 1 must be a sequence of length 2, not {args.Count}"); } object nameOrOrdinal = args[0]; object dll = args[1]; IntPtr intPtrHandle = GetHandleFromObject(dll, "the _handle attribute of the second element must be an integer"); IntPtr tmpAddr; string funcName = args[0] as string; if (funcName != null) { tmpAddr = NativeFunctions.LoadFunction(intPtrHandle, funcName); } else { tmpAddr = NativeFunctions.LoadFunction(intPtrHandle, new IntPtr((int)nameOrOrdinal)); } if (tmpAddr == IntPtr.Zero) { if (CallingConvention == CallingConvention.StdCall && funcName != null) { // apply std call name mangling - prepend a _, append @bytes where // bytes is the number of bytes of the argument list. string mangled = "_" + funcName + "@"; for (int i = 0; i < 128 && tmpAddr == IntPtr.Zero; i += 4) { tmpAddr = NativeFunctions.LoadFunction(intPtrHandle, mangled + i); } } if (tmpAddr == IntPtr.Zero) { throw PythonOps.AttributeError($"function {args[0]} is not defined"); } } _memHolder = new MemoryHolder(IntPtr.Size); addr = tmpAddr; _id = Interlocked.Increment(ref _curId); }
/// <summary> /// Helper function for translating from memset to NT's FillMemory API. /// </summary> private static IntPtr StringAt(IntPtr src, int len) { byte[] res; if (len == -1) { res = MemoryHolder.ReadBytes(src, 0); } else { res = MemoryHolder.ReadBytes(src, 0, len); } return(GCHandle.ToIntPtr(GCHandle.Alloc(res))); }
internal object GetRawValue(MemoryHolder owner, int offset) { Debug.Assert(IsStringType); SimpleType st = (SimpleType)_type; if (st._type == SimpleTypeKind.Char) { return(owner.ReadBytes(offset, _length)); } else { return(owner.ReadUnicodeString(offset, _length)); } }
object INativeType.SetValue(MemoryHolder /*!*/ address, int offset, object value) { try { return(SetValueInternal(address, offset, value)); } catch (ArgumentTypeException e) { throw PythonOps.RuntimeError("({0}) <type 'exceptions.TypeError'>: {1}", Name, e.Message); } catch (ArgumentException e) { throw PythonOps.RuntimeError("({0}) <type 'exceptions.ValueError'>: {1}", Name, e.Message); } }
internal void SetValue(MemoryHolder address, int baseOffset, object value) { if (_bits == -1) { object keepAlive = _fieldType.SetValue(address, baseOffset + _offset, value); if (keepAlive != null) { address.AddObject(_index.ToString(), keepAlive); } } else { SetBitsValue(address, baseOffset, value); } }
internal void SetRawValue(MemoryHolder owner, int offset, object value) { Debug.Assert(_type is SimpleType st && st._type == SimpleTypeKind.Char); if (value is IBufferProtocol bufferProtocol) { var buffer = bufferProtocol.GetBuffer(); var span = buffer.AsReadOnlySpan(); if (span.Length > _length) { throw PythonOps.ValueError("byte string too long ({0}, maximum length {1})", span.Length, _length); } owner.WriteSpan(offset, span); return; } throw PythonOps.TypeErrorForBytesLikeTypeMismatch(value); }
internal string GetRawValue(MemoryHolder owner, int offset) { Debug.Assert(IsStringType); SimpleType st = (SimpleType)_type; string str; if (st._type == SimpleTypeKind.Char) { str = owner.ReadAnsiString(offset, _length); } else { str = owner.ReadUnicodeString(offset, _length); } return(str); }
private void WriteString(MemoryHolder address, int offset, string str) { SimpleType st = (SimpleType)_type; if (str.Length < _length) { str = str + '\x00'; } if (st._type == SimpleTypeKind.Char) { address.WriteAnsiString(offset, str); } else { address.WriteUnicodeString(offset, str); } }
public void __init__(params object[] args) { INativeType nativeType = NativeType; MemHolder = new MemoryHolder(nativeType.Size); if (args.Length > ((ArrayType)nativeType).Length) { throw PythonOps.IndexError("too many arguments"); } INativeType elementType = ElementType; for (var i = 0; i < args.Length; i++) { elementType.SetValue(MemHolder, checked (i * elementType.Size), args[i]); } }
public object this[int index] { get { INativeType type = ((PointerType)NativeType)._type; MemoryHolder address = _memHolder.ReadMemoryHolder(0); return(type.GetValue(address, this, checked (type.Size * index), false)); } set { MemoryHolder address = _memHolder.ReadMemoryHolder(0); INativeType type = ((PointerType)NativeType)._type; object keepAlive = type.SetValue(address, checked (type.Size * index), value); if (keepAlive != null) { _memHolder.AddObject(index.ToString(), keepAlive); } } }
object INativeType.GetValue(MemoryHolder owner, object readingFrom, int offset, bool raw) { if (IsStringType) { SimpleType st = (SimpleType)_type; if (st._type == SimpleTypeKind.Char) { IList <byte> str = owner.ReadBytes(offset, _length); // remove any trailing nulls for (int i = 0; i < str.Count; i++) { if (str[i] == 0) { return(str.Substring(0, i)); } } return(str); } else { string str = owner.ReadUnicodeString(offset, _length); // remove any trailing nulls for (int i = 0; i < str.Length; i++) { if (str[i] == '\x00') { return(str.Substring(0, i)); } } return(str); } } _Array arr = (_Array)CreateInstance(Context.SharedContext); arr._memHolder = new MemoryHolder(owner.UnsafeAddress.Add(offset), ((INativeType)this).Size, owner); return(arr); }
object INativeType.SetValue(MemoryHolder address, int offset, object value) { if (value is int) { address.WriteIntPtr(offset, new IntPtr((int)value)); } else if (value is BigInteger) { address.WriteIntPtr(offset, new IntPtr((long)(BigInteger)value)); } else if (value is _CFuncPtr) { address.WriteIntPtr(offset, ((_CFuncPtr)value).addr); return(value); } else { throw PythonOps.TypeErrorForTypeMismatch("func pointer", value); } return(null); }
public void WriteIntPtr(int offset, MemoryHolder address) { Marshal.WriteIntPtr(_data, offset, address.UnsafeAddress); GC.KeepAlive(this); GC.KeepAlive(address); }
object INativeType.SetValue(MemoryHolder address, int offset, object value) { throw new NotImplementedException("union set value"); }
public static object CreateMemoryHolder(IntPtr data, int size) { var res = new MemoryHolder(size); res.CopyFrom(data, new IntPtr(size)); return res; }
public void __init__() { _memHolder = new MemoryHolder(Size); }
object INativeType.GetValue(MemoryHolder/*!*/ owner, object readingFrom, int offset, bool raw) { object res; switch (_type) { case SimpleTypeKind.Boolean: res = owner.ReadByte(offset) != 0 ? ScriptingRuntimeHelpers.True : ScriptingRuntimeHelpers.False; break; case SimpleTypeKind.Char: res = new string((char)owner.ReadByte(offset), 1); break; case SimpleTypeKind.SignedByte: res = GetIntReturn((int)(sbyte)owner.ReadByte(offset)); break; case SimpleTypeKind.UnsignedByte: res = GetIntReturn((int)owner.ReadByte(offset)); break; case SimpleTypeKind.SignedShort: res = GetIntReturn((int)owner.ReadInt16(offset)); break; case SimpleTypeKind.WChar: res = new string((char)owner.ReadInt16(offset), 1); break; case SimpleTypeKind.UnsignedShort: res = GetIntReturn((int)(ushort)owner.ReadInt16(offset)); break; case SimpleTypeKind.VariantBool: res = owner.ReadInt16(offset) != 0 ? ScriptingRuntimeHelpers.True : ScriptingRuntimeHelpers.False; break; case SimpleTypeKind.SignedInt: res = GetIntReturn((int)owner.ReadInt32(offset)); break; case SimpleTypeKind.UnsignedInt: res = GetIntReturn((uint)owner.ReadInt32(offset)); break; case SimpleTypeKind.UnsignedLong: res = GetIntReturn((uint)owner.ReadInt32(offset)); break; case SimpleTypeKind.SignedLong: res = GetIntReturn(owner.ReadInt32(offset)); break; case SimpleTypeKind.Single: res = GetSingleReturn(owner.ReadInt32(offset)); break; case SimpleTypeKind.Double: res = GetDoubleReturn(owner.ReadInt64(offset)); break; case SimpleTypeKind.UnsignedLongLong: res = GetIntReturn((ulong)owner.ReadInt64(offset)); break; case SimpleTypeKind.SignedLongLong: res = GetIntReturn(owner.ReadInt64(offset)); break; case SimpleTypeKind.Object: res = GetObjectReturn(owner.ReadIntPtr(offset)); break; case SimpleTypeKind.Pointer: res = owner.ReadIntPtr(offset).ToPython(); break; case SimpleTypeKind.CharPointer: res = owner.ReadMemoryHolder(offset).ReadAnsiString(0); break; case SimpleTypeKind.WCharPointer: res = owner.ReadMemoryHolder(offset).ReadUnicodeString(0); break; default: throw new InvalidOperationException(); } if (!raw && IsSubClass) { res = PythonCalls.Call(this, res); } return res; }
internal void SetAddress(IntPtr address) { Debug.Assert(_memHolder == null); _memHolder = new MemoryHolder(address, NativeType.Size); }
/// <summary> /// Copies memory from one location to another keeping the associated memory holders alive during the /// operation. /// </summary> public void CopyTo(MemoryHolder /*!*/ destAddress, int writeOffset, int size) { NativeFunctions.CopyMemory(destAddress._data.Add(writeOffset), _data, new IntPtr(size)); GC.KeepAlive(destAddress); GC.KeepAlive(this); }
object INativeType.SetValue(MemoryHolder address, int offset, object value) { string str = value as string; if (str != null) { if (!IsStringType) { throw PythonOps.TypeError("expected {0} instance, got str", Name); } else if (str.Length > _length) { throw PythonOps.ValueError("string too long ({0}, maximum length {1})", str.Length, _length); } WriteString(address, offset, str); return(null); } else if (IsStringType) { IList <object> objList = value as IList <object>; if (objList != null) { StringBuilder res = new StringBuilder(objList.Count); foreach (object o in objList) { res.Append(Converter.ConvertToChar(o)); } WriteString(address, offset, res.ToString()); return(null); } throw PythonOps.TypeError("expected string or Unicode object, {0} found", DynamicHelpers.GetPythonType(value).Name); } object[] arrArgs = value as object[]; if (arrArgs == null) { PythonTuple pt = value as PythonTuple; if (pt != null) { arrArgs = pt._data; } } if (arrArgs != null) { if (arrArgs.Length > _length) { throw PythonOps.RuntimeError("invalid index"); } for (int i = 0; i < arrArgs.Length; i++) { _type.SetValue(address, checked (offset + i * _type.Size), arrArgs[i]); } } else { _Array arr = value as _Array; if (arr != null && arr.NativeType == this) { arr._memHolder.CopyTo(address, offset, ((INativeType)this).Size); return(arr._memHolder.EnsureObjects()); } throw PythonOps.TypeError("unexpected {0} instance, got {1}", Name, DynamicHelpers.GetPythonType(value).Name); } return(null); }
/// <summary> /// Called for fields which have been limited to a range of bits. Sets the /// specified value into the bits for the field. /// </summary> private void SetBitsValue(MemoryHolder address, int baseOffset, object value) { // get the value in the form of a ulong which can contain the biggest bitfield ulong newBits; if (value is int) { newBits = (ulong)(int)value; } else if (value is BigInteger) { newBits = (ulong)(long)(BigInteger)value; } else { throw PythonOps.TypeErrorForTypeMismatch("int or long", value); } // do the same for the existing value int offset = checked (_offset + baseOffset); object curValue = _fieldType.GetValue(address, null, offset, false); ulong valueBits; if (curValue is int) { valueBits = (ulong)(int)curValue; } else { valueBits = (ulong)(long)(BigInteger)curValue; } // get a mask for the bits this field owns ulong targetBits = ((1UL << _bits) - 1) << _bitsOffset; // clear the existing bits valueBits &= ~targetBits; // or in the new bits provided by the user valueBits |= (newBits << _bitsOffset) & targetBits; // and set the value if (IsSignedType) { if (_fieldType.Size <= 4) { _fieldType.SetValue(address, offset, (int)(long)valueBits); } else { _fieldType.SetValue(address, offset, (BigInteger)(long)valueBits); } } else { if (_fieldType.Size < 4) { _fieldType.SetValue(address, offset, (int)valueBits); } else { _fieldType.SetValue(address, offset, (BigInteger)valueBits); } } }
object INativeType.GetValue(MemoryHolder owner, int offset, bool raw) { if (IsStringType) { SimpleType st = (SimpleType)_type; string str; if (st._type == SimpleTypeKind.Char) { str = owner.ReadAnsiString(offset, _length); } else { str = owner.ReadUnicodeString(offset, _length); } // remove any trailing nulls for (int i = 0; i < str.Length; i++) { if (str[i] == '\x00') { return str.Substring(0, i); } } return str; } object[] res = new object[_length]; for (int i = 0; i < res.Length; i++) { res[i] = _type.GetValue(owner, checked(offset + _type.Size * i), raw); } return List.FromArrayNoCopy(res); }
public void __init__(object value) { _memHolder = new MemoryHolder(Size); NativeType.SetValue(_memHolder, 0, value); }
/// <summary> /// Helper function for reading char/wchar's. This is used for reading from /// arrays and pointers to avoid creating lots of 1-char strings. /// </summary> internal char ReadChar(MemoryHolder/*!*/ owner, int offset) { switch (_type) { case SimpleTypeKind.Char: return (char)owner.ReadByte(offset); case SimpleTypeKind.WChar: return (char)owner.ReadInt16(offset); default: throw new InvalidOperationException(); } }
object INativeType.SetValue(MemoryHolder address, int offset, object value) { Pointer ptr; _Array array; if (value == null) { address.WriteIntPtr(offset, IntPtr.Zero); } else if (value is int) { address.WriteIntPtr(offset, new IntPtr((int)value)); } else if (value is BigInteger) { address.WriteIntPtr(offset, new IntPtr((long)(BigInteger)value)); } else if ((ptr = value as Pointer) != null) { address.WriteIntPtr(offset, ptr._memHolder.ReadMemoryHolder(0)); return PythonOps.MakeDictFromItems(ptr, "0", ptr._objects, "1"); } else if ((array = value as _Array) != null) { address.WriteIntPtr(offset, array._memHolder); return array; } else { throw PythonOps.TypeErrorForTypeMismatch(Name, value); } return null; }
object INativeType.SetValue(MemoryHolder/*!*/ owner, int offset, object value) { SimpleCData data = value as SimpleCData; if (data != null && data.NativeType == this) { data._memHolder.CopyTo(owner, offset, ((INativeType)this).Size); return null; } switch (_type) { case SimpleTypeKind.Boolean: owner.WriteByte(offset, ModuleOps.GetBoolean(value, this)); break; case SimpleTypeKind.Char: owner.WriteByte(offset, ModuleOps.GetChar(value, this)); break; case SimpleTypeKind.SignedByte: owner.WriteByte(offset, ModuleOps.GetSignedByte(value, this)); break; case SimpleTypeKind.UnsignedByte: owner.WriteByte(offset, ModuleOps.GetUnsignedByte(value, this)); break; case SimpleTypeKind.WChar: owner.WriteInt16(offset, (short)ModuleOps.GetWChar(value, this)); break; case SimpleTypeKind.SignedShort: owner.WriteInt16(offset, ModuleOps.GetSignedShort(value, this)); break; case SimpleTypeKind.UnsignedShort: owner.WriteInt16(offset, ModuleOps.GetUnsignedShort(value, this)); break; case SimpleTypeKind.VariantBool: owner.WriteInt16(offset, (short)ModuleOps.GetVariantBool(value, this)); break; case SimpleTypeKind.SignedInt: owner.WriteInt32(offset, ModuleOps.GetSignedInt(value, this)); break; case SimpleTypeKind.UnsignedInt: owner.WriteInt32(offset, ModuleOps.GetUnsignedInt(value, this)); break; case SimpleTypeKind.UnsignedLong: owner.WriteInt32(offset, ModuleOps.GetUnsignedLong(value, this)); break; case SimpleTypeKind.SignedLong: owner.WriteInt32(offset, ModuleOps.GetSignedLong(value, this)); break; case SimpleTypeKind.Single: owner.WriteInt32(offset, ModuleOps.GetSingleBits(value)); break; case SimpleTypeKind.Double: owner.WriteInt64(offset, ModuleOps.GetDoubleBits(value)); break; case SimpleTypeKind.UnsignedLongLong: owner.WriteInt64(offset, ModuleOps.GetUnsignedLongLong(value, this)); break; case SimpleTypeKind.SignedLongLong: owner.WriteInt64(offset, ModuleOps.GetSignedLongLong(value, this)); break; case SimpleTypeKind.Object: owner.WriteIntPtr(offset, ModuleOps.GetObject(value)); break; case SimpleTypeKind.Pointer: owner.WriteIntPtr(offset, ModuleOps.GetPointer(value)); break; case SimpleTypeKind.CharPointer: owner.WriteIntPtr(offset, ModuleOps.GetCharPointer(value)); return value; case SimpleTypeKind.WCharPointer: owner.WriteIntPtr(offset, ModuleOps.GetWCharPointer(value)); return value; default: throw new InvalidOperationException(); } return null; }
object INativeType.SetValue(MemoryHolder address, int offset, object value) { string str = value as string; if (str != null) { if (!IsStringType) { throw PythonOps.TypeError("expected {0} instance, got str", Name); } else if (str.Length > _length) { throw PythonOps.ValueError("string too long ({0}, maximum length {1})", str.Length, _length); } WriteString(address, offset, str); return null; } else if (IsStringType) { IList<object> objList = value as IList<object>; if (objList != null) { StringBuilder res = new StringBuilder(objList.Count); foreach (object o in objList) { res.Append(Converter.ConvertToChar(o)); } WriteString(address, offset, res.ToString()); return null; } throw PythonOps.TypeError("expected string or Unicode object, {0} found", DynamicHelpers.GetPythonType(value).Name); } object[] arrArgs = value as object[]; if (arrArgs == null) { PythonTuple pt = value as PythonTuple; if (pt != null) { arrArgs = pt._data; } } if (arrArgs != null) { if (arrArgs.Length > _length) { throw PythonOps.RuntimeError("invalid index"); } for (int i = 0; i < arrArgs.Length; i++) { _type.SetValue(address, checked(offset + i * _type.Size), arrArgs[i]); } } else { _Array arr = value as _Array; if (arr != null && arr.NativeType == this) { arr._memHolder.CopyTo(address, offset, ((INativeType)this).Size); return arr._memHolder.EnsureObjects(); } throw PythonOps.TypeError("unexpected {0} instance, got {1}", Name, DynamicHelpers.GetPythonType(value).Name); } return null; }
object INativeType.GetValue(MemoryHolder/*!*/ owner, object readingFrom, int offset, bool raw) { _Structure res = (_Structure)CreateInstance(this.Context.SharedContext); res._memHolder = owner.GetSubBlock(offset); return res; }
public void __init__(CodeContext /*!*/ context) { MemHolder = new MemoryHolder(Size); }
object INativeType.SetValue(MemoryHolder/*!*/ address, int offset, object value) { try { return SetValueInternal(address, offset, value); } catch (ArgumentTypeException e) { throw PythonOps.RuntimeError("({0}) <type 'exceptions.TypeError'>: {1}", Name, e.Message); } catch (ArgumentException e) { throw PythonOps.RuntimeError("({0}) <type 'exceptions.ValueError'>: {1}", Name, e.Message); } }
object INativeType.GetValue(MemoryHolder owner, int offset, bool raw) { throw new NotImplementedException("union get value"); }
object INativeType.SetValue(MemoryHolder /*!*/ owner, int offset, object value) { SimpleCData data = value as SimpleCData; if (data != null && data.NativeType == this) { data._memHolder.CopyTo(owner, offset, ((INativeType)this).Size); return(null); } switch (_type) { case SimpleTypeKind.Boolean: owner.WriteByte(offset, ModuleOps.GetBoolean(value, this)); break; case SimpleTypeKind.Char: owner.WriteByte(offset, ModuleOps.GetChar(value, this)); break; case SimpleTypeKind.SignedByte: owner.WriteByte(offset, ModuleOps.GetSignedByte(value, this)); break; case SimpleTypeKind.UnsignedByte: owner.WriteByte(offset, ModuleOps.GetUnsignedByte(value, this)); break; case SimpleTypeKind.WChar: owner.WriteInt16(offset, (short)ModuleOps.GetWChar(value, this)); break; case SimpleTypeKind.SignedShort: owner.WriteInt16(offset, ModuleOps.GetSignedShort(value, this)); break; case SimpleTypeKind.UnsignedShort: owner.WriteInt16(offset, ModuleOps.GetUnsignedShort(value, this)); break; case SimpleTypeKind.VariantBool: owner.WriteInt16(offset, (short)ModuleOps.GetVariantBool(value, this)); break; case SimpleTypeKind.SignedInt: owner.WriteInt32(offset, ModuleOps.GetSignedInt(value, this)); break; case SimpleTypeKind.UnsignedInt: owner.WriteInt32(offset, ModuleOps.GetUnsignedInt(value, this)); break; case SimpleTypeKind.UnsignedLong: owner.WriteInt32(offset, ModuleOps.GetUnsignedLong(value, this)); break; case SimpleTypeKind.SignedLong: owner.WriteInt32(offset, ModuleOps.GetSignedLong(value, this)); break; case SimpleTypeKind.Single: owner.WriteInt32(offset, ModuleOps.GetSingleBits(value)); break; case SimpleTypeKind.Double: owner.WriteInt64(offset, ModuleOps.GetDoubleBits(value)); break; case SimpleTypeKind.UnsignedLongLong: owner.WriteInt64(offset, ModuleOps.GetUnsignedLongLong(value, this)); break; case SimpleTypeKind.SignedLongLong: owner.WriteInt64(offset, ModuleOps.GetSignedLongLong(value, this)); break; case SimpleTypeKind.Object: owner.WriteIntPtr(offset, ModuleOps.GetObject(value)); break; case SimpleTypeKind.Pointer: owner.WriteIntPtr(offset, ModuleOps.GetPointer(value)); break; case SimpleTypeKind.CharPointer: owner.WriteIntPtr(offset, ModuleOps.GetCharPointer(value)); return(value); case SimpleTypeKind.WCharPointer: owner.WriteIntPtr(offset, ModuleOps.GetWCharPointer(value)); return(value); default: throw new InvalidOperationException(); } return(null); }
public _CFuncPtr() { _id = Interlocked.Increment(ref _curId); _memHolder = new MemoryHolder(IntPtr.Size); }
/// <summary> /// Copies memory from one location to another keeping the associated memory holders alive during the /// operation. /// </summary> public void CopyTo(MemoryHolder/*!*/ destAddress, int writeOffset, int size) { NativeFunctions.CopyMemory(destAddress._data.Add(writeOffset), _data, new IntPtr(size)); GC.KeepAlive(destAddress); GC.KeepAlive(this); }
/// <summary> /// Creates a new CFuncPtr which calls a COM method. /// </summary> public _CFuncPtr(int index, string name) { _memHolder = new MemoryHolder(IntPtr.Size); _comInterfaceIndex = index; _id = Interlocked.Increment(ref _curId); }
public Pointer() { _memHolder = new MemoryHolder(IntPtr.Size); }
/// <summary> /// Creates a new CFuncPtr with the specfied address. /// </summary> public _CFuncPtr([NotNull] BigInteger handle) { _memHolder = new MemoryHolder(IntPtr.Size); addr = new IntPtr((long)handle); _id = Interlocked.Increment(ref _curId); }
#pragma warning restore 414 public Pointer() { _memHolder = new MemoryHolder(IntPtr.Size); }
public _CFuncPtr(IntPtr handle) { _memHolder = new MemoryHolder(IntPtr.Size); addr = handle; _id = Interlocked.Increment(ref _curId); }
protected _Structure() { ((StructType)NativeType).EnsureFinal(); _memHolder = new MemoryHolder(NativeType.Size); }
internal object GetRawValue(MemoryHolder owner, int offset) { Debug.Assert(_type is SimpleType st && st._type == SimpleTypeKind.Char); return(owner.ReadBytes(offset, _length)); }
object INativeType.GetValue(MemoryHolder owner, object readingFrom, int offset, bool raw) { if (IsStringType) { SimpleType st = (SimpleType)_type; string str; if (st._type == SimpleTypeKind.Char) { str = owner.ReadAnsiString(offset, _length); } else { str = owner.ReadUnicodeString(offset, _length); } // remove any trailing nulls for (int i = 0; i < str.Length; i++) { if (str[i] == '\x00') { return str.Substring(0, i); } } return str; } _Array arr = (_Array)CreateInstance(Context.SharedContext); arr._memHolder = new MemoryHolder(owner.UnsafeAddress.Add(offset), ((INativeType)this).Size, owner); return arr; }
object INativeType.SetValue(MemoryHolder address, int offset, object value) { if (_type is SimpleType st) { if (st._type == SimpleTypeKind.Char) { if (value is Bytes bytes) { if (bytes.Count > _length) { throw PythonOps.ValueError("byte string too long ({0}, maximum length {1})", bytes.Count, _length); } WriteBytes(address, offset, bytes); return(null); } throw PythonOps.TypeError("expected bytes, {0} found", PythonOps.GetPythonTypeName(value)); } if (st._type == SimpleTypeKind.WChar) { if (value is string str) { if (str.Length > _length) { throw PythonOps.ValueError("string too long ({0}, maximum length {1})", str.Length, _length); } WriteString(address, offset, str); return(null); } throw PythonOps.TypeError("unicode string expected instead of {0} instance", PythonOps.GetPythonTypeName(value)); } } object[] arrArgs = value as object[]; if (arrArgs == null) { if (value is PythonTuple pt) { arrArgs = pt._data; } } if (arrArgs != null) { if (arrArgs.Length > _length) { throw PythonOps.RuntimeError("invalid index"); } for (int i = 0; i < arrArgs.Length; i++) { _type.SetValue(address, checked (offset + i * _type.Size), arrArgs[i]); } } else { if (value is _Array arr && arr.NativeType == this) { arr.MemHolder.CopyTo(address, offset, ((INativeType)this).Size); return(arr.MemHolder.EnsureObjects()); } throw PythonOps.TypeError("unexpected {0} instance, got {1}", Name, PythonOps.GetPythonTypeName(value)); } return(null); }
internal string GetRawValue(MemoryHolder owner, int offset) { Debug.Assert(IsStringType); SimpleType st = (SimpleType)_type; string str; if (st._type == SimpleTypeKind.Char) { str = owner.ReadAnsiString(offset, _length); } else { str = owner.ReadUnicodeString(offset, _length); } return str; }
internal object SetValueInternal(MemoryHolder address, int offset, object value) { IList<object> init = value as IList<object>; if (init != null) { if (init.Count > _fields.Length) { throw PythonOps.TypeError("too many initializers"); } for (int i = 0; i < init.Count; i++) { _fields[i].SetValue(address, offset, init[i]); } } else { CData data = value as CData; if (data != null) { data._memHolder.CopyTo(address, offset, data.Size); return data._memHolder.EnsureObjects(); } else { throw new NotImplementedException("set value"); } } return null; }
/// <summary> /// Called for fields which have been limited to a range of bits. Sets the /// specified value into the bits for the field. /// </summary> private void SetBitsValue(MemoryHolder address, int baseOffset, object value) { // get the value in the form of a ulong which can contain the biggest bitfield ulong newBits; if (value is int) { newBits = (ulong)(int)value; } else if (value is BigInteger) { newBits = (ulong)(long)(BigInteger)value; } else { throw PythonOps.TypeErrorForTypeMismatch("int or long", value); } // do the same for the existing value int offset = checked(_offset + baseOffset); object curValue = _fieldType.GetValue(address, null, offset, false); ulong valueBits; if (curValue is int) { valueBits = (ulong)(int)curValue; } else { valueBits = (ulong)(long)(BigInteger)curValue; } // get a mask for the bits this field owns ulong targetBits = ((1UL << _bits) - 1) << _bitsOffset; // clear the existing bits valueBits &= ~targetBits; // or in the new bits provided by the user valueBits |= (newBits << _bitsOffset) & targetBits; // and set the value if (IsSignedType) { if (_fieldType.Size <= 4) { _fieldType.SetValue(address, offset, (int)(long)valueBits); } else { _fieldType.SetValue(address, offset, (BigInteger)(long)valueBits); } } else { if (_fieldType.Size < 4) { _fieldType.SetValue(address, offset, (int)valueBits); } else { _fieldType.SetValue(address, offset, (BigInteger)valueBits); } } }