Skip to content

Commit 7296b7e

Browse files
committed
CVS v1.1 source
1 parent 0bec97d commit 7296b7e

29 files changed

+268
-391
lines changed

abbreviations.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2010, Bruce Ediger
2+
Copyright (C) 2010-2011, Bruce Ediger
33
44
This file is part of acl.
55
@@ -20,7 +20,7 @@
2020
*/
2121

2222

23-
/* $Id: abbreviations.c,v 1.6 2010/08/10 20:50:39 bediger Exp $ */
23+
/* $Id: abbreviations.c,v 1.8 2011/06/12 18:22:00 bediger Exp $ */
2424

2525
/*
2626
* Abbreviations: an identifier can reference an entire parse tree
@@ -60,7 +60,11 @@ abbreviation_lookup(const char *id)
6060
/* make an arena-allocated copy of the abbreviation, so that
6161
* reduce_graph() can destructively reduce the resulting
6262
* parse tree. */
63-
if (p) r = arena_copy_graph((struct node *)p);
63+
if (p)
64+
{
65+
preallocate_nodes(((struct node *)p)->tree_size);
66+
r = arena_copy_graph((struct node *)p);
67+
}
6468
return r;
6569
}
6670

@@ -112,10 +116,12 @@ copy_graph(struct node *p)
112116
case APPLICATION:
113117
r->left = copy_graph(p->left);
114118
r->right = copy_graph(p->right);
119+
r->tree_size = r->left->tree_size + r->right->tree_size + 1;
115120
break;
116121
case ATOM:
117122
r->rule = p->rule;
118123
r->left = r->right = NULL;
124+
r->tree_size = 1;
119125
break;
120126
}
121127
return r;

abbreviations.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2010, Bruce Ediger
2+
Copyright (C) 2010-2011, Bruce Ediger
33
44
This file is part of acl.
55
@@ -18,7 +18,7 @@
1818
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1919
2020
*/
21-
/* $Id: abbreviations.h,v 1.3 2010/04/02 03:43:39 bediger Exp $ */
21+
/* $Id: abbreviations.h,v 1.4 2011/06/12 18:19:11 bediger Exp $ */
2222

2323
void setup_abbreviation_table(struct hashtable *h);
2424

aho_corasick.c

+44-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2010, Bruce Ediger
2+
Copyright (C) 2010-2011, Bruce Ediger
33
44
This file is part of acl.
55
@@ -18,7 +18,7 @@
1818
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1919
2020
*/
21-
/* $Id: aho_corasick.c,v 1.3 2010/08/10 20:50:39 bediger Exp $ */
21+
/* $Id: aho_corasick.c,v 1.8 2011/06/12 18:22:00 bediger Exp $ */
2222

2323
/*
2424
* This code based on:
@@ -56,6 +56,7 @@ struct stack_elem {
5656
struct node *n; /* 1 */
5757
int state_at_n; /* 2 */
5858
int visited; /* 3 */
59+
int node_number;
5960
};
6061

6162
void set_output_length(struct gto *p, int state, int node_count);
@@ -258,7 +259,6 @@ construct_failure(struct gto *g)
258259
int i;
259260
struct queue *q;
260261

261-
262262
g->failure = malloc(g->ary_len * sizeof(int));
263263

264264
for (i = 0; i < g->ary_len; ++i)
@@ -394,6 +394,7 @@ algorithm_d(struct gto *g, struct node *t, int subject_node_count, int pat_path_
394394
{
395395
int top = 1;
396396
int matched = 0;
397+
int breadth_counter = 0;
397398
int next_state;
398399
int i;
399400
int *count;
@@ -415,23 +416,34 @@ algorithm_d(struct gto *g, struct node *t, int subject_node_count, int pat_path_
415416
stack[top].n = t;
416417
stack[top].state_at_n = next_state;
417418
stack[top].visited = 0;
419+
stack[top].node_number = breadth_counter++;
418420

419421
matched += tabulate(g, stack, top, next_state, pat_path_cnt, count);
420422

421423
if (!matched)
422424
{
423-
if (var_in_tree(t, abstr_var_name))
424-
next_state = g->delta[0][(int)'+'];
425-
else
426-
next_state = g->delta[0][(int)'-'];
427-
428-
matched += tabulate(g, stack, top, next_state, pat_path_cnt, count);
425+
if (any_var_in_tree(t))
426+
{
427+
if (var_in_tree(t, abstr_var_name))
428+
next_state = g->delta[0][(int)'+'];
429+
else
430+
next_state = g->delta[0][(int)'-'];
431+
matched += tabulate(g, stack, top, next_state, pat_path_cnt, count);
432+
} else {
433+
next_state = g->delta[0][(int)'!'];
434+
matched += tabulate(g, stack, top, next_state, pat_path_cnt, count);
435+
if (!matched)
436+
{
437+
next_state = g->delta[0][(int)'-'];
438+
matched += tabulate(g, stack, top, next_state, pat_path_cnt, count);
439+
}
440+
}
429441
}
430442

431443
while (!matched && top > 0)
432444
{
433445
struct node *next_node, *this_node = stack[top].n;
434-
int intstate, next_state, this_state = stack[top].state_at_n;
446+
int intstate, nxt_st, this_state = stack[top].state_at_n;
435447
int visited = stack[top].visited;
436448

437449
if (visited == 2 || this_node->typ == ATOM || top > g->max_node_count)
@@ -444,27 +456,35 @@ algorithm_d(struct gto *g, struct node *t, int subject_node_count, int pat_path_
444456
matched += tabulate(g, stack, top, intstate, pat_path_cnt, count);
445457

446458
next_node = (visited == 1)? this_node->left: this_node->right;
447-
next_state = intstate;
459+
nxt_st = intstate;
448460

449461
p = (next_node->name != abstr_var_name)? next_node->name: abstr_meta_var;
450462
while (*p)
451-
next_state = g->delta[next_state][(int)*p++];
463+
nxt_st = g->delta[nxt_st][(int)*p++];
452464

453465
++top;
454466
stack[top].n = next_node;
455-
stack[top].state_at_n = next_state;
467+
stack[top].state_at_n = nxt_st;
456468
stack[top].visited = 0;
469+
stack[top].node_number = breadth_counter++;
457470

458471
if (top <= g->max_node_count)
459472
{
460-
matched += tabulate(g, stack, top, next_state, pat_path_cnt, count);
461-
462-
if (var_in_tree(next_node, abstr_var_name))
463-
next_state = g->delta[intstate][(int)'+'];
464-
else
465-
next_state = g->delta[intstate][(int)'-'];
466-
467-
matched += tabulate(g, stack, top, next_state, pat_path_cnt, count);
473+
matched += tabulate(g, stack, top, nxt_st, pat_path_cnt, count);
474+
475+
if (any_var_in_tree(next_node))
476+
{
477+
if (var_in_tree(next_node, abstr_var_name))
478+
nxt_st = g->delta[intstate][(int)'+'];
479+
else
480+
nxt_st = g->delta[intstate][(int)'-'];
481+
} else {
482+
nxt_st = g->delta[intstate][(int)'!'];
483+
if (0 == nxt_st)
484+
nxt_st = g->delta[intstate][(int)'-'];
485+
}
486+
487+
matched += tabulate(g, stack, top, nxt_st, pat_path_cnt, count);
468488
}
469489
}
470490
}
@@ -494,11 +514,11 @@ tabulate(struct gto *g, struct stack_elem *stack, int top, int state, int pat_le
494514

495515
for (i = 0; r == 0 && i < oxt->len; ++i)
496516
{
497-
struct node *n = stack[top - oxt->out[i] + 1].n;
517+
int idx = top - oxt->out[i] + 1;
498518

499-
++count[n->node_number];
519+
++count[stack[idx].node_number];
500520

501-
if (count[n->node_number] == pat_leaf_count)
521+
if (count[stack[idx].node_number] == pat_leaf_count)
502522
r = 1;
503523
}
504524

aho_corasick.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2010, Bruce Ediger
2+
Copyright (C) 2010-2011, Bruce Ediger
33
44
This file is part of acl.
55
@@ -18,7 +18,7 @@
1818
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1919
2020
*/
21-
/* $Id: aho_corasick.h,v 1.2 2010/07/19 17:55:40 bediger Exp $ */
21+
/* $Id: aho_corasick.h,v 1.3 2011/06/12 18:19:11 bediger Exp $ */
2222

2323
struct output_extent {
2424
int *out; /* array of int, lengths of matched paths */

arena.c

+9-49
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2010, Bruce Ediger
2+
Copyright (C) 2010-2011, Bruce Ediger
33
44
This file is part of acl.
55
@@ -18,12 +18,11 @@
1818
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1919
2020
*/
21-
/* $Id: arena.c,v 1.2 2010/04/02 03:43:39 bediger Exp $ */
21+
/* $Id: arena.c,v 1.6 2011/06/12 18:22:00 bediger Exp $ */
2222

2323
#include <stdio.h>
2424
#include <unistd.h> /* getpagesize() */
2525
#include <stdlib.h> /* malloc(), free() */
26-
#include <assert.h> /* malloc(), free() */
2726

2827
#include <arena.h>
2928

@@ -50,7 +49,6 @@ struct memory_arena {
5049
int sz; /* max free size */
5150
int remaining; /* how many bytes remain in allocation */
5251
struct memory_arena *next;
53-
int usage_info;
5452
int sn; /* serial number: malloc order */
5553
};
5654

@@ -65,31 +63,29 @@ static int combosize = 0;
6563
* the malloc() of one or more pages, and a 2nd element in the stack.
6664
*/
6765
struct memory_arena *
68-
new_arena(int memory_info_flag)
66+
new_arena(void)
6967
{
7068
struct memory_arena *ra = NULL;
7169

72-
if (!pagesize)
73-
{
74-
combosize = sizeof(union combo);
75-
pagesize = getpagesize();
76-
headersize = roundup(sizeof(struct memory_arena), combosize);
77-
}
70+
/* This is not so costly that it can't happen
71+
* on every call to new_arena(). */
72+
combosize = sizeof(union combo);
73+
pagesize = getpagesize();
74+
headersize = roundup(sizeof(struct memory_arena), combosize);
7875

7976
ra = malloc(sizeof(*ra));
8077

8178
ra->sz = 0;
8279
ra->remaining = 0;
8380
ra->first_allocation = ra->next_allocation = NULL;
8481
ra->next = NULL;
85-
ra->usage_info = memory_info_flag;
8682
ra->sn = 0;
8783

8884
return ra;
8985
}
9086

9187
void
92-
deallocate_arena(struct memory_arena *ma, int memory_info_flag)
88+
deallocate_arena(struct memory_arena *ma)
9389
{
9490
int free_arena_cnt = 0;
9591
int maxsz = 0;
@@ -105,52 +101,18 @@ deallocate_arena(struct memory_arena *ma, int memory_info_flag)
105101
ma = tmp;
106102
++free_arena_cnt;
107103
}
108-
109-
if (memory_info_flag)
110-
{
111-
fprintf(stderr, "Page size %d (0x%x)\n", pagesize, pagesize);
112-
fprintf(stderr, "Header size %d (0x%x)\n", headersize, headersize);
113-
fprintf(stderr, "Allocated %d arenas\n", free_arena_cnt);
114-
fprintf(stderr, "Max arena size %d (0x%x)\n", maxsz, maxsz);
115-
}
116104
}
117105

118106
void
119107
free_arena_contents(struct memory_arena *ma)
120108
{
121-
int last_sn = ma->sn;
122-
int max_sn = ma->sn;
123109
int arena_count = 0;
124110

125111
ma = ma->next; /* dummy head node */
126112
while (ma)
127113
{
128114
++arena_count;
129115

130-
if (ma->sn >= last_sn + 1)
131-
{
132-
fprintf(stderr, "Loop in chain of allocations: S/N %d, next S/N %d\n",
133-
last_sn, ma->sn);
134-
fprintf(stderr, "Allocated %d arenas\n", max_sn);
135-
break;
136-
} else
137-
last_sn = ma->sn;
138-
139-
if (ma->sn > max_sn)
140-
{
141-
fprintf(stderr, "Chain of allocations out of order: max S/N %d, S/N %d\n",
142-
max_sn, ma->sn);
143-
fprintf(stderr, "Allocated %d arenas\n", max_sn);
144-
break;
145-
}
146-
147-
if (arena_count > max_sn)
148-
{
149-
fprintf(stderr, "Found at least %d allocations on chain, should only have %d\n",
150-
arena_count, max_sn);
151-
break;
152-
}
153-
154116
ma->remaining = ma->sz;
155117
ma->next_allocation = ma->first_allocation;
156118
ma = ma->next;
@@ -164,8 +126,6 @@ arena_alloc(struct memory_arena *ma, size_t size)
164126
struct memory_arena *ca;
165127
size_t nsize;
166128

167-
assert(NULL != ma);
168-
169129
/* What you actually have to allocate to get to
170130
* a block "suitably aligned" for any use. */
171131
nsize = roundup(size, combosize);

arena.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2010, Bruce Ediger
2+
Copyright (C) 2010-2011, Bruce Ediger
33
44
This file is part of acl.
55
@@ -18,11 +18,11 @@
1818
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1919
2020
*/
21-
/* $Id: arena.h,v 1.2 2010/04/02 03:43:39 bediger Exp $ */
21+
/* $Id: arena.h,v 1.5 2011/06/12 18:19:11 bediger Exp $ */
2222

23-
struct memory_arena *new_arena(int memory_info_flag);
23+
struct memory_arena *new_arena(void);
2424
void free_arena_contents(struct memory_arena *ma);
25-
void deallocate_arena(struct memory_arena *ma, int memory_info_flag);
25+
void deallocate_arena(struct memory_arena *ma);
2626

2727
void *arena_alloc(struct memory_arena *ma, size_t size);
2828

atom.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2010, Bruce Ediger
2+
Copyright (C) 2010-2011, Bruce Ediger
33
44
This file is part of acl.
55
@@ -18,7 +18,7 @@
1818
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1919
2020
*/
21-
/* $Id: atom.c,v 1.4 2010/08/10 20:50:39 bediger Exp $ */
21+
/* $Id: atom.c,v 1.5 2011/06/12 18:22:00 bediger Exp $ */
2222

2323
/*
2424
* A stripped-down "atom" datatype, a la "C Interfaces and Implementation".

0 commit comments

Comments
 (0)