Made requesting instance's allocation count for resource available via the service...
[keystone-rtos/rm-lld.git] / src / rm_allocator.c
index 50beaa526e7e087301b410d7915894d0ce2cb8ed..c18b64502b7888f432a9c2ebaba34bc7989ef140 100644 (file)
@@ -109,7 +109,7 @@ static Rm_Allocator *allocatorAdd(Rm_Handle rmHandle, const char *resourceName)
  *              instance node is in the list of resource node owners.  Otherwise,
  *              returns 0.
  */
-static int allocatorResNodeIsOwnedBy(Rm_Handle rmHandle, Rm_ResourceNode *node, void *serviceInstNode)
+static int allocatorResNodeIsOwnedBy(Rm_Handle rmHandle, Rm_ResourceNode *node, Rm_PolicyValidInstNode *serviceInstNode)
 {
     Rm_Inst  *rmInst = (Rm_Inst *)rmHandle;
     Rm_Owner *owner = node->ownerList;
@@ -128,7 +128,7 @@ static int allocatorResNodeIsOwnedBy(Rm_Handle rmHandle, Rm_ResourceNode *node,
  ***********************************************************************
  * DESCRIPTION: Increments a resource owner's reference count
  */
-static void allocatorResNodeOwnerRefCntInc(Rm_Handle rmHandle, Rm_ResourceNode *node, void *serviceInstNode)
+static void allocatorResNodeOwnerRefCntInc(Rm_Handle rmHandle, Rm_ResourceNode *node, Rm_PolicyValidInstNode *serviceInstNode)
 {
     Rm_Inst  *rmInst = (Rm_Inst *)rmHandle;
     Rm_Owner *owner = node->ownerList;
@@ -148,7 +148,7 @@ static void allocatorResNodeOwnerRefCntInc(Rm_Handle rmHandle, Rm_ResourceNode *
  ***********************************************************************
  * DESCRIPTION: Decrements a resource owner's reference count
  */
-static void allocatorResNodeOwnerRefCntDec(Rm_Handle rmHandle, Rm_ResourceNode *node, void *serviceInstNode)
+static void allocatorResNodeOwnerRefCntDec(Rm_Handle rmHandle, Rm_ResourceNode *node, Rm_PolicyValidInstNode *serviceInstNode)
 {
     Rm_Inst  *rmInst = (Rm_Inst *)rmHandle;
     Rm_Owner *owner = node->ownerList;
@@ -164,13 +164,33 @@ static void allocatorResNodeOwnerRefCntDec(Rm_Handle rmHandle, Rm_ResourceNode *
     }
 }
 
+/* FUNCTION PURPOSE: Returns an owner's refCnt
+ ***********************************************************************
+ * DESCRIPTION: Returns a resource owner's reference count
+ */
+static uint16_t allocatorResNodeOwnerGetRefCnt(Rm_Handle rmHandle, Rm_ResourceNode *node, Rm_PolicyValidInstNode *serviceInstNode)
+{
+    Rm_Inst  *rmInst = (Rm_Inst *)rmHandle;
+    Rm_Owner *owner = node->ownerList;
+
+    while (owner) {
+        RM_SS_OBJ_INV(owner, Rm_Owner);
+        if (owner->instNameNode == serviceInstNode) {
+            return (owner->refCnt);
+        }
+        owner = owner->nextOwner;
+    }
+
+    return(0);
+}
+
 /* FUNCTION PURPOSE: Adds an owner to an allocator resource
  ***********************************************************************
  * DESCRIPTION: Adds a RM instance node to a resource node's
  *              list of owners.  If the owner is already present that
  *              owner's reference count is incremented
  */
-static void allocatorResNodeOwnerAdd(Rm_Handle rmHandle, Rm_ResourceNode *node, void *serviceInstNode)
+static void allocatorResNodeOwnerAdd(Rm_Handle rmHandle, Rm_ResourceNode *node, Rm_PolicyValidInstNode *serviceInstNode)
 {
     Rm_Inst  *rmInst = (Rm_Inst *)rmHandle;    
     Rm_Owner *ownerList = node->ownerList;
@@ -397,7 +417,7 @@ static void allocatorResNodeOwnerClear(Rm_Handle rmHandle, Rm_ResourceNode *node
  *              resource's owner reference count is returned if the 
  *              resource range is found.
  */
-static int32_t allocatorStatus(Rm_Allocator *allocator, Rm_AllocatorOpInfo *opInfo)
+static int32_t allocatorStatus(Rm_Handle rmHandle, Rm_Allocator *allocator, Rm_AllocatorOpInfo *opInfo)
 {
     Rm_ResourceNode  findNode;
     Rm_ResourceNode *matchingNode = NULL; 
@@ -415,6 +435,8 @@ static int32_t allocatorStatus(Rm_Allocator *allocator, Rm_AllocatorOpInfo *opIn
         findEnd = findNode.base + findNode.length - 1;
         if ((findNode.base >= matchingNode->base) && (findEnd <= matchingEnd)) {        
             opInfo->resourceInfo->ownerCount = matchingNode->allocationCount;
+            opInfo->resourceInfo->instAllocCount = allocatorResNodeOwnerGetRefCnt(rmHandle, matchingNode,
+                                                                                  opInfo->serviceSrcInstNode);            
             retVal = RM_SERVICE_APPROVED;
         }
         else {
@@ -768,8 +790,11 @@ static int32_t allocatorAllocate(Rm_Handle rmHandle, Rm_Allocator *allocator, in
                     /* Always reinsert matchingNode */                
                     RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);
                     
-                    /* Matching node contains new reference count after alloc.  Return new owner count. */
+                    /* Matching node contains new reference count after alloc.  Return new owner count
+                     * and originating instance allocation reference count. */
                     opInfo->resourceInfo->ownerCount = matchingNode->allocationCount;
+                    opInfo->resourceInfo->instAllocCount = allocatorResNodeOwnerGetRefCnt(rmHandle, matchingNode,
+                                                                                          opInfo->serviceSrcInstNode);
                 }   
                 else if ((findNode.base > matchingNode->base) && (findEnd < matchingEnd)) {
                     /* findNode range is subset of matchingNode range and neither boundary is
@@ -793,8 +818,11 @@ static int32_t allocatorAllocate(Rm_Handle rmHandle, Rm_Allocator *allocator, in
                     RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);
                     RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);
                     
-                    /* Matching node contains new reference count after alloc.  Return new owner count. */
+                    /* Matching node contains new reference count after alloc.  Return new owner count
+                     * and originating instance allocation reference count. */
                     opInfo->resourceInfo->ownerCount = matchingNode->allocationCount;
+                    opInfo->resourceInfo->instAllocCount = allocatorResNodeOwnerGetRefCnt(rmHandle, matchingNode, 
+                                                                                          opInfo->serviceSrcInstNode);
                 }  
                 else {    
                     if (findNode.base == matchingNode->base) {
@@ -825,8 +853,11 @@ static int32_t allocatorAllocate(Rm_Handle rmHandle, Rm_Allocator *allocator, in
                         matchingNode->length = matchingEnd - findEnd;  
 
                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);
-                        /* Left node contains new reference count after alloc.  Return new owner count. */
+                        /* Left node contains new reference count after alloc.  Return new owner count
+                         * and originating instance allocation reference count. */
                         opInfo->resourceInfo->ownerCount = leftNode->allocationCount;
+                        opInfo->resourceInfo->instAllocCount = allocatorResNodeOwnerGetRefCnt(rmHandle, leftNode, 
+                                                                                              opInfo->serviceSrcInstNode);
                     }
                     else if (findEnd == matchingEnd) {
                         /* findNode end and matchingNode end are equivalent.  May be combine
@@ -856,8 +887,11 @@ static int32_t allocatorAllocate(Rm_Handle rmHandle, Rm_Allocator *allocator, in
                         matchingNode->length -= findNode.length;  
 
                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);
-                        /* Right node contains new reference count after alloc.  Return new owner count. */
+                        /* Right node contains new reference count after alloc.  Return new owner count
+                         * and originating instance allocation reference count. */
                         opInfo->resourceInfo->ownerCount = rightNode->allocationCount;
+                        opInfo->resourceInfo->instAllocCount = allocatorResNodeOwnerGetRefCnt(rmHandle, rightNode, 
+                                                                                              opInfo->serviceSrcInstNode);
                     }
                     /* Remove allocating instance from leftover matchingNode */
                     allocatorResNodeOwnerDelete(rmHandle, matchingNode, opInfo->serviceSrcInstNode);
@@ -981,8 +1015,11 @@ static int32_t allocatorFree(Rm_Handle rmHandle, Rm_Allocator *allocator, Rm_All
                         /* Always reinsert matchingNode */
                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);
                         
-                        /* Matching node is what remains after free.  Return remaining owner count. */
+                        /* Matching node is what remains after free.  Return remaining owner count
+                         * and originating instance allocation reference count. */
                         opInfo->resourceInfo->ownerCount = matchingNode->allocationCount;
+                        opInfo->resourceInfo->instAllocCount = allocatorResNodeOwnerGetRefCnt(rmHandle, matchingNode,
+                                                                                              opInfo->serviceSrcInstNode);
                     }
                     else if ((findNode.base > matchingNode->base) && (findEnd < matchingEnd)) {
                         /* Case 2: Free range is less than range in matched node. Split
@@ -1011,8 +1048,11 @@ static int32_t allocatorFree(Rm_Handle rmHandle, Rm_Allocator *allocator, Rm_All
                         matchingNode->length = findNode.length;
                         RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, matchingNode);
                         
-                        /* Matching node is what remains after free.  Return remaining owner count. */
+                        /* Matching node is what remains after free.  Return remaining owner count
+                         * and originating instance allocation reference count. */
                         opInfo->resourceInfo->ownerCount = matchingNode->allocationCount;
+                        opInfo->resourceInfo->instAllocCount = allocatorResNodeOwnerGetRefCnt(rmHandle, matchingNode,
+                                                                                              opInfo->serviceSrcInstNode);
                     }
                     else {                        
                         if (findNode.base == matchingNode->base) {
@@ -1044,8 +1084,11 @@ static int32_t allocatorFree(Rm_Handle rmHandle, Rm_Allocator *allocator, Rm_All
                             matchingNode->length = matchingEnd - findEnd;  
                             RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, leftNode);
                             
-                            /* Left node is what remains after free.  Return remaining owner count. */
+                            /* Left node is what remains after free.  Return remaining owner count
+                             * and originating instance allocation reference count. */
                             opInfo->resourceInfo->ownerCount = leftNode->allocationCount;
+                            opInfo->resourceInfo->instAllocCount = allocatorResNodeOwnerGetRefCnt(rmHandle, leftNode,
+                                                                                                  opInfo->serviceSrcInstNode);
                         }
                         else if (findEnd == matchingEnd) {
                             /* Case 4: Free range is on right boundary of matched node. Try to 
@@ -1076,8 +1119,11 @@ static int32_t allocatorFree(Rm_Handle rmHandle, Rm_Allocator *allocator, Rm_All
                             matchingNode->length -= findNode.length;  
                             RB_INSERT(_Rm_AllocatorResourceTree, allocator->allocatorRootEntry, rightNode);
                             
-                            /* Right node is what remains after free.  Return remaining owner count. */
+                            /* Right node is what remains after free.  Return remaining owner count
+                             * and originating instance allocation reference count. */
                             opInfo->resourceInfo->ownerCount = rightNode->allocationCount;
+                            opInfo->resourceInfo->instAllocCount = allocatorResNodeOwnerGetRefCnt(rmHandle, rightNode,
+                                                                                                  opInfo->serviceSrcInstNode);
                         }
 
                         /* Add freeing instance back into matchingNode allocations */
@@ -1087,14 +1133,20 @@ static int32_t allocatorFree(Rm_Handle rmHandle, Rm_Allocator *allocator, Rm_All
                     retVal = RM_SERVICE_APPROVED;
                 }
                 else {
-                    /* Return owner count.  In case it's a reference count check in application */
+                    /* Return owner count and instance alloc count.  In case it's a reference count
+                     * check in application */
                     opInfo->resourceInfo->ownerCount = matchingNode->allocationCount;
+                    opInfo->resourceInfo->instAllocCount = allocatorResNodeOwnerGetRefCnt(rmHandle, matchingNode,
+                                                                                          opInfo->serviceSrcInstNode);
                     retVal = RM_SERVICE_DENIED_RES_NOT_ALLOCD_TO_INST;
                 }
             }
             else {
-                /* Return owner count.  In case it's a reference count check in application */
+                /* Return owner count and instance alloc count.  In case it's a reference count
+                 * check in application */
                 opInfo->resourceInfo->ownerCount = matchingNode->allocationCount;
+                opInfo->resourceInfo->instAllocCount = allocatorResNodeOwnerGetRefCnt(rmHandle, matchingNode,
+                                                                                      opInfo->serviceSrcInstNode);
                 retVal = RM_SERVICE_DENIED_RES_ALREADY_FREE;
             }
         }
@@ -1497,7 +1549,7 @@ int32_t rmAllocatorOperation(Rm_Handle rmHandle, Rm_AllocatorOpInfo *opInfo)
         }
 
         if (opInfo->operation == Rm_allocatorOp_GET_STATUS) {
-            retVal = allocatorStatus(allocator, opInfo);
+            retVal = allocatorStatus(rmHandle, allocator, opInfo);
         }
         else if ((opInfo->operation == Rm_allocatorOp_PRE_ALLOCATE_INIT) ||
                  (opInfo->operation == Rm_allocatorOp_PRE_ALLOCATE_USE)) {