From 2076944268331c1938ffd0700155c8e7936c19ef Mon Sep 17 00:00:00 2001 From: axel Date: Thu, 22 Jun 2006 00:03:44 +1000 Subject: [PATCH] Use the intern function to share the strings used to describe various key bindings. This saves both performance, code size and memory use, but there is a pathological case where the user continually changes key bindings, resulting in more allocated memory than needed. darcs-hash:20060621140344-ac50b-c7eb89a94a96538215f9a6737f8e4bacd6a801fb.gz --- input.c | 34 ++++++++++++---------------------- input.h | 2 +- main.c | 3 ++- reader.c | 2 +- reader.h | 2 +- 5 files changed, 17 insertions(+), 26 deletions(-) diff --git a/input.c b/input.c index a67ecd644..84ba51202 100644 --- a/input.c +++ b/input.c @@ -61,6 +61,7 @@ implementation in fish is as of yet incomplete. #include "signal.h" #include "translate.h" #include "output.h" +#include "intern.h" static void input_read_inputrc( wchar_t *fn ); @@ -71,9 +72,9 @@ static void input_read_inputrc( wchar_t *fn ); typedef struct { - wchar_t *seq; /**< Character sequence which generates this event */ - wchar_t *seq_desc; /**< Description of the character sequence suitable for printing on-screen */ - wchar_t *command; /**< command that should be evaluated by this mapping */ + const wchar_t *seq; /**< Character sequence which generates this event */ + const wchar_t *seq_desc; /**< Description of the character sequence suitable for printing on-screen */ + const wchar_t *command; /**< command that should be evaluated by this mapping */ } mapping; @@ -237,7 +238,7 @@ static int inputrc_error = 0; */ static int is_init = 0; -wchar_t input_get_code( wchar_t *name ) +wchar_t input_get_code( const wchar_t *name ) { int i; @@ -340,18 +341,16 @@ void add_mapping( const wchar_t *mode, mapping *m = (mapping *)al_get( mappings, i ); if( wcscmp( m->seq, s ) == 0 ) { - free( m->command ); - free( m->seq_desc ); - m->seq_desc = wcsdup(d ); - m->command=wcsdup(c); + m->seq_desc = intern(d); + m->command = intern(c); return; } } mapping *m = malloc( sizeof( mapping ) ); - m->seq = wcsdup( s ); - m->seq_desc = wcsdup(d ); - m->command=wcsdup(c); + m->seq = intern( s ); + m->seq_desc = intern(d ); + m->command = intern(c); al_push( mappings, m ); } @@ -1427,20 +1426,11 @@ int input_init() */ static void destroy_mapping( void *key, void *val ) { - int i; array_list_t *mappings = (array_list_t *)val; - for( i=0; iseq ); - free( m->seq_desc ); - - free( m->command ); - free(m ); - } - + al_foreach( mappings, &free ); al_destroy( mappings ); + free((void *)key); free((void *)val); } diff --git a/input.h b/input.h index 8b3a3175e..ff6bb1cb1 100644 --- a/input.h +++ b/input.h @@ -111,6 +111,6 @@ void input_parse_inputrc_line( wchar_t *cmd ); /** Returns the function for the given function name. */ -wchar_t input_get_code( wchar_t *name ); +wchar_t input_get_code( const wchar_t *name ); #endif diff --git a/main.c b/main.c index 8cbdcc479..5c3b32cfd 100644 --- a/main.c +++ b/main.c @@ -117,6 +117,8 @@ int main( int argc, char **argv ) char *cmd=0; + halloc_util_init(); + wsetlocale( LC_ALL, L"" ); is_interactive_session=1; program_name=L"fish"; @@ -258,7 +260,6 @@ int main( int argc, char **argv ) no_exec = 0; } - halloc_util_init(); proc_init(); event_init(); diff --git a/reader.c b/reader.c index 4c7a7475c..b39b44426 100644 --- a/reader.c +++ b/reader.c @@ -2080,7 +2080,7 @@ int reader_get_cursor_pos() } -void reader_run_command( wchar_t *cmd ) +void reader_run_command( const wchar_t *cmd ) { wchar_t *ft; diff --git a/reader.h b/reader.h index ec67266d6..2c6c0c1f7 100644 --- a/reader.h +++ b/reader.h @@ -72,7 +72,7 @@ void repaint(); Run the specified command with the correct terminal modes, and while taking care to perform job notification, set the title, etc. */ -void reader_run_command( wchar_t *buff ); +void reader_run_command( const wchar_t *buff ); /** Get the string of character currently entered into the command