3535/* FUNCTION RELEASE */
3636/* */
3737/* _tx_byte_pool_search PORTABLE C */
38- /* 6.1 */
38+ /* 6.1.7 */
3939/* AUTHOR */
4040/* */
4141/* William E. Lamie, Microsoft Corporation */
7676/* */
7777/* DATE NAME DESCRIPTION */
7878/* */
79- /* 05-19-2020 William E. Lamie Initial Version 6.0 */
80- /* 09-30-2020 Yuxin Zhou Modified comment(s), */
79+ /* 05-19-2020 William E. Lamie Initial Version 6.0 */
80+ /* 09-30-2020 Yuxin Zhou Modified comment(s), */
8181/* resulting in version 6.1 */
82+ /* 06-02-2021 Scott Larson Improve possible free bytes */
83+ /* calculation, */
84+ /* resulting in version 6.1.7 */
8285/* */
8386/**************************************************************************/
8487UCHAR * _tx_byte_pool_search (TX_BYTE_POOL * pool_ptr , ULONG memory_size )
@@ -96,13 +99,16 @@ UINT first_free_block_found = TX_FALSE;
9699TX_THREAD * thread_ptr ;
97100ALIGN_TYPE * free_ptr ;
98101UCHAR * work_ptr ;
102+ ULONG total_theoretical_available ;
99103
100104
101105 /* Disable interrupts. */
102106 TX_DISABLE
103107
104108 /* First, determine if there are enough bytes in the pool. */
105- if (memory_size >= pool_ptr -> tx_byte_pool_available )
109+ /* Theoretical bytes available = free bytes + ((fragments-2) * overhead of each block) */
110+ total_theoretical_available = pool_ptr -> tx_byte_pool_available + ((pool_ptr -> tx_byte_pool_fragments - 2 ) * ((sizeof (UCHAR * )) + (sizeof (ALIGN_TYPE ))));
111+ if (memory_size >= total_theoretical_available )
106112 {
107113
108114 /* Restore interrupts. */
@@ -146,10 +152,9 @@ UCHAR *work_ptr;
146152 /* Determine if this is the first free block. */
147153 if (first_free_block_found == TX_FALSE )
148154 {
149-
150155 /* This is the first free block. */
151156 pool_ptr -> tx_byte_pool_search = current_ptr ;
152-
157+
153158 /* Set the flag to indicate we have found the first free
154159 block. */
155160 first_free_block_found = TX_TRUE ;
@@ -178,7 +183,7 @@ UCHAR *work_ptr;
178183 /* Clear the available bytes variable. */
179184 available_bytes = ((ULONG ) 0 );
180185
181- /* Not enough memory, check to see if the neighbor is
186+ /* Not enough memory, check to see if the neighbor is
182187 free and can be merged. */
183188 work_ptr = TX_UCHAR_POINTER_ADD (next_ptr , (sizeof (UCHAR * )));
184189 free_ptr = TX_UCHAR_TO_ALIGN_TYPE_POINTER_CONVERT (work_ptr );
@@ -207,22 +212,19 @@ UCHAR *work_ptr;
207212 /* See if the search pointer is affected. */
208213 if (pool_ptr -> tx_byte_pool_search == next_ptr )
209214 {
210-
211215 /* Yes, update the search pointer. */
212216 pool_ptr -> tx_byte_pool_search = current_ptr ;
213217 }
214218 }
215219 else
216220 {
217-
218221 /* Neighbor is not free so we can skip over it! */
219222 next_block_link_ptr = TX_UCHAR_TO_INDIRECT_UCHAR_POINTER_CONVERT (next_ptr );
220223 current_ptr = * next_block_link_ptr ;
221224
222225 /* Decrement the examined block count to account for this one. */
223226 if (examine_blocks != ((UINT ) 0 ))
224227 {
225-
226228 examine_blocks -- ;
227229
228230#ifdef TX_BYTE_POOL_ENABLE_PERFORMANCE_INFO
@@ -297,7 +299,7 @@ UCHAR *work_ptr;
297299
298300 /* Update the current pointer to point at the newly created block. */
299301 * this_block_link_ptr = next_ptr ;
300-
302+
301303 /* Set available equal to memory size for subsequent calculation. */
302304 available_bytes = memory_size ;
303305
0 commit comments