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 Memory */
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_allocate 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 allocate bytes function call. */
48 /* */
49 /* INPUT */
50 /* */
51 /* pool_ptr Pointer to pool control block */
52 /* memory_ptr Pointer to place allocated bytes */
53 /* pointer */
54 /* memory_size Number of bytes to allocate */
55 /* wait_option Suspension option */
56 /* */
57 /* OUTPUT */
58 /* */
59 /* TX_POOL_ERROR Invalid memory pool pointer */
60 /* TX_PTR_ERROR Invalid destination pointer */
61 /* TX_WAIT_ERROR Invalid wait option */
62 /* TX_CALLER_ERROR Invalid caller of this function */
63 /* TX_SIZE_ERROR Invalid size of memory request */
64 /* status Actual completion status */
65 /* */
66 /* CALLS */
67 /* */
68 /* _tx_byte_allocate Actual byte allocate function */
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_allocate(TX_BYTE_POOL * pool_ptr,VOID ** memory_ptr,ULONG memory_size,ULONG wait_option)83 UINT _txe_byte_allocate(TX_BYTE_POOL *pool_ptr, VOID **memory_ptr,
84 ULONG memory_size, ULONG wait_option)
85 {
86
87 UINT status;
88 #ifndef TX_TIMER_PROCESS_IN_ISR
89 TX_THREAD *thread_ptr;
90 #endif
91
92
93 /* Default status to success. */
94 status = TX_SUCCESS;
95
96 /* Check for an invalid byte pool pointer. */
97 if (pool_ptr == TX_NULL)
98 {
99
100 /* Byte pool pointer is invalid, return appropriate error code. */
101 status = TX_POOL_ERROR;
102 }
103
104 /* Now check for invalid pool ID. */
105 else if (pool_ptr -> tx_byte_pool_id != TX_BYTE_POOL_ID)
106 {
107
108 /* Byte pool pointer is invalid, return appropriate error code. */
109 status = TX_POOL_ERROR;
110 }
111
112 /* Check for an invalid destination for return pointer. */
113 else if (memory_ptr == TX_NULL)
114 {
115
116 /* Null destination pointer, return appropriate error. */
117 status = TX_PTR_ERROR;
118 }
119
120 /* Check for an invalid memory size. */
121 else if (memory_size == ((ULONG) 0))
122 {
123
124 /* Error in size, return appropriate error. */
125 status = TX_SIZE_ERROR;
126 }
127
128 /* Determine if the size is greater than the pool size. */
129 else if (memory_size > pool_ptr -> tx_byte_pool_size)
130 {
131
132 /* Error in size, return appropriate error. */
133 status = TX_SIZE_ERROR;
134 }
135
136 else
137 {
138
139 /* Check for a wait option error. Only threads are allowed any form of
140 suspension. */
141 if (wait_option != TX_NO_WAIT)
142 {
143
144 /* Is call from ISR or Initialization? */
145 if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
146 {
147
148 /* A non-thread is trying to suspend, return appropriate error code. */
149 status = TX_WAIT_ERROR;
150 }
151 }
152 }
153 #ifndef TX_TIMER_PROCESS_IN_ISR
154
155 /* Check for timer execution. */
156 if (status == TX_SUCCESS)
157 {
158
159 /* Pickup thread pointer. */
160 TX_THREAD_GET_CURRENT(thread_ptr)
161
162 /* Check for invalid caller of this function. First check for a calling thread. */
163 if (thread_ptr == &_tx_timer_thread)
164 {
165
166 /* Invalid caller of this function, return appropriate error code. */
167 status = TX_CALLER_ERROR;
168 }
169 }
170 #endif
171
172 /* Is everything still okay? */
173 if (status == TX_SUCCESS)
174 {
175
176 /* Check for interrupt call. */
177 if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
178 {
179
180 /* Now, make sure the call is from an interrupt and not initialization. */
181 if (TX_THREAD_GET_SYSTEM_STATE() < TX_INITIALIZE_IN_PROGRESS)
182 {
183
184 /* Invalid caller of this function, return appropriate error code. */
185 status = TX_CALLER_ERROR;
186 }
187 }
188 }
189
190 /* Determine if everything is okay. */
191 if (status == TX_SUCCESS)
192 {
193
194 /* Call actual byte memory allocate function. */
195 status = _tx_byte_allocate(pool_ptr, memory_ptr, memory_size, wait_option);
196 }
197
198 /* Return completion status. */
199 return(status);
200 }
201
202