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