Grow message by just the needed amount
authorAlexandre Oliva <lxoliva@fsfla.org>
Thu, 6 Mar 2014 16:17:12 +0000 (13:17 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Fri, 7 Mar 2014 22:54:25 +0000 (19:54 -0300)
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

index bfaf1ee..f300d69 100644 (file)
--- a/decfile.c
+++ b/decfile.c
@@ -430,13 +430,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;
        }