diff options
| author | Simon Tatham <anakin@pobox.com> | 1999-09-02 12:35:02 +0000 |
|---|---|---|
| committer | Simon Tatham <anakin@pobox.com> | 1999-09-02 12:35:02 +0000 |
| commit | d3c026f08f629659b5efe23fe8bffd3cf9b845f6 (patch) | |
| tree | e81d86bf7d706e0ac4c7a9a644ab193cd4f03e3c | |
| parent | 5842eaaf51d9045bc0b199f0b5e82dfc614b2e0c (diff) | |
| download | halibut-d3c026f08f629659b5efe23fe8bffd3cf9b845f6.zip halibut-d3c026f08f629659b5efe23fe8bffd3cf9b845f6.tar.gz halibut-d3c026f08f629659b5efe23fe8bffd3cf9b845f6.tar.bz2 halibut-d3c026f08f629659b5efe23fe8bffd3cf9b845f6.tar.xz | |
Redo memory allocation to use mknew macro
[originally from svn r214]
| -rw-r--r-- | buttress.h | 7 | ||||
| -rw-r--r-- | contents.c | 14 | ||||
| -rw-r--r-- | input.c | 24 | ||||
| -rw-r--r-- | keywords.c | 10 | ||||
| -rw-r--r-- | main.c | 2 | ||||
| -rw-r--r-- | malloc.c | 2 | ||||
| -rw-r--r-- | misc.c | 4 | ||||
| -rw-r--r-- | ustring.c | 12 |
8 files changed, 38 insertions, 37 deletions
@@ -16,8 +16,6 @@ #define FALSE 0 #endif -#define lenof(x) ( sizeof((x)) / sizeof(*(x)) ) - /* * Structure tags */ @@ -161,6 +159,11 @@ void free_word_list(word *w); void free_para_list(paragraph *p); word *dup_word_list(word *w); +#define mknew(type) ( (type *) smalloc (sizeof (type)) ) +#define mknewa(type, number) ( (type *) smalloc ((number) * sizeof (type)) ) +#define resize(array, len) ( srealloc ((array), (len) * sizeof (*(array))) ) +#define lenof(array) ( sizeof(array) / sizeof(*(array)) ) + /* * ustring.c */ @@ -18,13 +18,12 @@ struct numberstate_Tag { }; numberstate *number_init(void) { - numberstate *ret = smalloc(sizeof(numberstate)); + numberstate *ret = mknew(numberstate); ret->chapternum = 0; ret->appendixnum = -1; ret->ischapter = 1; ret->maxsectlevel = 32; - ret->sectionlevels = smalloc(ret->maxsectlevel * - sizeof(*ret->sectionlevels)); + ret->sectionlevels = mknewa(int, ret->maxsectlevel); ret->listitem = -1; return ret; } @@ -34,7 +33,7 @@ void number_free(numberstate *state) { } static void dotext(word ***wret, wchar_t *text) { - word *mnewword = smalloc(sizeof(word)); + word *mnewword = mknew(word); mnewword->text = ustrdup(text); mnewword->type = word_Normal; mnewword->alt = NULL; @@ -44,7 +43,7 @@ static void dotext(word ***wret, wchar_t *text) { } static void dospace(word ***wret) { - word *mnewword = smalloc(sizeof(word)); + word *mnewword = mknew(word); mnewword->text = NULL; mnewword->type = word_WhiteSpace; mnewword->alt = NULL; @@ -109,9 +108,8 @@ word *number_mktext(numberstate *state, int para, int aux, int prev) { level = (para == para_Heading ? 0 : aux); if (state->maxsectlevel <= level) { state->maxsectlevel = level + 32; - state->sectionlevels = srealloc(state->sectionlevels, - state->maxsectlevel * - sizeof(*state->sectionlevels)); + state->sectionlevels = resize(state->sectionlevels, + state->maxsectlevel); } state->sectionlevels[level]++; for (i = level+1; i < state->maxsectlevel; i++) @@ -86,7 +86,7 @@ struct tagRdstring { static void rdadd(rdstring *rs, wchar_t c) { if (rs->pos >= rs->size-1) { rs->size = rs->pos + 128; - rs->text = srealloc(rs->text, rs->size * sizeof(wchar_t)); + rs->text = resize(rs->text, rs->size); } rs->text[rs->pos++] = c; rs->text[rs->pos] = 0; @@ -95,13 +95,13 @@ static void rdadds(rdstring *rs, wchar_t *p) { int len = ustrlen(p); if (rs->pos >= rs->size - len) { rs->size = rs->pos + len + 128; - rs->text = srealloc(rs->text, rs->size * sizeof(wchar_t)); + rs->text = resize(rs->text, rs->size); } ustrcpy(rs->text + rs->pos, p); rs->pos += len; } static wchar_t *rdtrim(rdstring *rs) { - rs->text = srealloc(rs->text, (rs->pos + 1) * sizeof(wchar_t)); + rs->text = resize(rs->text, rs->pos + 1); return rs->text; } @@ -410,7 +410,7 @@ token get_codepar_token(input *in) { * Adds a new word to a linked list */ static word *addword(word newword, word ***hptrptr) { - word *mnewword = smalloc(sizeof(word)); + word *mnewword = mknew(word); *mnewword = newword; /* structure copy */ mnewword->next = NULL; **hptrptr = mnewword; @@ -422,7 +422,7 @@ static word *addword(word newword, word ***hptrptr) { * Adds a new paragraph to a linked list */ static paragraph *addpara(paragraph newpara, paragraph ***hptrptr) { - paragraph *mnewpara = smalloc(sizeof(paragraph)); + paragraph *mnewpara = mknew(paragraph); *mnewpara = newpara; /* structure copy */ mnewpara->next = NULL; **hptrptr = mnewpara; @@ -668,7 +668,7 @@ static void read_file(paragraph ***ret, input *in) { case tok_lbrace: error(err_unexbrace, &t.pos); /* Error recovery: push nop */ - sitem = smalloc(sizeof(*sitem)); + sitem = mknew(struct stack_item); sitem->type = stack_nop; stk_push(parsestk, sitem); break; @@ -781,7 +781,7 @@ static void read_file(paragraph ***ret, input *in) { if (t.type != tok_lbrace) { error(err_explbr, &t.pos); } else { - sitem = smalloc(sizeof(*sitem)); + sitem = mknew(struct stack_item); sitem->type = stack_hyper; stk_push(parsestk, sitem); } @@ -795,7 +795,7 @@ static void read_file(paragraph ***ret, input *in) { error(err_nestedstyles, &t.pos); /* Error recovery: eat lbrace, push nop. */ dtor(t), t = get_token(in); - sitem = smalloc(sizeof(*sitem)); + sitem = mknew(struct stack_item); sitem->type = stack_nop; stk_push(parsestk, sitem); } @@ -806,7 +806,7 @@ static void read_file(paragraph ***ret, input *in) { style = (type == c_c ? word_Code : type == c_cw ? word_WeakCode : word_Emph); - sitem = smalloc(sizeof(*sitem)); + sitem = mknew(struct stack_item); sitem->type = stack_style; stk_push(parsestk, sitem); } @@ -819,11 +819,11 @@ static void read_file(paragraph ***ret, input *in) { error(err_nestedindex, &t.pos); /* Error recovery: eat lbrace, push nop. */ dtor(t), t = get_token(in); - sitem = smalloc(sizeof(*sitem)); + sitem = mknew(struct stack_item); sitem->type = stack_nop; stk_push(parsestk, sitem); } - sitem = smalloc(sizeof(*sitem)); + sitem = mknew(struct stack_item); sitem->type = stack_idx; dtor(t), t = get_token(in); /* @@ -881,7 +881,7 @@ static void read_file(paragraph ***ret, input *in) { * sidetrack from the main thread of the * paragraph. */ - sitem = smalloc(sizeof(*sitem)); + sitem = mknew(struct stack_item); sitem->type = stack_ualt; sitem->whptr = whptr; stk_push(parsestk, sitem); @@ -20,7 +20,7 @@ static void heap_add(keywordlist *kl, keyword *key) { int p; if (kl->nkeywords >= kl->size) { kl->size = kl->nkeywords + 128; - kl->keys = srealloc(kl->keys, sizeof(*kl->keys) * kl->size); + kl->keys = resize(kl->keys, kl->size); } p = kl->nkeywords++; kl->keys[p] = key; @@ -34,7 +34,7 @@ static void heap_sort(keywordlist *kl) { int i, j; kl->size = kl->nkeywords; - kl->keys = srealloc(kl->keys, sizeof(*kl->keys) * kl->size); + kl->keys = resize(kl->keys, kl->size); i = kl->nkeywords; while (i > 1) { @@ -86,7 +86,7 @@ keyword *kw_lookup(keywordlist *kl, wchar_t *str) { * finish). */ keywordlist *get_keywords(paragraph *source) { - keywordlist *kl = smalloc(sizeof(*kl)); + keywordlist *kl = mknew(keywordlist); numberstate *n = number_init(); int prevpara = para_NotParaType; @@ -105,7 +105,7 @@ keywordlist *get_keywords(paragraph *source) { if (source->kwtext || source->type == para_Biblio) { wchar_t *p = source->keyword; while (*p) { - keyword *kw = smalloc(sizeof(*kw)); + keyword *kw = mknew(keyword); kw->key = p; kw->text = source->kwtext; kw->para = source; @@ -149,7 +149,7 @@ void subst_keywords(paragraph *source, keywordlist *kl) { if (subst && ptr->type == word_LowerXref) ustrlow(subst->text); - close = smalloc(sizeof(word)); + close = mknew(word); close->text = NULL; close->alt = NULL; close->type = word_XrefEnd; @@ -21,7 +21,7 @@ int main(int argc, char **argv) { /* * Set up initial (default) parameters. */ - infiles = smalloc(argc*sizeof(char *)); + infiles = mknewa(char *, argc); outfile = NULL; nfiles = 0; nogo = errs = FALSE; @@ -82,7 +82,7 @@ word *dup_word_list(word *w) { word *head, **eptr = &head; while (w) { - word *newwd = smalloc(sizeof(word)); + word *newwd = mknew(word); *newwd = *w; /* structure copy */ newwd->text = ustrdup(w->text); if (w->alt) @@ -13,7 +13,7 @@ struct stackTag { stack stk_new(void) { stack s; - s = smalloc(sizeof(*s)); + s = mknew(stack); s->sp = 0; s->size = 0; s->data = NULL; @@ -29,7 +29,7 @@ void stk_free(stack s) { void stk_push(stack s, void *item) { if (s->size <= s->sp) { s->size = s->sp + 32; - s->data = srealloc(s->data, s->size * sizeof(*s->data)); + s->data = resize(s->data, s->size); } s->data[s->sp++] = item; } @@ -9,10 +9,10 @@ wchar_t *ustrdup(wchar_t *s) { wchar_t *r; if (s) { - r = smalloc((1+ustrlen(s)) * sizeof(wchar_t)); + r = mknewa(wchar_t, 1+ustrlen(s)); ustrcpy(r, s); } else { - r = smalloc(1); + r = mknew(wchar_t); *r = 0; } return r; @@ -85,7 +85,7 @@ wchar_t *ustrftime(wchar_t *wfmt, struct tm *timespec) { */ if (wfmt) { len = ustrlen(wfmt); - fmt = smalloc(2+len); + fmt = mknewa(char, 2+len); ustrtoa(wfmt, fmt+1, len+1); fmt[0] = ' '; } else @@ -93,15 +93,15 @@ wchar_t *ustrftime(wchar_t *wfmt, struct tm *timespec) { while (1) { size += USTRFTIME_DELTA; - blk = srealloc(blk, size); + blk = resize((char *)blk, size); len = strftime((char *)blk, size-1, fmt, timespec); if (len > 0) break; } /* Note: +1 for the terminating 0, -1 for the initial space in fmt */ - wblk = srealloc(blk, len * sizeof(wchar_t)); - text = smalloc(len); + wblk = resize((wchar_t *)blk, len); + text = mknewa(char, len); strftime(text, len, fmt+1, timespec); /* * We operate in the C locale, so this all ought to be kosher |