PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/base/Applications/Tests/Collect/Collect.cs

#
C# | 139 lines | 110 code | 21 blank | 8 comment | 10 complexity | f8d238edd01ef0d8e5b1592878156113 MD5 | raw file
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Research Singularity
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // Note: Simple Singularity test program.
  8. //
  9. using System;
  10. using Microsoft.Singularity.V1.Services;
  11. using Microsoft.Contracts;
  12. using Microsoft.Singularity.Channels;
  13. using Microsoft.SingSharp.Reflection;
  14. using Microsoft.Singularity.Applications;
  15. using Microsoft.Singularity.Io;
  16. using Microsoft.Singularity.Configuration;
  17. [assembly: Transform(typeof(ApplicationResourceTransform))]
  18. namespace Microsoft.Singularity.Applications
  19. {
  20. [ConsoleCategory(DefaultAction=true)]
  21. internal class Parameters {
  22. [InputEndpoint("data")]
  23. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  24. [OutputEndpoint("data")]
  25. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  26. [BoolParameter( "set", Default=false, HelpMessage="Run set test.")]
  27. internal bool doSetTest;
  28. reflective internal Parameters();
  29. internal int AppMain() {
  30. return Collect.AppMain(this);
  31. }
  32. }
  33. public class Collect
  34. {
  35. class LinkedList
  36. {
  37. private LinkedList next;
  38. private LinkedList prev;
  39. public int value;
  40. private byte[]! data;
  41. private LinkedList(int i, [Delayed] LinkedList _prev)
  42. {
  43. prev = _prev;
  44. value = i;
  45. if (i <= 1) {
  46. next = null;
  47. }
  48. else {
  49. next = new LinkedList(i - 1, this);
  50. }
  51. data = new byte [24576 + 1024 * i];
  52. }
  53. ~LinkedList()
  54. {
  55. DebugStub.Print("Finalizer called for Value={0}.\n", __arglist(value));
  56. }
  57. public void Print()
  58. {
  59. Console.WriteLine("Print {0}", value);
  60. }
  61. public void PrintNext()
  62. {
  63. Console.WriteLine("Linked Data: [{0} items]", value);
  64. PrintNext(value);
  65. }
  66. private void PrintNext(int _value)
  67. requires next != null;
  68. {
  69. Console.WriteLine(" Value = {0} [Next] {1}, {2} bytes payload",
  70. value, next.value, data.Length);
  71. if (next.value != _value) {
  72. next.PrintNext(_value);
  73. }
  74. }
  75. public void PrintPrev()
  76. {
  77. Console.WriteLine("PrintPrev {0}", value);
  78. PrintPrev(value);
  79. }
  80. private void PrintPrev(int _value)
  81. requires prev != null;
  82. {
  83. Console.WriteLine(" Value = {0} [Prev] {1}", value, prev.value);
  84. if (prev.value != _value) {
  85. prev.PrintPrev(_value);
  86. }
  87. }
  88. private LinkedList! Last()
  89. {
  90. if (next != null) {
  91. return next.Last();
  92. }
  93. return this;
  94. }
  95. public static LinkedList! CreateCycle(int links)
  96. {
  97. LinkedList root = new LinkedList(links, null);
  98. LinkedList last = root.Last();
  99. root.prev = last;
  100. last.next = root;
  101. return root;
  102. }
  103. }
  104. internal static int AppMain(Parameters! config)
  105. {
  106. LinkedList root = LinkedList.CreateCycle(4);
  107. root.PrintNext();
  108. root = null;
  109. Console.WriteLine();
  110. Console.WriteLine("Collecting garbage [before heap: {0,8} bytes]",
  111. GC.GetTotalMemory(false));
  112. GC.Collect();
  113. Console.WriteLine("Collecting garbage [after heap: {0,8} bytes]",
  114. GC.GetTotalMemory(false));
  115. return 0;
  116. }
  117. }
  118. }