1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** ThreadX Component                                                     */
16 /**                                                                       */
17 /**   Block Pool                                                          */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define TX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "tx_api.h"
28 #include "tx_initialize.h"
29 #include "tx_thread.h"
30 #include "tx_timer.h"
31 #include "tx_block_pool.h"
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _txe_block_pool_create                              PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    William E. Lamie, Microsoft Corporation                             */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function checks for errors in the create block memory pool     */
47 /*    function call.                                                      */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    pool_ptr                          Pointer to pool control block     */
52 /*    name_ptr                          Pointer to block pool name        */
53 /*    block_size                        Number of bytes in each block     */
54 /*    pool_start                        Address of beginning of pool area */
55 /*    pool_size                         Number of bytes in the block pool */
56 /*    pool_control_block_size           Size of block pool control block  */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    TX_POOL_ERROR                     Invalid pool pointer              */
61 /*    TX_PTR_ERROR                      Invalid starting address          */
62 /*    TX_SIZE_ERROR                     Invalid pool size                 */
63 /*    TX_CALLER_ERROR                   Invalid caller of pool            */
64 /*    status                            Actual completion status          */
65 /*                                                                        */
66 /*  CALLS                                                                 */
67 /*                                                                        */
68 /*    _tx_block_pool_create             Actual block pool create function */
69 /*    _tx_thread_system_preempt_check   Check for preemption              */
70 /*                                                                        */
71 /*  CALLED BY                                                             */
72 /*                                                                        */
73 /*    Application Code                                                    */
74 /*                                                                        */
75 /*  RELEASE HISTORY                                                       */
76 /*                                                                        */
77 /*    DATE              NAME                      DESCRIPTION             */
78 /*                                                                        */
79 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
80 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
81 /*                                            resulting in version 6.1    */
82 /*                                                                        */
83 /**************************************************************************/
_txe_block_pool_create(TX_BLOCK_POOL * pool_ptr,CHAR * name_ptr,ULONG block_size,VOID * pool_start,ULONG pool_size,UINT pool_control_block_size)84 UINT  _txe_block_pool_create(TX_BLOCK_POOL *pool_ptr, CHAR *name_ptr, ULONG block_size,
85                     VOID *pool_start, ULONG pool_size, UINT pool_control_block_size)
86 {
87 
88 TX_INTERRUPT_SAVE_AREA
89 
90 UINT            status;
91 ULONG           i;
92 TX_BLOCK_POOL   *next_pool;
93 #ifndef TX_TIMER_PROCESS_IN_ISR
94 TX_THREAD       *thread_ptr;
95 #endif
96 
97 
98     /* Default status to success.  */
99     status =  TX_SUCCESS;
100 
101     /* Check for an invalid pool pointer.  */
102     if (pool_ptr == TX_NULL)
103     {
104 
105         /* Pool pointer is invalid, return appropriate error code.  */
106         status =  TX_POOL_ERROR;
107     }
108 
109     /* Check for invalid control block size.  */
110     else if (pool_control_block_size != (sizeof(TX_BLOCK_POOL)))
111     {
112 
113         /* Pool pointer is invalid, return appropriate error code.  */
114         status =  TX_POOL_ERROR;
115     }
116     else
117     {
118 
119         /* Disable interrupts.  */
120         TX_DISABLE
121 
122         /* Increment the preempt disable flag.  */
123         _tx_thread_preempt_disable++;
124 
125         /* Restore interrupts.  */
126         TX_RESTORE
127 
128         /* Next see if it is already in the created list.  */
129         next_pool =   _tx_block_pool_created_ptr;
130         for (i = ((ULONG) 0); i < _tx_block_pool_created_count; i++)
131         {
132 
133             /* Determine if this block pool matches the pool in the list.  */
134             if (pool_ptr == next_pool)
135             {
136 
137                 break;
138             }
139             else
140             {
141                 /* Move to the next pool.  */
142                 next_pool =  next_pool -> tx_block_pool_created_next;
143             }
144         }
145 
146         /* Disable interrupts.  */
147         TX_DISABLE
148 
149         /* Decrement the preempt disable flag.  */
150         _tx_thread_preempt_disable--;
151 
152         /* Restore interrupts.  */
153         TX_RESTORE
154 
155         /* Check for preemption.  */
156         _tx_thread_system_preempt_check();
157 
158         /* At this point, check to see if there is a duplicate pool.  */
159         if (pool_ptr == next_pool)
160         {
161 
162             /* Pool is already created, return appropriate error code.  */
163             status =  TX_POOL_ERROR;
164         }
165 
166         /* Check for an invalid starting address.  */
167         else if (pool_start == TX_NULL)
168         {
169 
170             /* Null starting address pointer, return appropriate error.  */
171             status =  TX_PTR_ERROR;
172         }
173         else
174         {
175 
176             /* Check for invalid pool size.  */
177             if ((((block_size/(sizeof(void *)))*(sizeof(void *))) + (sizeof(void *))) >
178                                             ((pool_size/(sizeof(void *)))*(sizeof(void *))))
179             {
180 
181                 /* Not enough memory for one block, return appropriate error.  */
182                 status =  TX_SIZE_ERROR;
183             }
184             else
185             {
186 
187 #ifndef TX_TIMER_PROCESS_IN_ISR
188 
189                 /* Pickup thread pointer.  */
190                 TX_THREAD_GET_CURRENT(thread_ptr)
191 
192                 /* Check for invalid caller of this function.  First check for a calling thread.  */
193                 if (thread_ptr == &_tx_timer_thread)
194                 {
195 
196                     /* Invalid caller of this function, return appropriate error code.  */
197                     status =  TX_CALLER_ERROR;
198                 }
199 #endif
200 
201                 /* Check for interrupt call.  */
202                 if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
203                 {
204 
205                     /* Now, make sure the call is from an interrupt and not initialization.  */
206                     if (TX_THREAD_GET_SYSTEM_STATE() < TX_INITIALIZE_IN_PROGRESS)
207                     {
208 
209                         /* Invalid caller of this function, return appropriate error code.  */
210                         status =  TX_CALLER_ERROR;
211                     }
212                 }
213             }
214         }
215     }
216 
217     /* Determine if everything is okay.  */
218     if (status == TX_SUCCESS)
219     {
220 
221         /* Call actual block pool create function.  */
222         status =  _tx_block_pool_create(pool_ptr, name_ptr, block_size, pool_start, pool_size);
223     }
224 
225     /* Return completion status.  */
226     return(status);
227 }
228 
229