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 /**   Module Manager                                                      */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define TX_SOURCE_CODE
24 
25 #include "tx_api.h"
26 #include "tx_thread.h"
27 #include "txm_module.h"
28 
29 /**************************************************************************/
30 /*                                                                        */
31 /*  FUNCTION                                               RELEASE        */
32 /*                                                                        */
33 /*    _txm_module_manager_object_deallocate               PORTABLE C      */
34 /*                                                           6.1          */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    Scott Larson, Microsoft Corporation                                 */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    This function deallocates a previously allocated object.            */
42 /*                                                                        */
43 /*  INPUT                                                                 */
44 /*                                                                        */
45 /*    object_ptr                        Object pointer to deallocate      */
46 /*                                                                        */
47 /*  OUTPUT                                                                */
48 /*                                                                        */
49 /*    status                            Completion status                 */
50 /*                                                                        */
51 /*  CALLS                                                                 */
52 /*                                                                        */
53 /*    _txe_mutex_get                        Get module instance mutex     */
54 /*    _txe_mutex_put                        Release module instance mutex */
55 /*    _txe_byte_release                     Release object back to pool   */
56 /*                                                                        */
57 /*                                                                        */
58 /*  CALLED BY                                                             */
59 /*                                                                        */
60 /*    Application code                                                    */
61 /*                                                                        */
62 /*  RELEASE HISTORY                                                       */
63 /*                                                                        */
64 /*    DATE              NAME                      DESCRIPTION             */
65 /*                                                                        */
66 /*  09-30-2020      Scott Larson            Initial Version 6.1           */
67 /*                                                                        */
68 /**************************************************************************/
_txm_module_manager_object_deallocate(VOID * object_ptr)69 UINT  _txm_module_manager_object_deallocate(VOID *object_ptr)
70 {
71 TXM_MODULE_INSTANCE         *module_instance;
72 TXM_MODULE_ALLOCATED_OBJECT *module_allocated_object_ptr;
73 UINT                        return_value;
74 
75     /* Get module manager protection mutex.  */
76     _txe_mutex_get(&_txm_module_manager_mutex, TX_WAIT_FOREVER);
77 
78     /* Determine if an object pool was created.  */
79     if (_txm_module_manager_object_pool_created)
80     {
81 
82     TXM_MODULE_ALLOCATED_OBJECT   *next_object, *previous_object;
83 
84         /* Pickup module instance pointer.  */
85         module_instance =  _tx_thread_current_ptr -> tx_thread_module_instance_ptr;
86 
87         /* Setup the memory pointer.  */
88         module_allocated_object_ptr =  (TXM_MODULE_ALLOCATED_OBJECT *) object_ptr;
89 
90         /* Position the object pointer backwards to position back to the module manager information.  */
91         previous_object =  module_allocated_object_ptr--;
92 
93         /* Make sure the object is valid.  */
94         if ((module_allocated_object_ptr == TX_NULL) || (module_allocated_object_ptr -> txm_module_allocated_object_module_instance != module_instance) || (module_instance -> txm_module_instance_object_list_count == 0))
95         {
96             /* Set return value to invalid pointer.  */
97             return_value =  TX_PTR_ERROR;
98         }
99         else
100         {
101 
102             /* Unlink the node.  */
103             if ((--module_instance -> txm_module_instance_object_list_count) == 0)
104             {
105                 /* Only allocated object, just set the allocated list to NULL.  */
106                 module_instance -> txm_module_instance_object_list_head =  TX_NULL;
107             }
108             else
109             {
110                 /* Otherwise, not the only allocated object, link-up the neighbors.  */
111                 next_object =                                           module_allocated_object_ptr -> txm_module_allocated_object_next;
112                 previous_object =                                       module_allocated_object_ptr -> txm_module_allocated_object_previous;
113                 next_object -> txm_module_allocated_object_previous =   previous_object;
114                 previous_object -> txm_module_allocated_object_next =   next_object;
115 
116                 /* See if we have to update the allocated object list head pointer.  */
117                 if (module_instance -> txm_module_instance_object_list_head == module_allocated_object_ptr)
118                 {
119                     /* Yes, move the head pointer to the next link. */
120                     module_instance -> txm_module_instance_object_list_head =  next_object;
121                 }
122             }
123 
124             /* Release the object memory.  */
125             return_value =  (ULONG)  _txe_byte_release((VOID *) module_allocated_object_ptr);
126         }
127     }
128     else
129     {
130         /* Set return value to not enabled.  */
131         return_value =  TX_NOT_AVAILABLE;
132     }
133 
134     /* Release the protection mutex.  */
135     _txe_mutex_put(&_txm_module_manager_mutex);
136 
137     return(return_value);
138 }
139