[PATCH 4/3] Grow message by just the needed amount

Alexandre Oliva lxoliva en fsfla.org
Jue Mar 6 16:17:12 UTC 2014


Do not allocate more space than required for the message to grow.
We used to nearly double the amount of space every time, which is
nice to avoid quadratic complexity, but files are small enough
that it probably doesn't matter much, and it just makes things
confusing.
---
 decfile.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/decfile.c b/decfile.c
index b6f9e93..46c8984 100644
--- a/decfile.c
+++ b/decfile.c
@@ -331,13 +331,15 @@ static int append_stripped_reg_ctrl(struct rnet_message **message, char *line)
 {
 	size_t len;
 	struct rnet_message *msg = *message;
+	int growth;
 	if (!decfile_reg_is_dec(line))
 		return 0;
 	len = strlen(line);
 	if (len < 12)
 		return -EINVAL;
-	if (msg->alen - msg->len < len) {
-		if (rnet_message_expand(message, MAX(msg->len, len)))
+	growth = msg->len + len - 10 - msg->alen;
+	if (growth > 0) {
+		if (rnet_message_expand(message, growth))
 			return -ENOMEM;
 		msg = *message;
 	}

-- 
Alexandre Oliva, freedom fighter    http://FSFLA.org/~lxoliva/
You must be the change you wish to see in the world. -- Gandhi
Be Free! -- http://FSFLA.org/   FSF Latin America board member
Free Software Evangelist     Red Hat Brazil Toolchain Engineer


Más información sobre la lista de distribución Softwares-impostos