]> granicus.if.org Git - neomutt/commitdiff
drop Buffer.destroy
authorRichard Russon <rich@flatcap.org>
Fri, 19 Jul 2019 21:48:51 +0000 (22:48 +0100)
committerRichard Russon <rich@flatcap.org>
Sat, 20 Jul 2019 22:09:27 +0000 (23:09 +0100)
This member controlled whether, when destroyed, a Buffer should delete
its data.  The member was only used in a couple of places, so it seems
better to duplicate the data in those cases and *always* destroy it
afterwards.

hcache/serialize.c
icommands.c
init.c
mutt/buffer.h
test/pattern/extract.c

index f3f525b03d4834010461b371011615fe3d225fb0..a786beec281817a12426c5969d4005aeb4711d78 100644 (file)
@@ -318,7 +318,6 @@ unsigned char *serial_dump_buffer(struct Buffer *b, unsigned char *d, int *off,
   d = serial_dump_char_size(b->data, d, off, b->dsize + 1, convert);
   d = serial_dump_int(b->dptr - b->data, d, off);
   d = serial_dump_int(b->dsize, d, off);
-  d = serial_dump_int(b->destroy, d, off);
 
   return d;
 }
@@ -347,8 +346,6 @@ void serial_restore_buffer(struct Buffer **b, const unsigned char *d, int *off,
   (*b)->dptr = (*b)->data + offset;
   serial_restore_int(&used, d, off);
   (*b)->dsize = used;
-  serial_restore_int(&used, d, off);
-  (*b)->destroy = used;
 }
 
 /**
index 2e1c041665fd15b1d6b0e5dcb824a1eb6f667172..43a78ef9dd6299794b2151d9e190fa92554366ec 100644 (file)
@@ -78,26 +78,22 @@ enum CommandResult mutt_parse_icommand(/* const */ char *line, struct Buffer *er
 
   enum CommandResult rc = MUTT_CMD_ERROR;
 
-  struct Buffer expn, token;
-
-  mutt_buffer_init(&expn);
-  mutt_buffer_init(&token);
-
-  expn.data = expn.dptr = line;
-  expn.dsize = mutt_str_strlen(line);
+  struct Buffer *token = mutt_buffer_pool_get();
+  struct Buffer *expn = mutt_buffer_from(line);
+  expn->dptr = expn->data;
 
   mutt_buffer_reset(err);
 
-  SKIPWS(expn.dptr);
-  while (*expn.dptr)
+  SKIPWS(expn->dptr);
+  while (*expn->dptr != '\0')
   {
-    mutt_extract_token(&token, &expn, MUTT_TOKEN_NO_FLAGS);
+    mutt_extract_token(token, expn, MUTT_TOKEN_NO_FLAGS);
     for (size_t i = 0; ICommandList[i].name; i++)
     {
-      if (mutt_str_strcmp(token.data, ICommandList[i].name) != 0)
+      if (mutt_str_strcmp(token->data, ICommandList[i].name) != 0)
         continue;
 
-      rc = ICommandList[i].func(&token, &expn, ICommandList[i].data, err);
+      rc = ICommandList[i].func(token, expn, ICommandList[i].data, err);
       if (rc != 0)
         goto finish;
 
@@ -106,8 +102,8 @@ enum CommandResult mutt_parse_icommand(/* const */ char *line, struct Buffer *er
   }
 
 finish:
-  if (expn.destroy)
-    FREE(&expn.data);
+  mutt_buffer_pool_release(&token);
+  mutt_buffer_free(&expn);
   return rc;
 }
 
diff --git a/init.c b/init.c
index a5653a409cd7f1a3ec6dce474404155a2c11400a..ff060e5f1b1872c4a2640e189c85959608636af3 100644 (file)
--- a/init.c
+++ b/init.c
@@ -2773,11 +2773,8 @@ int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags
         ptr = mutt_mem_malloc(tok->dsize);
         memcpy(ptr, expn.data, expnlen);
         strcpy(ptr + expnlen, tok->dptr);
-        if (tok->destroy)
-          FREE(&tok->data);
-        tok->data = ptr;
-        tok->dptr = ptr;
-        tok->destroy = 1; /* mark that the caller should destroy this data */
+        tok->data = mutt_str_strdup(ptr);
+        tok->dptr = tok->data;
         ptr = NULL;
         FREE(&expn.data);
       }
@@ -3211,36 +3208,33 @@ int mutt_init(bool skip_sys_rc, struct ListHead *commands)
 enum CommandResult mutt_parse_rc_line(/* const */ char *line,
                                       struct Buffer *token, struct Buffer *err)
 {
-  int i;
-  enum CommandResult rc = MUTT_CMD_SUCCESS;
-  struct Buffer expn;
-
   if (!line || !*line)
     return 0;
 
-  mutt_buffer_init(&expn);
-  expn.data = line;
-  expn.dptr = line;
-  expn.dsize = mutt_str_strlen(line);
+  int i;
+  enum CommandResult rc = MUTT_CMD_SUCCESS;
+
+  struct Buffer *expn = mutt_buffer_from(line);
+  expn->dptr = expn->data;
 
   *err->data = 0;
 
-  SKIPWS(expn.dptr);
-  while (*expn.dptr)
+  SKIPWS(expn->dptr);
+  while (*expn->dptr != '\0')
   {
-    if (*expn.dptr == '#')
+    if (*expn->dptr == '#')
       break; /* rest of line is a comment */
-    if (*expn.dptr == ';')
+    if (*expn->dptr == ';')
     {
-      expn.dptr++;
+      expn->dptr++;
       continue;
     }
-    mutt_extract_token(token, &expn, MUTT_TOKEN_NO_FLAGS);
+    mutt_extract_token(token, expn, MUTT_TOKEN_NO_FLAGS);
     for (i = 0; Commands[i].name; i++)
     {
       if (mutt_str_strcmp(token->data, Commands[i].name) == 0)
       {
-        rc = Commands[i].func(token, &expn, Commands[i].data, err);
+        rc = Commands[i].func(token, expn, Commands[i].data, err);
         if (rc != MUTT_CMD_SUCCESS)
         {              /* -1 Error, +1 Finish */
           goto finish; /* Propagate return code */
@@ -3256,8 +3250,7 @@ enum CommandResult mutt_parse_rc_line(/* const */ char *line,
     }
   }
 finish:
-  if (expn.destroy)
-    FREE(&expn.data);
+  mutt_buffer_free(&expn);
   return rc;
 }
 
index 826af35969c6d113c99c4810c796372b920209f3..e74ac334f8ecea56ebf083626e77ff8c0e85cc02 100644 (file)
@@ -35,7 +35,6 @@ struct Buffer
   char *data;   ///< Pointer to data
   char *dptr;   ///< Current read/write position
   size_t dsize; ///< Length of data
-  int destroy;  ///< Destroy 'data' when done?
 };
 
 /* Convert a buffer to a const char * "string" */
index b49116df421a89f6f90072f4e5e0b924299898d9..23e1c9935ccb31a8913ea0c19004b6b3c97bbf92 100644 (file)
@@ -191,11 +191,8 @@ int mutt_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags
         ptr = mutt_mem_malloc(tok->dsize);
         memcpy(ptr, expn.data, expnlen);
         strcpy(ptr + expnlen, tok->dptr);
-        if (tok->destroy)
-          FREE(&tok->data);
-        tok->data = ptr;
-        tok->dptr = ptr;
-        tok->destroy = 1; /* mark that the caller should destroy this data */
+        tok->data = mutt_str_strdup(ptr);
+        tok->dptr = tok->data;
         ptr = NULL;
         FREE(&expn.data);
       }