[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 2/2] Reduce memory allocations in pk_cmd_exec
|
From: |
Tim Rühsen |
|
Subject: |
[PATCH 2/2] Reduce memory allocations in pk_cmd_exec |
|
Date: |
Thu, 2 Apr 2020 13:27:59 +0200 |
2020-04-02 Tim Rühsen <address@hidden>
* src/pk-cmd.c (pk_cmd_exec): Reduce memory allocations.
---
ChangeLog | 4 ++++
src/pk-cmd.c | 15 +++++++++------
2 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 20185502..841e8724 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2020-04-02 Tim Rühsen <address@hidden>
+
+ * src/pk-cmd.c (pk_cmd_exec): Reduce memory allocations.
+
2020-04-02 Tim Rühsen <address@hidden>
* src/pkl-parser.c (pkl_parse_buffer): Add const to buffer param.
diff --git a/src/pk-cmd.c b/src/pk-cmd.c
index d5a0d1cf..3682bd7b 100644
--- a/src/pk-cmd.c
+++ b/src/pk-cmd.c
@@ -617,14 +617,11 @@ pk_cmd_exec (char *str)
return pk_cmd_exec_1 (cmd + 1, cmds_trie, NULL);
else
{
- char *ecmd, *end;
+ char *ecmd = cmd, *end;
pvm_val val;
int what; /* 0 -> declaration, 1 -> statement */
int retval = 1;
- ecmd = xmalloc (strlen (cmd) + 2);
- strcpy (ecmd, cmd);
-
if (strncmp (ecmd, "defun ", 6) == 0
|| strncmp (ecmd, "defun\t", 6) == 0)
what = 0;
@@ -643,7 +640,12 @@ pk_cmd_exec (char *str)
if (strncmp (ecmd, "defun ", 6) != 0
&& strncmp (ecmd, "defun\t", 6) != 0)
- strcat (ecmd, ";");
+ {
+ size_t len = strlen (cmd);
+ ecmd = xmalloc (len + 2);
+ memcpy (ecmd, cmd, len);
+ memcpy (ecmd + len, ";", 2); /* incl. trailing 0 */
+ }
if (what == 0)
{
@@ -674,7 +676,8 @@ pk_cmd_exec (char *str)
}
cleanup:
- free(ecmd);
+ if (ecmd != cmd)
+ free (ecmd);
return retval;
}
}
--
2.26.0