Beispiel #1
0
            public void PrepareDeserializationTarget(BufferReader reader, ref SharedContainer <T> target, SerializationContext context)
            {
                SharedPool <T> sharedPool = null;
                T resource = default(T);

                if (target != null)
                {
                    target.Release();
                    sharedPool = target.SharedPool;
                    sharedPool?.TryGet(out resource);
                }

                target = new SharedContainer <T>(resource, sharedPool);
            }
Beispiel #2
0
 /// <summary>
 /// Creates a <see cref="Shared{T}"/> instance wrapping the specified resource.
 /// The returned instance must be disposed.
 /// </summary>
 /// <remarks>
 /// The reference count of the resource is incremented before this method returns.
 /// When the returned <see cref="Shared{T}"/> is disposed, resource's reference count is decremented.
 /// When the reference count reaches 0, the wrapped resource is returned to the provided <see cref="SharedPool{T}"/>.
 /// </remarks>
 /// <typeparam name="T">The type of resource being wrapped</typeparam>
 /// <param name="resource">The resource to wrap. Usually a large memory allocation.</param>
 /// <param name="recycler">The resource pool that will take over the resource once released</param>
 /// <returns>A <see cref="Shared{T}"/> instance wrapping the specified resource</returns>
 public static Shared <T> Create <T>(T resource, SharedPool <T> recycler = null)
     where T : class
 {
     return(new Shared <T>(resource, recycler));
 }
Beispiel #3
0
 internal SharedContainer(T resource, SharedPool <T> pool)
 {
     this.sharedPool = pool;
     this.resource   = resource;
     this.refCount   = 1;
 }
Beispiel #4
0
 internal SharedContainer(T resource, SharedPool <T> recycler)
 {
     this.recycler = recycler;
     this.resource = resource;
     this.refCount = 1;
 }