Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libedit split the allocation functions, their mixed usag...



details:   https://anonhg.NetBSD.org/src/rev/3da021dda4fc
branches:  trunk
changeset: 806401:3da021dda4fc
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Feb 22 02:16:19 2015 +0000

description:
split the allocation functions, their mixed usage was too confusing.

diffstat:

 lib/libedit/chartype.c |  71 ++++++++++++++++++++++++++++---------------------
 lib/libedit/chartype.h |   8 +++--
 2 files changed, 46 insertions(+), 33 deletions(-)

diffs (147 lines):

diff -r 5934b5daf726 -r 3da021dda4fc lib/libedit/chartype.c
--- a/lib/libedit/chartype.c    Sun Feb 22 00:50:30 2015 +0000
+++ b/lib/libedit/chartype.c    Sun Feb 22 02:16:19 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: chartype.c,v 1.11 2015/02/22 00:46:58 christos Exp $   */
+/*     $NetBSD: chartype.c,v 1.12 2015/02/22 02:16:19 christos Exp $   */
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 #include "config.h"
 #if !defined(lint) && !defined(SCCSID)
-__RCSID("$NetBSD: chartype.c,v 1.11 2015/02/22 00:46:58 christos Exp $");
+__RCSID("$NetBSD: chartype.c,v 1.12 2015/02/22 02:16:19 christos Exp $");
 #endif /* not lint && not SCCSID */
 #include "el.h"
 #include <stdlib.h>
@@ -47,32 +47,44 @@
 
 #ifdef WIDECHAR
 protected int
-ct_conv_buff_resize(ct_buffer_t *conv, size_t mincsize, size_t minwsize)
+ct_conv_cbuff_resize(ct_buffer_t *conv, size_t csize)
 {
        void *p;
-       if (mincsize > conv->csize) {
-               conv->csize = mincsize;
-               p = el_realloc(conv->cbuff, conv->csize * sizeof(*conv->cbuff));
-               if (p == NULL) {
-                       conv->csize = 0;
-                       el_free(conv->cbuff);
-                       conv->cbuff = NULL;
-                       return -1;
-               } else 
-                       conv->cbuff = p;
+
+       if (csize <= conv->csize)
+               return 0;
+
+       conv->csize = csize;
+
+       p = el_realloc(conv->cbuff, conv->csize * sizeof(*conv->cbuff));
+       if (p == NULL) {
+               conv->csize = 0;
+               el_free(conv->cbuff);
+               conv->cbuff = NULL;
+               return -1;
        }
+       conv->cbuff = p;
+       return 0;
+}
 
-       if (minwsize > conv->wsize) {
-               conv->wsize = minwsize;
-               p = el_realloc(conv->wbuff, conv->wsize * sizeof(*conv->wbuff));
-               if (p == NULL) {
-                       conv->wsize = 0;
-                       el_free(conv->wbuff);
-                       conv->wbuff = NULL;
-                       return -1;
-               } else
-                       conv->wbuff = p;
+protected int
+ct_conv_wbuff_resize(ct_buffer_t *conv, size_t wsize)
+{
+       void *p;
+
+       if (wsize <= conv->wsize) 
+               return 0;
+
+       conv->wsize = wsize;
+
+       p = el_realloc(conv->wbuff, conv->wsize * sizeof(*conv->wbuff));
+       if (p == NULL) {
+               conv->wsize = 0;
+               el_free(conv->wbuff);
+               conv->wbuff = NULL;
+               return -1;
        }
+       conv->wbuff = p;
        return 0;
 }
 
@@ -90,8 +102,8 @@
        for (;;) {
                used = (ssize_t)(dst - conv->cbuff);
                if ((conv->csize - (size_t)used) < 5) {
-                       if (ct_conv_buff_resize(conv, conv->csize + CT_BUFSIZ,
-                           (size_t)0) == -1)
+                       if (ct_conv_cbuff_resize(conv,
+                           conv->csize + CT_BUFSIZ) == -1)
                                return NULL;
                        dst = conv->cbuff + used;
                }
@@ -119,8 +131,8 @@
        if (len == (size_t)-1)
                return NULL;
 
-       if (conv->csize < ++len)
-               if (ct_conv_buff_resize(conv, (size_t)0, len + CT_BUFSIZ) == -1)
+       if (conv->wsize < ++len)
+               if (ct_conv_wbuff_resize(conv, len + CT_BUFSIZ) == -1)
                        return NULL;
 
        ct_mbstowcs(conv->wbuff, s, conv->wsize);
@@ -141,9 +153,8 @@
         * the argv strings. */
        for (i = 0, bufspace = 0; i < argc; ++i)
                bufspace += argv[i] ? strlen(argv[i]) + 1 : 0;
-       if (conv->csize < ++bufspace)
-               if (ct_conv_buff_resize(conv, (size_t)0, bufspace + CT_BUFSIZ)
-                   == -1)
+       if (conv->wsize < ++bufspace)
+               if (ct_conv_wbuff_resize(conv, bufspace + CT_BUFSIZ) == -1)
                        return NULL;
 
        wargv = el_malloc((size_t)argc * sizeof(*wargv));
diff -r 5934b5daf726 -r 3da021dda4fc lib/libedit/chartype.h
--- a/lib/libedit/chartype.h    Sun Feb 22 00:50:30 2015 +0000
+++ b/lib/libedit/chartype.h    Sun Feb 22 02:16:19 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: chartype.h,v 1.12 2015/02/22 00:46:58 christos Exp $   */
+/*     $NetBSD: chartype.h,v 1.13 2015/02/22 02:16:19 christos Exp $   */
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -189,7 +189,8 @@
 protected Char **ct_decode_argv(int, const char *[],  ct_buffer_t *);
 
 /* Resizes the conversion buffer(s) if needed. */
-protected int ct_conv_buff_resize(ct_buffer_t *, size_t, size_t);
+protected int ct_conv_cbuff_resize(ct_buffer_t *, size_t);
+protected int ct_conv_wbuff_resize(ct_buffer_t *, size_t);
 protected ssize_t ct_encode_char(char *, size_t, Char);
 protected size_t ct_enc_width(Char);
 
@@ -199,7 +200,8 @@
 #define        ct_encode_string(s, b)  (s)
 #define ct_decode_string(s, b) (s)
 #define ct_decode_argv(l, s, b)        (s)
-#define ct_conv_buff_resize(b, os, ns)
+#define ct_conv_cbuff_resize(b, s) ((s) == (0))
+#define ct_conv_wbuff_resize(b, s) ((s) == (0))
 #define ct_encode_char(d, l, s)        (*d = s, 1)
 #define ct_free_argv(s)
 #endif



Home | Main Index | Thread Index | Old Index