From 300f275f23b65c09f69a437c635c48ddd126f21a Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Mon, 19 Nov 2001 10:31:49 +0000 Subject: [PATCH] * src/system.h (LIST_FREE, shortcpy): New. * src/LR0.c: Use them. * src/output.c (free_itemsets, free_reductions, free_shifts): Remove, replaced by LIST_FREE. --- ChangeLog | 8 ++++++++ src/LR0.c | 30 ++++-------------------------- src/lalr.c | 9 +-------- src/output.c | 44 +++----------------------------------------- src/system.h | 15 +++++++++++++++ 5 files changed, 31 insertions(+), 75 deletions(-) diff --git a/ChangeLog b/ChangeLog index d833a18c..f67485eb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2001-11-19 Akim Demaille + + * src/system.h (LIST_FREE, shortcpy): New. + * src/LR0.c: Use them. + * src/output.c (free_itemsets, free_reductions, free_shifts): + Remove, replaced by LIST_FREE. + + 2001-11-19 Akim Demaille * src/state.h (CORE_ALLOC, SHIFTS_ALLOC, ERRS_ALLOC) diff --git a/src/LR0.c b/src/LR0.c index e3db49aa..db42fe30 100644 --- a/src/LR0.c +++ b/src/LR0.c @@ -196,9 +196,6 @@ new_state (int symbol) { int n; core *p; - short *isp1; - short *isp2; - short *iend; #if TRACE fprintf (stderr, "Entering new_state, symbol = %d, state = %d\n", @@ -208,18 +205,14 @@ new_state (int symbol) if (nstates >= MAXSHORT) fatal (_("too many states (max %d)"), MAXSHORT); - isp1 = kernel_base[symbol]; - iend = kernel_end[symbol]; - n = iend - isp1; + n = kernel_end[symbol] - kernel_base[symbol]; p = CORE_ALLOC (n); p->accessing_symbol = symbol; p->number = nstates; p->nitems = n; - isp2 = p->items; - while (isp1 < iend) - *isp2++ = *isp1++; + shortcpy (p->items, kernel_base[symbol], n); last_state->next = p; last_state = p; @@ -360,21 +353,13 @@ static void save_shifts (void) { shifts *p; - short *sp1; - short *sp2; - short *send; p = SHIFTS_ALLOC (nshifts); p->number = this_state->number; p->nshifts = nshifts; - sp1 = shiftset; - sp2 = p->shifts; - send = shiftset + nshifts; - - while (sp1 < send) - *sp2++ = *sp1++; + shortcpy (p->shifts, shiftset, nshifts); if (last_shift) { @@ -603,8 +588,6 @@ static void save_reductions (void) { short *isp; - short *rp1; - short *rp2; int item; int count; reductions *p; @@ -630,12 +613,7 @@ save_reductions (void) p->number = this_state->number; p->nreds = count; - rp1 = redset; - rp2 = p->rules; - rend = rp1 + count; - - for (/* nothing */; rp1 < rend; ++rp1, ++rp2) - *rp2 = *rp1; + shortcpy (p->rules, redset, count); if (last_reduction) { diff --git a/src/lalr.c b/src/lalr.c index 46e1a6d5..42cd05ef 100644 --- a/src/lalr.c +++ b/src/lalr.c @@ -653,14 +653,7 @@ compute_lookaheads (void) /* Free LOOKBACK. */ for (i = 0; i < state_table[nstates].lookaheads; i++) - { - shorts *sptmp; - for (sp = lookback[i]; sp; sp = sptmp) - { - sptmp = sp->next; - XFREE (sp); - } - } + LIST_FREE (shorts, lookback[i]); XFREE (lookback); XFREE (F); diff --git a/src/output.c b/src/output.c index 87ab880e..43172154 100644 --- a/src/output.c +++ b/src/output.c @@ -567,33 +567,6 @@ token_actions (void) } -static void -free_shifts (void) -{ - shifts *sp, *sptmp; /* JF derefrenced freed ptr */ - - for (sp = first_shift; sp; sp = sptmp) - { - sptmp = sp->next; - XFREE (sp); - } -} - - -static void -free_reductions (void) -{ - reductions *rp, *rptmp; /* JF fixed freed ptr */ - - for (rp = first_reduction; rp; rp = rptmp) - { - rptmp = rp->next; - XFREE (rp); - } -} - - - static void save_column (int symbol, int default_state) { @@ -946,8 +919,8 @@ output_actions (void) width = XCALLOC (short, nvectors); token_actions (); - free_shifts (); - free_reductions (); + LIST_FREE (shifts, first_shift); + LIST_FREE (reductions, first_reduction); XFREE (LA); XFREE (LAruleno); @@ -1045,17 +1018,6 @@ output_master_parser (void) } -static void -free_itemsets (void) -{ - core *cp, *cptmp; - for (cp = first_state; cp; cp = cptmp) - { - cptmp = cp->next; - XFREE (cp); - } -} - /* FIXME. */ #define MUSCLE_INSERT_INT(Key, Value) \ @@ -1115,7 +1077,7 @@ output (void) { obstack_init (&output_obstack); - free_itemsets (); + LIST_FREE (core, first_state); output_token_translations (); output_gram (); diff --git a/src/system.h b/src/system.h index bfd24463..b8a73edb 100644 --- a/src/system.h +++ b/src/system.h @@ -279,6 +279,21 @@ do { \ #endif +/* As memcpy, but for shorts. */ +#define shortcpy(Dest, Src, Num) \ + memcpy (Dest, Src, Num * sizeof (short)) + +/* Free a linked list. */ +#define LIST_FREE(Type, List) \ +do { \ + Type *_node, *_next; \ + for (_node = List; _node; _node = _next) \ + { \ + _next = _node->next; \ + XFREE (_node); \ + } \ +} while (0) + /*---------------------------------. | Debugging the memory allocator. | `---------------------------------*/ -- 2.47.2