@@ -28,11 +28,14 @@ public static class ObjectStore
2828 // Lookup handles by object.
2929 static Dictionary < object , int > objectHandleCache ;
3030
31- // Stack of available handles.
32- static Stack < int > handles ;
33-
3431 // Stored objects. The first is never used so 0 can be "null".
3532 static object [ ] objects ;
33+
34+ // Stack of available handles.
35+ static int [ ] handles ;
36+
37+ // Index of the next available handle
38+ static int nextHandleIndex ;
3639
3740 // The maximum number of objects to store. Must be positive.
3841 static int maxObjects ;
@@ -41,20 +44,21 @@ public static void Init(int maxObjects)
4144 {
4245 ObjectStore . maxObjects = maxObjects ;
4346 objectHandleCache = new Dictionary < object , int > ( maxObjects ) ;
44- handles = new Stack < int > ( maxObjects ) ;
4547
4648 // Initialize the objects as all null plus room for the
4749 // first to always be null.
4850 objects = new object [ maxObjects + 1 ] ;
4951
5052 // Initialize the handles stack as 1, 2, 3, ...
53+ handles = new int [ maxObjects ] ;
5154 for (
5255 int i = 0 , handle = maxObjects ;
5356 i < maxObjects ;
5457 ++ i , -- handle )
5558 {
56- handles . Push ( handle ) ;
59+ handles [ i ] = handle ;
5760 }
61+ nextHandleIndex = maxObjects - 1 ;
5862 }
5963
6064 public static int Store ( object obj )
@@ -68,7 +72,8 @@ public static int Store(object obj)
6872 lock ( objects )
6973 {
7074 // Pop a handle off the stack
71- int handle = handles . Pop ( ) ;
75+ int handle = handles [ nextHandleIndex ] ;
76+ nextHandleIndex -- ;
7277
7378 // Store the object
7479 objects [ handle ] = obj ;
@@ -121,7 +126,8 @@ public static object Remove(int handle)
121126 objects [ handle ] = null ;
122127
123128 // Push the handle onto the stack
124- handles . Push ( handle ) ;
129+ nextHandleIndex ++ ;
130+ handles [ nextHandleIndex ] = handle ;
125131
126132 // Remove the object from the cache
127133 objectHandleCache . Remove ( obj ) ;
0 commit comments