1 /* $Id: states.c,v 1.9 2005-01-15 21:45:42 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 #define TRAN_CHUNK 100
36 int init_DFA_states (struct DFA_states **dfasp, DFASetType st, int hash)
38 struct DFA_states *dfas;
42 dfas = (struct DFA_states *) imalloc (sizeof(struct DFA_states));
44 dfas->hasharray = (struct DFA_state **)
45 imalloc (sizeof(struct DFA_state*) * hash);
46 assert (dfas->hasharray);
48 dfas->freelist = dfas->unmarked = dfas->marked = NULL;
49 dfas->statemem = NULL;
54 dfas->transmem = tm = (struct DFA_trans *)
55 imalloc (sizeof(struct DFA_trans));
58 tm->size = TRAN_CHUNK;
60 tm->tran_block = (struct DFA_tran *)
61 imalloc (sizeof(struct DFA_tran) * tm->size);
62 assert (tm->tran_block);
64 dfas->sortarray = NULL;
65 for (i=0; i<dfas->hash; i++)
66 dfas->hasharray[i] = NULL;
70 int rm_DFA_states (struct DFA_states **dfasp)
72 struct DFA_states *dfas = *dfasp;
74 struct DFA_trans *tm, *tm1;
78 ifree (dfas->hasharray);
79 ifree (dfas->sortarray);
81 for (tm=dfas->transmem; tm; tm=tm1)
83 ifree (tm->tran_block);
87 for (sm=dfas->statemem; sm; sm=sm1)
89 ifree (sm->state_block);
98 int add_DFA_state (struct DFA_states *dfas, DFASet *s, struct DFA_state **sp)
101 struct DFA_state *si, **sip;
106 assert (dfas->hasharray);
107 sip = dfas->hasharray + (hash_DFASet (dfas->st, *s) % dfas->hash);
108 for (si = *sip; si; si=si->link)
109 if (eq_DFASet (dfas->st, si->set, *s))
112 *s = rm_DFASet (dfas->st, *s);
117 sb = (DFA_stateb *) imalloc (sizeof(*sb));
118 sb->next = dfas->statemem;
120 sb->state_block = si = dfas->freelist =
121 (struct DFA_state *) imalloc (sizeof(struct DFA_state)*DFA_CHUNK);
122 for (i = 0; i<DFA_CHUNK-1; i++, si++)
128 dfas->freelist = si->next;
130 si->next = dfas->unmarked;
136 si->no = (dfas->no)++;
144 void add_DFA_tran (struct DFA_states *dfas, struct DFA_state *s,
145 int ch0, int ch1, int to)
147 struct DFA_trans *tm;
151 if (tm->ptr == tm->size)
153 tm = (struct DFA_trans *) imalloc (sizeof(struct DFA_trans));
155 tm->next = dfas->transmem;
157 tm->size = s->tran_no >= TRAN_CHUNK ? s->tran_no+8 : TRAN_CHUNK;
158 tm->tran_block = (struct DFA_tran *)
159 imalloc (sizeof(struct DFA_tran) * tm->size);
160 assert (tm->tran_block);
162 memcpy (tm->tran_block, s->trans,
163 s->tran_no*sizeof (struct DFA_tran));
164 tm->ptr = s->tran_no;
165 s->trans = tm->tran_block;
168 t = tm->tran_block + tm->ptr++;
174 struct DFA_state *get_DFA_state (struct DFA_states *dfas)
176 struct DFA_state *si;
178 if (!(si = dfas->unmarked))
180 dfas->unmarked = si->next;
181 si->next = dfas->marked;
183 si->trans = dfas->transmem->tran_block + dfas->transmem->ptr;
188 void sort_DFA_states (struct DFA_states *dfas)
192 dfas->sortarray = (struct DFA_state **)
193 imalloc (sizeof(struct DFA_state *)*dfas->no);
194 for (s = dfas->marked; s; s=s->next)
195 dfas->sortarray[s->no] = s;
196 ifree (dfas->hasharray);
197 dfas->hasharray = NULL;