예제 #1
0
 public bool TryGetReadScope(TimeSpan timeout, out IDisposable scope)
 {
     scope = default(IDisposable);
     if (_slim.TryEnterReadLock(timeout))
     {
         scope = new ReadScope(_slim);
         return(true);
     }
     return(false);
 }
        protected override void OnProcessOutputSchema(MutableObject newSchema)
        {
            foreach (var entry in TraceScope.GetEntries(newSchema))
            {
                foreach (var subentry in AllocateScope.GetEntries(entry))
                {
                    AllocatePositionTarget.SetValue(4f, subentry);
                    AllocateSizeTarget.SetValue(.4f, subentry);
                    AllocateColorTarget.SetValue(Color.magenta, subentry);
                    AllocateEndIndexTarget.SetValue((int)12, subentry);
                }

                foreach (var subentry in ReadScope.GetEntries(entry))
                {
                    ReadPositionTarget.SetValue(4f, subentry);
                    ReadSizeTarget.SetValue(.4f, subentry);
                    ReadColorTarget.SetValue(Color.magenta, subentry);
                }

                foreach (var subentry in WriteScope.GetEntries(entry))
                {
                    WritePositionTarget.SetValue(4f, subentry);
                    WriteSizeTarget.SetValue(.4f, subentry);
                    WriteColorTarget.SetValue(Color.magenta, subentry);
                }
            }

            GridLinesTarget.SetValue(new List <MutableObject>
            {
                new MutableObject {
                    { "Visual Position", .5f }, { "Visual Weight", .2f }
                },
                new MutableObject {
                    { "Visual Position", 1f }, { "Visual Weight", .1f }
                }
            }, newSchema);

            Router.TransmitAllSchema(newSchema);
        }
        public override IEnumerator ReceivePayload(VisualPayload payload)
        {
            //var outOfBoundsColor = OutOfBoundsColor.GetFirstValue( payload.Data );

            var outOfBoundsReadColor  = OutOfBoundsReadColor.GetFirstValue(payload.Data);
            var outOfBoundsWriteColor = OutOfBoundsWriteColor.GetFirstValue(payload.Data);

            var allocatedAlpha = AllocationAlpha.GetFirstValue(payload.Data);

            var valueStepSize   = ValueStepSize.GetFirstValue(payload.Data);
            var allocationColor = AllocationColor.GetFirstValue(payload.Data);

            float allocH, allocS, allocV;

            ColorUtility.RGBToHSV(allocationColor, out allocH, out allocS, out allocV);


            //var readWriteDifferentiation = ReadWriteDifferentiation.GetFirstValue( payload.Data );

            foreach (var entry in TraceScope.GetEntries(payload.Data))
            {
                var memSpace = new MemorySpace();

                //var saturation = Saturation.GetValue( entry );
                //var hueStep = HueStepSize.GetValue( entry );
                //var value = Value.GetValue( entry );

                int maxIndex = MaxIndex.GetValue(entry);

                // match allocations and frees

                var allocations = new Dictionary <uint, MemorySpacePoint>();
                foreach (var allocation in AllocateScope.GetEntries(entry))
                {
                    var address = AllocateAddress.GetValue(allocation);
                    var size    = AllocateSize.GetValue(allocation);
                    var index   = AllocateIndex.GetValue(allocation);

                    var newPoint = memSpace.AddMemoryAllocation(address, size);

                    newPoint.StartIndex          = index;
                    newPoint.EndPoint.StartIndex = index;
                    newPoint.EndIndex            = maxIndex;
                    newPoint.EndPoint.EndIndex   = maxIndex;

                    allocations.Add(newPoint.Address, newPoint);
                }

                // assign the end indices of any allocates with matched frees
                foreach (var free in FreeScope.GetEntries(entry))
                {
                    var index   = FreeIndex.GetValue(free);
                    var address = FreeAddress.GetValue(free);

                    if (allocations.ContainsKey(address))
                    {
                        var matchedAllocation = allocations[address];

                        matchedAllocation.EndIndex          = index;
                        matchedAllocation.EndPoint.EndIndex = index;
                    }
                }


                // import reads to the memory space
                var readPoints = new Dictionary <int, MemorySpacePoint>();
                foreach (var read in ReadScope.GetEntries(entry))
                {
                    var address = ReadAddress.GetValue(read);
                    var size    = ReadSize.GetValue(read);
                    var index   = ReadIndex.GetValue(read);

                    var readPoint = MemorySpacePoint.GeneratePoint(address, size);
                    readPoint.AllIndices = index;


                    MemorySpacePoint containingPoint;

                    if (memSpace.IsAccessContained(readPoint, readPoint.EndPoint, out containingPoint))
                    {
                        readPoint.ContainingAllocation = containingPoint;
                    }
                    else
                    {
                        var outOfBoundsAlloc = memSpace.AddMemoryAllocation(address, size, 0, maxIndex);
                        outOfBoundsAlloc.IsOutOfBounds = true;

                        readPoint.ContainingAllocation = outOfBoundsAlloc;
                    }

                    readPoints.Add(index, readPoint);
                }

                // import Writes to the memory space
                var writePoints = new Dictionary <int, MemorySpacePoint>();
                foreach (var write in WriteScope.GetEntries(entry))
                {
                    var address = WriteAddress.GetValue(write);
                    var size    = WriteSize.GetValue(write);
                    var index   = WriteIndex.GetValue(write);

                    var writePoint = MemorySpacePoint.GeneratePoint(address, size);
                    writePoint.AllIndices = index;

                    MemorySpacePoint containingPoint;

                    if (memSpace.IsAccessContained(writePoint, writePoint.EndPoint, out containingPoint))
                    {
                        writePoint.ContainingAllocation = containingPoint;
                    }
                    else
                    {
                        var outOfBoundsAlloc = memSpace.AddMemoryAllocation(address, size, 0, maxIndex);
                        outOfBoundsAlloc.IsOutOfBounds = true;

                        writePoint.ContainingAllocation = outOfBoundsAlloc;
                    }

                    writePoints.Add(index, writePoint);
                }


                // generate visual space for memory allocations (include out of bounds allocations)
                memSpace.SetVisualBounding();


                // set colors for memory allocations (inclde out of bounds allocations with special color)
                //var hueRotor = HueStart.GetValue( entry );

                foreach (var allocation in memSpace.RegisteredSpace.Where(s => s.IsStart))
                {
                    Color nextColor = ColorUtility.HsvtoRgb(allocH, allocS, allocV);

                    allocV = .3f + (allocV + valueStepSize) % .7f;

                    /*(allocation.IsOutOfBounds)?
                     * outOfBoundsColor:
                     * ColorUtility.HsvtoRgb(hueRotor, saturation,
                     * value);*/

                    nextColor.a = allocatedAlpha;

                    //if ( !allocation.IsOutOfBounds )
                    //    hueRotor += hueStep;

                    allocation.VisualColor = nextColor;
                }

                // set colors and visual locations for all reads
                foreach (var read in readPoints)
                {
                    if (read.Value.ContainingAllocation == null)
                    {
                        Debug.LogError("Danger will robinson!  Danger!");
                    }
                    read.Value.VisualColor =
                        outOfBoundsReadColor;
                    //           read.Value.ContainingAllocation.VisualColor == outOfBoundsColor?
                    //           outOfBoundsReadColor:
                    //           Color.Lerp(
                    //           read.Value.ContainingAllocation.VisualColor,
                    //       Color.white, readWriteDifferentiation);

                    //read.Value.VisualStart = memSpace.AddressToVisualPosition( read.Value.Address );
                    //read.Value.VisualEnd = memSpace.AddressToVisualPosition( read.Value.EndPoint.Address );
                    read.Value.ComputeVisualPositionsFromContainer();
                }


                // set colors and visual locations for all writes
                foreach (var write in writePoints)
                {
                    if (write.Value.ContainingAllocation == null)
                    {
                        Debug.LogError("Danger will robinson!  Danger!");
                    }
                    write.Value.VisualColor =
                        outOfBoundsWriteColor;
                    //       write.Value.ContainingAllocation.VisualColor == outOfBoundsColor?
                    //       outOfBoundsWriteColor:
                    //       Color.Lerp(
                    //       write.Value.ContainingAllocation.VisualColor,
                    //   Color.black, readWriteDifferentiation);

                    write.Value.ComputeVisualPositionsFromContainer();

                    //write.Value.VisualStart = memSpace.AddressToVisualPosition(write.Value.Address);
                    //write.Value.VisualEnd = memSpace.AddressToVisualPosition(write.Value.EndPoint.Address);
                }

                // FINALLY, write this information into the corresponding targets
                foreach (var allocation in AllocateScope.GetEntries(entry))
                {
                    var targetAddress = AllocateAddress.GetValue(allocation);
                    //var targetIndex = AllocateIndex.GetValue( allocation );

                    if (!allocations.ContainsKey(targetAddress))
                    {
                        throw new Exception("Allocation address not found!");
                    }
                    var foundAllocation = allocations[targetAddress];

                    AllocateColorTarget.SetValue(foundAllocation.VisualColor, allocation);
                    AllocatePositionTarget.SetValue(foundAllocation.VisualStart, allocation);
                    AllocateSizeTarget.SetValue(foundAllocation.VisualEnd - foundAllocation.VisualStart, allocation);
                    AllocateEndIndexTarget.SetValue(foundAllocation.EndIndex, allocation);
                }

                foreach (var read in ReadScope.GetEntries(entry))
                {
                    var targetIndex = ReadIndex.GetValue(read);

                    if (!readPoints.ContainsKey(targetIndex))
                    {
                        throw new Exception("Read index not found!");
                    }
                    var foundRead = readPoints[targetIndex];

                    ReadColorTarget.SetValue(foundRead.VisualColor, read);
                    ReadPositionTarget.SetValue(foundRead.VisualStart, read);
                    ReadSizeTarget.SetValue(foundRead.VisualEnd - foundRead.VisualStart, read);
                }


                foreach (var write in WriteScope.GetEntries(entry))
                {
                    var targetIndex = WriteIndex.GetValue(write);

                    if (!writePoints.ContainsKey(targetIndex))
                    {
                        throw new Exception("Write index not found!");
                        continue;
                    }
                    var foundwrite = writePoints[targetIndex];

                    WriteColorTarget.SetValue(foundwrite.VisualColor, write);
                    WritePositionTarget.SetValue(foundwrite.VisualStart, write);
                    WriteSizeTarget.SetValue(foundwrite.VisualEnd - foundwrite.VisualStart, write);
                }


                // write grid lines
                var  gridLines    = new List <MutableObject>();
                uint priorAddress = memSpace.RegisteredSpace.First().Address;


                gridLines.Add(new MutableObject
                {
                    { "Visual Position", 0f },
                    { "Visual Weight", 1000f }
                });

                foreach (var space in memSpace.RegisteredSpace)
                {
                    if (space.IsEnd)
                    {
                        continue;
                    }
                    var newGridLine = new MutableObject
                    {
                        { "Visual Position", space.VisualStart },
                        { "Visual Weight", (float)(Mathf.Max(0, priorAddress - space.StartPoint.Address) + 1) }
                    };
                    priorAddress = space.EndPoint.Address;
                    gridLines.Add(newGridLine);
                }

                gridLines.Add(new MutableObject
                {
                    { "Visual Position", 1f },
                    { "Visual Weight", 1000f }
                });

                GridLinesTarget.SetValue(gridLines, entry);
            }


            var iterator = Router.TransmitAll(payload);

            while (iterator.MoveNext())
            {
                yield return(null);
            }
        }
예제 #4
0
        // TODO: add comments (ez)
        // <! this is a comment !>

        /// <summary>
        /// </summary>
        private static Node FromCGML_0_4_0(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(null);
            }

            Node root = null;

            ReadElement elementType = ReadElement.None;
            bool        reading     = false;
            ReadScope   scope       = ReadScope.Default;

            Node          thisNode = null;
            StringBuilder element  = new StringBuilder();
            char          lastChar = (char)0;

            Func <string> commitElement = new Func <string>(() =>
            {
                string s = element.ToString();
                element.Clear();
                return(s);
            });

            Action commitNodeKey = new Action(() =>
            {
                Node newNode = new Node(commitElement());

                if (root == null)
                {
                    root = newNode;
                }
                else
                {
                    thisNode.Push(newNode);
                }

                thisNode = newNode;
            });

            Action commitAttributeKey = new Action(() =>
            {
                thisNode.Attributes.Set(new Attribute(commitElement()));
                elementType = ReadElement.None;
            });

            Action commitValue = new Action(() =>
            {
                switch (scope)
                {
                case ReadScope.Node:
                    {
                        thisNode.Value = commitElement();

                        break;
                    }

                case ReadScope.Attribute:
                    {
                        thisNode.Attributes[thisNode.Attributes.Count - 1].Value = commitElement();

                        break;
                    }
                }
                elementType = ReadElement.None;
            });

            for (int i = 0; i < str.Length; i++)
            {
                if (i < str.Length)
                {
                    char c        = str[i];
                    char nextChar = i < str.Length - 1 ? str[i + 1] : (char)0;

                    if (reading)
                    {
                        switch (c)
                        {
                        case CGML.STRING_BEGIN_END:
                        {
                            if (lastChar == '\\')
                            {
                                break;
                            }

                            commitValue();
                            reading = false;

                            break;
                        }

                        default:
                        {
                            element.Append(c);
                            break;
                        }
                        }
                    }
                    else
                    {
                        switch (c)
                        {
                        // Ignore newlines
                        case '\n':
                            break;

                        // Ignore carriage return
                        case '\r':
                            break;

                        // Ignore tabs
                        case '\t':
                            break;

                        case CGML.NODE_BEGIN:
                        {
                            scope       = ReadScope.Node;
                            elementType = ReadElement.Key;

                            break;
                        }

                        case CGML.NODE_END:
                        {
                            scope       = ReadScope.Default;
                            elementType = ReadElement.None;

                            break;
                        }

                        case CGML.STRING_BEGIN_END:
                        {
                            elementType = ReadElement.Value;
                            reading     = true;

                            break;
                        }

                        case CGML.EQUAL_OPERATOR:
                        {
                            if (elementType == ReadElement.Key && scope == ReadScope.Attribute)
                            {
                                commitAttributeKey();
                                break;
                            }

                            break;
                        }

                        case ' ':
                        {
                            scope       = ReadScope.Attribute;
                            elementType = ReadElement.Key;

                            break;
                        }

                        case CGML.NODE_ENDMARK:
                        {
                            thisNode = thisNode.Parent;

                            break;
                        }

                        default:
                        {
                            if (elementType != ReadElement.None)
                            {
                                element.Append(c);
                            }

                            break;
                        }
                        }
                    }

                    if (scope == ReadScope.Node && elementType == ReadElement.Key)
                    {
                        if (nextChar == CGML.NODE_END || nextChar == CGML.EQUAL_OPERATOR || nextChar == ' ')
                        {
                            commitNodeKey();
                        }
                    }

                    lastChar = c;
                }
            }

            return(root);
        }