Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libedit eliminate static buffer with custom resizing code.
details: https://anonhg.NetBSD.org/src/rev/a92f73e30ead
branches: trunk
changeset: 345014:a92f73e30ead
user: christos <christos%NetBSD.org@localhost>
date: Mon May 02 16:48:34 2016 +0000
description:
eliminate static buffer with custom resizing code.
diffstat:
lib/libedit/chartype.c | 69 ++++++++++++++++++++++---------------------------
lib/libedit/chartype.h | 4 +-
lib/libedit/el.c | 6 ++-
lib/libedit/el.h | 3 +-
lib/libedit/terminal.c | 8 ++--
5 files changed, 43 insertions(+), 47 deletions(-)
diffs (190 lines):
diff -r b18bf5dd4f39 -r a92f73e30ead lib/libedit/chartype.c
--- a/lib/libedit/chartype.c Mon May 02 16:35:17 2016 +0000
+++ b/lib/libedit/chartype.c Mon May 02 16:48:34 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: chartype.c,v 1.28 2016/04/11 18:56:31 christos Exp $ */
+/* $NetBSD: chartype.c,v 1.29 2016/05/02 16:48:34 christos Exp $ */
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
*/
#include "config.h"
#if !defined(lint) && !defined(SCCSID)
-__RCSID("$NetBSD: chartype.c,v 1.28 2016/04/11 18:56:31 christos Exp $");
+__RCSID("$NetBSD: chartype.c,v 1.29 2016/05/02 16:48:34 christos Exp $");
#endif /* not lint && not SCCSID */
#include <ctype.h>
@@ -211,51 +211,44 @@
}
protected const wchar_t *
-ct_visual_string(const wchar_t *s)
+ct_visual_string(const wchar_t *s, ct_buffer_t *conv)
{
- static wchar_t *buff = NULL;
- static size_t buffsize = 0;
- void *p;
wchar_t *dst;
- ssize_t used = 0;
+ ssize_t used;
if (!s)
return NULL;
- if (!buff) {
- buffsize = CT_BUFSIZ;
- buff = el_malloc(buffsize * sizeof(*buff));
- }
- dst = buff;
+
+ if (ct_conv_wbuff_resize(conv, CT_BUFSIZ) == -1)
+ return NULL;
+
+ used = 0;
+ dst = conv->wbuff;
while (*s) {
- used = ct_visual_char(dst, buffsize - (size_t)(dst - buff), *s);
- if (used == -1) { /* failed to encode, need more buffer space */
- used = dst - buff;
- buffsize += CT_BUFSIZ;
- p = el_realloc(buff, buffsize * sizeof(*buff));
- if (p == NULL)
- goto out;
- buff = p;
- dst = buff + used;
- /* don't increment s here - we want to retry it! */
+ used = ct_visual_char(dst,
+ conv->wsize - (size_t)(dst - conv->wbuff), *s);
+ if (used != -1) {
+ ++s;
+ dst += used;
+ continue;
}
- else
- ++s;
- dst += used;
+
+ /* failed to encode, need more buffer space */
+ used = dst - conv->wbuff;
+ if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1)
+ return NULL;
+ dst = conv->wbuff + used;
}
- if (dst >= (buff + buffsize)) { /* sigh */
- buffsize += 1;
- p = el_realloc(buff, buffsize * sizeof(*buff));
- if (p == NULL)
- goto out;
- buff = p;
- dst = buff + buffsize - 1;
+
+ if (dst >= (conv->wbuff + conv->wsize)) { /* sigh */
+ used = dst - conv->wbuff;
+ if (ct_conv_wbuff_resize(conv, conv->wsize + CT_BUFSIZ) == -1)
+ return NULL;
+ dst = conv->wbuff + used;
}
- *dst = 0;
- return buff;
-out:
- el_free(buff);
- buffsize = 0;
- return NULL;
+
+ *dst = L'\0';
+ return conv->wbuff;
}
diff -r b18bf5dd4f39 -r a92f73e30ead lib/libedit/chartype.h
--- a/lib/libedit/chartype.h Mon May 02 16:35:17 2016 +0000
+++ b/lib/libedit/chartype.h Mon May 02 16:48:34 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: chartype.h,v 1.32 2016/05/02 16:35:17 christos Exp $ */
+/* $NetBSD: chartype.h,v 1.33 2016/05/02 16:48:34 christos Exp $ */
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -95,7 +95,7 @@
/* Convert the given string into visual format, using the ct_visual_char()
* function. Uses a static buffer, so not threadsafe. */
-protected const wchar_t *ct_visual_string(const wchar_t *);
+protected const wchar_t *ct_visual_string(const wchar_t *, ct_buffer_t *);
/* printable character, use ct_visual_width() to find out display width */
diff -r b18bf5dd4f39 -r a92f73e30ead lib/libedit/el.c
--- a/lib/libedit/el.c Mon May 02 16:35:17 2016 +0000
+++ b/lib/libedit/el.c Mon May 02 16:48:34 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: el.c,v 1.89 2016/04/19 19:50:53 christos Exp $ */
+/* $NetBSD: el.c,v 1.90 2016/05/02 16:48:34 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)el.c 8.2 (Berkeley) 1/3/94";
#else
-__RCSID("$NetBSD: el.c,v 1.89 2016/04/19 19:50:53 christos Exp $");
+__RCSID("$NetBSD: el.c,v 1.90 2016/05/02 16:48:34 christos Exp $");
#endif
#endif /* not lint && not SCCSID */
@@ -147,6 +147,8 @@
sig_end(el);
el_free(el->el_prog);
+ el_free(el->el_visual.cbuff);
+ el_free(el->el_visual.wbuff);
el_free(el->el_scratch.cbuff);
el_free(el->el_scratch.wbuff);
el_free(el->el_lgcyconv.cbuff);
diff -r b18bf5dd4f39 -r a92f73e30ead lib/libedit/el.h
--- a/lib/libedit/el.h Mon May 02 16:35:17 2016 +0000
+++ b/lib/libedit/el.h Mon May 02 16:48:34 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: el.h,v 1.38 2016/04/19 19:50:53 christos Exp $ */
+/* $NetBSD: el.h,v 1.39 2016/05/02 16:48:34 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -133,6 +133,7 @@
el_search_t el_search; /* Search stuff */
el_signal_t el_signal; /* Signal handling stuff */
struct el_read_t *el_read; /* Character reading stuff */
+ ct_buffer_t el_visual; /* Buffer for displayable str */
ct_buffer_t el_scratch; /* Scratch conversion buffer */
ct_buffer_t el_lgcyconv; /* Buffer for legacy wrappers */
LineInfo el_lgcylinfo; /* Legacy LineInfo buffer */
diff -r b18bf5dd4f39 -r a92f73e30ead lib/libedit/terminal.c
--- a/lib/libedit/terminal.c Mon May 02 16:35:17 2016 +0000
+++ b/lib/libedit/terminal.c Mon May 02 16:48:34 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: terminal.c,v 1.30 2016/04/18 17:01:19 christos Exp $ */
+/* $NetBSD: terminal.c,v 1.31 2016/05/02 16:48:34 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)term.c 8.2 (Berkeley) 4/30/95";
#else
-__RCSID("$NetBSD: terminal.c,v 1.30 2016/04/18 17:01:19 christos Exp $");
+__RCSID("$NetBSD: terminal.c,v 1.31 2016/05/02 16:48:34 christos Exp $");
#endif
#endif /* not lint && not SCCSID */
@@ -1316,8 +1316,8 @@
const char *ub;
if (*ts && **ts) {
ub = ct_encode_string(ct_visual_string(
- ct_decode_string(*ts, &el->el_scratch)),
- &el->el_scratch);
+ ct_decode_string(*ts, &el->el_scratch),
+ &el->el_visual), &el->el_scratch);
} else {
ub = "(empty)";
}
Home |
Main Index |
Thread Index |
Old Index