7f2d78d866ceb57be2ad0d9bb0e3fcdd6ac615bc
1 /**
2 * @file rm_tree.c
3 *
4 * @brief
5 * Resource Manager Tree Manipulation Source.
6 *
7 * \par
8 * ============================================================================
9 * @n (C) Copyright 2012-2013, Texas Instruments, Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the
21 * distribution.
22 *
23 * Neither the name of Texas Instruments Incorporated nor the names of
24 * its contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 * \par
40 */
42 /* Standard includes */
43 #include <string.h>
44 #include <stdbool.h>
46 /* RM external API includes */
47 #include <ti/drv/rm/rm.h>
49 /* RM internal API includes */
50 #include <ti/drv/rm/include/rm_treeloc.h>
52 /* RM OSAL layer */
53 #include <rm_osal.h>
55 /**********************************************************************
56 ********************* NameServer Tree Functions **********************
57 **********************************************************************/
59 Rm_NameServerNode *rmNameServerNodeNew(Rm_NameServerNodeCfg *nodeCfg)
60 {
61 Rm_NameServerNode *newNode = NULL;
63 newNode = Rm_osalMalloc(sizeof(Rm_NameServerNode));
64 if (newNode) {
65 strncpy(newNode->objName, nodeCfg->objName, RM_NAME_MAX_CHARS);
66 strncpy(newNode->resourceName, nodeCfg->resourceName, RM_NAME_MAX_CHARS);
67 newNode->resourceBase = nodeCfg->resourceBase;
68 newNode->resourceLength = nodeCfg->resourceLength;
69 }
71 return(newNode);
72 }
74 void rmNameServerNodeFree(Rm_NameServerNode *node)
75 {
76 if (node) {
77 Rm_osalFree((void *)node, sizeof(Rm_NameServerNode));
78 }
79 }
81 /* Prototype for NameServer node comparison function
82 * node1 < node2 --> return < 0
83 * node1 = node2 --> return 0
84 * node1 > node2 --> return > 0 */
85 int rmNameServerNodeCompare(Rm_NameServerNode *node1, Rm_NameServerNode *node2)
86 {
87 return(strcmp(node1->objName, node2->objName));
88 }
90 /* Generate the NameServer tree manipulation functions */
91 RB_GENERATE(_Rm_NameServerTree, _Rm_NameServerNode, linkage, rmNameServerNodeCompare);
93 /**********************************************************************
94 *************** Policy Valid Instance Tree Functions *****************
95 **********************************************************************/
96 Rm_PolicyValidInstNode *rmPolicyValidInstNodeNew(char *instName)
97 {
98 Rm_PolicyValidInstNode *newNode = NULL;
100 newNode = Rm_osalMalloc(sizeof(Rm_PolicyValidInstNode));
102 strncpy(newNode->name, instName, RM_NAME_MAX_CHARS);
103 newNode->allocRefCount = 0;
104 newNode->deletePending = false;
106 return(newNode);
107 }
109 void rmPolicyValidInstNodeFree(Rm_PolicyValidInstNode *node)
110 {
111 if (node->allocRefCount == 0) {
112 Rm_osalFree((void *)node, sizeof(Rm_PolicyValidInstNode));
113 }
114 }
116 int rmPolicyValidInstNodeCompare(Rm_PolicyValidInstNode *node1, Rm_PolicyValidInstNode *node2)
117 {
118 return(strcmp(node1->name, node2->name));
119 }
121 /* Generate the valid instance tree manipulation functions */
122 RB_GENERATE(_Rm_PolicyValidInstTree, _Rm_PolicyValidInstNode, linkage, rmPolicyValidInstNodeCompare);
124 /**********************************************************************
125 ***************** Allocator Resource Tree Functions ******************
126 **********************************************************************/
128 Rm_ResourceNode *rmResourceNodeNew(uint32_t resourceBase, uint32_t resourceLength)
129 {
130 Rm_ResourceNode *newNode = NULL;
132 newNode = Rm_osalMalloc(sizeof(Rm_ResourceNode));
133 memset((void *)newNode, 0, sizeof(Rm_ResourceNode));
135 newNode->base = resourceBase;
136 newNode->length = resourceLength;
137 newNode->allocationCount = 0;
138 newNode->ownerList = NULL;
140 return(newNode);
141 }
143 void rmResourceNodeFree(Rm_ResourceNode *node)
144 {
145 if (node->allocationCount == 0) {
146 Rm_osalFree((void *)node, sizeof(Rm_ResourceNode));
147 }
148 }
150 int rmResourceNodeCompare(Rm_ResourceNode *node1, Rm_ResourceNode *node2)
151 {
152 uint32_t node1End = node1->base + node1->length - 1;
153 uint32_t node2End = node2->base + node2->length - 1;
155 if (node1End < node2->base) {
156 /* End of node1 range is less than the start of node2's range. Return a negative
157 * value */
158 return (-1);
159 }
160 else if (node1->base > node2End) {
161 /* Start of node1 range is after end of node2's range. Return a positive value */
162 return (1);
163 }
164 else {
165 /* If neither of the latter conditions were satisfied there is some overlap between
166 * node1 and node2. Return 0 since the application must handle this overlap. */
167 return (0);
168 }
169 }
171 /* Generate the red-black tree manipulation functions */
172 RB_GENERATE(_Rm_AllocatorResourceTree, _Rm_ResourceNode, linkage, rmResourceNodeCompare)