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