Coverity fixes
[keystone-rtos/rm-lld.git] / src / rm_allocator.c
index 59d44c9ff302ef16058c327058ebd110753d5cb9..23ce8afec41bc8b6c96dd755b6f1b40abfce77fb 100644 (file)
@@ -303,18 +303,20 @@ static void allocatorResNodeOwnerDelete(Rm_Handle rmHandle, Rm_ResourceNode *nod
         owner = owner->nextOwner;
     }
 
-    if (prevOwner == NULL) {
-        node->ownerList = owner->nextOwner;
-    }
-    else {
-        prevOwner->nextOwner = owner->nextOwner;
-        RM_SS_OBJ_WB(prevOwner, Rm_Owner);
+    if (owner) {
+        if (prevOwner == NULL) {
+            node->ownerList = owner->nextOwner;
+        }
+        else {
+            prevOwner->nextOwner = owner->nextOwner;
+            RM_SS_OBJ_WB(prevOwner, Rm_Owner);
+        }
+        
+        node->allocationCount--;
+        owner->instNameNode->allocRefCount--;
+        RM_SS_OBJ_WB(owner->instNameNode, Rm_PolicyValidInstNode);
+        Rm_osalFree((void *)owner, sizeof(*owner));
     }
-    
-    node->allocationCount--;
-    owner->instNameNode->allocRefCount--;
-    RM_SS_OBJ_WB(owner->instNameNode, Rm_PolicyValidInstNode);
-    Rm_osalFree((void *)owner, sizeof(*owner));
 }
 
 /* FUNCTION PURPOSE: Copies the owners of a resource node
@@ -421,8 +423,19 @@ static int32_t allocatorPreAllocate(Rm_Handle rmHandle, Rm_Allocator *allocator,
     int                nodePassesPolicy;
     int32_t            retVal;
 
+    if (opInfo->operation == Rm_allocatorOp_PRE_ALLOCATE_INIT) {
+        policyCheckType = Rm_policyCheck_INIT;
+    }
+    else if (opInfo->operation == Rm_allocatorOp_PRE_ALLOCATE_USE) {
+        policyCheckType = Rm_policyCheck_USE;
+    }
+    else {
+        retVal = RM_ERROR_INVALID_SERVICE_TYPE;
+        return (retVal);
+    }
+    
     opInfo->resourceInfo->base = rmPolicyGetResourceBase(opInfo->policy, opInfo->serviceSrcInstNode, 
-                                                         resourcePolicy, opInfo->allocType, 
+                                                         resourcePolicy, policyCheckType, 
                                                          &retVal);
     if (retVal != RM_OK) {
         return (retVal);
@@ -443,12 +456,6 @@ static int32_t allocatorPreAllocate(Rm_Handle rmHandle, Rm_Allocator *allocator,
 
     /* Configure policy checking structure */
     memset((void *)&policyCheckCfg, 0, sizeof(policyCheckCfg));
-    if (RM_policy_GET_PERM(opInfo->allocType, RM_POLICY_PERM_INIT_SHIFT)) {
-        policyCheckType = Rm_policyCheck_INIT;
-    }
-    else if (RM_policy_GET_PERM(opInfo->allocType, RM_POLICY_PERM_USE_SHIFT)) {
-        policyCheckType = Rm_policyCheck_USE;
-    }
     policyCheckCfg.policyDtb = opInfo->policy;
     policyCheckCfg.resourceOffset = resourcePolicy;
     
@@ -554,19 +561,24 @@ static int32_t allocatorAllocate(Rm_Handle rmHandle, Rm_Allocator *allocator, in
     uint32_t            matchingEnd;  
     int32_t             retVal;
 
+    if (opInfo->operation == Rm_allocatorOp_ALLOCATE_INIT) {
+        policyCheckType = Rm_policyCheck_INIT;
+    }
+    else if (opInfo->operation == Rm_allocatorOp_ALLOCATE_USE) {
+        policyCheckType = Rm_policyCheck_USE;
+    }
+    else {
+        retVal = RM_ERROR_INVALID_SERVICE_TYPE;
+        return (retVal);
+    }   
+
     memset((void *)&findNode, 0, sizeof(findNode));
     findNode.base = opInfo->resourceInfo->base;
     findNode.length = opInfo->resourceInfo->length;
     matchingNode = RB_FIND(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, &findNode);
 
     /* Prepare privilege checks */
-    memset((void *)&policyCheckCfg, 0, sizeof(policyCheckCfg));
-    if (RM_policy_GET_PERM(opInfo->allocType, RM_POLICY_PERM_INIT_SHIFT)) {
-        policyCheckType = Rm_policyCheck_INIT;
-    }
-    else if (RM_policy_GET_PERM(opInfo->allocType, RM_POLICY_PERM_USE_SHIFT)) {
-        policyCheckType = Rm_policyCheck_USE;
-    }
+    memset((void *)&policyCheckCfg, 0, sizeof(policyCheckCfg)); 
 
     if (matchingNode) {
         findEnd = findNode.base + findNode.length - 1;
@@ -671,7 +683,9 @@ static int32_t allocatorAllocate(Rm_Handle rmHandle, Rm_Allocator *allocator, in
 
                             allocatorResNodeOwnerClear(rmHandle, leftNode);
                             rmResourceNodeFree(leftNode);
-                            RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);                        
+                            if (rightNode) {
+                                RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);  
+                            }
                         }
                         else if (combineRight) {
                             /* Combine right and matching nodes.  Reinsert left. */
@@ -679,12 +693,18 @@ static int32_t allocatorAllocate(Rm_Handle rmHandle, Rm_Allocator *allocator, in
 
                             allocatorResNodeOwnerClear(rmHandle, rightNode);
                             rmResourceNodeFree(rightNode);
-                            RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);
+                            if (leftNode) {
+                                RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);
+                            }
                         }
                         else {
                             /* No combine. */
-                            RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);
-                            RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);
+                            if (leftNode) {
+                                RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);
+                            }
+                            if (rightNode) {
+                                RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);
+                            }
                         }
 
                         /* Always reinsert matchingNode */                
@@ -873,7 +893,9 @@ static int32_t allocatorFree(Rm_Handle rmHandle, Rm_Allocator *allocator, Rm_All
 
                             allocatorResNodeOwnerClear(rmHandle, leftNode);
                             rmResourceNodeFree(leftNode);
-                            RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);                        
+                            if (rightNode) {
+                                RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode); 
+                            }
                         }
                         else if (combineRight) {
                             /* Combine right and matching nodes.  Reinsert left. */
@@ -881,12 +903,18 @@ static int32_t allocatorFree(Rm_Handle rmHandle, Rm_Allocator *allocator, Rm_All
 
                             allocatorResNodeOwnerClear(rmHandle, rightNode);
                             rmResourceNodeFree(rightNode);
-                            RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);
+                            if (leftNode) {
+                                RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);
+                            }
                         }
                         else {
                             /* No combine. */
-                            RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);
-                            RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);
+                            if (leftNode) {
+                                RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);
+                            }
+                            if (rightNode) {
+                                RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);
+                            }
                         }
 
                         /* Always reinsert matchingNode */
@@ -1082,7 +1110,7 @@ static int32_t allocatorFindLinuxResource(Rm_Handle rmHandle, const char *resour
     int32_t             propOffset;
     int32_t             nodeOffset = RM_DTB_UTIL_STARTING_NODE_OFFSET;
     int32_t             prevDepth = RM_DTB_UTIL_STARTING_DEPTH;
-    int32_t             depth;
+    int32_t             depth = RM_DTB_UTIL_STARTING_DEPTH;
     int32_t             propertyLen;
     const char         *propertyName;
     const void         *propertyData; 
@@ -1095,7 +1123,7 @@ static int32_t allocatorFindLinuxResource(Rm_Handle rmHandle, const char *resour
     strncpy(resourceInfo.name, resourceName, RM_NAME_MAX_CHARS);
     opInfo.policy = rmInst->u.server.globalPolicy;
     opInfo.serviceSrcInstNode = rmPolicyGetLinuxInstNode(rmHandle);
-    opInfo.operation = Rm_allocatorOp_ALLOCATE;
+    opInfo.operation = Rm_allocatorOp_ALLOCATE_INIT;
     opInfo.resourceInfo = &resourceInfo;    
 
     while(linuxAlias) {
@@ -1285,10 +1313,12 @@ int32_t rmAllocatorOperation(Rm_Handle rmHandle, Rm_AllocatorOpInfo *opInfo)
             rmResourceTreeInv(allocator->allocatorRootEntry);
         }
         
-        if (opInfo->operation == Rm_allocatorOp_PRE_ALLOCATE) {
+        if ((opInfo->operation == Rm_allocatorOp_PRE_ALLOCATE_INIT) ||
+            (opInfo->operation == Rm_allocatorOp_PRE_ALLOCATE_USE)) {
             retVal = allocatorPreAllocate(rmHandle, allocator, resourceOffsetInPolicy, opInfo);
         }               
-        else if (opInfo->operation == Rm_allocatorOp_ALLOCATE) {
+        else if ((opInfo->operation == Rm_allocatorOp_ALLOCATE_INIT) ||
+                 (opInfo->operation == Rm_allocatorOp_ALLOCATE_USE)) {
             retVal = allocatorAllocate(rmHandle, allocator, resourceOffsetInPolicy, opInfo);
         }
         else if (opInfo->operation == Rm_allocatorOp_FREE) {