1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** ThreadX Component                                                     */
17 /**                                                                       */
18 /**   Byte Pool                                                           */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define TX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "tx_api.h"
29 #include "tx_initialize.h"
30 #include "tx_thread.h"
31 #include "tx_timer.h"
32 #include "tx_byte_pool.h"
33 
34 
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _txe_byte_pool_create                               PORTABLE C      */
40 /*                                                           6.1          */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    William E. Lamie, Microsoft Corporation                             */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This function checks for errors in the create byte pool memory      */
48 /*    function.                                                           */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    pool_ptr                          Pointer to pool control block     */
53 /*    name_ptr                          Pointer to byte pool name         */
54 /*    pool_start                        Address of beginning of pool area */
55 /*    pool_size                         Number of bytes in the byte pool  */
56 /*    pool_control_block_size           Size of byte pool control block   */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    TX_POOL_ERROR                     Invalid byte pool pointer         */
61 /*    TX_PTR_ERROR                      Invalid pool starting address     */
62 /*    TX_SIZE_ERROR                     Invalid pool size                 */
63 /*    TX_CALLER_ERROR                   Invalid caller of this function   */
64 /*    status                            Actual completion status          */
65 /*                                                                        */
66 /*  CALLS                                                                 */
67 /*                                                                        */
68 /*    _tx_byte_pool_create              Actual byte 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_byte_pool_create(TX_BYTE_POOL * pool_ptr,CHAR * name_ptr,VOID * pool_start,ULONG pool_size,UINT pool_control_block_size)84 UINT  _txe_byte_pool_create(TX_BYTE_POOL *pool_ptr, CHAR *name_ptr, VOID *pool_start, ULONG pool_size, UINT pool_control_block_size)
85 {
86 
87 TX_INTERRUPT_SAVE_AREA
88 
89 UINT            status;
90 ULONG           i;
91 TX_BYTE_POOL    *next_pool;
92 #ifndef TX_TIMER_PROCESS_IN_ISR
93 TX_THREAD       *thread_ptr;
94 #endif
95 
96 
97     /* Default status to success.  */
98     status =  TX_SUCCESS;
99 
100     /* Check for an invalid byte pool pointer.  */
101     if (pool_ptr == TX_NULL)
102     {
103 
104         /* Byte pool pointer is invalid, return appropriate error code.  */
105         status =  TX_POOL_ERROR;
106     }
107 
108     /* Now see if the pool control block size is valid.  */
109     else if (pool_control_block_size != (sizeof(TX_BYTE_POOL)))
110     {
111 
112         /* Byte pool pointer is invalid, return appropriate error code.  */
113         status =  TX_POOL_ERROR;
114     }
115     else
116     {
117 
118         /* Disable interrupts.  */
119         TX_DISABLE
120 
121         /* Increment the preempt disable flag.  */
122         _tx_thread_preempt_disable++;
123 
124         /* Restore interrupts.  */
125         TX_RESTORE
126 
127         /* Next see if it is already in the created list.  */
128         next_pool =   _tx_byte_pool_created_ptr;
129         for (i = ((ULONG) 0); i < _tx_byte_pool_created_count; i++)
130         {
131 
132             /* Determine if this byte pool matches the pool in the list.  */
133             if (pool_ptr == next_pool)
134             {
135 
136                 break;
137             }
138             else
139             {
140 
141                 /* Move to the next pool.  */
142                 next_pool =  next_pool -> tx_byte_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 
174         /* Check for invalid pool size.  */
175         else if (pool_size < TX_BYTE_POOL_MIN)
176         {
177 
178             /* Pool not big enough, return appropriate error.  */
179             status =  TX_SIZE_ERROR;
180         }
181         else
182         {
183 
184 #ifndef TX_TIMER_PROCESS_IN_ISR
185 
186             /* Pickup thread pointer.  */
187             TX_THREAD_GET_CURRENT(thread_ptr)
188 
189             /* Check for invalid caller of this function.  First check for a calling thread.  */
190             if (thread_ptr == &_tx_timer_thread)
191             {
192 
193                 /* Invalid caller of this function, return appropriate error code.  */
194                 status =  TX_CALLER_ERROR;
195             }
196 #endif
197 
198             /* Check for interrupt call.  */
199             if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
200             {
201 
202                 /* Now, make sure the call is from an interrupt and not initialization.  */
203                 if (TX_THREAD_GET_SYSTEM_STATE() < TX_INITIALIZE_IN_PROGRESS)
204                 {
205 
206                     /* Invalid caller of this function, return appropriate error code.  */
207                     status =  TX_CALLER_ERROR;
208                 }
209             }
210         }
211     }
212 
213     /* Determine if everything is okay.  */
214     if (status == TX_SUCCESS)
215     {
216 
217         /* Call actual byte pool create function.  */
218         status =  _tx_byte_pool_create(pool_ptr, name_ptr, pool_start, pool_size);
219     }
220 
221     /* Return completion status.  */
222     return(status);
223 }
224 
225