-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fsm_vacuum, which reduces the state array when over-allocated.
- Loading branch information
1 parent
073305b
commit 94ffdcc
Showing
6 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2024 Scott Vokes | ||
* | ||
* See LICENCE for the full copyright terms. | ||
*/ | ||
|
||
#include <stdlib.h> | ||
#include <fsm/fsm.h> | ||
#include <fsm/alloc.h> | ||
|
||
#include <assert.h> | ||
|
||
#include "internal.h" | ||
|
||
enum fsm_vacuum_res | ||
fsm_vacuum(struct fsm *fsm) | ||
{ | ||
assert(fsm != NULL); | ||
assert(fsm->statecount <= fsm->statealloc); | ||
size_t nceil = fsm->statealloc; | ||
|
||
while ((fsm->statecount < nceil/2) && nceil > 1) { | ||
nceil /= 2; | ||
} | ||
|
||
if (nceil == fsm->statealloc) { | ||
return FSM_VACUUM_NO_CHANGE; | ||
} | ||
|
||
struct fsm_state *nstates = f_realloc(fsm->opt->alloc, | ||
fsm->states, nceil * sizeof(nstates[0])); | ||
if (nstates == NULL) { | ||
return FSM_VACUUM_ERROC_REALLOC; | ||
} | ||
|
||
fsm->states = nstates; | ||
fsm->statealloc = nceil; | ||
|
||
return FSM_VACUUM_REDUCED_MEMORY; | ||
} |