PageRenderTime 119ms CodeModel.GetById 109ms app.highlight 8ms RepoModel.GetById 1ms 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
  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
 16using System; using Microsoft;
 17
 18using Microsoft.Scripting;
 19using Microsoft.Scripting.Runtime;
 20
 21using IronPython.Runtime;
 22using IronPython.Runtime.Operations;
 23using IronPython.Runtime.Types;
 24
 25using SpecialName = System.Runtime.CompilerServices.SpecialNameAttribute;
 26
 27[assembly: PythonModule("gc", typeof(IronPython.Modules.PythonGC))]
 28namespace IronPython.Modules {
 29    public static class PythonGC {
 30        public static PythonType gc = DynamicHelpers.GetPythonTypeFromType(typeof(PythonGC));
 31        public const int DEBUG_STATS = 1;
 32        public const int DEBUG_COLLECTABLE = 2;
 33        public const int DEBUG_UNCOLLECTABLE = 4;
 34        public const int DEBUG_INSTANCES = 8;
 35        public const int DEBUG_OBJECTS = 16;
 36        public const int DEBUG_SAVEALL = 32;
 37        public const int DEBUG_LEAK = (DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE | DEBUG_INSTANCES | DEBUG_OBJECTS | DEBUG_SAVEALL);
 38
 39        private static readonly object _threadholdKey = new object();
 40
 41        [SpecialName]
 42        public static void PerformModuleReload(PythonContext/*!*/ context, IAttributesCollection/*!*/ dict) {            
 43            context.SetModuleState(_threadholdKey, PythonTuple.MakeTuple(64 * 1024, 256 * 1024, 1024 * 1024));
 44        }
 45
 46        public static void enable() {
 47        }
 48
 49        public static void disable() {
 50            throw PythonOps.NotImplementedError("gc.disable isn't implemented");
 51        }
 52
 53        public static object isenabled() {
 54            return RuntimeHelpers.True;
 55        }
 56
 57        public static int collect(int generation) {
 58            if (generation > GC.MaxGeneration || generation < 0) throw PythonOps.ValueError("invalid generation {0}", generation);
 59
 60            long start = GC.GetTotalMemory(false);
 61#if !SILVERLIGHT // GC.Collect
 62            GC.Collect(generation);
 63#else
 64            GC.Collect();
 65#endif
 66            GC.WaitForPendingFinalizers();
 67            
 68            return (int)Math.Max(start - GC.GetTotalMemory(false), 0);
 69        }
 70
 71        public static int collect() {
 72            long start = GC.GetTotalMemory(false);
 73            GC.Collect();
 74            GC.WaitForPendingFinalizers();
 75            return (int)Math.Max(start - GC.GetTotalMemory(false), 0);
 76        }
 77
 78        public static void set_debug(object o) {
 79            throw PythonOps.NotImplementedError("gc.set_debug isn't implemented");
 80        }
 81
 82        public static object get_debug() {
 83            return null;
 84        }
 85
 86        public static object[] get_objects() {
 87            throw PythonOps.NotImplementedError("gc.get_objects isn't implemented");
 88        }
 89
 90        public static void set_threshold(CodeContext/*!*/ context, params object[] args) {
 91            SetThresholds(context, PythonTuple.MakeTuple(args));
 92        }
 93
 94        public static PythonTuple get_threshold(CodeContext/*!*/ context) {
 95            return GetThresholds(context);
 96        }
 97
 98        public static object[] get_referrers(params object[] objs) {
 99            throw PythonOps.NotImplementedError("gc.get_referrers isn't implemented");
100        }
101
102        public static object[] get_referents(params object[] objs) {
103            throw PythonOps.NotImplementedError("gc.get_referents isn't implemented");
104        }
105
106
107        public static List garbage {
108            get {
109                return new List();
110            }
111        }
112
113        private static PythonTuple GetThresholds(CodeContext/*!*/ context) {
114            return (PythonTuple)PythonContext.GetContext(context).GetModuleState(_threadholdKey);
115        }
116
117        private static void SetThresholds(CodeContext/*!*/ context, PythonTuple thresholds) {
118            PythonContext.GetContext(context).SetModuleState(_threadholdKey, thresholds);
119        }
120    }
121}