A wrapper around allocated memory to ensure it gets released and isn't accessed when it could be finalized.
Inheritance: System.Runtime.ConstrainedExecution.CriticalFinalizerObject
コード例 #1
0
ファイル: SimpleCData.cs プロジェクト: TerabyteX/ironpython3
 public void __init__(object value) {
     _memHolder = new MemoryHolder(Size);
     NativeType.SetValue(_memHolder, 0, value);
     if (IsString) {
         _memHolder.AddObject("str", value);
     }
 }
コード例 #2
0
ファイル: MemoryHolder.cs プロジェクト: rudimk/dlr-dotnet
 /// <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;
 }
コード例 #3
0
ファイル: Structure.cs プロジェクト: m4dc4p/ironruby
            public void __init__(params object[] args) {
                CheckAbstract();

                INativeType nativeType = NativeType;
                StructType st = (StructType)nativeType;

                _memHolder = new MemoryHolder(nativeType.Size);
                st.SetValueInternal(_memHolder, 0, args);
            }
コード例 #4
0
ファイル: Pointer.cs プロジェクト: nieve/ironruby
 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);
     }
 }
コード例 #5
0
ファイル: Array.cs プロジェクト: kevinkeeney/ironruby
            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);
            }
コード例 #6
0
ファイル: _ctypes.cs プロジェクト: play3577/ironpython2
        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;
        }
コード例 #7
0
            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);
            }
コード例 #8
0
ファイル: Structure.cs プロジェクト: m4dc4p/ironruby
            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);
                }
            }
コード例 #9
0
            // __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);
            }
コード例 #10
0
        /// <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)));
        }
コード例 #11
0
ファイル: ArrayType.cs プロジェクト: FYourLove/ironpython3
            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));
                }
            }
コード例 #12
0
ファイル: StructType.cs プロジェクト: wangpei421/ironpython2
 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);
     }
 }
コード例 #13
0
 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);
     }
 }
コード例 #14
0
 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);
 }
コード例 #15
0
            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);
            }
コード例 #16
0
            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);
                }
            }
コード例 #17
0
            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]);
                }
            }
コード例 #18
0
            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);
                    }
                }
            }
コード例 #19
0
ファイル: ArrayType.cs プロジェクト: FYourLove/ironpython3
            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);
            }
コード例 #20
0
 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);
 }
コード例 #21
0
ファイル: MemoryHolder.cs プロジェクト: rudimk/dlr-dotnet
 public void WriteIntPtr(int offset, MemoryHolder address) {
     Marshal.WriteIntPtr(_data, offset, address.UnsafeAddress);
     GC.KeepAlive(this);
     GC.KeepAlive(address);
 }
コード例 #22
0
ファイル: UnionType.cs プロジェクト: xerxesb/ironruby
 object INativeType.SetValue(MemoryHolder address, int offset, object value) {
     throw new NotImplementedException("union set value");
 }
コード例 #23
0
ファイル: ModuleOps.cs プロジェクト: nieve/ironruby
 public static object CreateMemoryHolder(IntPtr data, int size) {
     var res = new MemoryHolder(size);
     res.CopyFrom(data, new IntPtr(size));
     return res;
 }
コード例 #24
0
ファイル: ArrayType.cs プロジェクト: techarch/ironruby
            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);
                }

            }
コード例 #25
0
 public void __init__()
 {
     _memHolder = new MemoryHolder(Size);
 }
コード例 #26
0
ファイル: SimpleType.cs プロジェクト: jschementi/iron
            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;
            }
コード例 #27
0
ファイル: CData.cs プロジェクト: richardy2012/ironpython2
 internal void SetAddress(IntPtr address)
 {
     Debug.Assert(_memHolder == null);
     _memHolder = new MemoryHolder(address, NativeType.Size);
 }
コード例 #28
0
 /// <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);
 }
コード例 #29
0
            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);
            }
コード例 #30
0
            /// <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);
                    }
                }
            }
コード例 #31
0
ファイル: Field.cs プロジェクト: kevinkeeney/ironruby
 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);
     }
 }
コード例 #32
0
ファイル: ArrayType.cs プロジェクト: m4dc4p/ironruby
            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);
            }
コード例 #33
0
ファイル: SimpleCData.cs プロジェクト: m4dc4p/ironruby
 public void __init__(object value) {
     _memHolder = new MemoryHolder(Size);
     NativeType.SetValue(_memHolder, 0, value);
 }
コード例 #34
0
ファイル: SimpleType.cs プロジェクト: jschementi/iron
 /// <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();
     }
 }
コード例 #35
0
ファイル: PointerType.cs プロジェクト: kevinkeeney/ironruby
            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;
            }
コード例 #36
0
ファイル: SimpleType.cs プロジェクト: jschementi/iron
            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;
            }
コード例 #37
0
ファイル: _ctypes.cs プロジェクト: techarch/ironruby
        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;
        }
コード例 #38
0
ファイル: CData.cs プロジェクト: nieve/ironruby
 internal void SetAddress(IntPtr address) {
     Debug.Assert(_memHolder == null);
     _memHolder = new MemoryHolder(address, NativeType.Size);
 }
コード例 #39
0
ファイル: ArrayType.cs プロジェクト: techarch/ironruby
            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;
            }
コード例 #40
0
ファイル: StructType.cs プロジェクト: joshholmes/ironruby
 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;
 }
コード例 #41
0
ファイル: Union.cs プロジェクト: slozier/ironpython3
 public void __init__(CodeContext /*!*/ context)
 {
     MemHolder = new MemoryHolder(Size);
 }
コード例 #42
0
ファイル: StructType.cs プロジェクト: joshholmes/ironruby
 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);
     }
 }
コード例 #43
0
ファイル: UnionType.cs プロジェクト: xerxesb/ironruby
 object INativeType.GetValue(MemoryHolder owner, int offset, bool raw) {
     throw new NotImplementedException("union get value");
 }
コード例 #44
0
 public void WriteIntPtr(int offset, MemoryHolder address)
 {
     Marshal.WriteIntPtr(_data, offset, address.UnsafeAddress);
     GC.KeepAlive(this);
     GC.KeepAlive(address);
 }
コード例 #45
0
            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);
            }
コード例 #46
0
 public _CFuncPtr()
 {
     _id        = Interlocked.Increment(ref _curId);
     _memHolder = new MemoryHolder(IntPtr.Size);
 }
コード例 #47
0
ファイル: MemoryHolder.cs プロジェクト: rudimk/dlr-dotnet
 /// <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);
 }
コード例 #48
0
 /// <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);
 }
コード例 #49
0
ファイル: Pointer.cs プロジェクト: nieve/ironruby
 public Pointer() {
     _memHolder = new MemoryHolder(IntPtr.Size);
 }
コード例 #50
0
 /// <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);
 }
コード例 #51
0
#pragma warning restore 414

            public Pointer()
            {
                _memHolder = new MemoryHolder(IntPtr.Size);
            }
コード例 #52
0
 public _CFuncPtr(IntPtr handle)
 {
     _memHolder = new MemoryHolder(IntPtr.Size);
     addr       = handle;
     _id        = Interlocked.Increment(ref _curId);
 }
コード例 #53
0
ファイル: SimpleCData.cs プロジェクト: TerabyteX/ironpython3
 public void __init__() {
     _memHolder = new MemoryHolder(Size);
 }
コード例 #54
0
 protected _Structure()
 {
     ((StructType)NativeType).EnsureFinal();
     _memHolder = new MemoryHolder(NativeType.Size);
 }
コード例 #55
0
 internal object GetRawValue(MemoryHolder owner, int offset)
 {
     Debug.Assert(_type is SimpleType st && st._type == SimpleTypeKind.Char);
     return(owner.ReadBytes(offset, _length));
 }
コード例 #56
0
ファイル: ArrayType.cs プロジェクト: techarch/ironruby
            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;
            }
コード例 #57
0
            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);
            }
コード例 #58
0
ファイル: ArrayType.cs プロジェクト: techarch/ironruby
            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;
            }
コード例 #59
0
ファイル: StructType.cs プロジェクト: joshholmes/ironruby
            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;
            }
コード例 #60
0
ファイル: Field.cs プロジェクト: kevinkeeney/ironruby
            /// <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);
                    }
                }
            }