Skip to content

Commit

Permalink
Merge pull request katef#474 from katef/kate/fsm_new_statealloc
Browse files Browse the repository at this point in the history
Add fsm_new_statealloc()
  • Loading branch information
katef authored May 30, 2024
2 parents 1732de7 + 748b7af commit 65d8624
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
9 changes: 9 additions & 0 deletions include/fsm/fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ struct fsm_end_ids {
struct fsm *
fsm_new(const struct fsm_options *opt);

/*
* As fsm_new(), but with an explicit number of pre-allocated states.
* This is intended to save on internal reallocations for situations
* where it's known in advance exactly how many states an FSM will have.
* You can still add more states per usual.
*/
struct fsm *
fsm_new_statealloc(const struct fsm_options *opt, size_t statealloc);

/*
* Free a structure created by fsm_new(), and all of its contents.
* No other pointers returned by this API are to be freed individually.
Expand Down
3 changes: 1 addition & 2 deletions src/libfsm/charset.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ fsm_intersect_charset(struct fsm *a, size_t n, const char *charset)
{
fsm_state_t state;

// TODO: pass .statealloc explicitly, we know it's 1. the default is overkill
b = fsm_new(a->opt);
b = fsm_new_statealloc(a->opt, 1);
if (b == NULL) {
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libfsm/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fsm_clone(const struct fsm *fsm)
assert(fsm != NULL);
assert(fsm->opt != NULL);

new = fsm_new(fsm->opt);
new = fsm_new_statealloc(fsm->opt, fsm->statecount);
if (new == NULL) {
return NULL;
}
Expand Down
14 changes: 12 additions & 2 deletions src/libfsm/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ free_contents(struct fsm *fsm)
}

struct fsm *
fsm_new(const struct fsm_options *opt)
fsm_new_statealloc(const struct fsm_options *opt, size_t statealloc)
{
static const struct fsm_options defaults;
struct fsm *new, f;

if (statealloc == 0) {
return fsm_new(opt);
}

if (opt == NULL) {
opt = &defaults;
}
Expand All @@ -60,7 +64,7 @@ fsm_new(const struct fsm_options *opt)
return NULL;
}

new->statealloc = FSM_DEFAULT_STATEALLOC;
new->statealloc = statealloc;
new->statecount = 0;
new->endcount = 0;
new->capture_info = NULL;
Expand Down Expand Up @@ -92,6 +96,12 @@ fsm_new(const struct fsm_options *opt)
return new;
}

struct fsm *
fsm_new(const struct fsm_options *opt)
{
return fsm_new_statealloc(opt, FSM_DEFAULT_STATEALLOC);
}

void
fsm_free(struct fsm *fsm)
{
Expand Down
2 changes: 1 addition & 1 deletion src/libfsm/reverse.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fsm_reverse(struct fsm *fsm)
if (fsm->endcount == 0 || !fsm_getstart(fsm, &prevstart)) {
struct fsm *new;

new = fsm_new(fsm->opt);
new = fsm_new_statealloc(fsm->opt, 1);
if (new == NULL) {
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/libre/re_strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ re_strings_build(struct re_strings *g,
}
}

/* TODO: count trie nodes and fsm_new_statealloc() */
fsm = fsm_new(opt);
if (fsm == NULL) {
goto error;
Expand Down

0 comments on commit 65d8624

Please sign in to comment.