]> git.mdlowis.com Git - archive/gapbuf.git/commitdiff
removed die function and added emalloc to gracefully handle out of memory errors
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 6 Mar 2018 19:07:56 +0000 (14:07 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 6 Mar 2018 19:07:56 +0000 (14:07 -0500)
buf.c
testbuf.c

diff --git a/buf.c b/buf.c
index 4959e86a4d9c3ef6b3a9cdc661ad02fd6aa74164..af16a9d02bb1fd9d5ddc425068319ad652412e8f 100644 (file)
--- a/buf.c
+++ b/buf.c
 #include <fcntl.h>
 #include <time.h>
 
-int CfgTabWidth = 4;
-int CfgExpandTabs = true;
-
 /******************************************************************************/
 
+static void* emalloc(size_t sz) {
+    void* ptr = malloc(sz);
+    if (NULL == ptr) {
+        perror("malloc() :");
+        exit(1);
+    }
+    return ptr;
+}
+
 static size_t pagealign(size_t sz) {
-    size_t pgsize = sysconf(_SC_PAGE_SIZE);
-    size_t alignmask = pgsize - 1;
+    size_t pgsize = sysconf(_SC_PAGE_SIZE), alignmask = pgsize - 1;
     if (sz & alignmask)
         sz += pgsize - (sz & alignmask);
     return sz;
 }
 
-void die(const char* msgfmt, ...) {
-    va_list args;
-    va_start(args, msgfmt);
-    fprintf(stderr, "error: ");
-    vfprintf(stderr, msgfmt, args);
-    va_end(args);
-    if (*msgfmt && msgfmt[strlen(msgfmt)-1] == ':')
-        fprintf(stderr, " %s", strerror(errno));
-    fprintf(stderr, "\n");
-    exit(EXIT_FAILURE);
-}
-
 /******************************************************************************/
 
 void buf_init(Buf* buf, void (*errfn)(char*)) {
@@ -46,10 +39,10 @@ void buf_init(Buf* buf, void (*errfn)(char*)) {
     }
     /* reset the state to defaults */
     buf->modified    = false;
-    buf->expand_tabs = CfgExpandTabs;
+    buf->expand_tabs = true;
     buf->crlf        = 0;
     buf->bufsize     = pagealign(1);
-    buf->bufstart    = malloc(buf->bufsize * sizeof(Rune));
+    buf->bufstart    = emalloc(buf->bufsize * sizeof(Rune));
     buf->bufend      = buf->bufstart + buf->bufsize;
     buf->gapstart    = buf->bufstart;
     buf->gapend      = buf->bufend;
@@ -57,7 +50,6 @@ void buf_init(Buf* buf, void (*errfn)(char*)) {
     buf->redo        = NULL;
     buf->errfn       = errfn;
     buf->path        = NULL;
-    assert(buf->bufstart);
 }
 
 void buf_load(Buf* buf, Sel* sel, char* path) {
@@ -80,11 +72,10 @@ void buf_load(Buf* buf, Sel* sel, char* path) {
         buf->bufend   = buf->bufstart + buf->bufsize;
         buf->gapstart = buf->bufstart;
         buf->gapend   = buf->bufend;
-        if (!buf->bufstart) die("malloc() :");
         /* Read the file into the buffer */
         while (sb.st_size && (nread = read(fd, buf->gapstart, sb.st_size)) > 0)
             buf->gapstart += nread, sb.st_size -= nread;
-        if (nread < 0) die("read() :");
+        if (nread < 0) buf->errfn("Failed to read file");
     }
     if (fd > 0) close(fd);
 }
index 5d89e1e00fde3a31afc2f184b4aa582c5117d182..e56012ca79447b4f738aca563ea7eec5c95d3c7a 100644 (file)
--- a/testbuf.c
+++ b/testbuf.c
@@ -5,11 +5,6 @@
 #include <edit.h>
 #include <unistd.h>
 
-extern int CfgTabWidth;
-extern int CfgExpandTabs;
-extern int CfgCopyIndent;
-
-
 static Buf TestBuf;
 
 static void onerror(char* msg) {
@@ -49,7 +44,7 @@ TEST_SUITE(BufferTests) {
         buf_init(&buf, (void*)0x12345678);
         CHECK(buf.path        == NULL);
         CHECK(buf.modified    == false);
-        CHECK(buf.expand_tabs == CfgExpandTabs);
+        CHECK(buf.expand_tabs == true);
         CHECK(buf.crlf        == 0);
         CHECK(buf.bufsize     == sysconf(_SC_PAGE_SIZE));
         CHECK(buf.bufstart    != NULL);
@@ -68,7 +63,7 @@ TEST_SUITE(BufferTests) {
         buf_init(&buf, (void*)0x12345678);
         CHECK(buf.path        == NULL);
         CHECK(buf.modified    == false);
-        CHECK(buf.expand_tabs == CfgExpandTabs);
+        CHECK(buf.expand_tabs == true);
         CHECK(buf.crlf        == 0);
         CHECK(buf.bufsize     == sysconf(_SC_PAGE_SIZE));
         CHECK(buf.bufstart    != NULL);
@@ -248,8 +243,11 @@ TEST_SUITE(BufferTests) {
         CHECK(end == buf_last(&TestBuf));
         CHECK(TestBuf.modified == false);
     }
+#endif
+    /* Access
+     *************************************************************************/
 
-
+#if 0
     /* Insertions
      *************************************************************************/
     TEST(buf_putc should insert at 0 in empty buf) {