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 /**   Byte 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_byte_pool.h"
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _txe_byte_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 byte pool memory      */
47 /*    function.                                                           */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    pool_ptr                          Pointer to pool control block     */
52 /*    name_ptr                          Pointer to byte pool name         */
53 /*    pool_start                        Address of beginning of pool area */
54 /*    pool_size                         Number of bytes in the byte pool  */
55 /*    pool_control_block_size           Size of byte pool control block   */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    TX_POOL_ERROR                     Invalid byte pool pointer         */
60 /*    TX_PTR_ERROR                      Invalid pool starting address     */
61 /*    TX_SIZE_ERROR                     Invalid pool size                 */
62 /*    TX_CALLER_ERROR                   Invalid caller of this function   */
63 /*    status                            Actual completion status          */
64 /*                                                                        */
65 /*  CALLS                                                                 */
66 /*                                                                        */
67 /*    _tx_byte_pool_create              Actual byte pool create function  */
68 /*    _tx_thread_system_preempt_check   Check for preemption              */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    Application Code                                                    */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
79 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
80 /*                                            resulting in version 6.1    */
81 /*                                                                        */
82 /**************************************************************************/
_txe_byte_pool_create(TX_BYTE_POOL * pool_ptr,CHAR * name_ptr,VOID * pool_start,ULONG pool_size,UINT pool_control_block_size)83 UINT  _txe_byte_pool_create(TX_BYTE_POOL *pool_ptr, CHAR *name_ptr, VOID *pool_start, ULONG pool_size, UINT pool_control_block_size)
84 {
85 
86 TX_INTERRUPT_SAVE_AREA
87 
88 UINT            status;
89 ULONG           i;
90 TX_BYTE_POOL    *next_pool;
91 #ifndef TX_TIMER_PROCESS_IN_ISR
92 TX_THREAD       *thread_ptr;
93 #endif
94 
95 
96     /* Default status to success.  */
97     status =  TX_SUCCESS;
98 
99     /* Check for an invalid byte pool pointer.  */
100     if (pool_ptr == TX_NULL)
101     {
102 
103         /* Byte pool pointer is invalid, return appropriate error code.  */
104         status =  TX_POOL_ERROR;
105     }
106 
107     /* Now see if the pool control block size is valid.  */
108     else if (pool_control_block_size != (sizeof(TX_BYTE_POOL)))
109     {
110 
111         /* Byte pool pointer is invalid, return appropriate error code.  */
112         status =  TX_POOL_ERROR;
113     }
114     else
115     {
116 
117         /* Disable interrupts.  */
118         TX_DISABLE
119 
120         /* Increment the preempt disable flag.  */
121         _tx_thread_preempt_disable++;
122 
123         /* Restore interrupts.  */
124         TX_RESTORE
125 
126         /* Next see if it is already in the created list.  */
127         next_pool =   _tx_byte_pool_created_ptr;
128         for (i = ((ULONG) 0); i < _tx_byte_pool_created_count; i++)
129         {
130 
131             /* Determine if this byte pool matches the pool in the list.  */
132             if (pool_ptr == next_pool)
133             {
134 
135                 break;
136             }
137             else
138             {
139 
140                 /* Move to the next pool.  */
141                 next_pool =  next_pool -> tx_byte_pool_created_next;
142             }
143         }
144 
145         /* Disable interrupts.  */
146         TX_DISABLE
147 
148         /* Decrement the preempt disable flag.  */
149         _tx_thread_preempt_disable--;
150 
151         /* Restore interrupts.  */
152         TX_RESTORE
153 
154         /* Check for preemption.  */
155         _tx_thread_system_preempt_check();
156 
157         /* At this point, check to see if there is a duplicate pool.  */
158         if (pool_ptr == next_pool)
159         {
160 
161             /* Pool is already created, return appropriate error code.  */
162             status =  TX_POOL_ERROR;
163         }
164 
165         /* Check for an invalid starting address.  */
166         else if (pool_start == TX_NULL)
167         {
168 
169             /* Null starting address pointer, return appropriate error.  */
170             status =  TX_PTR_ERROR;
171         }
172 
173         /* Check for invalid pool size.  */
174         else if (pool_size < TX_BYTE_POOL_MIN)
175         {
176 
177             /* Pool not big enough, return appropriate error.  */
178             status =  TX_SIZE_ERROR;
179         }
180         else
181         {
182 
183 #ifndef TX_TIMER_PROCESS_IN_ISR
184 
185             /* Pickup thread pointer.  */
186             TX_THREAD_GET_CURRENT(thread_ptr)
187 
188             /* Check for invalid caller of this function.  First check for a calling thread.  */
189             if (thread_ptr == &_tx_timer_thread)
190             {
191 
192                 /* Invalid caller of this function, return appropriate error code.  */
193                 status =  TX_CALLER_ERROR;
194             }
195 #endif
196 
197             /* Check for interrupt call.  */
198             if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
199             {
200 
201                 /* Now, make sure the call is from an interrupt and not initialization.  */
202                 if (TX_THREAD_GET_SYSTEM_STATE() < TX_INITIALIZE_IN_PROGRESS)
203                 {
204 
205                     /* Invalid caller of this function, return appropriate error code.  */
206                     status =  TX_CALLER_ERROR;
207                 }
208             }
209         }
210     }
211 
212     /* Determine if everything is okay.  */
213     if (status == TX_SUCCESS)
214     {
215 
216         /* Call actual byte pool create function.  */
217         status =  _tx_byte_pool_create(pool_ptr, name_ptr, pool_start, pool_size);
218     }
219 
220     /* Return completion status.  */
221     return(status);
222 }
223 
224