NetObjectCache.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using ProtoBuf.Meta;
  4. namespace ProtoBuf
  5. {
  6. internal sealed class NetObjectCache
  7. {
  8. internal const int Root = 0;
  9. private MutableList underlyingList;
  10. private MutableList List => underlyingList ?? (underlyingList = new MutableList());
  11. internal object GetKeyedObject(int key)
  12. {
  13. if (key-- == Root)
  14. {
  15. if (rootObject == null) throw new ProtoException("No root object assigned");
  16. return rootObject;
  17. }
  18. BasicList list = List;
  19. if (key < 0 || key >= list.Count)
  20. {
  21. Helpers.DebugWriteLine("Missing key: " + key);
  22. throw new ProtoException("Internal error; a missing key occurred");
  23. }
  24. object tmp = list[key];
  25. if (tmp == null)
  26. {
  27. throw new ProtoException("A deferred key does not have a value yet");
  28. }
  29. return tmp;
  30. }
  31. internal void SetKeyedObject(int key, object value)
  32. {
  33. if (key-- == Root)
  34. {
  35. if (value == null) throw new ArgumentNullException(nameof(value));
  36. if (rootObject != null && ((object)rootObject != (object)value)) throw new ProtoException("The root object cannot be reassigned");
  37. rootObject = value;
  38. }
  39. else
  40. {
  41. MutableList list = List;
  42. if (key < list.Count)
  43. {
  44. object oldVal = list[key];
  45. if (oldVal == null)
  46. {
  47. list[key] = value;
  48. }
  49. else if (!ReferenceEquals(oldVal, value))
  50. {
  51. throw new ProtoException("Reference-tracked objects cannot change reference");
  52. } // otherwise was the same; nothing to do
  53. }
  54. else if (key != list.Add(value))
  55. {
  56. throw new ProtoException("Internal error; a key mismatch occurred");
  57. }
  58. }
  59. }
  60. private object rootObject;
  61. internal int AddObjectKey(object value, out bool existing)
  62. {
  63. if (value == null) throw new ArgumentNullException(nameof(value));
  64. if ((object)value == (object)rootObject) // (object) here is no-op, but should be
  65. { // preserved even if this was typed - needs ref-check
  66. existing = true;
  67. return Root;
  68. }
  69. string s = value as string;
  70. BasicList list = List;
  71. int index;
  72. if (s == null)
  73. {
  74. #if CF || PORTABLE // CF has very limited proper object ref-tracking; so instead, we'll search it the hard way
  75. index = list.IndexOfReference(value);
  76. #else
  77. if (objectKeys == null)
  78. {
  79. objectKeys = new Dictionary<object, int>(ReferenceComparer.Default);
  80. index = -1;
  81. }
  82. else
  83. {
  84. if (!objectKeys.TryGetValue(value, out index)) index = -1;
  85. }
  86. #endif
  87. }
  88. else
  89. {
  90. if (stringKeys == null)
  91. {
  92. stringKeys = new Dictionary<string, int>();
  93. index = -1;
  94. }
  95. else
  96. {
  97. if (!stringKeys.TryGetValue(s, out index)) index = -1;
  98. }
  99. }
  100. if (!(existing = index >= 0))
  101. {
  102. index = list.Add(value);
  103. if (s == null)
  104. {
  105. #if !CF && !PORTABLE // CF can't handle the object keys very well
  106. objectKeys.Add(value, index);
  107. #endif
  108. }
  109. else
  110. {
  111. stringKeys.Add(s, index);
  112. }
  113. }
  114. return index + 1;
  115. }
  116. private int trapStartIndex; // defaults to 0 - optimization for RegisterTrappedObject
  117. // to make it faster at seeking to find deferred-objects
  118. internal void RegisterTrappedObject(object value)
  119. {
  120. if (rootObject == null)
  121. {
  122. rootObject = value;
  123. }
  124. else
  125. {
  126. if (underlyingList != null)
  127. {
  128. for (int i = trapStartIndex; i < underlyingList.Count; i++)
  129. {
  130. trapStartIndex = i + 1; // things never *become* null; whether or
  131. // not the next item is null, it will never
  132. // need to be checked again
  133. if (underlyingList[i] == null)
  134. {
  135. underlyingList[i] = value;
  136. break;
  137. }
  138. }
  139. }
  140. }
  141. }
  142. private Dictionary<string, int> stringKeys;
  143. #if !CF && !PORTABLE // CF lacks the ability to get a robust reference-based hash-code, so we'll do it the harder way instead
  144. private System.Collections.Generic.Dictionary<object, int> objectKeys;
  145. private sealed class ReferenceComparer : IEqualityComparer<object>
  146. {
  147. public readonly static ReferenceComparer Default = new ReferenceComparer();
  148. private ReferenceComparer() { }
  149. bool IEqualityComparer<object>.Equals(object x, object y)
  150. {
  151. return x == y; // ref equality
  152. }
  153. int IEqualityComparer<object>.GetHashCode(object obj)
  154. {
  155. return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(obj);
  156. }
  157. }
  158. #endif
  159. internal void Clear()
  160. {
  161. trapStartIndex = 0;
  162. rootObject = null;
  163. if (underlyingList != null) underlyingList.Clear();
  164. if (stringKeys != null) stringKeys.Clear();
  165. #if !CF && !PORTABLE
  166. if (objectKeys != null) objectKeys.Clear();
  167. #endif
  168. }
  169. }
  170. }