From f59c437a25af1e4c028d268cca3819476ef4f725 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Mon, 19 Nov 2001 10:29:17 +0000 Subject: [PATCH 1/1] * src/state.h (CORE_ALLOC, SHIFTS_ALLOC, ERRS_ALLOC) (REDUCTIONS_ALLOC): New. * src/LR0.c, src/conflicts.c: Use them to de-obfuscate memory allocation. --- ChangeLog | 8 ++++++++ src/LR0.c | 38 ++++++++++++++++---------------------- src/conflicts.c | 4 ++-- src/state.h | 15 ++++++++++++++- 4 files changed, 40 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index e7f63fff..d833a18c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2001-11-19 Akim Demaille + + * src/state.h (CORE_ALLOC, SHIFTS_ALLOC, ERRS_ALLOC) + (REDUCTIONS_ALLOC): New. + * src/LR0.c, src/conflicts.c: Use them to de-obfuscate memory + allocation. + + 2001-11-19 Akim Demaille * src/LR0.c (new_state): Complete trace code. diff --git a/src/LR0.c b/src/LR0.c index 3288d9dc..e3db49aa 100644 --- a/src/LR0.c +++ b/src/LR0.c @@ -151,7 +151,8 @@ new_itemsets (void) int symbol; #if TRACE - fprintf (stderr, "Entering new_itemsets\n"); + fprintf (stderr, "Entering new_itemsets, state = %d\n", + this_state->number); #endif for (i = 0; i < nsyms; i++) @@ -211,8 +212,7 @@ new_state (int symbol) iend = kernel_end[symbol]; n = iend - isp1; - p = - (core *) xcalloc ((unsigned) (sizeof (core) + (n - 1) * sizeof (short)), 1); + p = CORE_ALLOC (n); p->accessing_symbol = symbol; p->number = nstates; p->nitems = n; @@ -350,7 +350,7 @@ new_states (void) { core *p; - p = (core *) xcalloc ((unsigned) (sizeof (core) - sizeof (short)), 1); + p = CORE_ALLOC (0); first_state = last_state = this_state = p; nstates = 1; } @@ -364,8 +364,7 @@ save_shifts (void) short *sp2; short *send; - p = (shifts *) xcalloc ((unsigned) (sizeof (shifts) + - (nshifts - 1) * sizeof (short)), 1); + p = SHIFTS_ALLOC (nshifts); p->number = this_state->number; p->nshifts = nshifts; @@ -401,7 +400,7 @@ insert_start_shift (void) core *statep; shifts *sp; - statep = (core *) xcalloc ((unsigned) (sizeof (core) - sizeof (short)), 1); + statep = CORE_ALLOC (0); statep->number = nstates; statep->accessing_symbol = start_symbol; @@ -409,7 +408,7 @@ insert_start_shift (void) last_state = statep; /* Make a shift from this state to (what will be) the final state. */ - sp = XCALLOC (shifts, 1); + sp = SHIFTS_ALLOC (1); sp->number = nstates++; sp->nshifts = 1; sp->shifts[0] = nstates; @@ -466,10 +465,7 @@ augment_automaton (void) if (sp && sp->number == k) { - sp2 = (shifts *) xcalloc ((unsigned) (sizeof (shifts) - + - sp->nshifts * - sizeof (short)), 1); + sp2 = SHIFTS_ALLOC (sp->nshifts + 1); sp2->number = k; sp2->nshifts = sp->nshifts + 1; sp2->shifts[0] = nstates; @@ -486,7 +482,7 @@ augment_automaton (void) } else { - sp2 = XCALLOC (shifts, 1); + sp2 = SHIFTS_ALLOC (1); sp2->number = k; sp2->nshifts = 1; sp2->shifts[0] = nstates; @@ -505,8 +501,7 @@ augment_automaton (void) going to the next-to-final state (yet to be made). */ sp = first_shift; - sp2 = (shifts *) xcalloc (sizeof (shifts) - + sp->nshifts * sizeof (short), 1); + sp2 = SHIFTS_ALLOC (sp->nshifts + 1); sp2->nshifts = sp->nshifts + 1; /* Stick this shift into the vector at the proper place. */ @@ -539,7 +534,7 @@ augment_automaton (void) { /* The initial state didn't even have any shifts. Give it one shift, to the next-to-final state. */ - sp = XCALLOC (shifts, 1); + sp = SHIFTS_ALLOC (1); sp->nshifts = 1; sp->shifts[0] = nstates; @@ -557,7 +552,7 @@ augment_automaton (void) /* There are no shifts for any state. Make one shift, from the initial state to the next-to-final state. */ - sp = XCALLOC (shifts, 1); + sp = SHIFTS_ALLOC (1); sp->nshifts = 1; sp->shifts[0] = nstates; @@ -573,13 +568,13 @@ augment_automaton (void) /* Make the final state--the one that follows a shift from the next-to-final state. The symbol for that shift is 0 (end-of-file). */ - statep = (core *) xcalloc ((unsigned) (sizeof (core) - sizeof (short)), 1); + statep = CORE_ALLOC (0); statep->number = nstates; last_state->next = statep; last_state = statep; /* Make the shift from the final state to the termination state. */ - sp = XCALLOC (shifts, 1); + sp = SHIFTS_ALLOC (1); sp->number = nstates++; sp->nshifts = 1; sp->shifts[0] = nstates; @@ -591,7 +586,7 @@ augment_automaton (void) final_state = nstates; /* Make the termination state. */ - statep = (core *) xcalloc ((unsigned) (sizeof (core) - sizeof (short)), 1); + statep = CORE_ALLOC (0); statep->number = nstates++; last_state->next = statep; last_state = statep; @@ -630,8 +625,7 @@ save_reductions (void) if (count) { - p = (reductions *) xcalloc ((unsigned) (sizeof (reductions) + - (count - 1) * sizeof (short)), 1); + p = REDUCTIONS_ALLOC (count); p->number = this_state->number; p->nreds = count; diff --git a/src/conflicts.c b/src/conflicts.c index fd7af88c..2ca3cff8 100644 --- a/src/conflicts.c +++ b/src/conflicts.c @@ -90,7 +90,7 @@ resolve_sr_conflict (int state, int lookaheadnum) unsigned *fp1; unsigned *fp2; int redprec; - errs *errp = (errs *) xcalloc (sizeof (errs) + ntokens * sizeof (short), 1); + errs *errp = ERRS_ALLOC (ntokens + 1); short *errtokens = errp->errs; /* find the rule to reduce by to get precedence of reduction */ @@ -170,7 +170,7 @@ resolve_sr_conflict (int state, int lookaheadnum) /* Some tokens have been explicitly made errors. Allocate a permanent errs structure for this state, to record them. */ i = (char *) errtokens - (char *) errp; - err_table[state] = (errs *) xcalloc ((unsigned int) i, 1); + err_table[state] = ERRS_ALLOC (i + 1); bcopy (errp, err_table[state], i); } else diff --git a/src/state.h b/src/state.h index f4242555..4c60a271 100644 --- a/src/state.h +++ b/src/state.h @@ -99,7 +99,9 @@ typedef struct core } core; - +#define CORE_ALLOC(Nitems) \ + (core *) xcalloc ((unsigned) (sizeof (core) \ + + (Nitems - 1) * sizeof (short)), 1) typedef struct shifts { @@ -110,6 +112,9 @@ typedef struct shifts } shifts; +#define SHIFTS_ALLOC(Nshifts) \ + (shifts *) xcalloc ((unsigned) (sizeof (shifts) \ + + (Nshifts - 1) * sizeof (short)), 1) typedef struct errs @@ -119,6 +124,10 @@ typedef struct errs } errs; +#define ERRS_ALLOC(Nerrs) \ + (errs *) xcalloc ((unsigned) (sizeof (errs) \ + + (Nerrs - 1) * sizeof (short)), 1) + typedef struct reductions @@ -130,4 +139,8 @@ typedef struct reductions } reductions; +#define REDUCTIONS_ALLOC(Nreductions) \ + (reductions *) xcalloc ((unsigned) (sizeof (reductions) \ + + (Nreductions - 1) * sizeof (short)), 1) + #endif /* !STATE_H_ */ -- 2.45.2