PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_2_0/Src/IronPython.Modules/gc.cs

#
C# | 121 lines | 82 code | 25 blank | 14 comment | 2 complexity | 6539b671c152afab219370a0669f1a70 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System; using Microsoft;
  16. using Microsoft.Scripting;
  17. using Microsoft.Scripting.Runtime;
  18. using IronPython.Runtime;
  19. using IronPython.Runtime.Operations;
  20. using IronPython.Runtime.Types;
  21. using SpecialName = System.Runtime.CompilerServices.SpecialNameAttribute;
  22. [assembly: PythonModule("gc", typeof(IronPython.Modules.PythonGC))]
  23. namespace IronPython.Modules {
  24. public static class PythonGC {
  25. public static PythonType gc = DynamicHelpers.GetPythonTypeFromType(typeof(PythonGC));
  26. public const int DEBUG_STATS = 1;
  27. public const int DEBUG_COLLECTABLE = 2;
  28. public const int DEBUG_UNCOLLECTABLE = 4;
  29. public const int DEBUG_INSTANCES = 8;
  30. public const int DEBUG_OBJECTS = 16;
  31. public const int DEBUG_SAVEALL = 32;
  32. public const int DEBUG_LEAK = (DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE | DEBUG_INSTANCES | DEBUG_OBJECTS | DEBUG_SAVEALL);
  33. private static readonly object _threadholdKey = new object();
  34. [SpecialName]
  35. public static void PerformModuleReload(PythonContext/*!*/ context, IAttributesCollection/*!*/ dict) {
  36. context.SetModuleState(_threadholdKey, PythonTuple.MakeTuple(64 * 1024, 256 * 1024, 1024 * 1024));
  37. }
  38. public static void enable() {
  39. }
  40. public static void disable() {
  41. throw PythonOps.NotImplementedError("gc.disable isn't implemented");
  42. }
  43. public static object isenabled() {
  44. return RuntimeHelpers.True;
  45. }
  46. public static int collect(int generation) {
  47. if (generation > GC.MaxGeneration || generation < 0) throw PythonOps.ValueError("invalid generation {0}", generation);
  48. long start = GC.GetTotalMemory(false);
  49. #if !SILVERLIGHT // GC.Collect
  50. GC.Collect(generation);
  51. #else
  52. GC.Collect();
  53. #endif
  54. GC.WaitForPendingFinalizers();
  55. return (int)Math.Max(start - GC.GetTotalMemory(false), 0);
  56. }
  57. public static int collect() {
  58. long start = GC.GetTotalMemory(false);
  59. GC.Collect();
  60. GC.WaitForPendingFinalizers();
  61. return (int)Math.Max(start - GC.GetTotalMemory(false), 0);
  62. }
  63. public static void set_debug(object o) {
  64. throw PythonOps.NotImplementedError("gc.set_debug isn't implemented");
  65. }
  66. public static object get_debug() {
  67. return null;
  68. }
  69. public static object[] get_objects() {
  70. throw PythonOps.NotImplementedError("gc.get_objects isn't implemented");
  71. }
  72. public static void set_threshold(CodeContext/*!*/ context, params object[] args) {
  73. SetThresholds(context, PythonTuple.MakeTuple(args));
  74. }
  75. public static PythonTuple get_threshold(CodeContext/*!*/ context) {
  76. return GetThresholds(context);
  77. }
  78. public static object[] get_referrers(params object[] objs) {
  79. throw PythonOps.NotImplementedError("gc.get_referrers isn't implemented");
  80. }
  81. public static object[] get_referents(params object[] objs) {
  82. throw PythonOps.NotImplementedError("gc.get_referents isn't implemented");
  83. }
  84. public static List garbage {
  85. get {
  86. return new List();
  87. }
  88. }
  89. private static PythonTuple GetThresholds(CodeContext/*!*/ context) {
  90. return (PythonTuple)PythonContext.GetContext(context).GetModuleState(_threadholdKey);
  91. }
  92. private static void SetThresholds(CodeContext/*!*/ context, PythonTuple thresholds) {
  93. PythonContext.GetContext(context).SetModuleState(_threadholdKey, thresholds);
  94. }
  95. }
  96. }