diff options
author | Adrian Thurston <thurston@colm.net> | 2018-06-18 11:03:46 +0700 |
---|---|---|
committer | Adrian Thurston <thurston@colm.net> | 2018-06-18 11:03:46 +0700 |
commit | 90ff6d71d42811aef9b04326499d0e86a6d7953d (patch) | |
tree | 18212b2dd0af995f8efe7f75c86b090d944da795 | |
parent | 15ad961a04bb7ca85f1e9767239283b93414641e (diff) | |
download | colm-90ff6d71d42811aef9b04326499d0e86a6d7953d.tar.gz |
moved stream destructor code to respective callbacks
-rw-r--r-- | src/ctinput.cc | 4 | ||||
-rw-r--r-- | src/input.c | 88 | ||||
-rw-r--r-- | src/input.h | 51 |
3 files changed, 44 insertions, 99 deletions
diff --git a/src/ctinput.cc b/src/ctinput.cc index d85c5d44..09fe92e0 100644 --- a/src/ctinput.cc +++ b/src/ctinput.cc @@ -84,7 +84,7 @@ LangEl *inputStreamPatternGetLangEl( struct stream_impl_ct *ss, long *bindId, return klangEl; } -void inputStreamPatternDestructor( struct stream_impl_ct *ss ) +void inputStreamPatternDestructor( program_t *prg, tree_t **sp, struct stream_impl_ct *ss ) { } @@ -281,7 +281,7 @@ LangEl *inputStreamConsGetLangEl( struct stream_impl_ct *ss, long *bindId, char return klangEl; } -void inputStreamConsDestructor( struct stream_impl_ct *ss ) +void inputStreamConsDestructor( program_t *prg, tree_t **sp, struct stream_impl_ct *ss ) { } diff --git a/src/input.c b/src/input.c index 40052573..77f0bf03 100644 --- a/src/input.c +++ b/src/input.c @@ -116,9 +116,9 @@ static void transfer_loc_data( location_t *loc, struct stream_impl_data *ss ) } void colm_clear_source_stream( struct colm_program *prg, - tree_t **sp, struct stream_impl_seq *source_stream ) + tree_t **sp, struct stream_impl_seq *si ) { - struct run_buf *buf = source_stream->queue; + struct run_buf *buf = si->queue; while ( buf != 0 ) { switch ( buf->type ) { case RUN_BUF_DATA_TYPE: @@ -137,7 +137,7 @@ void colm_clear_source_stream( struct colm_program *prg, buf = next; } - source_stream->queue = 0; + si->queue = 0; } void colm_close_stream_file( FILE *file ) @@ -152,23 +152,9 @@ void colm_close_stream_file( FILE *file ) void colm_stream_destroy( program_t *prg, tree_t **sp, struct_t *s ) { stream_t *stream = (stream_t*) s; - colm_clear_source_stream( prg, sp, (struct stream_impl_seq*)stream->impl ); + struct stream_impl *si = stream->impl; - if ( stream->impl->file != 0 ) - colm_close_stream_file( stream->impl->file ); - - if ( stream->impl->collect != 0 ) { - str_collect_destroy( stream->impl->collect ); - free( stream->impl->collect ); - } - - /* FIXME: Need to leak this for now. Until we can return strings to a - * program loader and free them at a later date (after the colm program is - * deleted). */ - // if ( stream->impl->name != 0 ) - // free( stream->impl->name ); - - free( stream->impl ); + si->funcs->destructor( prg, sp, si ); } /* Keep the position up to date after consuming text. */ @@ -319,8 +305,25 @@ static int data_get_data( struct stream_impl_data *ss, char *dest, int length ) return copied; } -static void data_destructor( struct stream_impl_data *si ) +static void data_destructor( program_t *prg, tree_t **sp, struct stream_impl_data *si ) { +// colm_clear_source_stream( prg, sp, (struct stream_impl_seq*)stream->impl ); + + if ( si->file != 0 ) + colm_close_stream_file( si->file ); + + if ( si->collect != 0 ) { + str_collect_destroy( si->collect ); + free( si->collect ); + } + + /* FIXME: Need to leak this for now. Until we can return strings to a + * program loader and free them at a later date (after the colm program is + * deleted). */ + // if ( si->name != 0 ) + // free( si->name ); + + free( si ); } static int data_get_parse_block( struct stream_impl_data *ss, int skip, char **pdp, int *copied ) @@ -494,32 +497,6 @@ void init_stream_impl_data( struct stream_impl_data *is, char *name ) is->level = COLM_INDENT_OFF; } -void colm_clear_stream_impl( struct colm_program *prg, tree_t **sp, - struct stream_impl *input_stream ) -{ - struct run_buf *buf = input_stream->queue; - while ( buf != 0 ) { - switch ( buf->type ) { - case RUN_BUF_DATA_TYPE: - break; - - case RUN_BUF_TOKEN_TYPE: - case RUN_BUF_IGNORE_TYPE: - colm_tree_downref( prg, sp, buf->tree ); - break; - - case RUN_BUF_SOURCE_TYPE: - break; - } - - struct run_buf *next = buf->next; - free( buf ); - buf = next; - } - - input_stream->queue = 0; -} - static struct run_buf *input_stream_seq_pop_head( struct stream_impl_seq *is ) { struct run_buf *ret = is->queue; @@ -605,8 +582,25 @@ static void stream_unset_eof( struct stream_impl_seq *is ) } } -static void stream_destructor( struct stream_impl_seq *is ) +static void stream_destructor( program_t *prg, tree_t **sp, struct stream_impl_seq *si ) { + colm_clear_source_stream( prg, sp, si ); + + if ( si->file != 0 ) + colm_close_stream_file( si->file ); + + if ( si->collect != 0 ) { + str_collect_destroy( si->collect ); + free( si->collect ); + } + + /* FIXME: Need to leak this for now. Until we can return strings to a + * program loader and free them at a later date (after the colm program is + * deleted). */ + // if ( stream->impl->name != 0 ) + // free( stream->impl->name ); + + free( si ); } static int stream_get_parse_block( struct stream_impl_seq *is, int skip, char **pdp, int *copied ) diff --git a/src/input.h b/src/input.h index 39c5c7b8..1fff1d3f 100644 --- a/src/input.h +++ b/src/input.h @@ -75,59 +75,10 @@ struct run_buf struct run_buf *new_run_buf( int sz ); -/* Deleting a stream impl. */ -typedef void (*destructor_t)( struct stream_impl *si ); - -/* Main get of items. */ -typedef int (*get_parse_block_t)( struct stream_impl *si, int skip, char **pdp, int *copied ); - -/* Probably this should be replaced with get_parse_block calls. */ -typedef int (*get_data_t)( struct stream_impl *si, char *dest, int length ); - -/* Consuming data. */ -typedef int (*consume_data_t)( struct stream_impl *si, int length, struct colm_location *loc ); -typedef int (*undo_consume_data_t)( struct stream_impl *si, const char *data, int length ); - -/* Consuming trees. */ -typedef struct colm_tree *(*consume_tree_t)( struct stream_impl *si ); -typedef void (*undo_consume_tree_t)( struct stream_impl *si, - struct colm_tree *tree, int ignore ); - -/* Language elments (compile-time). */ -typedef struct LangEl *(*consume_lang_el_t)( struct stream_impl *si, - long *bind_id, char **data, long *length ); -typedef void (*undo_consume_lang_el_t)( struct stream_impl *si ); - -/* Private implmentation for some shared get data functions. */ -typedef int (*get_data_source_t)( struct stream_impl *si, char *dest, int length ); - -typedef void (*set_eof_t)( struct stream_impl *si ); -typedef void (*unset_eof_t)( struct stream_impl *si ); - -/* Prepending to a stream. */ -typedef void (*prepend_data_t)( struct stream_impl *si, const char *data, long len ); -typedef void (*prepend_tree_t)( struct stream_impl *si, struct colm_tree *tree, int ignore ); -typedef void (*prepend_stream_t)( struct stream_impl *si, struct colm_stream *stream ); - -/* Undoing prepend. */ -typedef int (*undo_prepend_data_t)( struct stream_impl *si, int length ); -typedef struct colm_tree *(*undo_prepend_tree_t)( struct stream_impl *si ); -typedef struct colm_tree *(*undo_prepend_stream_t)( struct stream_impl *si ); - -/* Appending to a stream. */ -typedef void (*append_data_t)( struct stream_impl *si, const char *data, long len ); -typedef void (*append_tree_t)( struct stream_impl *si, struct colm_tree *tree ); -typedef void (*append_stream_t)( struct stream_impl *si, struct colm_stream *stream ); - -/* Undoing append. */ -typedef struct colm_tree *(*undo_append_data_t)( struct stream_impl *si, int length ); -typedef struct colm_tree *(*undo_append_tree_t)( struct stream_impl *si ); -typedef struct colm_tree *(*undo_append_stream_t)( struct stream_impl *si ); - #define DEF_STREAM_FUNCS( stream_funcs, stream_impl ) \ struct stream_funcs \ { \ - void (*destructor)( struct stream_impl *si ); \ + void (*destructor)( struct colm_program *prg, struct colm_tree **sp, struct stream_impl *si ); \ int (*get_parse_block)( struct stream_impl *si, int skip, char **pdp, int *copied ); \ int (*get_data)( struct stream_impl *si, char *dest, int length ); \ int (*consume_data)( struct stream_impl *si, int length, struct colm_location *loc ); \ |