1+ #if NET40
2+ // Licensed to the .NET Foundation under one or more agreements.
3+ // The .NET Foundation licenses this file to you under the MIT license.
4+ // See the LICENSE file in the project root for more information.
5+
6+ using System . Runtime . CompilerServices ;
7+ using System . Threading ;
8+
9+ using JavaScriptEngineSwitcher . ChakraCore . Polyfills . System . Threading ;
10+
11+ namespace JavaScriptEngineSwitcher . ChakraCore . Polyfills . System . Buffers
12+ {
13+ /// <summary>
14+ /// Provides a resource pool that enables reusing instances of type <see cref="T:T[]"/>
15+ /// </summary>
16+ /// <remarks>
17+ /// <para>
18+ /// Renting and returning buffers with an <see cref="ArrayPool{T}"/> can increase performance
19+ /// in situations where arrays are created and destroyed frequently, resulting in significant
20+ /// memory pressure on the garbage collector.
21+ /// </para>
22+ /// <para>
23+ /// This class is thread-safe. All members may be used by multiple threads concurrently.
24+ /// </para>
25+ /// </remarks>
26+ internal abstract class ArrayPool < T >
27+ {
28+ /// <summary>
29+ /// The lazily-initialized shared pool instance
30+ /// </summary>
31+ private static ArrayPool < T > s_sharedInstance = null ;
32+
33+ /// <summary>
34+ /// Retrieves a shared <see cref="ArrayPool{T}"/> instance
35+ /// </summary>
36+ /// <remarks>
37+ /// The shared pool provides a default implementation of <see cref="ArrayPool{T}"/>
38+ /// that's intended for general applicability. It maintains arrays of multiple sizes, and
39+ /// may hand back a larger array than was actually requested, but will never hand back a smaller
40+ /// array than was requested. Renting a buffer from it with <see cref="Rent"/> will result in an
41+ /// existing buffer being taken from the pool if an appropriate buffer is available or in a new
42+ /// buffer being allocated if one is not available.
43+ /// </remarks>
44+ public static ArrayPool < T > Shared
45+ {
46+ [ MethodImpl ( ( MethodImplOptions ) 256 /* AggressiveInlining */ ) ]
47+ get
48+ {
49+ return Volatile . Read ( ref s_sharedInstance ) ?? EnsureSharedCreated ( ) ;
50+ }
51+ }
52+
53+
54+ /// <summary>
55+ /// Ensures that <see cref="s_sharedInstance"/> has been initialized to a pool and returns it
56+ /// </summary>
57+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
58+ private static ArrayPool < T > EnsureSharedCreated ( )
59+ {
60+ Interlocked . CompareExchange ( ref s_sharedInstance , new DefaultArrayPool < T > ( ) , null ) ;
61+ return s_sharedInstance ;
62+ }
63+
64+ /// <summary>
65+ /// Retrieves a buffer that is at least the requested length
66+ /// </summary>
67+ /// <remarks>
68+ /// This buffer is loaned to the caller and should be returned to the same pool via
69+ /// <see cref="Return"/> so that it may be reused in subsequent usage of <see cref="Rent"/>.
70+ /// It is not a fatal error to not return a rented buffer, but failure to do so may lead to
71+ /// decreased application performance, as the pool may need to create a new buffer to replace
72+ /// the one lost.
73+ /// </remarks>
74+ /// <param name="minimumLength">The minimum length of the array needed</param>
75+ /// <returns>An <see cref="T:T[]"/> that is at least <paramref name="minimumLength"/> in length</returns>
76+ public abstract T [ ] Rent ( int minimumLength ) ;
77+
78+ /// <summary>
79+ /// Returns to the pool an array that was previously obtained via <see cref="Rent"/> on the same
80+ /// <see cref="ArrayPool{T}"/> instance
81+ /// </summary>
82+ /// <remarks>
83+ /// Once a buffer has been returned to the pool, the caller gives up all ownership of the buffer
84+ /// and must not use it. The reference returned from a given call to <see cref="Rent"/> must only be
85+ /// returned via <see cref="Return"/> once. The default <see cref="ArrayPool{T}"/>
86+ /// may hold onto the returned buffer in order to rent it again, or it may release the returned buffer
87+ /// if it's determined that the pool already has enough buffers stored.
88+ /// </remarks>
89+ /// <param name="array">The buffer previously obtained from <see cref="Rent"/> to return to the pool</param>
90+ /// <param name="clearArray">If <c>true</c> and if the pool will store the buffer to enable subsequent
91+ /// reuse, <see cref="Return"/> will clear <paramref name="array"/> of its contents so that a subsequent
92+ /// consumer via <see cref="Rent"/> will not see the previous consumer's content. If <c>false</c> or
93+ /// if the pool will release the buffer, the array's contents are left unchanged.</param>
94+ public abstract void Return ( T [ ] array , bool clearArray = false ) ;
95+ }
96+ }
97+ #endif
0 commit comments