pussuw commented on code in PR #9030:
URL: https://github.com/apache/nuttx/pull/9030#discussion_r1169738431


##########
sched/addrenv/addrenv.c:
##########
@@ -237,62 +208,48 @@ int addrenv_switch(FAR struct tcb_s *tcb)
  *   Allocate an address environment for a new process.
  *
  * Input Parameters:
- *   tcb   - The tcb of the newly created task.
- *   ttype - The type of the task.
+ *   None
  *
  * Returned Value:
- *   This is a NuttX internal function so it follows the convention that
- *   0 (OK) is returned on success and a negated errno is returned on
- *   failure.
+ *   Pointer to the new address environment, or NULL if out of memory.
  *
  ****************************************************************************/
 
-int addrenv_allocate(FAR struct tcb_s *tcb, uint8_t ttype)
+FAR struct addrenv_s *addrenv_allocate(void)
 {
-  int ret = OK;
+  FAR struct addrenv_s *addrenv;
 
-  if ((ttype & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_KERNEL)
+  addrenv = (FAR struct addrenv_s *)kmm_zalloc(sizeof(struct addrenv_s));
+  if (addrenv)
     {
-      tcb->addrenv_own = NULL;
-    }
-  else
-    {
-      tcb->addrenv_own = (FAR struct addrenv_s *)
-        kmm_zalloc(sizeof(struct addrenv_s));
-      if (tcb->addrenv_own == NULL)
-        {
-          ret = -ENOMEM;
-        }
+      /* Take reference so this won't get freed */
+
+      addrenv->refs = 1;
     }
 
-  return ret;
+  return addrenv;
 }
 
 /****************************************************************************
  * Name: addrenv_free
  *
  * Description:
- *   Free an address environment for a process.
+ *   Unconditionally free an address environment.

Review Comment:
   Here "undonditionally" means "do not check for holders / references". The 
struct addrenv_s has a reference counter that protects it from being freed if 
someone is using it.
   
   This function is intended to handle special cases where no one is for SURE 
not yet using it, e.g. inside elf_addrenv_alloc. This function is called to 
simply free the kmalloc'd memory obtained by addrenv_allocate().
   
   The "if (addrenv)" test is simply a sanity check, so if someone gives a NULL 
pointer here the system won't crash.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to