From 92c367a844f2f26d325d4ee6ca032894e94f7a06 Mon Sep 17 00:00:00 2001 From: Kristaps Dzonsons Date: Thu, 17 Mar 2011 08:49:34 +0000 Subject: Move mandoc_{realloc,malloc,calloc} out of libmandoc.h and into mandoc.h so that everybody can use them. This follows the convention of libXXXX.h being internal to a library and XXXX.h being the external interface. Not only does this allow the removal of lots of redundant NULL-checking code, it also sets the tone for adding new mandoc-global routines. --- chars.c | 15 +++------------ html.c | 14 +++----------- libmandoc.h | 5 +---- main.c | 8 ++------ mandoc.h | 14 +++++++------- out.c | 4 ++-- term.c | 15 +++------------ term_ps.c | 9 ++------- 8 files changed, 23 insertions(+), 61 deletions(-) diff --git a/chars.c b/chars.c index 18bd209b..ba4de8da 100644 --- a/chars.c +++ b/chars.c @@ -1,4 +1,4 @@ -/* $Id: chars.c,v 1.32 2011/01/30 16:05:37 schwarze Exp $ */ +/* $Id: chars.c,v 1.33 2011/03/17 08:49:34 kristaps Exp $ */ /* * Copyright (c) 2009, 2010 Kristaps Dzonsons * Copyright (c) 2011 Ingo Schwarze @@ -92,17 +92,8 @@ chars_init(enum chars type) * (they're in-line re-ordered during lookup). */ - tab = malloc(sizeof(struct ctab)); - if (NULL == tab) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } - - htab = calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **)); - if (NULL == htab) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } + tab = mandoc_malloc(sizeof(struct ctab)); + htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **)); for (i = 0; i < LINES_MAX; i++) { hash = (int)lines[i].code[0] - PRINT_LO; diff --git a/html.c b/html.c index 69f10ae2..c75b5b15 100644 --- a/html.c +++ b/html.c @@ -1,4 +1,4 @@ -/* $Id: html.c,v 1.127 2011/03/15 16:23:51 kristaps Exp $ */ +/* $Id: html.c,v 1.128 2011/03/17 08:49:34 kristaps Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2011 Ingo Schwarze @@ -120,11 +120,7 @@ ml_alloc(char *outopts, enum htmltype type) toks[2] = "includes"; toks[3] = NULL; - h = calloc(1, sizeof(struct html)); - if (NULL == h) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } + h = mandoc_calloc(1, sizeof(struct html)); h->type = type; h->tags.head = NULL; @@ -400,11 +396,7 @@ print_otag(struct html *h, enum htmltag tag, /* Push this tags onto the stack of open scopes. */ if ( ! (HTML_NOSTACK & htmltags[tag].flags)) { - t = malloc(sizeof(struct tag)); - if (NULL == t) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } + t = mandoc_malloc(sizeof(struct tag)); t->tag = tag; t->next = h->tags.head; h->tags.head = t; diff --git a/libmandoc.h b/libmandoc.h index 3a8559b9..eadaf257 100644 --- a/libmandoc.h +++ b/libmandoc.h @@ -1,4 +1,4 @@ -/* $Id: libmandoc.h,v 1.11 2011/03/07 01:35:51 schwarze Exp $ */ +/* $Id: libmandoc.h,v 1.12 2011/03/17 08:49:34 kristaps Exp $ */ /* * Copyright (c) 2009, 2010 Kristaps Dzonsons * @@ -20,10 +20,7 @@ __BEGIN_DECLS int mandoc_special(char *); -void *mandoc_calloc(size_t, size_t); char *mandoc_strdup(const char *); -void *mandoc_malloc(size_t); -void *mandoc_realloc(void *, size_t); char *mandoc_getarg(char **, mandocmsg, void *, int, int *); char *mandoc_normdate(char *, mandocmsg, void *, int, int); int mandoc_eos(const char *, size_t, int); diff --git a/main.c b/main.c index 496024e1..083b74ee 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.151 2011/03/16 15:28:35 kristaps Exp $ */ +/* $Id: main.c,v 1.152 2011/03/17 08:49:34 kristaps Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010, 2011 Ingo Schwarze @@ -405,11 +405,7 @@ resize_buf(struct buf *buf, size_t initial) { buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial; - buf->buf = realloc(buf->buf, buf->sz); - if (NULL == buf->buf) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } + buf->buf = mandoc_realloc(buf->buf, buf->sz); } diff --git a/mandoc.h b/mandoc.h index b5984b26..ad0345db 100644 --- a/mandoc.h +++ b/mandoc.h @@ -1,4 +1,4 @@ -/* $Id: mandoc.h,v 1.58 2011/03/07 01:35:51 schwarze Exp $ */ +/* $Id: mandoc.h,v 1.59 2011/03/17 08:49:34 kristaps Exp $ */ /* * Copyright (c) 2010, 2011 Kristaps Dzonsons * @@ -312,14 +312,14 @@ struct regset { struct reg regs[REG__MAX]; }; +typedef int (*mandocmsg)(enum mandocerr, void *, + int, int, const char *); + __BEGIN_DECLS -/* - * Callback function for warnings, errors, and fatal errors as they - * occur in the compilers libroff, libmdoc, and libman. - */ -typedef int (*mandocmsg)(enum mandocerr, void *, - int, int, const char *); +void *mandoc_calloc(size_t, size_t); +void *mandoc_malloc(size_t); +void *mandoc_realloc(void *, size_t); __END_DECLS diff --git a/out.c b/out.c index a8d5794b..eb303d51 100644 --- a/out.c +++ b/out.c @@ -1,4 +1,4 @@ -/* $Id: out.c,v 1.38 2011/03/15 16:23:51 kristaps Exp $ */ +/* $Id: out.c,v 1.39 2011/03/17 08:49:34 kristaps Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2011 Ingo Schwarze @@ -431,7 +431,7 @@ tblcalc(struct rofftbl *tbl, const struct tbl_span *sp) */ assert(NULL == tbl->cols); - tbl->cols = calloc + tbl->cols = mandoc_calloc ((size_t)sp->tbl->cols, sizeof(struct roffcol)); hp = sp->head; diff --git a/term.c b/term.c index 736029a4..bdf73bf2 100644 --- a/term.c +++ b/term.c @@ -1,4 +1,4 @@ -/* $Id: term.c,v 1.178 2011/03/15 16:23:51 kristaps Exp $ */ +/* $Id: term.c,v 1.179 2011/03/17 08:49:34 kristaps Exp $ */ /* * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons * Copyright (c) 2010, 2011 Ingo Schwarze @@ -80,12 +80,7 @@ term_alloc(enum termenc enc) { struct termp *p; - p = calloc(1, sizeof(struct termp)); - if (NULL == p) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } - + p = mandoc_calloc(1, sizeof(struct termp)); p->enc = enc; return(p); } @@ -579,11 +574,7 @@ adjbuf(struct termp *p, size_t sz) while (sz >= p->maxcols) p->maxcols <<= 2; - p->buf = realloc(p->buf, p->maxcols); - if (NULL == p->buf) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } + p->buf = mandoc_realloc(p->buf, p->maxcols); } diff --git a/term_ps.c b/term_ps.c index ddb8459e..233118b8 100644 --- a/term_ps.c +++ b/term_ps.c @@ -1,4 +1,4 @@ -/* $Id: term_ps.c,v 1.47 2011/03/07 01:58:24 schwarze Exp $ */ +/* $Id: term_ps.c,v 1.48 2011/03/17 08:49:34 kristaps Exp $ */ /* * Copyright (c) 2010 Kristaps Dzonsons * @@ -366,14 +366,9 @@ ps_growbuf(struct termp *p, size_t sz) p->engine.ps.psmargsz += sz; - p->engine.ps.psmarg = realloc + p->engine.ps.psmarg = mandoc_realloc (p->engine.ps.psmarg, p->engine.ps.psmargsz); - - if (NULL == p->engine.ps.psmarg) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } } static double ps_hspan(const struct termp *, -- cgit v1.2.3