|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
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 * |
|
8 * - Redistributions of source code must retain the above copyright notice, |
|
9 * this list of conditions and the following disclaimer. |
|
10 * - Redistributions in binary form must reproduce the above copyright notice, |
|
11 * this list of conditions and the following disclaimer in the documentation |
|
12 * and/or other materials provided with the distribution. |
|
13 * - Neither the name of Nokia Corporation nor the names of its contributors |
|
14 * may be used to endorse or promote products derived from this software |
|
15 * without specific prior written permission. |
|
16 * |
|
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
27 * POSSIBILITY OF SUCH DAMAGE. |
|
28 * |
|
29 * Initial Contributors: |
|
30 * Nokia Corporation - initial contribution. |
|
31 * |
|
32 * Contributors: |
|
33 * |
|
34 * Description: |
|
35 * |
|
36 */ |
|
37 |
|
38 using System; |
|
39 using System.Collections; |
|
40 using System.Collections.Generic; |
|
41 using System.Text; |
|
42 using SymbianUtils.RawItems; |
|
43 using SymbianUtils.Utilities; |
|
44 using HeapLib.Array; |
|
45 using HeapLib.Cells; |
|
46 |
|
47 namespace HeapLib.Relationships |
|
48 { |
|
49 public class RelationshipManager |
|
50 { |
|
51 #region Constructors & destructor |
|
52 internal RelationshipManager( HeapCell aCell ) |
|
53 { |
|
54 iCell = aCell; |
|
55 } |
|
56 #endregion |
|
57 |
|
58 #region API |
|
59 internal void AddReferencedBy( HeapCell aCell ) |
|
60 { |
|
61 iReferencedBy.Add( aCell ); |
|
62 } |
|
63 |
|
64 internal void AddEmbeddedReferenceTo( RawItem aRawItemInThisCell, HeapCell aToCell ) |
|
65 { |
|
66 System.Diagnostics.Debug.Assert( aRawItemInThisCell.Tag == null ); |
|
67 |
|
68 if ( ContainsEmbeddedReference( aToCell ) == false ) |
|
69 { |
|
70 // Make reference description |
|
71 RelationshipInfo referenceTo = new RelationshipInfo( iCell, aRawItemInThisCell, aToCell ); |
|
72 |
|
73 // Set up relationship between a raw item in this cell and |
|
74 // the specified other cell. |
|
75 aRawItemInThisCell.Tag = referenceTo; |
|
76 |
|
77 // This cell now contains a reference to the other cell. |
|
78 iEmbeddedReferencesTo.Add( aToCell.Address, referenceTo ); |
|
79 |
|
80 // aAnotherCell has been referenced by 'me' |
|
81 aToCell.RelationshipManager.ReferencedBy.Add( Parent ); |
|
82 } |
|
83 else |
|
84 { |
|
85 //System.Diagnostics.Debug.WriteLine( "PREVENTING CYCLIC LINK - Cell: 0x" + aCell.Address.ToString("x8") + " already contains a linkRef to: 0x" + cell.Address.ToString("x8") ); |
|
86 } |
|
87 } |
|
88 |
|
89 public HeapCell EmbeddedReference( uint aAddress ) |
|
90 { |
|
91 HeapCell ret = null; |
|
92 // |
|
93 if ( ContainsEmbeddedReference( aAddress ) ) |
|
94 { |
|
95 RelationshipInfo info = iEmbeddedReferencesTo[ aAddress ]; |
|
96 ret = info.ToCell; |
|
97 } |
|
98 // |
|
99 return ret; |
|
100 } |
|
101 |
|
102 public bool ContainsEmbeddedReference( uint aAddress ) |
|
103 { |
|
104 bool found = iEmbeddedReferencesTo.ContainsKey( aAddress ); |
|
105 return found; |
|
106 } |
|
107 |
|
108 public bool ContainsEmbeddedReference( HeapCell aCell ) |
|
109 { |
|
110 return ContainsEmbeddedReference( aCell.Address ); |
|
111 } |
|
112 #endregion |
|
113 |
|
114 #region Properties |
|
115 public HeapCell Parent |
|
116 { |
|
117 get { return iCell; } |
|
118 } |
|
119 |
|
120 // <summary> |
|
121 // The array of cells that this particular cell contains references |
|
122 // to within it's raw item array (i.e. it's payload section). |
|
123 // </summary> |
|
124 public RelationshipCollection EmbeddedReferencesTo |
|
125 { |
|
126 get { return new RelationshipCollection( iEmbeddedReferencesTo ); } |
|
127 } |
|
128 |
|
129 // <summary> |
|
130 // The array of cells that are referencing this cell, i.e. the |
|
131 // list of other cells that contain addresses that point to 'me' |
|
132 // </summary> |
|
133 public HeapCellArrayBase ReferencedBy |
|
134 { |
|
135 get { return iReferencedBy; } |
|
136 } |
|
137 |
|
138 public HeapCell ReferencedByUnique |
|
139 { |
|
140 get |
|
141 { |
|
142 if (iReferencedBy.Count == 1) |
|
143 return iReferencedBy[0]; |
|
144 |
|
145 |
|
146 if (iReferencedBy.Count > 1) |
|
147 { |
|
148 HeapCell firstHeapCell = iReferencedBy[0]; |
|
149 |
|
150 for (int i = 1; i < iReferencedBy.Count; i++) |
|
151 { |
|
152 if (!firstHeapCell.Equals(iReferencedBy[i])) |
|
153 { |
|
154 return null; |
|
155 } |
|
156 } |
|
157 return iReferencedBy[0]; |
|
158 } |
|
159 |
|
160 return null; |
|
161 } |
|
162 } |
|
163 |
|
164 public uint PayloadLengthOfEmbeddedCells |
|
165 { |
|
166 get |
|
167 { |
|
168 uint ret = 0; |
|
169 // |
|
170 foreach ( RelationshipInfo info in EmbeddedReferencesTo ) |
|
171 { |
|
172 ret += info.ToCell.PayloadLength; |
|
173 } |
|
174 // |
|
175 return ret; |
|
176 } |
|
177 } |
|
178 #endregion |
|
179 |
|
180 #region Data members |
|
181 private readonly HeapCell iCell; |
|
182 private Dictionary<uint, RelationshipInfo> iEmbeddedReferencesTo = new Dictionary<uint, RelationshipInfo>(); |
|
183 private HeapCellArrayUnsorted iReferencedBy = new HeapCellArrayUnsorted(); |
|
184 #endregion |
|
185 } |
|
186 } |