diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/XMREngine/XMRHeapTracker.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/XMREngine/XMRHeapTracker.cs | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/XMREngine/XMRHeapTracker.cs b/OpenSim/Region/ScriptEngine/XMREngine/XMRHeapTracker.cs new file mode 100644 index 0000000..c906f21 --- /dev/null +++ b/OpenSim/Region/ScriptEngine/XMREngine/XMRHeapTracker.cs | |||
@@ -0,0 +1,172 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using OpenSim.Region.ScriptEngine.Shared.ScriptBase; | ||
29 | using OpenSim.Region.ScriptEngine.XMREngine; | ||
30 | using System; | ||
31 | using System.Collections.Generic; | ||
32 | using System.IO; | ||
33 | using System.Reflection; | ||
34 | using System.Reflection.Emit; | ||
35 | |||
36 | using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat; | ||
37 | using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger; | ||
38 | using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString; | ||
39 | using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list; | ||
40 | using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion; | ||
41 | using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString; | ||
42 | using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3; | ||
43 | |||
44 | namespace OpenSim.Region.ScriptEngine.XMREngine | ||
45 | { | ||
46 | public class HeapTrackerBase { | ||
47 | private int usage; | ||
48 | private XMRInstAbstract instance; | ||
49 | |||
50 | public HeapTrackerBase (XMRInstAbstract inst) | ||
51 | { | ||
52 | if (inst == null) throw new ArgumentNullException ("inst"); | ||
53 | instance = inst; | ||
54 | } | ||
55 | |||
56 | ~HeapTrackerBase () | ||
57 | { | ||
58 | usage = instance.UpdateHeapUse (usage, 0); | ||
59 | } | ||
60 | |||
61 | protected void NewUse (int newuse) | ||
62 | { | ||
63 | usage = instance.UpdateHeapUse (usage, newuse); | ||
64 | } | ||
65 | } | ||
66 | |||
67 | public class HeapTrackerList : HeapTrackerBase { | ||
68 | private LSL_List value; | ||
69 | |||
70 | public HeapTrackerList (XMRInstAbstract inst) : base (inst) { } | ||
71 | |||
72 | public void Pop (LSL_List lis) | ||
73 | { | ||
74 | NewUse (Size (lis)); | ||
75 | value = lis; | ||
76 | } | ||
77 | |||
78 | public LSL_List Push () | ||
79 | { | ||
80 | return value; | ||
81 | } | ||
82 | |||
83 | public static int Size (LSL_List lis) | ||
84 | { | ||
85 | return (!typeof (LSL_List).IsValueType && (lis == null)) ? 0 : lis.Size; | ||
86 | } | ||
87 | } | ||
88 | |||
89 | public class HeapTrackerObject : HeapTrackerBase { | ||
90 | public const int HT_CHAR = 2; | ||
91 | public const int HT_DELE = 8; | ||
92 | public const int HT_DOUB = 8; | ||
93 | public const int HT_SING = 4; | ||
94 | public const int HT_SFLT = 4; | ||
95 | public const int HT_INT = 4; | ||
96 | public const int HT_VEC = HT_DOUB * 3; | ||
97 | public const int HT_ROT = HT_DOUB * 4; | ||
98 | |||
99 | private object value; | ||
100 | |||
101 | public HeapTrackerObject (XMRInstAbstract inst) : base (inst) { } | ||
102 | |||
103 | public void Pop (object obj) | ||
104 | { | ||
105 | NewUse (Size (obj)); | ||
106 | value = obj; | ||
107 | } | ||
108 | |||
109 | public object Push () | ||
110 | { | ||
111 | return value; | ||
112 | } | ||
113 | |||
114 | public static int Size (object obj) | ||
115 | { | ||
116 | if (obj == null) return 0; | ||
117 | |||
118 | if (obj is char) return HT_CHAR; | ||
119 | if (obj is Delegate) return HT_DELE; | ||
120 | if (obj is double) return HT_DOUB; | ||
121 | if (obj is float) return HT_SING; | ||
122 | if (obj is int) return HT_INT; | ||
123 | if (obj is LSL_Float) return HT_SFLT; | ||
124 | if (obj is LSL_Integer) return HT_INT; | ||
125 | if (obj is LSL_List) return ((LSL_List)obj).Size; | ||
126 | if (obj is LSL_Rotation) return HT_ROT; | ||
127 | if (obj is LSL_String) return ((LSL_String)obj).m_string.Length * HT_CHAR; | ||
128 | if (obj is LSL_Vector) return HT_VEC; | ||
129 | if (obj is string) return ((string)obj).Length * HT_CHAR; | ||
130 | if (obj is XMR_Array) return 0; | ||
131 | if (obj is XMRArrayListKey) return ((XMRArrayListKey)obj).Size; | ||
132 | if (obj is XMRSDTypeClObj) return 0; | ||
133 | |||
134 | if (obj is Array) { | ||
135 | Array ar = (Array)obj; | ||
136 | int len = ar.Length; | ||
137 | if (len == 0) return 0; | ||
138 | Type et = ar.GetType ().GetElementType (); | ||
139 | if (et.IsValueType) return Size (ar.GetValue (0)) * len; | ||
140 | int size = 0; | ||
141 | for (int i = 0; i < len; i ++) { | ||
142 | size += Size (ar.GetValue (i)); | ||
143 | } | ||
144 | return size; | ||
145 | } | ||
146 | |||
147 | throw new Exception ("unknown size of type " + obj.GetType ().Name); | ||
148 | } | ||
149 | } | ||
150 | |||
151 | public class HeapTrackerString : HeapTrackerBase { | ||
152 | private string value; | ||
153 | |||
154 | public HeapTrackerString (XMRInstAbstract inst) : base (inst) { } | ||
155 | |||
156 | public void Pop (string str) | ||
157 | { | ||
158 | NewUse (Size (str)); | ||
159 | value = str; | ||
160 | } | ||
161 | |||
162 | public string Push () | ||
163 | { | ||
164 | return value; | ||
165 | } | ||
166 | |||
167 | public static int Size (string str) | ||
168 | { | ||
169 | return (str == null) ? 0 : str.Length * HeapTrackerObject.HT_CHAR; | ||
170 | } | ||
171 | } | ||
172 | } | ||