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 "txm_module.h"
26
27 /**************************************************************************/
28 /* */
29 /* FUNCTION RELEASE */
30 /* */
31 /* _txm_module_manager_object_allocate PORTABLE C */
32 /* 6.1 */
33 /* AUTHOR */
34 /* */
35 /* Scott Larson, Microsoft Corporation */
36 /* */
37 /* DESCRIPTION */
38 /* */
39 /* This function allocates memory for an object from the memory pool */
40 /* supplied to txm_module_manager_initialize. */
41 /* */
42 /* INPUT */
43 /* */
44 /* object_ptr Destination of object pointer on */
45 /* successful allocation */
46 /* object_size Size in bytes of the object to be */
47 /* allocated */
48 /* module_instance The module instance that the */
49 /* object belongs to */
50 /* */
51 /* OUTPUT */
52 /* */
53 /* status Completion status */
54 /* */
55 /* CALLS */
56 /* */
57 /* _txe_mutex_get Get module instance mutex */
58 /* _txe_mutex_put Release module instance mutex */
59 /* _txe_byte_allocate Allocate object from pool */
60 /* */
61 /* CALLED BY */
62 /* */
63 /* Application code */
64 /* */
65 /* RELEASE HISTORY */
66 /* */
67 /* DATE NAME DESCRIPTION */
68 /* */
69 /* 09-30-2020 Scott Larson Initial Version 6.1 */
70 /* */
71 /**************************************************************************/
_txm_module_manager_object_allocate(VOID ** object_ptr_ptr,ULONG object_size,TXM_MODULE_INSTANCE * module_instance)72 UINT _txm_module_manager_object_allocate(VOID **object_ptr_ptr, ULONG object_size, TXM_MODULE_INSTANCE *module_instance)
73 {
74
75 TXM_MODULE_ALLOCATED_OBJECT *object_ptr;
76 UINT return_value;
77
78
79 /* Ensure the object pointer pointer is valid. */
80 if (object_ptr_ptr == (VOID **) TX_NULL)
81 {
82
83 /* The object pointer pointer is invalid, return an error. */
84 return(TXM_MODULE_INVALID_MEMORY);
85 }
86
87 /* Initialize the return pointer to NULL. */
88 *((VOID **) object_ptr_ptr) = TX_NULL;
89
90 /* Get module manager protection mutex. */
91 _txe_mutex_get(&_txm_module_manager_mutex, TX_WAIT_FOREVER);
92
93 /* Determine if an object pool was created. */
94 if (_txm_module_manager_object_pool_created)
95 {
96
97 TXM_MODULE_ALLOCATED_OBJECT *next_object, *previous_object;
98
99 /* Allocate the object requested by the module - adding an extra ULONG in order to
100 store the module instance pointer. */
101 return_value = (ULONG) _txe_byte_allocate(&_txm_module_manager_object_pool, (VOID **) &object_ptr,
102 (ULONG) (object_size + sizeof(TXM_MODULE_ALLOCATED_OBJECT)), TX_NO_WAIT);
103
104 /* Determine if the request was successful. */
105 if (return_value == TX_SUCCESS)
106 {
107 /* Yes, now store the module instance in the allocated memory block. */
108
109 /* Link the allocated memory to the module instance. */
110 if (module_instance -> txm_module_instance_object_list_count++ == 0)
111 {
112 /* The allocated object list is empty. Add object to empty list. */
113 module_instance -> txm_module_instance_object_list_head = object_ptr;
114 object_ptr -> txm_module_allocated_object_next = object_ptr;
115 object_ptr -> txm_module_allocated_object_previous = object_ptr;
116 }
117 else
118 {
119 /* This list is not NULL, add to the end of the list. */
120 next_object = module_instance -> txm_module_instance_object_list_head;
121 previous_object = next_object -> txm_module_allocated_object_previous;
122
123 /* Place the new object in the list. */
124 next_object -> txm_module_allocated_object_previous = object_ptr;
125 previous_object -> txm_module_allocated_object_next = object_ptr;
126
127 /* Setup this object's allocated links. */
128 object_ptr -> txm_module_allocated_object_previous = previous_object;
129 object_ptr -> txm_module_allocated_object_next = next_object;
130 }
131
132 /* Setup the module instance pointer in the allocated object. */
133 object_ptr -> txm_module_allocated_object_module_instance = module_instance;
134
135 /* Set the object size. */
136 object_ptr -> txm_module_object_size = object_size;
137
138 /* Move the object pointer forward. This is what the module is given. */
139 object_ptr++;
140
141 /* Return this pointer to the application. */
142 *((VOID **) object_ptr_ptr) = object_ptr;
143 }
144 }
145 else
146 {
147 /* Set return value to not enabled. */
148 return_value = TX_NOT_AVAILABLE;
149 }
150
151 /* Release the protection mutex. */
152 _txe_mutex_put(&_txm_module_manager_mutex);
153
154 return(return_value);
155 }
156