From de0e1c62843398e211d2588d9fe3c95923733afb Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Thu, 7 Aug 2008 20:20:52 +0000 Subject: [PATCH] Lose ensure* stuff. --- CHANGES | 6 +++++- array.h | 27 +++++++++++++++++------ buffer.c | 9 ++++++-- tmux.h | 16 +------------- window-copy.c | 26 +++++++++++------------ xmalloc.c | 59 +-------------------------------------------------- 6 files changed, 47 insertions(+), 96 deletions(-) diff --git a/CHANGES b/CHANGES index aa82a4bf..3e8c7c03 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +07 August 2008 + +* Lose some unused/useless wrapper functions. + 25 July 2008 * Shell variables may now be defined and used in configuration file. Define @@ -636,4 +640,4 @@ (including mutt, emacs). No status bar yet and no key remapping or other customisation. -$Id: CHANGES,v 1.155 2008-07-25 17:20:40 nicm Exp $ +$Id: CHANGES,v 1.156 2008-08-07 20:20:49 nicm Exp $ diff --git a/array.h b/array.h index c9d6512b..c861b075 100644 --- a/array.h +++ b/array.h @@ -1,4 +1,4 @@ -/* $Id: array.h,v 1.5 2008-06-20 08:36:20 nicm Exp $ */ +/* $Id: array.h,v 1.6 2008-08-07 20:20:52 nicm Exp $ */ /* * Copyright (c) 2006 Nicholas Marriott @@ -28,6 +28,22 @@ #define ARRAY_ITEM(a, i) ((a)->list[i]) #define ARRAY_ITEMSIZE(a) (sizeof *(a)->list) +#define ARRAY_INITIALSPACE(a) (10 * ARRAY_ITEMSIZE(a)) + +#define ARRAY_ENSURE(a, n) do { \ + if (SIZE_MAX - (n) < (a)->num) \ + fatalx("number too big"); \ + if (SIZE_MAX / ((a)->num + (n)) < ARRAY_ITEMSIZE(a)) \ + fatalx("size too big"); \ + if ((a)->space == 0) { \ + (a)->space = ARRAY_INITIALSPACE(a); \ + (a)->list = xrealloc((a)->list, 1, (a)->space); \ + } \ + while ((a)->space <= ((a)->num + (n)) * ARRAY_ITEMSIZE(a)) { \ + (a)->list = xrealloc((a)->list, 2, (a)->space); \ + (a)->space *= 2; \ + } \ +} while (0) #define ARRAY_EMPTY(a) ((a) == NULL || (a)->num == 0) #define ARRAY_LENGTH(a) ((a)->num) @@ -50,12 +66,12 @@ } while (0) #define ARRAY_ADD(a, s) do { \ - ENSURE_SIZE2((a)->list, (a)->space, (a)->num + 1, ARRAY_ITEMSIZE(a)); \ + ARRAY_ENSURE(a, 1); \ (a)->list[(a)->num] = s; \ (a)->num++; \ } while (0) #define ARRAY_INSERT(a, i, s) do { \ - ENSURE_SIZE2((a)->list, (a)->space, (a)->num + 1, ARRAY_ITEMSIZE(a)); \ + ARRAY_ENSURE(a, 1); \ if ((i) < (a)->num) { \ memmove((a)->list + (i) + 1, (a)->list + (i), \ ARRAY_ITEMSIZE(a) * ((a)->num - (i))); \ @@ -74,7 +90,7 @@ } while (0) #define ARRAY_EXPAND(a, n) do { \ - ENSURE_SIZE2((a)->list, (a)->space, (a)->num + n, ARRAY_ITEMSIZE(a)); \ + ARRAY_ENSURE(a, n); \ (a)->num += n; \ } while (0) #define ARRAY_TRUNC(a, n) do { \ @@ -85,8 +101,7 @@ } while (0) #define ARRAY_CONCAT(a, b) do { \ - ENSURE_SIZE2((a)->list, (a)->space, (a)->num + (b)->num, \ - ARRAY_ITEMSIZE(a)); \ + ARRAY_ENSURE(a, (b)->num); \ memcpy((a)->list + (a)->num, (b)->list, (b)->num * ARRAY_ITEMSIZE(a)) \ (a)->num += (b)->num; \ } while (0) diff --git a/buffer.c b/buffer.c index 7bf0e4b0..6dd3e3ee 100644 --- a/buffer.c +++ b/buffer.c @@ -1,4 +1,4 @@ -/* $Id: buffer.c,v 1.4 2007-12-06 09:46:21 nicm Exp $ */ +/* $Id: buffer.c,v 1.5 2008-08-07 20:20:52 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -71,7 +71,12 @@ buffer_ensure(struct buffer *b, size_t size) b->off = 0; } - ENSURE_FOR(b->base, b->space, b->size, size); + if (SIZE_MAX - b->size < size) + fatalx("size too big"); + while (b->space < b->size + size) { + b->base = xrealloc(b->base, 2, b->space); + b->space *= 2; + } } /* Adjust buffer after data appended. */ diff --git a/tmux.h b/tmux.h index 40a68c4f..6d8f9647 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.179 2008-07-25 17:20:40 nicm Exp $ */ +/* $Id: tmux.h,v 1.180 2008-08-07 20:20:52 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -115,17 +115,6 @@ extern const char *__progname; #define printflike3 __attribute__ ((format (printf, 3, 4))) #define printflike4 __attribute__ ((format (printf, 4, 5))) -/* Ensure buffer size. */ -#define ENSURE_SIZE(buf, len, size) do { \ - (buf) = ensure_size(buf, &(len), 1, size); \ -} while (0) -#define ENSURE_SIZE2(buf, len, nmemb, size) do { \ - (buf) = ensure_size(buf, &(len), nmemb, size); \ -} while (0) -#define ENSURE_FOR(buf, len, size, adj) do { \ - (buf) = ensure_for(buf, &(len), size, adj); \ -} while (0) - /* Buffer macros. */ #define BUFFER_USED(b) ((b)->size) #define BUFFER_FREE(b) ((b)->space - (b)->off - (b)->size) @@ -1321,9 +1310,6 @@ __dead void log_fatal(const char *, ...); __dead void log_fatalx(const char *, ...); /* xmalloc.c */ -void *ensure_size(void *, size_t *, size_t, size_t); -void *ensure_for(void *, size_t *, size_t, size_t); -char *xmemstrdup(const char *, size_t); char *xstrdup(const char *); void *xcalloc(size_t, size_t); void *xmalloc(size_t); diff --git a/window-copy.c b/window-copy.c index f3943b07..869effa3 100644 --- a/window-copy.c +++ b/window-copy.c @@ -1,4 +1,4 @@ -/* $Id: window-copy.c,v 1.27 2008-07-24 21:42:40 nicm Exp $ */ +/* $Id: window-copy.c,v 1.28 2008-08-07 20:20:52 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -43,7 +43,7 @@ void window_copy_start_selection(struct window *); int window_copy_update_selection(struct window *); void window_copy_copy_selection(struct window *, struct client *); void window_copy_copy_line( - struct window *, char **, size_t *, size_t *, u_int, u_int, u_int); + struct window *, char **, size_t *, u_int, u_int, u_int); u_int window_copy_find_length(struct window *, u_int); void window_copy_cursor_start_of_line(struct window *); void window_copy_cursor_end_of_line(struct window *); @@ -364,14 +364,13 @@ window_copy_copy_selection(struct window *w, struct client *c) struct window_copy_mode_data *data = w->modedata; struct screen *s = &data->screen; char *buf; - size_t len, off; + size_t off; u_int i, xx, yy, sx, sy, ex, ey, limit; if (!s->sel.flag) return; - len = BUFSIZ; - buf = xmalloc(len); + buf = xmalloc(1); off = 0; *buf = '\0'; @@ -399,18 +398,17 @@ window_copy_copy_selection(struct window *w, struct client *c) /* Copy the lines. */ if (sy == ey) - window_copy_copy_line(w, &buf, &off, &len, sy, sx, ex); + window_copy_copy_line(w, &buf, &off, sy, sx, ex); else { xx = window_copy_find_length(w, sy); - window_copy_copy_line(w, &buf, &off, &len, sy, sx, xx); + window_copy_copy_line(w, &buf, &off, sy, sx, xx); if (ey - sy > 1) { for (i = sy + 1; i < ey - 1; i++) { xx = window_copy_find_length(w, i); - window_copy_copy_line( - w, &buf, &off, &len, i, 0, xx); + window_copy_copy_line(w, &buf, &off, i, 0, xx); } } - window_copy_copy_line(w, &buf, &off, &len, ey, 0, ex); + window_copy_copy_line(w, &buf, &off, ey, 0, ex); } /* Terminate buffer, overwriting final \n. */ @@ -424,8 +422,8 @@ window_copy_copy_selection(struct window *w, struct client *c) } void -window_copy_copy_line(struct window *w, - char **buf, size_t *off, size_t *len, u_int sy, u_int sx, u_int ex) +window_copy_copy_line( + struct window *w, char **buf, size_t *off, u_int sy, u_int sx, u_int ex) { u_char i, xx; @@ -440,13 +438,13 @@ window_copy_copy_line(struct window *w, if (sx < ex) { for (i = sx; i < ex; i++) { - *buf = ensure_size(*buf, len, 1, *off + 1); + *buf = xrealloc(*buf, 1, (*off) + 1); (*buf)[*off] = w->base.grid_data[sy][i]; (*off)++; } } - *buf = ensure_size(*buf, len, 1, *off + 1); + *buf = xrealloc(*buf, 1, (*off) + 1); (*buf)[*off] = '\n'; (*off)++; } diff --git a/xmalloc.c b/xmalloc.c index ad2c4b70..50b22fef 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -1,4 +1,4 @@ -/* $Id: xmalloc.c,v 1.5 2007-10-19 20:50:01 nicm Exp $ */ +/* $Id: xmalloc.c,v 1.6 2008-08-07 20:20:52 nicm Exp $ */ /* * Copyright (c) 2004 Nicholas Marriott @@ -26,63 +26,6 @@ #include "tmux.h" -void * -ensure_for(void *buf, size_t *len, size_t size, size_t adj) -{ - if (adj == 0) - fatalx("zero adj"); - - if (SIZE_MAX - size < adj) - fatalx("size + adj > SIZE_MAX"); - size += adj; - - if (*len == 0) { - *len = BUFSIZ; - buf = xmalloc(*len); - } - - while (*len <= size) { - buf = xrealloc(buf, 2, *len); - *len *= 2; - } - - return (buf); -} - -void * -ensure_size(void *buf, size_t *len, size_t nmemb, size_t size) -{ - if (nmemb == 0 || size == 0) - fatalx("zero size"); - if (SIZE_MAX / nmemb < size) - fatalx("nmemb * size > SIZE_MAX"); - - if (*len == 0) { - *len = BUFSIZ; - buf = xmalloc(*len); - } - - while (*len <= nmemb * size) { - buf = xrealloc(buf, 2, *len); - *len *= 2; - } - - return (buf); -} - -char * -xmemstrdup(const char *buf, size_t len) -{ - char *s; - - s = xmalloc(len + 1); - if (len > 0) - memcpy(s, buf, len); - s[len] = '\0'; - - return (s); -} - char * xstrdup(const char *s) {