Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define TX_SOURCE_CODE
#include "tx_api.h"
#include "tx_trace.h"
#include "tx_thread.h"
#include "tx_byte_pool.h"
...
...
_tx_byte_release(void *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesthreadxcommon/src/tx_byte_release.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** ThreadX Component */ /** */ /** Byte Memory */ /** */... /**************************************************************************/ /**************************************************************************/ #define TX_SOURCE_CODE /* Include necessary system files. */ #include "tx_api.h" #include "tx_trace.h" #include "tx_thread.h" #include "tx_byte_pool.h" ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _tx_byte_release PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function returns previously allocated memory to its */ /* associated memory byte pool. */ /* */ /* INPUT */ /* */ /* memory_ptr Pointer to allocated memory */ /* */ /* OUTPUT */ /* */ /* [TX_PTR_ERROR | TX_SUCCESS] Completion status */ /* */ /* CALLS */ /* */ /* _tx_thread_system_preempt_check Check for preemption */ /* _tx_thread_system_resume Resume thread service */ /* _tx_thread_system_ni_resume Non-interruptable resume thread */ /* _tx_byte_pool_search Search the byte pool for memory */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 William E. Lamie Initial Version 6.0 */ /* 09-30-2020 Yuxin Zhou Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _tx_byte_release(VOID *memory_ptr) { TX_INTERRUPT_SAVE_AREA UINT status; TX_BYTE_POOL *pool_ptr; TX_THREAD *thread_ptr; UCHAR *work_ptr; UCHAR *temp_ptr; UCHAR *next_block_ptr; TX_THREAD *susp_thread_ptr; UINT suspended_count; TX_THREAD *next_thread; TX_THREAD *previous_thread; ULONG memory_size; ALIGN_TYPE *free_ptr; TX_BYTE_POOL **byte_pool_ptr; UCHAR **block_link_ptr; UCHAR **suspend_info_ptr; /* Default to successful status. */ status = TX_SUCCESS; /* Set the pool pointer to NULL. */ pool_ptr = TX_NULL; /* Lockout interrupts. */ TX_DISABLE /* Determine if the memory pointer is valid. */ work_ptr = TX_VOID_TO_UCHAR_POINTER_CONVERT(memory_ptr); if (work_ptr != TX_NULL) { /* Back off the memory pointer to pickup its header. */ work_ptr = TX_UCHAR_POINTER_SUB(work_ptr, ((sizeof(UCHAR *)) + (sizeof(ALIGN_TYPE)))); /* There is a pointer, pickup the pool pointer address. */ temp_ptr = TX_UCHAR_POINTER_ADD(work_ptr, (sizeof(UCHAR *))); free_ptr = TX_UCHAR_TO_ALIGN_TYPE_POINTER_CONVERT(temp_ptr); if ((*free_ptr) != TX_BYTE_BLOCK_FREE) { /* Pickup the pool pointer. */ temp_ptr = TX_UCHAR_POINTER_ADD(work_ptr, (sizeof(UCHAR *))); byte_pool_ptr = TX_UCHAR_TO_INDIRECT_BYTE_POOL_POINTER(temp_ptr); pool_ptr = *byte_pool_ptr; /* See if we have a valid pool pointer. */ if (pool_ptr == TX_NULL) { /* Return pointer error. */ status = TX_PTR_ERROR; }if (pool_ptr == TX_NULL) { ... } else { /* See if we have a valid pool. */ if (pool_ptr -> tx_byte_pool_id != TX_BYTE_POOL_ID) { /* Return pointer error. */ status = TX_PTR_ERROR; /* Reset the pool pointer is NULL. */ pool_ptr = TX_NULL; }if (pool_ptr -> tx_byte_pool_id != TX_BYTE_POOL_ID) { ... } }else { ... } }if ((*free_ptr) != TX_BYTE_BLOCK_FREE) { ... } else { /* Return pointer error. */ status = TX_PTR_ERROR; }else { ... } }if (work_ptr != TX_NULL) { ... } else { /* Return pointer error. */ status = TX_PTR_ERROR; }else { ... } /* Determine if the pointer is valid. */ if (pool_ptr == TX_NULL) { /* Restore interrupts. */ TX_RESTORE }if (pool_ptr == TX_NULL) { ... } else { /* At this point, we know that the pointer is valid. */ /* Pickup thread pointer. */ TX_THREAD_GET_CURRENT(thread_ptr) /* Indicate that this thread is the current owner. */ pool_ptr -> tx_byte_pool_owner = thread_ptr; #ifdef TX_BYTE_POOL_ENABLE_PERFORMANCE_INFO /* Increment the total release counter. */ _tx_byte_pool_performance_release_count++; /* Increment the number of releases on this pool. */ pool_ptr -> tx_byte_pool_performance_release_count++;/* ... */ #endif /* If trace is enabled, insert this event into the trace buffer. */ TX_TRACE_IN_LINE_INSERT(TX_TRACE_BYTE_RELEASE, pool_ptr, TX_POINTER_TO_ULONG_CONVERT(memory_ptr), pool_ptr -> tx_byte_pool_suspended_count, pool_ptr -> tx_byte_pool_available, TX_TRACE_BYTE_POOL_EVENTS) /* Log this kernel call. */ TX_EL_BYTE_RELEASE_INSERT /* Release the memory. */ temp_ptr = TX_UCHAR_POINTER_ADD(work_ptr, (sizeof(UCHAR *))); free_ptr = TX_UCHAR_TO_ALIGN_TYPE_POINTER_CONVERT(temp_ptr); *free_ptr = TX_BYTE_BLOCK_FREE; /* Update the number of available bytes in the pool. */ block_link_ptr = TX_UCHAR_TO_INDIRECT_UCHAR_POINTER_CONVERT(work_ptr); next_block_ptr = *block_link_ptr; pool_ptr -> tx_byte_pool_available = pool_ptr -> tx_byte_pool_available + TX_UCHAR_POINTER_DIF(next_block_ptr, work_ptr); /* Determine if the free block is prior to current search pointer. */ if (work_ptr < (pool_ptr -> tx_byte_pool_search)) { /* Yes, update the search pointer to the released block. */ pool_ptr -> tx_byte_pool_search = work_ptr; }if (work_ptr < (pool_ptr -> tx_byte_pool_search)) { ... } /* Determine if there are threads suspended on this byte pool. */ if (pool_ptr -> tx_byte_pool_suspended_count != TX_NO_SUSPENSIONS) { /* Now examine the suspension list to find threads waiting for memory. Maybe it is now available! *//* ... */ while (pool_ptr -> tx_byte_pool_suspended_count != TX_NO_SUSPENSIONS) { /* Pickup the first suspended thread pointer. */ susp_thread_ptr = pool_ptr -> tx_byte_pool_suspension_list; /* Pickup the size of the memory the thread is requesting. */ memory_size = susp_thread_ptr -> tx_thread_suspend_info; /* Restore interrupts. */ TX_RESTORE /* See if the request can be satisfied. */ work_ptr = _tx_byte_pool_search(pool_ptr, memory_size); /* Optional processing extension. */ TX_BYTE_RELEASE_EXTENSION /* Disable interrupts. */ TX_DISABLE /* Indicate that this thread is the current owner. */ pool_ptr -> tx_byte_pool_owner = thread_ptr; /* If there is not enough memory, break this loop! */ if (work_ptr == TX_NULL) { /* Break out of the loop. */ break; }if (work_ptr == TX_NULL) { ... } /* Check to make sure the thread is still suspended. */ if (susp_thread_ptr == pool_ptr -> tx_byte_pool_suspension_list) { /* Also, makes sure the memory size is the same. */ if (susp_thread_ptr -> tx_thread_suspend_info == memory_size) { /* Remove the suspended thread from the list. */ /* Decrement the number of threads suspended. */ pool_ptr -> tx_byte_pool_suspended_count--; /* Pickup the suspended count. */ suspended_count = pool_ptr -> tx_byte_pool_suspended_count; /* See if this is the only suspended thread on the list. */ if (suspended_count == TX_NO_SUSPENSIONS) { /* Yes, the only suspended thread. */ /* Update the head pointer. */ pool_ptr -> tx_byte_pool_suspension_list = TX_NULL; }if (suspended_count == TX_NO_SUSPENSIONS) { ... } else { /* At least one more thread is on the same expiration list. */ /* Update the list head pointer. */ next_thread = susp_thread_ptr -> tx_thread_suspended_next; pool_ptr -> tx_byte_pool_suspension_list = next_thread; /* Update the links of the adjacent threads. */ previous_thread = susp_thread_ptr -> tx_thread_suspended_previous; next_thread -> tx_thread_suspended_previous = previous_thread; previous_thread -> tx_thread_suspended_next = next_thread; }else { ... } /* Prepare for resumption of the thread. */ /* Clear cleanup routine to avoid timeout. */ susp_thread_ptr -> tx_thread_suspend_cleanup = TX_NULL; /* Return this block pointer to the suspended thread waiting for a block. *//* ... */ suspend_info_ptr = TX_VOID_TO_INDIRECT_UCHAR_POINTER_CONVERT(susp_thread_ptr -> tx_thread_additional_suspend_info); *suspend_info_ptr = work_ptr; /* Clear the memory pointer to indicate that it was given to the suspended thread. */ work_ptr = TX_NULL; /* Put return status into the thread control block. */ susp_thread_ptr -> tx_thread_suspend_status = TX_SUCCESS; #ifdef TX_NOT_INTERRUPTABLE /* Resume the thread! */ _tx_thread_system_ni_resume(susp_thread_ptr); /* Restore interrupts. */ TX_RESTORE/* ... */ #else /* Temporarily disable preemption. */ _tx_thread_preempt_disable++; /* Restore interrupts. */ TX_RESTORE /* Resume thread. */ _tx_thread_system_resume(susp_thread_ptr);/* ... */ #endif /* Lockout interrupts. */ TX_DISABLE }if (susp_thread_ptr -> tx_thread_suspend_info == memory_size) { ... } }if (susp_thread_ptr == pool_ptr -> tx_byte_pool_suspension_list) { ... } /* Determine if the memory was given to the suspended thread. */ if (work_ptr != TX_NULL) { /* No, it wasn't given to the suspended thread. */ /* Put the memory back on the available list since this thread is no longer suspended. *//* ... */ work_ptr = TX_UCHAR_POINTER_SUB(work_ptr, (((sizeof(UCHAR *)) + (sizeof(ALIGN_TYPE))))); temp_ptr = TX_UCHAR_POINTER_ADD(work_ptr, (sizeof(UCHAR *))); free_ptr = TX_UCHAR_TO_ALIGN_TYPE_POINTER_CONVERT(temp_ptr); *free_ptr = TX_BYTE_BLOCK_FREE; /* Update the number of available bytes in the pool. */ block_link_ptr = TX_UCHAR_TO_INDIRECT_UCHAR_POINTER_CONVERT(work_ptr); next_block_ptr = *block_link_ptr; pool_ptr -> tx_byte_pool_available = pool_ptr -> tx_byte_pool_available + TX_UCHAR_POINTER_DIF(next_block_ptr, work_ptr); /* Determine if the current pointer is before the search pointer. */ if (work_ptr < (pool_ptr -> tx_byte_pool_search)) { /* Yes, update the search pointer. */ pool_ptr -> tx_byte_pool_search = work_ptr; }if (work_ptr < (pool_ptr -> tx_byte_pool_search)) { ... } }if (work_ptr != TX_NULL) { ... } }while (pool_ptr -> tx_byte_pool_suspended_count != TX_NO_SUSPENSIONS) { ... } /* Restore interrupts. */ TX_RESTORE /* Check for preemption. */ _tx_thread_system_preempt_check(); }if (pool_ptr -> tx_byte_pool_suspended_count != TX_NO_SUSPENSIONS) { ... } else { /* No, threads suspended, restore interrupts. */ TX_RESTORE }else { ... } }else { ... } /* Return completion status. */ return(status); }{ ... }