Ejemplo n.º 1
0
 /// <summary>
 /// Iterates over elements in <paramref name="tsList"/> and calls <paramref name="handler"/>
 /// </summary>
 /// <typeparam name="T">Type to store in list</typeparam>
 /// <param name="tsList">A thread-safe list</param>
 /// <param name="handler">Called for each element</param>
 /// <param name="startIndex">Start index</param>
 /// <param name="endIndex">End index. <c>-1</c> means <see cref="ThreadSafe.IList{T}.Count_NoLock"/></param>
 /// <param name="reverseOrder"><c>true</c> if we should iterate in the reverse order</param>
 public static void Iterate <T>(this ThreadSafe.IList <T> tsList, int startIndex, int endIndex, bool reverseOrder, IterateDelegate <T> handler)
 {
     tsList.ExecuteLocked <object, object>(null, (tsList2, arg) => {
         if (reverseOrder)
         {
             int i = (endIndex < 0 ? tsList2.Count_NoLock : endIndex) - 1;
             for (; i >= startIndex; i--)
             {
                 if (!handler(tsList2, i, tsList2.Get_NoLock(i)))
                 {
                     break;
                 }
             }
         }
         else
         {
             // Count property can change so check it each time in the loop
             for (int i = startIndex; i < (endIndex < 0 ? tsList2.Count_NoLock : endIndex); i++)
             {
                 if (!handler(tsList2, i, tsList2.Get_NoLock(i)))
                 {
                     break;
                 }
             }
         }
         return(null);
     });
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Iterates over all elements in <paramref name="tsList"/> in the reverse order and calls
 /// <paramref name="handler"/>
 /// </summary>
 /// <typeparam name="T">Type to store in list</typeparam>
 /// <param name="tsList">A thread-safe list</param>
 /// <param name="handler">Called for each element</param>
 public static void IterateAllReverse <T>(this ThreadSafe.IList <T> tsList, IterateAllDelegate <T> handler)
 {
     tsList.Iterate(0, -1, true, (tsList2, index, value) => {
         handler(tsList2, index, value);
         return(true);
     });
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Default constructor
 /// </summary>
 public CilBody()
 {
     this.initLocals        = true;
     this.instructions      = ThreadSafeListCreator.Create <Instruction>();
     this.exceptionHandlers = ThreadSafeListCreator.Create <ExceptionHandler>();
     this.localList         = new LocalList();
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="initLocals">Init locals flag</param>
 /// <param name="instructions">All instructions. This instance will own the list.</param>
 /// <param name="exceptionHandlers">All exception handlers. This instance will own the list.</param>
 /// <param name="locals">All locals. This instance will own the locals in the list.</param>
 public CilBody(bool initLocals, IList <Instruction> instructions, IList <ExceptionHandler> exceptionHandlers, IList <Local> locals)
 {
     this.initLocals        = initLocals;
     this.instructions      = ThreadSafeListCreator.MakeThreadSafe(instructions);
     this.exceptionHandlers = ThreadSafeListCreator.MakeThreadSafe(exceptionHandlers);
     this.localList         = new LocalList(locals);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Dispose method
        /// </summary>
        /// <param name="disposing"><c>true</c> if called by <see cref="Dispose()"/></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposing)
            {
                return;
            }
            Dispose(peImage);
            Dispose(stringsStream);
            Dispose(usStream);
            Dispose(blobStream);
            Dispose(guidStream);
            Dispose(tablesStream);
            var as2 = allStreams;

            if (as2 != null)
            {
                foreach (var stream in as2.GetSafeEnumerable())
                {
                    Dispose(stream);
                }
            }
            peImage                   = null;
            cor20Header               = null;
            mdHeader                  = null;
            stringsStream             = null;
            usStream                  = null;
            blobStream                = null;
            guidStream                = null;
            tablesStream              = null;
            allStreams                = null;
            fieldRidToTypeDefRid      = null;
            methodRidToTypeDefRid     = null;
            typeDefRidToNestedClasses = null;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Locks the list and then calls <paramref name="handler"/>. <paramref name="handler"/>
        /// must only call <c>*_NoLock()</c> methods. The list is unlocked once this method returns.
        /// </summary>
        /// <typeparam name="T">List type</typeparam>
        /// <typeparam name="TArgType">Argument type</typeparam>
        /// <typeparam name="TRetType">Return type</typeparam>
        /// <param name="tsList">A list</param>
        /// <param name="arg">Passed to <paramref name="handler"/></param>
        /// <param name="handler">Handler that should execute when the lock is held</param>
        /// <returns>The value <paramref name="handler"/> returns</returns>
        public static TRetType ExecuteLocked <T, TArgType, TRetType>(this ThreadSafe.IList <T> tsList, TArgType arg, ExecuteLockedDelegate <T, TArgType, TRetType> handler)
        {
#if THREAD_SAFE
            return(tsList.ExecuteLocked <TArgType, TRetType>(arg, handler));
#else
            return(handler(tsList, arg));
#endif
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="ctor">Custom attribute constructor</param>
        /// <param name="arguments">Constructor arguments. The list is now owned by this instance.</param>
        /// <param name="namedArguments">Named arguments. The list is now owned by this instance.</param>
        /// <param name="blobReader">A reader that returns the original custom attribute blob data</param>
        internal CustomAttribute(ICustomAttributeType ctor, List <CAArgument> arguments, List <CANamedArgument> namedArguments, IBinaryReader blobReader)
        {
            this.ctor      = ctor;
            this.arguments = arguments == null?ThreadSafeListCreator.Create <CAArgument>() : ThreadSafeListCreator.MakeThreadSafe(arguments);

            this.namedArguments = namedArguments == null?ThreadSafeListCreator.Create <CANamedArgument>() : ThreadSafeListCreator.MakeThreadSafe(namedArguments);

            this.blobReader = blobReader;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="ctor">Custom attribute constructor</param>
        /// <param name="arguments">Constructor arguments or <c>null</c> if none</param>
        /// <param name="namedArguments">Named arguments or <c>null</c> if none</param>
        /// <param name="blobReader">A reader that returns the original custom attribute blob data</param>
        public CustomAttribute(ICustomAttributeType ctor, IEnumerable <CAArgument> arguments, IEnumerable <CANamedArgument> namedArguments, IBinaryReader blobReader)
        {
            this.ctor      = ctor;
            this.arguments = arguments == null?ThreadSafeListCreator.Create <CAArgument>() : ThreadSafeListCreator.Create <CAArgument>(arguments);

            this.namedArguments = namedArguments == null?ThreadSafeListCreator.Create <CANamedArgument>() : ThreadSafeListCreator.Create <CANamedArgument>(namedArguments);

            this.blobReader = blobReader;
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="peImage">The PE image</param>
 /// <param name="cor20Header">The .NET header</param>
 /// <param name="mdHeader">The MD header</param>
 protected MetaData(IPEImage peImage, ImageCor20Header cor20Header, MetaDataHeader mdHeader)
 {
     try
     {
         this.allStreams  = ThreadSafeListCreator.Create <DotNetStream>();
         this.peImage     = peImage;
         this.cor20Header = cor20Header;
         this.mdHeader    = mdHeader;
     }
     catch
     {
         if (peImage != null)
         {
             peImage.Dispose();
         }
         throw;
     }
 }
Ejemplo n.º 10
0
        private void Initialize(ModuleDefMD module)
        {
            var info = module.MetaData.ImageCor20Header.VTableFixups;

            if (info.VirtualAddress == 0 || info.Size == 0)
            {
                this.vtables = ThreadSafeListCreator.Create <VTable>();
                return;
            }
            this.rva     = info.VirtualAddress;
            this.vtables = ThreadSafeListCreator.Create <VTable>((int)info.Size / 8);

            var peImage = module.MetaData.PEImage;

            using (var reader = peImage.CreateFullStream())
            {
                reader.Position = (long)peImage.ToFileOffset(info.VirtualAddress);
                long endPos = reader.Position + info.Size;
                while (reader.Position + 8 <= endPos && reader.CanRead(8))
                {
                    RVA tableRva = (RVA)reader.ReadUInt32();
                    int numSlots = reader.ReadUInt16();
                    var flags    = (VTableFlags)reader.ReadUInt16();
                    var vtable   = new VTable(tableRva, flags, numSlots);
                    vtables.Add(vtable);

                    var pos = reader.Position;
                    reader.Position = (long)peImage.ToFileOffset(tableRva);
                    int slotSize = vtable.Is64Bit ? 8 : 4;
                    while (numSlots-- > 0 && reader.CanRead(slotSize))
                    {
                        vtable.Methods.Add(module.ResolveToken(reader.ReadUInt32()) as IMethod);
                        if (slotSize == 8)
                        {
                            reader.ReadUInt32();
                        }
                    }
                    reader.Position = pos;
                }
            }
        }
Ejemplo n.º 11
0
        private bool ContainsGenericParameter(ThreadSafe.IList <TypeSig> types)
        {
            if (types == null)
            {
                return(false);
            }
            if (!recursionCounter.Increment())
            {
                return(false);
            }

            bool res = false;

            foreach (var type in types.GetSafeEnumerable())
            {
                if (ContainsGenericParameter(type))
                {
                    res = true;
                    break;
                }
            }
            recursionCounter.Decrement();
            return(res);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Default constructor
 /// </summary>
 public VTableFixups()
 {
     this.vtables = ThreadSafeListCreator.Create <VTable>();
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="rva">RVA of this vtable</param>
 /// <param name="flags">Flgas</param>
 /// <param name="methods">Vtable methods</param>
 public VTable(RVA rva, VTableFlags flags, IEnumerable <IMethod> methods)
 {
     this.rva     = rva;
     this.flags   = flags;
     this.methods = ThreadSafeListCreator.Create <IMethod>(methods);
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="rva">RVA of this vtable</param>
 /// <param name="flags">Flgas</param>
 /// <param name="numSlots">Number of methods in vtable</param>
 public VTable(RVA rva, VTableFlags flags, int numSlots)
 {
     this.rva     = rva;
     this.flags   = flags;
     this.methods = ThreadSafeListCreator.Create <IMethod>(numSlots);
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="flags">Flags</param>
 public VTable(VTableFlags flags)
 {
     this.flags   = flags;
     this.methods = ThreadSafeListCreator.Create <IMethod>();
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="args">Generic args</param>
 public GenericInstMethodSig(IList <TypeSig> args)
 {
     this.callingConvention = CallingConvention.GenericInst;
     this.genericArgs       = ThreadSafeListCreator.Create <TypeSig>(args);
 }
Ejemplo n.º 17
0
 /// <summary>Reset <see cref="AddMethod"/>, <see cref="InvokeMethod"/>, <see cref="RemoveMethod"/>, <see cref="OtherMethods"/></summary>
 protected void ResetMethods()
 {
     otherMethods = null;
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Calls <see cref="ThreadSafe.IList{T}.Count_NoLock"/>
 /// </summary>
 /// <typeparam name="T">Type to store in list</typeparam>
 /// <param name="tsList">A thread-safe list</param>
 /// <returns>Number of elements in the list</returns>
 public static int Count_NoLock <T>(this ThreadSafe.IList <T> tsList)
 {
     return(tsList.Count_NoLock);
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="local1">Local type #1</param>
 /// <param name="local2">Local type #2</param>
 /// <param name="local3">Local type #3</param>
 public LocalSig(TypeSig local1, TypeSig local2, TypeSig local3)
 {
     this.callingConvention = CallingConvention.LocalSig;
     this.locals            = ThreadSafeListCreator.Create <TypeSig>(local1, local2, local3);
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="locals">All locals</param>
 public LocalSig(IList <TypeSig> locals)
 {
     this.callingConvention = CallingConvention.LocalSig;
     this.locals            = ThreadSafeListCreator.Create <TypeSig>(locals);
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="locals">All locals (this instance now owns it)</param>
 /// <param name="dummy">Dummy</param>
 internal LocalSig(IList <TypeSig> locals, bool dummy)
 {
     this.callingConvention = CallingConvention.LocalSig;
     this.locals            = ThreadSafeListCreator.MakeThreadSafe(locals);
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="callingConvention">Calling convention (must have GenericInst set)</param>
 /// <param name="size">Number of generic args</param>
 internal GenericInstMethodSig(CallingConvention callingConvention, uint size)
 {
     this.callingConvention = callingConvention;
     this.genericArgs       = ThreadSafeListCreator.Create <TypeSig>((int)size);
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="attrType">Attribute type</param>
 /// <param name="namedArguments">Named arguments that will be owned by this instance</param>
 public SecurityAttribute(ITypeDefOrRef attrType, IList <CANamedArgument> namedArguments)
 {
     this.attrType       = attrType;
     this.namedArguments = ThreadSafeListCreator.MakeThreadSafe(namedArguments ?? new List <CANamedArgument>());
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="callingConvention">Calling convention (must have LocalSig set)</param>
 /// <param name="count">Number of locals</param>
 internal LocalSig(CallingConvention callingConvention, uint count)
 {
     this.callingConvention = callingConvention;
     this.locals            = ThreadSafeListCreator.Create <TypeSig>((int)count);
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Calls <see cref="ThreadSafe.IList{T}.IsReadOnly_NoLock"/>
 /// </summary>
 /// <typeparam name="T">Type to store in list</typeparam>
 /// <param name="tsList">A thread-safe list</param>
 public static bool IsReadOnly_NoLock <T>(this ThreadSafe.IList <T> tsList)
 {
     return(tsList.IsReadOnly_NoLock);
 }
Ejemplo n.º 26
0
 /// <summary>
 /// Default constructor
 /// </summary>
 public VTable()
 {
     this.methods = ThreadSafeListCreator.Create <IMethod>();
 }
Ejemplo n.º 27
0
 /// <summary>
 /// Iterates over all elements in <paramref name="tsList"/> in the reverse order and calls
 /// <paramref name="handler"/>
 /// </summary>
 /// <typeparam name="T">Type to store in list</typeparam>
 /// <param name="tsList">A thread-safe list</param>
 /// <param name="handler">Called for each element</param>
 public static void IterateReverse <T>(this ThreadSafe.IList <T> tsList, IterateDelegate <T> handler)
 {
     tsList.Iterate(0, -1, true, handler);
 }
Ejemplo n.º 28
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="arg1">Generic arg #1</param>
 /// <param name="arg2">Generic arg #2</param>
 /// <param name="arg3">Generic arg #3</param>
 public GenericInstMethodSig(TypeSig arg1, TypeSig arg2, TypeSig arg3)
 {
     this.callingConvention = CallingConvention.GenericInst;
     this.genericArgs       = ThreadSafeListCreator.Create <TypeSig>(arg1, arg2, arg3);
 }