[hackers] [st] simplified buffer_create

From: Anselm R. Garbe <arg_AT_suckless.org>
Date: Fri Mar 02 15:47:18 2007

changeset: 52:a76e7be497b5
tag: tip
user: Anselm R. Garbe <arg_AT_suckless.org>
date: Fri Mar 02 15:46:30 2007 +0100
summary: simplified buffer_create

diff -r 95f09e8f2334 -r a76e7be497b5 screen.c
--- a/screen.c Fri Mar 02 15:41:43 2007 +0100
+++ b/screen.c Fri Mar 02 15:46:30 2007 +0100
@@ -17,16 +17,9 @@ buffer_create(int rows, int cols) {
         int i;
         text_row_t *tr;
 
- tr = calloc(rows, sizeof(text_row_t));
- if(!tr) {
- fprintf(stderr, "Could not allocate %dx%d bytes\n",
- rows, sizeof(text_row_t));
- exit(1);
- }
- for(i = 0; i < rows; i++) {
- tr[i].line = calloc(cols, sizeof(text_letter_t));
- assert(tr[i].line);
- }
+ tr = emallocz(rows * sizeof(text_row_t));
+ for(i = 0; i < rows; i++)
+ tr[i].line = emallocz(cols * sizeof(text_letter_t));
         return tr;
 }
 
diff -r 95f09e8f2334 -r a76e7be497b5 st.h
--- a/st.h Fri Mar 02 15:41:43 2007 +0100
+++ b/st.h Fri Mar 02 15:46:30 2007 +0100
@@ -108,5 +108,6 @@ extern text_row_t *buffer_create(int row
 
 /* util.c */
 void *emalloc(unsigned int size);
+void *emallocz(unsigned int size);
 void eprint(const char *errstr, ...);
 char *estrdup(const char *str);
diff -r 95f09e8f2334 -r a76e7be497b5 util.c
--- a/util.c Fri Mar 02 15:41:43 2007 +0100
+++ b/util.c Fri Mar 02 15:46:30 2007 +0100
@@ -8,6 +8,15 @@
 #include <string.h>
 #include <sys/wait.h>
 #include <unistd.h>
+
+void *
+emallocz(unsigned int size) {
+ void *res = calloc(1, size);
+
+ if(!res)
+ eprint("fatal: could not malloc() %u bytes\n", size);
+ return res;
+}
 
 void *
 emalloc(unsigned int size) {
Received on Fri Mar 02 2007 - 15:47:18 UTC

This archive was generated by hypermail 2.2.0 : Sun Jul 13 2008 - 15:56:04 UTC