>From 39992c36c5306d8907365c8e62ea070982676afc Mon Sep 17 00:00:00 2001 From: "Roberto E. Vargas Caballero" <k...@shike2.com> Date: Fri, 7 Sep 2012 12:18:26 +0200 Subject: [PATCH] Add xmalloc and xrealloc wrappers
If malloc or realloc fail they return NULL. Theorically this condition should be tested in the code, but it's a strange condition today (basically if this is hapenning thenyou have a big problem), and even Linux never returns NULL in the default configuration (only if the process don't have room in the space address, something a bit impossible in the case of st). But stis enough small for being executed in low resources computers where this can be a real problem. So the easy way is creating a wrappers function for them and call to die in case of error. --- st.c | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/st.c b/st.c index 7d93802..c7ab04a 100644 --- a/st.c +++ b/st.c @@ -326,6 +326,9 @@ static int utf8encode(long *, char *); static int utf8size(char *); static int isfullutf8(char *, int); +static void *xmalloc(size_t); +static void *xrealloc(void *, size_t); + static void (*handler[LASTEvent])(XEvent *) = { [KeyPress] = kpress, [ClientMessage] = cmessage, @@ -359,6 +362,21 @@ static char *opt_title = NULL; static char *opt_embed = NULL; static char *opt_class = NULL; +void * +xmalloc(size_t len) { + void *p = malloc(len); + if(!p) + die("Out of memory"); + return p; +} + +void * +xrealloc(void *p, size_t len) { + if((p = xrealloc(p, len)) == NULL) + die("Out of memory"); + return p; +} + int utf8decode(char *s, long *u) { uchar c; @@ -565,7 +583,7 @@ selcopy(void) { else { bufsize = (term.col+1) * (sel.e.y-sel.b.y+1) * UTF_SIZ; - ptr = str = malloc(bufsize); + ptr = str = xmalloc(bufsize); /* append every set & selected glyph to the selection */ for(y = 0; y < term.row; y++) { @@ -920,14 +938,14 @@ void tnew(int col, int row) { /* set screen size */ term.row = row, term.col = col; - term.line = malloc(term.row * sizeof(Line)); - term.alt = malloc(term.row * sizeof(Line)); - term.dirty = malloc(term.row * sizeof(*term.dirty)); - term.tabs = malloc(term.col * sizeof(*term.tabs)); + term.line = xmalloc(term.row * sizeof(Line)); + term.alt = xmalloc(term.row * sizeof(Line)); + term.dirty = xmalloc(term.row * sizeof(*term.dirty)); + term.tabs = xmalloc(term.col * sizeof(*term.tabs)); for(row = 0; row < term.row; row++) { - term.line[row] = malloc(term.col * sizeof(Glyph)); - term.alt [row] = malloc(term.col * sizeof(Glyph)); + term.line[row] = xmalloc(term.col * sizeof(Glyph)); + term.alt [row] = xmalloc(term.col * sizeof(Glyph)); term.dirty[row] = 0; } memset(term.tabs, 0, term.col * sizeof(*term.tabs)); @@ -1761,16 +1779,16 @@ tresize(int col, int row) { } /* resize to new height */ - term.line = realloc(term.line, row * sizeof(Line)); - term.alt = realloc(term.alt, row * sizeof(Line)); - term.dirty = realloc(term.dirty, row * sizeof(*term.dirty)); - term.tabs = realloc(term.tabs, col * sizeof(*term.tabs)); + term.line = xrealloc(term.line, row * sizeof(Line)); + term.alt = xrealloc(term.alt, row * sizeof(Line)); + term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty)); + term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs)); /* resize each row to new width, zero-pad if needed */ for(i = 0; i < minrow; i++) { term.dirty[i] = 1; - term.line[i] = realloc(term.line[i], col * sizeof(Glyph)); - term.alt[i] = realloc(term.alt[i], col * sizeof(Glyph)); + term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph)); + term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph)); for(x = mincol; x < col; x++) { term.line[i][x].state = 0; term.alt[i][x].state = 0; -- 1.7.10.4
>From 24b6ac1e65f68ae23c93ffa69993386d533d5eb2 Mon Sep 17 00:00:00 2001 From: "Roberto E. Vargas Caballero" <k...@shike2.com> Date: Fri, 7 Sep 2012 11:31:04 +0200 Subject: [PATCH] Remove buffering to fileio instead of calling fflush By default text files are line buffered, and this means that -f option will not write the line until a \n is printed. This is not very useful for debugging, so a call to fflush was added. This patch substitute this call (which will be done by each character painted) by the full remove of the buffering in the file. --- st.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/st.c b/st.c index 75c6cea..7d93802 100644 --- a/st.c +++ b/st.c @@ -810,6 +810,8 @@ ttynew(void) { opt_io, strerror(errno)); } } + if (fileio) + setvbuf(fileio, NULL, _IONBF, 0); } } } @@ -1581,10 +1583,8 @@ void tputc(char *c) { char ascii = *c; - if(fileio) { + if(fileio) putc(ascii, fileio); - fflush(fileio); - } if(term.esc & ESC_START) { if(term.esc & ESC_CSI) { -- 1.7.10.4