From: Michael D. Lowis Date: Tue, 6 Mar 2018 19:07:56 +0000 (-0500) Subject: removed die function and added emalloc to gracefully handle out of memory errors X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=e4b1b0f67f58565e9d85e493e4721f0a1b6f1b7d;p=archive%2Fgapbuf.git removed die function and added emalloc to gracefully handle out of memory errors --- diff --git a/buf.c b/buf.c index 4959e86..af16a9d 100644 --- a/buf.c +++ b/buf.c @@ -11,31 +11,24 @@ #include #include -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); } diff --git a/testbuf.c b/testbuf.c index 5d89e1e..e56012c 100644 --- a/testbuf.c +++ b/testbuf.c @@ -5,11 +5,6 @@ #include #include -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) {