xiaoxiang781216 commented on code in PR #20583:
URL: https://github.com/apache/nuttx/pull/20583#discussion_r4301350676
##########
libs/libc/environ/env_putenv.c:
##########
@@ -93,12 +95,10 @@ int putenv(FAR const char *string)
ret = setenv(pname, pequal + 1, TRUE);
Review Comment:
let's create a new internal api support setenv by pass key_len and than
remove line 80 and remove env_malloc:
https://github.com/apache/nuttx/pull/20583/changes#diff-202cd5c752a566ec62b3c5f2377a5bf628263aaf2f9f6fa61b124f0d86a1d2d6R55
to avoid the user confuse between env_alloc and env_malloc.
##########
libs/libc/environ/env_release.c:
##########
@@ -61,33 +55,26 @@
*
****************************************************************************/
-void env_release(FAR struct task_group_s *group)
+void env_release(FAR struct task_info_s *info)
{
- int i;
+ FAR struct env_block_s *block;
- DEBUGASSERT(group != NULL);
-
- if (group->tg_envp)
- {
- /* Free any allocate environment strings */
+ DEBUGASSERT(info != NULL);
- for (i = 0; group->tg_envp[i] != NULL; i++)
- {
- group_free(group, group->tg_envp[i]);
- }
+ /* Free every block that libc allocated for the environment */
- /* Free the environment */
-
- group_free(group, group->tg_envp);
+ while ((block = info->ta_envmem) != NULL)
+ {
+ info->ta_envmem = block->flink;
+ env_free(info, block);
Review Comment:
let's call env_drop, remove line 68 and remove env_free at:
https://github.com/apache/nuttx/pull/20583/changes#diff-202cd5c752a566ec62b3c5f2377a5bf628263aaf2f9f6fa61b124f0d86a1d2d6R57
to avoid confuse which one (env_drop and env_free) should be invoked.
BTW, let's move env_block_s to env_alloc.c since no other files reference
it's field directly,
##########
libs/libc/environ/env_alloc.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * libs/libc/environ/env_alloc.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+
+#include <nuttx/tls.h>
+
+#include "environ/environ.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: env_alloc
+ ****************************************************************************/
+
+FAR void *env_alloc(FAR struct task_info_s *info, size_t size)
+{
+ FAR struct env_block_s *block;
+
+ block = env_malloc(info, sizeof(*block) + size);
+ if (block == NULL)
+ {
+ return NULL;
+ }
+
+ block->flink = info->ta_envmem;
+ info->ta_envmem = block;
+ return block + 1;
+}
+
+/****************************************************************************
+ * Name: env_drop
+ ****************************************************************************/
+
+void env_drop(FAR struct task_info_s *info, FAR void *mem)
+{
+ FAR struct env_block_s *block = (FAR struct env_block_s *)mem - 1;
+ FAR struct env_block_s **prev;
+
+ for (prev = (FAR struct env_block_s **)&info->ta_envmem;
+ *prev != NULL;
+ prev = &(*prev)->flink)
+ {
+ if (*prev == block)
+ {
+ *prev = block->flink;
+ env_free(info, block);
+ return;
+ }
+ }
+
+ DEBUGPANIC();
+}
+
+/****************************************************************************
+ * Name: env_reserve
+ ****************************************************************************/
+
+int env_reserve(FAR struct task_info_s *info, ssize_t more)
Review Comment:
change `ssize_t more` to `size_t more`
##########
libs/libc/environ/env_alloc.c:
##########
@@ -0,0 +1,140 @@
+/****************************************************************************
+ * libs/libc/environ/env_alloc.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+
+#include <nuttx/tls.h>
+
+#include "environ/environ.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: env_alloc
+ ****************************************************************************/
+
+FAR void *env_alloc(FAR struct task_info_s *info, size_t size)
+{
+ FAR struct env_block_s *block;
+
+ block = env_malloc(info, sizeof(*block) + size);
+ if (block == NULL)
+ {
+ return NULL;
+ }
+
+ block->flink = info->ta_envmem;
+ info->ta_envmem = block;
+ return block + 1;
+}
+
+/****************************************************************************
+ * Name: env_drop
+ ****************************************************************************/
+
+void env_drop(FAR struct task_info_s *info, FAR void *mem)
+{
+ FAR struct env_block_s *block = (FAR struct env_block_s *)mem - 1;
+ FAR struct env_block_s **prev;
+
+ for (prev = (FAR struct env_block_s **)&info->ta_envmem;
+ *prev != NULL;
+ prev = &(*prev)->flink)
+ {
+ if (*prev == block)
+ {
+ *prev = block->flink;
+ env_free(info, block);
+ return;
+ }
+ }
+
+ DEBUGPANIC();
+}
+
+/****************************************************************************
+ * Name: env_reserve
+ ****************************************************************************/
+
+int env_reserve(FAR struct task_info_s *info, ssize_t more)
+{
+ FAR char **envp = info->ta_envp;
+ FAR char **arr;
+ ssize_t envc = 0;
+ ssize_t envpc;
Review Comment:
all ssize_t to size_t
##########
libs/libc/environ/environ.h:
##########
@@ -102,6 +112,61 @@ extern "C"
ssize_t env_findvar(FAR struct task_info_s *info, FAR const char *pname);
+/****************************************************************************
+ * Name: env_alloc
+ *
+ * Description:
+ * Allocate a block for the environment of a task. The block goes on a
+ * list of the task, and env_release() frees it when the task exits.
+ * libc does not free the strings before then, so a pointer that getenv()
+ * returned stays valid, and the program can keep a string or an array
+ * that it took from environ.
+ *
+ * Input Parameters:
+ * info - The task that owns the environment.
+ * size - The size of the block.
+ *
+ * Returned Value:
+ * The block, or NULL if there is no memory.
+ *
+ ****************************************************************************/
+
+FAR void *env_alloc(FAR struct task_info_s *info, size_t size);
+
+/****************************************************************************
+ * Name: env_drop
+ *
+ * Description:
+ * Free a block from env_alloc() before the task exits. Only for a block
+ * that nothing else can reach.
+ *
+ ****************************************************************************/
+
+void env_drop(FAR struct task_info_s *info, FAR void *mem);
Review Comment:
change to env_free
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]