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_thread.h"
29 #include "tx_timer.h"
30 #include "tx_block_pool.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _txe_block_allocate                                 PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    William E. Lamie, Microsoft Corporation                             */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function checks for errors in the allocate block memory        */
46 /*    function call.                                                      */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    pool_ptr                          Pointer to pool control block     */
51 /*    block_ptr                         Pointer to place allocated block  */
52 /*                                        pointer                         */
53 /*    wait_option                       Suspension option                 */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    TX_POOL_ERROR                     Invalid pool pointer              */
58 /*    TX_PTR_ERROR                      Invalid destination pointer       */
59 /*    TX_WAIT_ERROR                     Invalid wait option               */
60 /*    status                            Actual Completion status          */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    _tx_block_allocate                Actual block allocate function    */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application Code                                                    */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
75 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*                                                                        */
78 /**************************************************************************/
_txe_block_allocate(TX_BLOCK_POOL * pool_ptr,VOID ** block_ptr,ULONG wait_option)79 UINT  _txe_block_allocate(TX_BLOCK_POOL *pool_ptr, VOID **block_ptr, ULONG wait_option)
80 {
81 
82 UINT            status;
83 
84 #ifndef TX_TIMER_PROCESS_IN_ISR
85 
86 TX_THREAD       *current_thread;
87 #endif
88 
89     /* Default status to success.  */
90     status =  TX_SUCCESS;
91 
92     /* Check for an invalid pool pointer.  */
93     if (pool_ptr == TX_NULL)
94     {
95 
96         /* Pool pointer is invalid, return appropriate error code.  */
97         status =  TX_POOL_ERROR;
98     }
99 
100     /* Check for an invalid pool pointer.  */
101     else if (pool_ptr -> tx_block_pool_id != TX_BLOCK_POOL_ID)
102     {
103 
104         /* Pool pointer is invalid, return appropriate error code.  */
105         status =  TX_POOL_ERROR;
106     }
107 
108     /* Check for an invalid destination for return pointer.  */
109     else if (block_ptr == TX_NULL)
110     {
111 
112         /* Null destination pointer, return appropriate error.  */
113         status =  TX_PTR_ERROR;
114     }
115     else
116     {
117 
118         /* Check for a wait option error.  Only threads are allowed any form of
119            suspension.  */
120         if (wait_option != TX_NO_WAIT)
121         {
122 
123             /* Is the call from an ISR or Initialization?  */
124             if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
125             {
126 
127                 /* A non-thread is trying to suspend, return appropriate error code.  */
128                 status =  TX_WAIT_ERROR;
129             }
130 
131 #ifndef TX_TIMER_PROCESS_IN_ISR
132             else
133             {
134 
135                 /* Pickup thread pointer.  */
136                 TX_THREAD_GET_CURRENT(current_thread)
137 
138                 /* Is the current thread the timer thread?  */
139                 if (current_thread == &_tx_timer_thread)
140                 {
141 
142                     /* A non-thread is trying to suspend, return appropriate error code.  */
143                     status =  TX_WAIT_ERROR;
144                 }
145             }
146 #endif
147         }
148     }
149 
150     /* Determine if everything is okay.  */
151     if (status == TX_SUCCESS)
152     {
153 
154         /* Call actual block allocate function.  */
155         status =  _tx_block_allocate(pool_ptr, block_ptr, wait_option);
156     }
157 
158     /* Return completion status.  */
159     return(status);
160 }
161 
162