diff --git a/src/engine/builtins.c b/src/engine/builtins.c index a0c2f0c42..b1d9feca4 100644 --- a/src/engine/builtins.c +++ b/src/engine/builtins.c @@ -432,7 +432,7 @@ LIST * builtin_calc( FRAME * frame, int flags ) { LIST * arg = lol_get( frame->args, 0 ); - LIST * result = 0; + LIST * result = L0; long lhs_value; long rhs_value; long result_value; @@ -440,17 +440,18 @@ LIST * builtin_calc( FRAME * frame, int flags ) char const * lhs; char const * op; char const * rhs; + LISTITER iter = list_begin( arg ), end = list_end( arg ); - if ( arg == 0 ) return L0; - lhs = object_str( arg->value ); + if ( iter == end ) return L0; + lhs = object_str( list_item( iter ) ); - arg = list_next( arg ); - if ( arg == 0 ) return L0; - op = object_str( arg->value ); + iter = list_next( iter ); + if ( iter == end ) return L0; + op = object_str( list_item( iter ) ); - arg = list_next( arg ); - if ( arg == 0 ) return L0; - rhs = object_str( arg->value ); + iter = list_next( iter ); + if ( iter == end ) return L0; + rhs = object_str( list_item( iter ) ); lhs_value = atoi( lhs ); rhs_value = atoi( rhs ); @@ -486,11 +487,12 @@ LIST * builtin_depends( FRAME * frame, int flags ) { LIST * targets = lol_get( frame->args, 0 ); LIST * sources = lol_get( frame->args, 1 ); - LIST * l; - - for ( l = targets; l; l = list_next( l ) ) + LISTITER iter, end; + + iter = list_begin( targets ), end = list_end( targets ); + for ( ; iter != end; iter = list_next( iter ) ) { - TARGET * t = bindtarget( l->value ); + TARGET * t = bindtarget( list_item( iter ) ); /* If doing INCLUDES, switch to the TARGET's include */ /* TARGET, creating it if needed. The internal include */ @@ -510,9 +512,10 @@ LIST * builtin_depends( FRAME * frame, int flags ) } /* Enter reverse links */ - for ( l = sources; l; l = list_next( l ) ) + iter = list_begin( sources ), end = list_end( sources ); + for ( ; iter != end; iter = list_next( iter ) ) { - TARGET * s = bindtarget( l->value ); + TARGET * s = bindtarget( list_item( iter ) ); s->dependants = targetlist( s->dependants, targets ); } @@ -532,11 +535,11 @@ LIST * builtin_rebuilds( FRAME * frame, int flags ) { LIST * targets = lol_get( frame->args, 0 ); LIST * rebuilds = lol_get( frame->args, 1 ); - LIST * l; + LISTITER iter = list_begin( targets ), end = list_end( targets ); - for ( l = targets; l; l = list_next( l ) ) + for ( ; iter != end; iter = list_next( iter ) ) { - TARGET * t = bindtarget( l->value ); + TARGET * t = bindtarget( list_item( iter ) ); t->rebuilds = targetlist( t->rebuilds, rebuilds ); } @@ -569,11 +572,12 @@ LIST * builtin_echo( FRAME * frame, int flags ) LIST * builtin_exit( FRAME * frame, int flags ) { + LIST * code = lol_get( frame->args, 1 ); list_print( lol_get( frame->args, 0 ) ); printf( "\n" ); - if ( lol_get( frame->args, 1 ) ) + if ( !list_empty( code ) ) { - exit( atoi( object_str( lol_get( frame->args, 1 )->value ) ) ); + exit( atoi( object_str( list_front( code ) ) ) ); } else { @@ -593,8 +597,9 @@ LIST * builtin_exit( FRAME * frame, int flags ) LIST * builtin_flags( FRAME * frame, int flags ) { LIST * l = lol_get( frame->args, 0 ); - for ( ; l; l = list_next( l ) ) - bindtarget( l->value )->flags |= flags; + LISTITER iter = list_begin( l ), end = list_end( l ); + for ( ; iter != end; iter = list_next( iter ) ) + bindtarget( list_item( iter ) )->flags |= flags; return L0; } @@ -632,6 +637,7 @@ static void builtin_glob_back LIST * l; PATHNAME f; string buf[ 1 ]; + LISTITER iter, end; /* Null out directory for matching. We wish we had file_dirscan() pass up a * PATHNAME. @@ -656,9 +662,10 @@ static void builtin_glob_back if ( globbing->case_insensitive ) downcase_inplace( buf->value ); - for ( l = globbing->patterns; l; l = l->next ) + iter = list_begin( globbing->patterns ), end = list_end( globbing->patterns ); + for ( ; iter != end; iter = list_next( iter ) ) { - if ( !glob( object_str( l->value ), buf->value ) ) + if ( !glob( object_str( list_item( iter ) ), buf->value ) ) { globbing->results = list_new( globbing->results, object_copy( file ) ); break; @@ -673,17 +680,17 @@ static void builtin_glob_back static LIST * downcase_list( LIST * in ) { - LIST * result = 0; + LIST * result = L0; + LISTITER iter = list_begin( in ), end = list_end( in ); string s[ 1 ]; string_new( s ); - while ( in ) + for ( ; iter != end; iter = list_next( iter ) ) { - string_copy( s, object_str( in->value ) ); + string_copy( s, object_str( list_item( iter ) ) ); downcase_inplace( s->value ); - result = list_append( result, list_new( 0, object_new( s->value ) ) ); - in = in->next; + result = list_append( result, list_new( L0, object_new( s->value ) ) ); } string_free( s ); @@ -696,6 +703,7 @@ LIST * builtin_glob( FRAME * frame, int flags ) LIST * l = lol_get( frame->args, 0 ); LIST * r = lol_get( frame->args, 1 ); + LISTITER iter, end; struct globbing globbing; globbing.results = L0; @@ -711,8 +719,9 @@ LIST * builtin_glob( FRAME * frame, int flags ) if ( globbing.case_insensitive ) globbing.patterns = downcase_list( r ); - for ( ; l; l = list_next( l ) ) - file_dirscan( l->value, builtin_glob_back, &globbing ); + iter = list_begin( l ), end = list_end( l ); + for ( ; iter != end; iter = list_next( iter ) ) + file_dirscan( list_item( iter ), builtin_glob_back, &globbing ); if ( globbing.case_insensitive ) list_free( globbing.patterns ); @@ -812,24 +821,24 @@ LIST * glob_recursive( const char * pattern ) if ( has_wildcards( basename->value ) ) { - LIST * d; OBJECT * b = object_new( basename->value ); - for ( d = dirs ; d; d = d->next ) - result = list_append( result, glob1( d->value, b ) ); + LISTITER iter = list_begin( dirs ), end = list_end( dirs ); + for ( ; iter != end; iter = list_next( iter ) ) + result = list_append( result, glob1( list_item( iter ), b ) ); object_free( b ); } else { - LIST * d; + LISTITER iter = list_begin( dirs ), end = list_end( dirs ); string file_string[ 1 ]; string_new( file_string ); /* No wildcard in basename. */ - for ( d = dirs ; d; d = d->next ) + for ( ; iter != end; iter = list_next( iter ) ) { OBJECT * p; - path->f_dir.ptr = object_str( d->value ); - path->f_dir.len = strlen( object_str( d->value ) ); + path->f_dir.ptr = object_str( list_item( iter ) ); + path->f_dir.len = strlen( object_str( list_item( iter ) ) ); path_build( path, file_string, 0 ); p = object_new( file_string->value ); @@ -866,8 +875,9 @@ LIST * builtin_glob_recursive( FRAME * frame, int flags ) { LIST * result = L0; LIST * l = lol_get( frame->args, 0 ); - for ( ; l; l = l->next ) - result = list_append( result, glob_recursive( object_str( l->value ) ) ); + LISTITER iter = list_begin( l ), end = list_end( l ); + for ( ; iter != end; iter = list_next( iter ) ) + result = list_append( result, glob_recursive( object_str( list_item( iter ) ) ) ); return result; } @@ -880,22 +890,27 @@ LIST * builtin_match( FRAME * frame, int flags ) { LIST * l; LIST * r; - LIST * result = 0; + LIST * result = L0; + LISTITER l_iter, l_end, r_iter, r_end; string buf[ 1 ]; string_new( buf ); /* For each pattern */ - for ( l = lol_get( frame->args, 0 ); l; l = l->next ) + l = lol_get( frame->args, 0 ); + l_iter = list_begin( l ), l_end = list_end( l ); + for (; l_iter != l_end; l_iter = list_next( l_iter ) ) { /* Result is cached and intentionally never freed. */ - regexp * re = regex_compile( l->value ); + regexp * re = regex_compile( list_item( l_iter ) ); /* For each string to match against. */ - for ( r = lol_get( frame->args, 1 ); r; r = r->next ) + r = lol_get( frame->args, 1 ); + r_iter = list_begin( r ), r_end = list_end( r ); + for ( ; r_iter != r_end; r_iter = list_next( r_iter ) ) { - if ( regexec( re, object_str( r->value ) ) ) + if ( regexec( re, object_str( list_item( r_iter ) ) ) ) { int i; int top; @@ -931,10 +946,10 @@ LIST * builtin_split_by_characters( FRAME * frame, int flags ) string buf[ 1 ]; - const char * delimiters = object_str( l2->value ); + const char * delimiters = object_str( list_front( l2 ) ); char * t; - string_copy( buf, object_str( l1->value ) ); + string_copy( buf, object_str( list_front( l1 ) ) ); t = strtok( buf->value, delimiters) ; while ( t ) @@ -951,15 +966,16 @@ LIST * builtin_split_by_characters( FRAME * frame, int flags ) LIST * builtin_hdrmacro( FRAME * frame, int flags ) { LIST * l = lol_get( frame->args, 0 ); + LISTITER iter = list_begin( l ), end = list_end( l ); - for ( ; l; l = list_next( l ) ) + for ( ; iter != end; iter = list_next( iter ) ) { - TARGET * t = bindtarget( l->value ); + TARGET * t = bindtarget( list_item( iter ) ); /* Scan file for header filename macro definitions. */ if ( DEBUG_HEADER ) printf( "scanning '%s' for header file macro definitions\n", - object_str( l->value ) ); + object_str( list_item( iter ) ) ); macro_headers( t ); } @@ -988,7 +1004,7 @@ LIST * builtin_rulenames( FRAME * frame, int flags ) { LIST * arg0 = lol_get( frame->args, 0 ); LIST * result = L0; - module_t * source_module = bindmodule( arg0 ? arg0->value : 0 ); + module_t * source_module = bindmodule( !list_empty( arg0 ) ? list_front( arg0 ) : 0 ); if ( source_module->rules ) hashenumerate( source_module->rules, add_rule_name, &result ); @@ -1017,7 +1033,7 @@ LIST * builtin_varnames( FRAME * frame, int flags ) { LIST * arg0 = lol_get( frame->args, 0 ); LIST * result = L0; - module_t * source_module = bindmodule( arg0 ? arg0->value : 0 ); + module_t * source_module = bindmodule( !list_empty(arg0) ? list_front(arg0) : 0 ); struct hash * vars = source_module->variables; @@ -1037,7 +1053,7 @@ LIST * builtin_delete_module( FRAME * frame, int flags ) { LIST * arg0 = lol_get( frame->args, 0 ); LIST * result = L0; - module_t * source_module = bindmodule( arg0 ? arg0->value : 0 ); + module_t * source_module = bindmodule( !list_empty(arg0) ? list_front(arg0) : 0 ); delete_module( source_module ); return result; } @@ -1090,27 +1106,27 @@ LIST * builtin_import( FRAME * frame, int flags ) LIST * localize = lol_get( frame->args, 4 ); module_t * target_module = - bindmodule( target_module_list ? target_module_list->value : 0 ); + bindmodule( !list_empty( target_module_list ) ? list_front( target_module_list ) : 0 ); module_t * source_module = - bindmodule( source_module_list ? source_module_list->value : 0 ); + bindmodule( !list_empty( source_module_list ) ? list_front( source_module_list ) : 0 ); - LIST * source_name; - LIST * target_name; + LISTITER source_iter = list_begin( source_rules ), source_end = list_end( source_rules ); + LISTITER target_iter = list_begin( target_rules ), target_end = list_end( target_rules ); - for ( source_name = source_rules, target_name = target_rules; - source_name && target_name; - source_name = list_next( source_name ), - target_name = list_next( target_name ) ) + for ( ; + source_iter != source_end && target_iter != target_end; + source_iter = list_next( source_iter ), + target_iter = list_next( target_iter ) ) { RULE * r; RULE * imported; if ( !source_module->rules || - !(r = (RULE *)hash_find( source_module->rules, source_name->value ) ) ) - unknown_rule( frame, "IMPORT", source_module, source_name->value ); + !(r = (RULE *)hash_find( source_module->rules, list_item( source_iter ) ) ) ) + unknown_rule( frame, "IMPORT", source_module, list_item( source_iter ) ); - imported = import_rule( r, target_module, target_name->value ); - if ( localize ) + imported = import_rule( r, target_module, list_item( target_iter ) ); + if ( !list_empty( localize ) ) imported->module = target_module; /* This rule is really part of some other module. Just refer to it here, * but do not let it out. @@ -1118,7 +1134,7 @@ LIST * builtin_import( FRAME * frame, int flags ) imported->exported = 0; } - if ( source_name || target_name ) + if ( source_iter != source_end || target_iter != target_end ) { backtrace_line( frame->prev ); printf( "import error: length of source and target rule name lists don't match!\n" ); @@ -1147,14 +1163,15 @@ LIST * builtin_export( FRAME * frame, int flags ) { LIST * module_list = lol_get( frame->args, 0 ); LIST * rules = lol_get( frame->args, 1 ); - module_t * m = bindmodule( module_list ? module_list->value : 0 ); + module_t * m = bindmodule( !list_empty( module_list ) ? list_front( module_list ) : 0 ); - for ( ; rules; rules = list_next( rules ) ) + LISTITER iter = list_begin( rules ), end = list_end( rules ); + for ( ; iter != end; iter = list_next( iter ) ) { RULE * r; - if ( !m->rules || !(r = (RULE *)hash_find( m->rules, rules->value ) ) ) - unknown_rule( frame, "EXPORT", m, rules->value ); + if ( !m->rules || !(r = (RULE *)hash_find( m->rules, list_item( iter ) ) ) ) + unknown_rule( frame, "EXPORT", m, list_item( iter ) ); r->exported = 1; } @@ -1244,7 +1261,7 @@ void backtrace( FRAME * frame ) LIST * builtin_backtrace( FRAME * frame, int flags ) { LIST * levels_arg = lol_get( frame->args, 0 ); - int levels = levels_arg ? atoi( object_str( levels_arg->value ) ) : (int)( (unsigned int)(-1) >> 1 ) ; + int levels = !list_empty( levels_arg ) ? atoi( object_str( list_front( levels_arg ) ) ) : (int)( (unsigned int)(-1) >> 1 ) ; LIST * result = L0; for ( ; ( frame = frame->prev ) && levels ; --levels ) @@ -1286,7 +1303,7 @@ LIST * builtin_backtrace( FRAME * frame, int flags ) LIST * builtin_caller_module( FRAME * frame, int flags ) { LIST * levels_arg = lol_get( frame->args, 0 ); - int levels = levels_arg ? atoi( object_str( levels_arg->value ) ) : 0 ; + int levels = !list_empty( levels_arg ) ? atoi( object_str( list_front( levels_arg ) ) ) : 0 ; int i; for ( i = 0; ( i < levels + 2 ) && frame->prev; ++i ) @@ -1319,9 +1336,10 @@ LIST * builtin_update( FRAME * frame, int flags ) { LIST * result = list_copy( L0, targets_to_update() ); LIST * arg1 = lol_get( frame->args, 0 ); + LISTITER iter = list_begin( arg1 ), end = list_end( arg1 ); clear_targets_to_update(); - for ( ; arg1; arg1 = list_next( arg1 ) ) - mark_target_for_updating( object_copy( arg1->value ) ); + for ( ; iter != end; iter = list_next( iter ) ) + mark_target_for_updating( object_copy( list_item( iter ) ) ); return result; } @@ -1350,11 +1368,12 @@ LIST * builtin_update_now( FRAME * frame, int flags ) int i; int original_noexec = 0; int original_quitquick = 0; + LISTITER iter, end; - if ( log ) + if ( !list_empty( log ) ) { - int fd = atoi( object_str( log->value ) ); + int fd = atoi( object_str( list_front( log ) ) ); /* Redirect stdout and stderr, temporary, to the log file. */ original_stdout = dup( 0 ); original_stderr = dup( 1 ); @@ -1362,7 +1381,7 @@ LIST * builtin_update_now( FRAME * frame, int flags ) dup2 ( fd, 1 ); } - if ( force ) + if ( !list_empty( force ) ) { original_noexec = globs.noexec; globs.noexec = 0; @@ -1370,7 +1389,7 @@ LIST * builtin_update_now( FRAME * frame, int flags ) globs.quitquick = 0; } - if ( continue_ ) + if ( !list_empty( continue_ ) ) { original_quitquick = globs.quitquick; globs.quitquick = 0; @@ -1378,23 +1397,24 @@ LIST * builtin_update_now( FRAME * frame, int flags ) targets_count = list_length( targets ); targets2 = (OBJECT * *)BJAM_MALLOC( targets_count * sizeof( OBJECT * ) ); - for (i = 0 ; targets; targets = list_next( targets ) ) - targets2[ i++ ] = targets->value; + iter = list_begin( targets ), end = list_end( targets ); + for (i = 0 ; iter != end; iter = list_next( iter ) ) + targets2[ i++ ] = list_item( iter ); status |= make( targets_count, targets2, anyhow); BJAM_FREE( (void *)targets2 ); - if (force) + if ( !list_empty( force ) ) { globs.noexec = original_noexec; globs.quitquick = original_quitquick; } - if ( continue_ ) + if ( !list_empty( continue_ ) ) { globs.quitquick = original_quitquick; } - if ( log ) + if ( !list_empty( log ) ) { /* Flush whatever stdio might have buffered, while descriptions 0 and 1 still refer to the log file. */ @@ -1419,7 +1439,7 @@ LIST * builtin_import_module( FRAME * frame, int flags ) { LIST * arg1 = lol_get( frame->args, 0 ); LIST * arg2 = lol_get( frame->args, 1 ); - module_t * m = arg2 ? bindmodule( arg2->value ) : root_module(); + module_t * m = !list_empty( arg2 ) ? bindmodule( list_front( arg2 ) ) : root_module(); import_module( arg1, m ); return L0; } @@ -1428,7 +1448,7 @@ LIST * builtin_import_module( FRAME * frame, int flags ) LIST * builtin_imported_modules( FRAME * frame, int flags ) { LIST * arg0 = lol_get( frame->args, 0 ); - return imported_modules( bindmodule( arg0 ? arg0->value : 0 ) ); + return imported_modules( bindmodule( !list_empty( arg0 ) ? list_front( arg0 ) : 0 ) ); } @@ -1436,8 +1456,8 @@ LIST * builtin_instance( FRAME * frame, int flags ) { LIST * arg1 = lol_get( frame->args, 0 ); LIST * arg2 = lol_get( frame->args, 1 ); - module_t * const instance = bindmodule( arg1->value ); - module_t * const class_module = bindmodule( arg2->value ); + module_t * const instance = bindmodule( list_front( arg1 ) ); + module_t * const class_module = bindmodule( list_front( arg2 ) ); instance->class_module = class_module; return L0; } @@ -1471,6 +1491,7 @@ LIST * builtin_normalize_path( FRAME * frame, int flags ) int dotdots = 0; int rooted = 0; OBJECT * result = 0; + LISTITER arg_iter = list_begin( arg ), arg_end = list_end( arg ); /* Make a copy of input: we should not change it. Prepend a '/' before it as * a guard for the algorithm later on and remember whether it was originally @@ -1478,16 +1499,16 @@ LIST * builtin_normalize_path( FRAME * frame, int flags ) */ string_new( in ); string_push_back( in, '/' ); - for ( ; arg; arg = list_next( arg ) ) + for ( ; arg_iter != arg_end; arg_iter = list_next( arg_iter ) ) { - if ( object_str( arg->value )[ 0 ] != '\0' ) + if ( object_str( list_item( arg_iter ) )[ 0 ] != '\0' ) { if ( in->size == 1 ) - rooted = ( ( object_str( arg->value )[ 0 ] == '/' ) || - ( object_str( arg->value )[ 0 ] == '\\' ) ); + rooted = ( ( object_str( list_item( arg_iter ) )[ 0 ] == '/' ) || + ( object_str( list_item( arg_iter ) )[ 0 ] == '\\' ) ); else string_append( in, "/" ); - string_append( in, object_str( arg->value ) ); + string_append( in, object_str( list_item( arg_iter ) ) ); } } @@ -1568,7 +1589,7 @@ LIST * builtin_normalize_path( FRAME * frame, int flags ) string_free( out ); string_free( in ); - return list_new( 0, result ); + return list_new( L0, result ); } @@ -1577,10 +1598,10 @@ LIST * builtin_native_rule( FRAME * frame, int flags ) LIST * module_name = lol_get( frame->args, 0 ); LIST * rule_name = lol_get( frame->args, 1 ); - module_t * module = bindmodule( module_name->value ); + module_t * module = bindmodule( list_front( module_name ) ); native_rule_t * np; - if ( module->native_rules && (np = (native_rule_t *)hash_find( module->native_rules, rule_name->value ) ) ) + if ( module->native_rules && (np = (native_rule_t *)hash_find( module->native_rules, list_front( rule_name ) ) ) ) { args_refer( np->arguments ); new_rule_body( module, np->name, np->arguments, np->procedure, 1 ); @@ -1589,7 +1610,7 @@ LIST * builtin_native_rule( FRAME * frame, int flags ) { backtrace_line( frame->prev ); printf( "error: no native rule \"%s\" defined in module \"%s.\"\n", - object_str( rule_name->value ), object_str( module->name ) ); + object_str( list_front( rule_name ) ), object_str( module->name ) ); backtrace( frame->prev ); exit( 1 ); } @@ -1603,14 +1624,14 @@ LIST * builtin_has_native_rule( FRAME * frame, int flags ) LIST * rule_name = lol_get( frame->args, 1 ); LIST * version = lol_get( frame->args, 2 ); - module_t * module = bindmodule( module_name->value ); + module_t * module = bindmodule( list_front( module_name ) ); native_rule_t * np; - if ( module->native_rules && (np = (native_rule_t *)hash_find( module->native_rules, rule_name->value ) ) ) + if ( module->native_rules && (np = (native_rule_t *)hash_find( module->native_rules, list_front( rule_name ) ) ) ) { - int expected_version = atoi( object_str( version->value ) ); + int expected_version = atoi( object_str( list_front( version ) ) ); if ( np->version == expected_version ) - return list_new( 0, object_copy( constant_true ) ); + return list_new( L0, object_copy( constant_true ) ); } return L0; } @@ -1619,9 +1640,10 @@ LIST * builtin_has_native_rule( FRAME * frame, int flags ) LIST * builtin_user_module( FRAME * frame, int flags ) { LIST * module_name = lol_get( frame->args, 0 ); - for ( ; module_name; module_name = module_name->next ) + LISTITER iter = list_begin( module_name ), end = list_end( module_name ); + for ( ; iter != end; iter = list_next( iter ) ) { - module_t * m = bindmodule( module_name->value ); + module_t * m = bindmodule( list_item( iter ) ); m->user_module = 1; } return L0; @@ -1636,7 +1658,7 @@ LIST * builtin_nearest_user_location( FRAME * frame, int flags ) return L0; { - LIST * result = 0; + LIST * result = L0; const char * file; int line; char buf[32]; @@ -1653,8 +1675,8 @@ LIST * builtin_nearest_user_location( FRAME * frame, int flags ) LIST * builtin_check_if_file( FRAME * frame, int flags ) { LIST * name = lol_get( frame->args, 0 ); - return file_is_file( name->value ) == 1 - ? list_new( 0, object_copy( constant_true ) ) + return file_is_file( list_front( name ) ) == 1 + ? list_new( L0, object_copy( constant_true ) ) : L0 ; } @@ -1662,7 +1684,7 @@ LIST * builtin_check_if_file( FRAME * frame, int flags ) LIST * builtin_md5( FRAME * frame, int flags ) { LIST * l = lol_get( frame->args, 0 ); - const char* s = object_str( l->value ); + const char* s = object_str( list_front( l ) ); md5_state_t state; md5_byte_t digest[16]; @@ -1682,8 +1704,8 @@ LIST * builtin_md5( FRAME * frame, int flags ) LIST *builtin_file_open( FRAME * frame, int flags ) { - const char * name = object_str( lol_get( frame->args, 0 )->value ); - const char * mode = object_str( lol_get( frame->args, 1 )->value ); + const char * name = object_str( list_front( lol_get( frame->args, 0 ) ) ); + const char * mode = object_str( list_front( lol_get( frame->args, 1 ) ) ); int fd; char buffer[sizeof("4294967295")]; @@ -1709,8 +1731,8 @@ LIST *builtin_file_open( FRAME * frame, int flags ) LIST *builtin_pad( FRAME * frame, int flags ) { - OBJECT * string = lol_get( frame->args, 0 )->value; - const char * width_s = object_str( lol_get( frame->args, 1 )->value ); + OBJECT * string = list_front( lol_get( frame->args, 0 ) ); + const char * width_s = object_str( list_front( lol_get( frame->args, 1 ) ) ); int current = strlen( object_str( string ) ); int desired = atoi( width_s ); @@ -1736,9 +1758,10 @@ LIST *builtin_precious( FRAME * frame, int flags ) { LIST * targets = lol_get(frame->args, 0); - for ( ; targets; targets = list_next( targets ) ) + LISTITER iter = list_begin( targets ), end = list_end( targets ); + for ( ; iter != end; iter = list_next( iter ) ) { - TARGET* t = bindtarget( targets->value ); + TARGET* t = bindtarget( list_item( iter ) ); t->flags |= T_FLAG_PRECIOUS; } @@ -1751,7 +1774,7 @@ LIST *builtin_self_path( FRAME * frame, int flags ) char * p = executable_path( saved_argv0 ); if ( p ) { - LIST* result = list_new( 0, object_new( p ) ); + LIST* result = list_new( L0, object_new( p ) ); free( p ); return result; } @@ -1765,9 +1788,9 @@ LIST *builtin_makedir( FRAME * frame, int flags ) { LIST * path = lol_get( frame->args, 0 ); - if ( file_mkdir( object_str( path->value ) ) == 0 ) + if ( file_mkdir( object_str( list_front( path ) ) ) == 0 ) { - LIST * result = list_new ( L0, object_copy( path->value ) ); + LIST * result = list_new ( L0, object_copy( list_front( path ) ) ); return result; } else @@ -1781,10 +1804,10 @@ LIST *builtin_makedir( FRAME * frame, int flags ) LIST * builtin_python_import_rule( FRAME * frame, int flags ) { static int first_time = 1; - const char * python_module = object_str( lol_get( frame->args, 0 )->value ); - const char * python_function = object_str( lol_get( frame->args, 1 )->value ); - OBJECT * jam_module = lol_get( frame->args, 2 )->value; - OBJECT * jam_rule = lol_get( frame->args, 3 )->value; + const char * python_module = object_str( list_front( lol_get( frame->args, 0 ) ) ); + const char * python_function = object_str( list_front( lol_get( frame->args, 1 ) ) ); + OBJECT * jam_module = list_front( lol_get( frame->args, 2 ) ); + OBJECT * jam_rule = list_front( lol_get( frame->args, 3 ) ); PyObject * pName; PyObject * pModule; @@ -1798,17 +1821,19 @@ LIST * builtin_python_import_rule( FRAME * frame, int flags ) */ LIST * extra = 0; module_t * outer_module = frame->module; + LISTITER iter, end; first_time = 0; extra = var_get( root_module(), constant_extra_pythonpath ); - for ( ; extra; extra = extra->next ) + iter = list_begin( extra ), end = list_end( extra ); + for ( ; iter != end; iter = list_next( iter ) ) { string buf[ 1 ]; string_new( buf ); string_append( buf, "import sys\nsys.path.append(\"" ); - string_append( buf, object_str( extra->value ) ); + string_append( buf, object_str( list_item( iter ) ) ); string_append( buf, "\")\n" ); PyRun_SimpleString( buf->value ); string_free( buf ); @@ -1945,10 +1970,10 @@ PyObject* bjam_call( PyObject * self, PyObject * args ) { PyObject * pyResult = PyList_New( list_length( result ) ); int i = 0; - while ( result ) + LISTITER iter = list_begin( result ), end = list_end( result ); + for ( ; iter != end; iter = list_next( iter ) ) { - PyList_SetItem( pyResult, i, PyString_FromString( object_str( result->value ) ) ); - result = list_next( result ); + PyList_SetItem( pyResult, i, PyString_FromString( object_str( list_item( iter ) ) ) ); i += 1; } list_free( result ); @@ -2086,6 +2111,7 @@ PyObject * bjam_variable( PyObject * self, PyObject * args ) PyObject * result; int i; OBJECT * varname; + LISTITER iter, end; if ( !PyArg_ParseTuple( args, "s", &name ) ) return NULL; @@ -2093,10 +2119,11 @@ PyObject * bjam_variable( PyObject * self, PyObject * args ) varname = object_new( name ); value = var_get( root_module(), varname ); object_free( varname ); + iter = list_begin( value ), end = list_end( value ); result = PyList_New( list_length( value ) ); - for ( i = 0; value; value = list_next( value ), ++i ) - PyList_SetItem( result, i, PyString_FromString( object_str( value->value ) ) ); + for ( i = 0; iter != end; iter = list_next( iter ), ++i ) + PyList_SetItem( result, i, PyString_FromString( object_str( list_item( iter ) ) ) ); return result; } @@ -2238,7 +2265,7 @@ static char * rtrim( char * s ) LIST * builtin_shell( FRAME * frame, int flags ) { LIST * command = lol_get( frame->args, 0 ); - LIST * result = 0; + LIST * result = L0; string s; int ret; char buffer[ 1024 ]; @@ -2252,17 +2279,17 @@ LIST * builtin_shell( FRAME * frame, int flags ) { int a = 1; LIST * arg = lol_get( frame->args, a ); - while ( arg ) + while ( !list_empty( arg ) ) { - if ( strcmp( "exit-status", object_str( arg->value ) ) == 0 ) + if ( strcmp( "exit-status", object_str( list_front( arg ) ) ) == 0 ) { exit_status_opt = 1; } - else if ( strcmp( "no-output", object_str( arg->value ) ) == 0 ) + else if ( strcmp( "no-output", object_str( list_front( arg ) ) ) == 0 ) { no_output_opt = 1; } - else if ( strcmp("strip-eol", object_str( arg->value ) ) == 0 ) + else if ( strcmp("strip-eol", object_str( list_front( arg ) ) ) == 0 ) { strip_eol_opt = 1; } @@ -2276,7 +2303,7 @@ LIST * builtin_shell( FRAME * frame, int flags ) */ fflush( NULL ); - p = popen( object_str( command->value ), "r" ); + p = popen( object_str( list_front( command ) ), "r" ); if ( p == NULL ) return L0; diff --git a/src/engine/class.c b/src/engine/class.c index 7ef8744f3..8b84fd723 100644 --- a/src/engine/class.c +++ b/src/engine/class.c @@ -17,11 +17,12 @@ static struct hash * classes = 0; static void check_defined( LIST * class_names ) { - for ( ; class_names; class_names = class_names->next ) + LISTITER iter = list_begin( class_names ), end = list_end( class_names ); + for ( ; iter != end; iter = list_next( iter ) ) { - if ( !hash_find( classes, class_names->value ) ) + if ( !hash_find( classes, list_item( iter ) ) ) { - printf( "Class %s is not defined\n", object_str( class_names->value ) ); + printf( "Class %s is not defined\n", object_str( list_item( iter ) ) ); abort(); } } @@ -113,23 +114,24 @@ static void import_base_rules( module_t * class_, OBJECT * base ) OBJECT * make_class_module( LIST * xname, LIST * bases, FRAME * frame ) { - OBJECT * name = class_module_name( xname->value ); - OBJECT * * pp = &xname->value; + OBJECT * name = class_module_name( list_front( xname ) ); + OBJECT * * pp; module_t * class_module = 0; module_t * outer_module = frame->module; int found; + LISTITER iter, end; if ( !classes ) classes = hashinit( sizeof( OBJECT * ), "classes" ); - pp = (OBJECT * *)hash_insert( classes, xname->value, &found ); + pp = (OBJECT * *)hash_insert( classes, list_front( xname ), &found ); if ( !found ) { - *pp = object_copy( xname->value ); + *pp = object_copy( list_front( xname ) ); } else { - printf( "Class %s already defined\n", object_str( xname->value ) ); + printf( "Class %s already defined\n", object_str( list_front( xname ) ) ); abort(); } check_defined( bases ); @@ -139,8 +141,9 @@ OBJECT * make_class_module( LIST * xname, LIST * bases, FRAME * frame ) var_set( class_module, constant_name, xname, VAR_SET ); var_set( class_module, constant_bases, bases, VAR_SET ); - for ( ; bases; bases = bases->next ) - import_base_rules( class_module, bases->value ); + iter = list_begin( bases ), end = list_end( bases ); + for ( ; iter != end; iter = list_next( iter ) ) + import_base_rules( class_module, list_item( iter ) ); return name; } diff --git a/src/engine/command.c b/src/engine/command.c index 88cb84a6f..4d252fe38 100644 --- a/src/engine/command.c +++ b/src/engine/command.c @@ -33,8 +33,9 @@ CMD * cmd_new( RULE * rule, LIST * targets, LIST * sources, LIST * shell ) { CMD * cmd = (CMD *)BJAM_MALLOC( sizeof( CMD ) ); + LISTITER iter = list_begin( shell ), end = list_end( shell ); /* Lift line-length limitation entirely when JAMSHELL is just "%". */ - int no_limit = ( shell && !strcmp(object_str(shell->value),"%") && !list_next(shell) ); + int no_limit = ( iter != end && !strcmp( object_str( list_item( iter ) ), "%") && list_next( iter ) == end ); int max_line = MAXLINE; FRAME frame[1]; diff --git a/src/engine/compile.c b/src/engine/compile.c index f516c024a..1fd40e258 100644 --- a/src/engine/compile.c +++ b/src/engine/compile.c @@ -115,7 +115,7 @@ void frame_free( FRAME* frame ) } -static void argument_error( const char * message, RULE * rule, FRAME * frame, LIST * arg ) +static void argument_error( const char * message, RULE * rule, FRAME * frame, OBJECT * arg ) { LOL * actual = frame->args; assert( rule->procedure != 0 ); @@ -124,7 +124,7 @@ static void argument_error( const char * message, RULE * rule, FRAME * frame, LI lol_print( rule->arguments->data ); printf( " )\n* called with: ( " ); lol_print( actual ); - printf( " )\n* %s %s\n", message, arg ? object_str ( arg->value ) : "" ); + printf( " )\n* %s %s\n", message, arg ? object_str ( arg ) : "" ); function_location( rule->procedure, &frame->file, &frame->line ); print_source_line( frame ); printf( "see definition of rule '%s' being called\n", object_str( rule->name ) ); @@ -157,11 +157,12 @@ static int is_type_name( const char * s ) * lists. */ -static char arg_modifier( LIST * formal ) +static char arg_modifier( LISTITER iter, LISTITER end ) { - if ( formal->next ) + iter = list_next( iter ); + if ( iter != end ) { - const char * next = object_str( formal->next->value ); + const char * next = object_str( list_item( iter ) ); if ( next && ( next[ 0 ] != 0 ) && ( next[ 1 ] == 0 ) ) return next[ 0 ]; } @@ -188,10 +189,11 @@ static void type_check LIST * values, FRAME * caller, RULE * called, - LIST * arg_name + OBJECT * arg_name ) { static module_t * typecheck = 0; + LISTITER iter, end; /* If nothing to check, bail now. */ if ( !values || !type_name ) @@ -206,7 +208,7 @@ static void type_check if ( !typecheck->rules || !hash_find( typecheck->rules, type_name ) ) return; - while ( values != 0 ) + for ( iter = list_begin( values ), end = list_end( values ); iter != end; iter = list_next( iter ) ) { LIST *error; FRAME frame[1]; @@ -216,14 +218,13 @@ static void type_check frame->prev_user = caller->module->user_module ? caller : caller->prev_user; /* Prepare the argument list */ - lol_add( frame->args, list_new( L0, object_copy( values->value ) ) ); + lol_add( frame->args, list_new( L0, object_copy( list_item( iter ) ) ) ); error = evaluate_rule( type_name, frame ); - if ( error ) - argument_error( object_str( error->value ), called, caller, arg_name ); + if ( !list_empty( error ) ) + argument_error( object_str( list_front( error ) ), called, caller, arg_name ); frame_free( frame ); - values = values->next; } } @@ -250,55 +251,59 @@ collect_arguments( RULE* rule, FRAME* frame ) OBJECT * type_name = 0; LIST *formal; - for ( formal = lol_get( all_formal, n ); formal; formal = formal->next ) + LISTITER formal_iter, formal_end; + LISTITER actual_iter = list_begin( actual ), actual_end = list_end( actual ); + for ( formal = lol_get( all_formal, n ), + formal_iter = list_begin( formal ), formal_end = list_end( formal ); + formal_iter != formal_end; formal_iter = list_next( formal_iter ) ) { - OBJECT * name = formal->value; + OBJECT * name = list_item( formal_iter ); if ( is_type_name( object_str( name ) ) ) { if ( type_name ) - argument_error( "missing argument name before type name:", rule, frame, formal ); + argument_error( "missing argument name before type name:", rule, frame, name ); - if ( !formal->next ) - argument_error( "missing argument name after type name:", rule, frame, formal ); + if ( list_next( formal_iter ) == formal_end ) + argument_error( "missing argument name after type name:", rule, frame, name ); - type_name = formal->value; + type_name = name; } else { - LIST* value = 0; + LIST* value = L0; char modifier; - LIST* arg_name = formal; /* hold the argument name for type checking */ + OBJECT* arg_name = list_item( formal_iter ); /* hold the argument name for type checking */ int multiple = 0; /* Stop now if a variable number of arguments are specified */ if ( object_str( name )[0] == '*' && object_str( name )[1] == 0 ) return locals; - modifier = arg_modifier( formal ); + modifier = arg_modifier( formal_iter, formal_end ); - if ( !actual && modifier != '?' && modifier != '*' ) - argument_error( "missing argument", rule, frame, formal ); + if ( actual_iter == actual_end && modifier != '?' && modifier != '*' ) + argument_error( "missing argument", rule, frame, name ); switch ( modifier ) { case '+': case '*': - value = list_copy( 0, actual ); + value = list_copy_range( actual, actual_iter, actual_end ); multiple = 1; - actual = 0; + actual_iter = actual_end; /* skip an extra element for the modifier */ - formal = formal->next; + formal_iter = list_next( formal_iter ); break; case '?': /* skip an extra element for the modifier */ - formal = formal->next; + formal_iter = list_next( formal_iter ); /* fall through */ default: - if ( actual ) /* in case actual is missing */ + if ( actual_iter != actual_end ) /* in case actual is missing */ { - value = list_new( 0, object_copy( actual->value ) ); - actual = actual->next; + value = list_new( L0, object_copy( list_item( actual_iter ) ) ); + actual_iter = list_next( actual_iter ); } } @@ -309,9 +314,9 @@ collect_arguments( RULE* rule, FRAME* frame ) } } - if ( actual ) + if ( actual_iter != actual_end ) { - argument_error( "extra argument", rule, frame, actual ); + argument_error( "extra argument", rule, frame, list_item( actual_iter ) ); } } } @@ -386,8 +391,8 @@ call_python_function(RULE* r, FRAME* frame) if (args->multiple) value = list_to_python(args->value); else { - if (args->value) - value = PyString_FromString(object_str(args->value->value)); + if (!list_empty(args->value)) + value = PyString_FromString(object_str(list_front(args->value))); } if (value) @@ -403,10 +408,11 @@ call_python_function(RULE* r, FRAME* frame) { PyObject * arg = PyList_New(0); LIST* l = lol_get( frame->args, i); + LISTITER iter = list_begin( l ), end = list_end( l ); - for ( ; l; l = l->next ) + for ( ; iter != end; iter = list_next( iter ) ) { - PyObject * v = PyString_FromString(object_str(l->value)); + PyObject * v = PyString_FromString(object_str(list_item(iter))); PyList_Append( arg, v ); Py_DECREF(v); } @@ -446,7 +452,7 @@ call_python_function(RULE* r, FRAME* frame) { OBJECT *s = python_to_string(py_result); if (s) - result = list_new(0, s); + result = list_new(L0, s); else /* We have tried all we could. Return empty list. There are cases, e.g. feature.feature function that should return diff --git a/src/engine/execnt.c b/src/engine/execnt.c index ad6db5295..f34b37854 100644 --- a/src/engine/execnt.c +++ b/src/engine/execnt.c @@ -220,7 +220,7 @@ void exec_cmd * for a JAMSHELL setting of "%", indicating that the command should be * invoked directly. */ - if ( shell && !strcmp( object_str( shell->value ), "%" ) && !list_next( shell ) ) + if ( !list_empty( shell ) && !strcmp( object_str( list_front( shell ) ), "%" ) && list_next( list_begin( shell ) ) == list_end( shell ) ) { raw_cmd = 1; shell = 0; @@ -290,8 +290,8 @@ void exec_cmd if ( DEBUG_EXECCMD ) { - if ( shell ) - printf( "using user-specified shell: %s", object_str( shell->value ) ); + if ( !list_empty( shell ) ) + printf( "using user-specified shell: %s", object_str( list_front( shell ) ) ); else printf( "Executing through .bat file\n" ); } @@ -305,16 +305,17 @@ void exec_cmd int i; char jobno[ 4 ]; int gotpercent = 0; + LISTITER shell_iter = list_begin( shell ), shell_end = list_end( shell ); sprintf( jobno, "%d", slot + 1 ); - for ( i = 0; shell && ( i < MAXARGC ); ++i, shell = list_next( shell ) ) + for ( i = 0; shell_iter != shell_end && ( i < MAXARGC ); ++i, shell_iter = list_next( shell_iter ) ) { - switch ( object_str( shell->value )[ 0 ] ) + switch ( object_str( list_item( shell_iter ) )[ 0 ] ) { case '%': argv[ i ] = command; ++gotpercent; break; case '!': argv[ i ] = jobno; break; - default : argv[ i ] = object_str( shell->value ); + default : argv[ i ] = object_str( list_item( shell_iter ) ); } if ( DEBUG_EXECCMD ) printf( "argv[%d] = '%s'\n", i, argv[ i ] ); diff --git a/src/engine/execunix.c b/src/engine/execunix.c index 9709d89cb..1bc24d7fc 100644 --- a/src/engine/execunix.c +++ b/src/engine/execunix.c @@ -140,21 +140,22 @@ void exec_cmd /* Forumulate argv. If shell was defined, be prepared for % and ! subs. * Otherwise, use stock /bin/sh on unix or cmd.exe on NT. */ - if ( shell ) + if ( !list_empty( shell ) ) { int i; char jobno[4]; int gotpercent = 0; + LISTITER iter = list_begin( shell ), end = list_end( shell ); sprintf( jobno, "%d", slot + 1 ); - for ( i = 0; shell && i < MAXARGC; ++i, shell = list_next( shell ) ) + for ( i = 0; iter != end && i < MAXARGC; ++i, iter = list_next( iter ) ) { - switch ( object_str( shell->value )[0] ) + switch ( object_str( list_item( iter ) )[0] ) { case '%': argv[ i ] = string; ++gotpercent; break; case '!': argv[ i ] = jobno; break; - default : argv[ i ] = object_str( shell->value ); + default : argv[ i ] = object_str( list_item( iter ) ); } if ( DEBUG_EXECCMD ) printf( "argv[%d] = '%s'\n", i, argv[ i ] ); diff --git a/src/engine/filent.c b/src/engine/filent.c index 89ff74be6..813748ec7 100644 --- a/src/engine/filent.c +++ b/src/engine/filent.c @@ -226,14 +226,14 @@ void file_dirscan( OBJECT * dir, scanback func, void * closure ) } /* Now enter contents of directory */ - if ( d->files ) + if ( !list_empty( d->files ) ) { LIST * files = d->files; - while ( files ) + LISTITER iter = list_begin( files ), end = list_end( files ); + for ( ; iter != end; iter = list_next( iter ) ) { - file_info_t * ff = file_info( files->value ); - (*func)( closure, files->value, 1 /* stat()'ed */, ff->time ); - files = list_next( files ); + file_info_t * ff = file_info( list_item( iter ) ); + (*func)( closure, list_item( iter ), 1 /* stat()'ed */, ff->time ); } } diff --git a/src/engine/filesys.c b/src/engine/filesys.c index a4104d3b6..c34cb9529 100644 --- a/src/engine/filesys.c +++ b/src/engine/filesys.c @@ -55,7 +55,7 @@ file_info_t * file_info( OBJECT * filename ) finfo->is_dir = 0; finfo->size = 0; finfo->time = 0; - finfo->files = 0; + finfo->files = L0; } object_free( filename ); @@ -67,13 +67,13 @@ static LIST * files_to_remove = L0; static void remove_files_atexit(void) { - /* we do pop front in case this exit function is called - more than once */ - while ( files_to_remove ) + LISTITER iter = list_begin( files_to_remove ), end = list_end( files_to_remove ); + for ( ; iter != end; iter = list_next( iter ) ) { - remove( object_str( files_to_remove->value ) ); - files_to_remove = list_pop_front( files_to_remove ); + remove( object_str( list_item( iter ) ) ); } + list_free( files_to_remove ); + files_to_remove = L0; } static void free_file_info ( void * xfile, void * data ) diff --git a/src/engine/fileunix.c b/src/engine/fileunix.c index 7f52a740a..2b93a775b 100644 --- a/src/engine/fileunix.c +++ b/src/engine/fileunix.c @@ -139,7 +139,7 @@ void file_dirscan( OBJECT * dir, scanback func, void * closure ) return; } - if ( ! d->files ) + if ( list_empty( d->files ) ) { LIST* files = L0; PATHNAME f; @@ -201,12 +201,13 @@ void file_dirscan( OBJECT * dir, scanback func, void * closure ) } /* Now enter contents of directory */ - if ( d->files ) + if ( !list_empty( d->files ) ) { LIST * files = d->files; - while ( files ) + LISTITER iter = list_begin( files ), end = list_end( files ); + for ( ; iter != end; iter = list_next( iter ) ) { - file_info_t * ff = file_info( files->value ); + file_info_t * ff = file_info( list_item( iter ) ); (*func)( closure, ff->name, 1 /* stat()'ed */, ff->time ); files = list_next( files ); } diff --git a/src/engine/function.c b/src/engine/function.c index 16ce038c1..1e8d87e37 100644 --- a/src/engine/function.c +++ b/src/engine/function.c @@ -56,7 +56,8 @@ void backtrace_line( FRAME * frame ); #define INSTR_JUMP_NOT_GLOB 19 -#define INSTR_TRY_POP_FRONT 20 +#define INSTR_FOR_INIT 56 +#define INSTR_FOR_LOOP 20 #define INSTR_SET_RESULT 21 #define INSTR_RETURN 22 @@ -390,7 +391,7 @@ static LIST * function_call_rule( JAM_FUNCTION * function, FRAME * frame, STACK frame->file = file; frame->line = line; - if ( !first ) + if ( list_empty( first ) ) { backtrace_line( frame ); printf( "warning: rulename %s expands to empty string\n", unexpanded ); @@ -406,7 +407,7 @@ static LIST * function_call_rule( JAM_FUNCTION * function, FRAME * frame, STACK return result; } - rulename = object_copy( first->value ); + rulename = object_copy( list_front( first ) ); frame_init( inner ); @@ -465,8 +466,8 @@ typedef struct PATHPART join; /* :J -- join list with char */ } VAR_EDITS; -static LIST * apply_modifiers_impl( LIST * result, string * buf, VAR_EDITS * edits, int n, LIST * iter, LIST * end ); -static void get_iters( subscript_t subscript, LIST * * first, LIST * * last, int length ); +static LIST * apply_modifiers_impl( LIST * result, string * buf, VAR_EDITS * edits, int n, LISTITER iter, LISTITER end ); +static void get_iters( subscript_t subscript, LISTITER * first, LISTITER * last, int length ); static void var_edit_file( const char * in, string * out, VAR_EDITS * edits ); static void var_edit_shift( string * out, size_t pos, VAR_EDITS * edits ); static int var_edit_parse( const char * mods, VAR_EDITS * edits, int havezeroed ); @@ -661,10 +662,10 @@ static int expand_modifiers( STACK * s, int n ) if ( total != 0 ) { VAR_EDITS * out = stack_allocate( s, total * sizeof(VAR_EDITS) ); - LIST * * iter = stack_allocate( s, n * sizeof(LIST *) ); + LISTITER * iter = stack_allocate( s, n * sizeof(LIST *) ); for (i = 0; i < n; ++i ) { - iter[i] = args[i]; + iter[i] = list_begin( args[i] ); } i = 0; { @@ -674,19 +675,19 @@ static int expand_modifiers( STACK * s, int n ) havezeroed = 0; for (i = 0; i < n; ++i ) { - havezeroed = var_edit_parse( object_str( iter[i]->value ), out, havezeroed ); + havezeroed = var_edit_parse( object_str( list_item( iter[i] ) ), out, havezeroed ); } ++out; while ( --i >= 0 ) { - if ( iter[i]->next ) + if ( list_next( iter[i] ) != list_end( args[i] ) ) { - iter[i] = iter[i]->next; + iter[i] = list_next( iter[i] ); goto loop; } else { - iter[i] = args[i]; + iter[i] = list_begin( args[i] ); } } } @@ -702,7 +703,7 @@ static LIST * apply_modifiers( STACK * s, int n ) VAR_EDITS * edits = (VAR_EDITS *)( (LIST * *)stack_get( s ) + 1 ); string buf[1]; string_new( buf ); - result = apply_modifiers_impl( result, buf, edits, n, value, L0 ); + result = apply_modifiers_impl( result, buf, edits, n, list_begin( value ), list_end( value ) ); string_free( buf ); return result; } @@ -775,16 +776,17 @@ static LIST * apply_subscript( STACK * s ) LIST * result = L0; int length = list_length( value ); string buf[1]; + LISTITER indices_iter = list_begin( indices ), indices_end = list_end( indices ); string_new( buf ); - for ( ; indices; indices = list_next( indices ) ) + for ( ; indices_iter != indices_end; indices_iter = list_next( indices_iter ) ) { - LIST * iter = value; - LIST * end; - subscript_t subscript = parse_subscript( object_str( indices->value ) ); + LISTITER iter = list_begin( value ); + LISTITER end = list_end( value ); + subscript_t subscript = parse_subscript( object_str( list_item( indices_iter ) ) ); get_iters( subscript, &iter, &end, length ); for ( ; iter != end; iter = list_next( iter ) ) { - result = list_new( result, object_copy( iter->value ) ); + result = list_new( result, object_copy( list_item( iter ) ) ); } } string_free( buf ); @@ -796,12 +798,12 @@ static LIST * apply_subscript( STACK * s ) * The results are written to *first and *last. */ -static void get_iters( subscript_t subscript, LIST * * first, LIST * * last, int length ) +static void get_iters( subscript_t subscript, LISTITER * first, LISTITER * last, int length ) { int start; int size; - LIST * iter; - LIST * end; + LISTITER iter; + LISTITER end; { if ( subscript.sub1 < 0 ) @@ -864,22 +866,22 @@ static LIST * apply_modifiers_empty( LIST * result, string * buf, VAR_EDITS * ed return result; } -static LIST * apply_modifiers_non_empty( LIST * result, string * buf, VAR_EDITS * edits, int n, LIST * begin, LIST * end ) +static LIST * apply_modifiers_non_empty( LIST * result, string * buf, VAR_EDITS * edits, int n, LISTITER begin, LISTITER end ) { int i; - LIST * iter; + LISTITER iter; for ( i = 0; i < n; ++i ) { if ( edits[i].join.ptr ) { - var_edit_file( object_str( begin->value ), buf, edits + i ); + var_edit_file( object_str( list_item( begin ) ), buf, edits + i ); var_edit_shift( buf, 0, edits + i ); for ( iter = list_next( begin ); iter != end; iter = list_next( iter ) ) { size_t size; string_append( buf, edits[i].join.ptr ); size = buf->size; - var_edit_file( object_str( iter->value ), buf, edits + i ); + var_edit_file( object_str( list_item( iter ) ), buf, edits + i ); var_edit_shift( buf, size, edits + i ); } result = list_new( result, object_new( buf->value ) ); @@ -887,9 +889,9 @@ static LIST * apply_modifiers_non_empty( LIST * result, string * buf, VAR_EDITS } else { - for ( iter = begin; iter != end; iter = iter->next ) + for ( iter = begin; iter != end; iter = list_next( iter ) ) { - var_edit_file( object_str( iter->value ), buf, edits + i ); + var_edit_file( object_str( list_item( iter ) ), buf, edits + i ); var_edit_shift( buf, 0, edits + i ); result = list_new( result, object_new( buf->value ) ); string_truncate( buf, 0 ); @@ -899,7 +901,7 @@ static LIST * apply_modifiers_non_empty( LIST * result, string * buf, VAR_EDITS return result; } -static LIST * apply_modifiers_impl( LIST * result, string * buf, VAR_EDITS * edits, int n, LIST * iter, LIST * end ) +static LIST * apply_modifiers_impl( LIST * result, string * buf, VAR_EDITS * edits, int n, LISTITER iter, LISTITER end ) { if ( iter != end ) { @@ -919,12 +921,13 @@ static LIST * apply_subscript_and_modifiers( STACK * s, int n ) VAR_EDITS * edits = (VAR_EDITS *)((LIST * *)stack_get( s ) + 2); int length = list_length( value ); string buf[1]; + LISTITER indices_iter = list_begin( indices ), indices_end = list_end( indices ); string_new( buf ); - for ( ; indices; indices = list_next( indices ) ) + for ( ; indices_iter != indices_end; indices_iter = list_next( indices_iter ) ) { - LIST * iter = value; - LIST * end; - subscript_t sub = parse_subscript( object_str( indices->value ) ); + LISTITER iter = list_begin( value ); + LISTITER end = list_end( value ); + subscript_t sub = parse_subscript( object_str( list_item( indices_iter ) ) ); get_iters( sub, &iter, &end, length ); result = apply_modifiers_impl( result, buf, edits, n, iter, end ); } @@ -934,7 +937,7 @@ static LIST * apply_subscript_and_modifiers( STACK * s, int n ) typedef struct expansion_item { - LIST * elem; + LISTITER elem; LIST * saved; int size; } expansion_item; @@ -949,11 +952,11 @@ static LIST * expand( expansion_item * elem, int length ) for ( i = 0; i < length; ++i ) { int max = 0; - LIST * l; - if ( !elem[i].elem ) return result; - for ( l = elem[i].elem; l; l = l->next ) + LISTITER iter = elem[i].elem, end = list_end( elem[i].saved ); + if ( iter == end ) return result; + for ( ; iter != end; iter = list_next( iter ) ) { - int len = strlen( object_str( l->value ) ); + int len = strlen( object_str( list_item( iter ) ) ); if ( len > max ) max = len; } size += max; @@ -966,20 +969,20 @@ static LIST * expand( expansion_item * elem, int length ) for ( ; i < length; ++i ) { elem[i].size = buf->size; - string_append( buf, object_str( elem[i].elem->value ) ); + string_append( buf, object_str( list_item( elem[i].elem ) ) ); } result = list_new( result, object_new( buf->value ) ); while ( --i >= 0 ) { - if(elem[i].elem->next) + if( list_next( elem[i].elem ) != list_end( elem[i].saved ) ) { - elem[i].elem = elem[i].elem->next; + elem[i].elem = list_next( elem[i].elem ); string_truncate( buf, elem[i].size ); goto loop; } else { - elem[i].elem = elem[i].saved; + elem[i].elem = list_begin( elem[i].saved ); } } } @@ -990,17 +993,17 @@ static LIST * expand( expansion_item * elem, int length ) static void combine_strings( STACK * s, int n, string * out ) { int i; - LIST * l; for ( i = 0; i < n; ++i ) { LIST * values = stack_pop( s ); - if ( values ) + LISTITER iter = list_begin( values ), end = list_end( values ); + if ( iter != end ) { - string_append( out, object_str( values->value ) ); - for ( l = list_next( values ); l; l = list_next( l ) ) + string_append( out, object_str( list_item( iter ) ) ); + for ( iter = list_next( iter ); iter != end; iter = list_next( iter ) ) { string_push_back( out, ' ' ); - string_append( out, object_str( l->value ) ); + string_append( out, object_str( list_item( iter ) ) ); } list_free( values ); } @@ -2108,26 +2111,29 @@ static const char * parse_type( PARSE * parse ) } } +static void compile_append_chain( PARSE * parse, compiler * c ) +{ + assert( parse->type == PARSE_APPEND ); + if ( parse->left->type == PARSE_NULL ) + { + compile_parse( parse->right, c, RESULT_STACK ); + } + else + { + if ( parse->left->type == PARSE_APPEND ) + compile_append_chain( parse->left, c ); + else + compile_parse( parse->left, c, RESULT_STACK ); + compile_parse( parse->right, c, RESULT_STACK ); + compile_emit( c, INSTR_PUSH_APPEND, 0 ); + } +} + static void compile_parse( PARSE * parse, compiler * c, int result_location ) { if ( parse->type == PARSE_APPEND ) { - /* - * append is associative, so flip the parse tree of chained - * appends around to keep the stack from getting too deep. - */ - compile_parse( parse->right, c, RESULT_STACK ); - while ( parse->left->type == PARSE_APPEND ) - { - compile_parse( parse->left->right, c, RESULT_STACK ); - compile_emit( c, INSTR_PUSH_APPEND, 0 ); - parse = parse->left; - } - if ( parse->left->type != PARSE_NULL ) - { - compile_parse( parse->left, c, RESULT_STACK ); - compile_emit( c, INSTR_PUSH_APPEND, 0 ); - } + compile_append_chain( parse, c ); adjust_result( c, RESULT_STACK, result_location ); } else if ( parse->type == PARSE_EVAL ) @@ -2173,8 +2179,9 @@ static void compile_parse( PARSE * parse, compiler * c, int result_location ) compile_emit( c, INSTR_SWAP, 1 ); } + compile_emit( c, INSTR_FOR_INIT, 0 ); compile_set_label( c, top ); - compile_emit_branch( c, INSTR_TRY_POP_FRONT, end ); + compile_emit_branch( c, INSTR_FOR_LOOP, end ); compile_emit( c, INSTR_SET, var ); compile_emit( c, INSTR_POP, 0 ); @@ -2696,10 +2703,11 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) case INSTR_PUSH_GROUP: { LIST * value = L0; + LISTITER iter, end; l = stack_pop( s ); - for ( r = l; r; r = list_next( r ) ) + for ( iter = list_begin( l ), end = list_end( l ); iter != end; iter = list_next( iter ) ) { - LIST * one = function_get_named_variable( function, frame, r->value ); + LIST * one = function_get_named_variable( function, frame, list_item( iter ) ); value = list_append( value, one ); } list_free( l ); @@ -2711,7 +2719,7 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) { r = stack_pop( s ); l = stack_pop( s ); - stack_push( s, list_append( r, l ) ); + stack_push( s, list_append( l, r ) ); break; } @@ -2839,18 +2847,29 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) * For */ - case INSTR_TRY_POP_FRONT: + case INSTR_FOR_INIT: { - l = stack_pop( s ); - if( !l ) + l = stack_top( s ); + *(LISTITER *)stack_allocate( s, sizeof( LISTITER ) ) = + list_begin( l ); + break; + } + + case INSTR_FOR_LOOP: + { + LISTITER iter = *(LISTITER *)stack_get( s ); + stack_deallocate( s, sizeof( LISTITER ) ); + l = stack_top( s ); + if( iter == list_end( l ) ) { + list_free( stack_pop( s ) ); code += code->arg; } else { - r = list_new( L0, object_copy( l->value ) ); - l = list_pop_front( l ); - stack_push( s, l ); + r = list_new( L0, object_copy( list_item( iter ) ) ); + iter = list_next( iter ); + *(LISTITER *)stack_allocate( s, sizeof( LISTITER ) ) = iter; stack_push( s, r ); } break; @@ -2866,8 +2885,8 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) const char * match; l = stack_pop( s ); r = stack_top( s ); - pattern = l ? object_str( l->value ) : ""; - match = r ? object_str( r->value ) : ""; + pattern = !list_empty( l ) ? object_str( list_front( l ) ) : ""; + match = !list_empty( r ) ? object_str( list_front( r ) ) : ""; if( glob( pattern, match ) ) { code += code->arg; @@ -2936,10 +2955,11 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) case INSTR_PUSH_LOCAL_GROUP: { LIST * value = stack_pop( s ); + LISTITER iter, end; l = stack_pop( s ); - for( r = l; r; r = list_next( r ) ) + for( iter = list_begin( l ), end = list_end( l ); iter != end; iter = list_next( iter ) ) { - LIST * saved = function_swap_named_variable( function, frame, r->value, list_copy( L0, value ) ); + LIST * saved = function_swap_named_variable( function, frame, list_item( iter ), list_copy( L0, value ) ); stack_push( s, saved ); } list_free( value ); @@ -2949,12 +2969,13 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) case INSTR_POP_LOCAL_GROUP: { + LISTITER iter, end; r = stack_pop( s ); l = list_reverse( r ); list_free( r ); - for( r = l; r; r = list_next( r ) ) + for( iter = list_begin( l ), end = list_end( l ); iter != end; iter = list_next( iter ) ) { - function_set_named_variable( function, frame, r->value, stack_pop( s ) ); + function_set_named_variable( function, frame, list_item( iter ), stack_pop( s ) ); } list_free( l ); break; @@ -2967,13 +2988,13 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) case INSTR_PUSH_ON: { LIST * targets = stack_top( s ); - if ( targets ) + if ( !list_empty( targets ) ) { /* * FIXME: push the state onto the stack instead of * using pushsettings. */ - TARGET * t = bindtarget( targets->value ); + TARGET * t = bindtarget( list_front( targets ) ); pushsettings( frame->module, t->settings ); } else @@ -2992,9 +3013,9 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) { LIST * result = stack_pop( s ); LIST * targets = stack_pop( s ); - if ( targets ) + if ( !list_empty( targets ) ) { - TARGET * t = bindtarget( targets->value ); + TARGET * t = bindtarget( list_front( targets ) ); popsettings( frame->module, t->settings ); } list_free( targets ); @@ -3007,15 +3028,15 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) LIST * targets = stack_pop( s ); LIST * value = stack_pop( s ); LIST * vars = stack_pop( s ); - LIST * ts; - for ( ts = targets; ts; ts = list_next( ts ) ) + LISTITER iter = list_begin( targets ), end = list_end( targets ); + for ( ; iter != end; iter = list_next( iter ) ) { - TARGET * t = bindtarget( ts->value ); - LIST * l; + TARGET * t = bindtarget( list_item( iter ) ); + LISTITER vars_iter = list_begin( vars ), vars_end = list_end( vars ); - for ( l = vars; l; l = list_next( l ) ) - t->settings = addsettings( t->settings, VAR_SET, l->value, - list_copy( L0, value ) ); + for ( ; vars_iter != vars_end; vars_iter = list_next( vars_iter ) ) + t->settings = addsettings( t->settings, VAR_SET, list_item( vars_iter ), + list_copy( L0, value ) ); } list_free( vars ); list_free( targets ); @@ -3028,15 +3049,15 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) LIST * targets = stack_pop( s ); LIST * value = stack_pop( s ); LIST * vars = stack_pop( s ); - LIST * ts; - for ( ts = targets; ts; ts = list_next( ts ) ) + LISTITER iter = list_begin( targets ), end = list_end( targets ); + for ( ; iter != end; iter = list_next( iter ) ) { - TARGET * t = bindtarget( ts->value ); - LIST * l; + TARGET * t = bindtarget( list_item( iter ) ); + LISTITER vars_iter = list_begin( vars ), vars_end = list_end( vars ); - for ( l = vars; l; l = list_next( l ) ) - t->settings = addsettings( t->settings, VAR_APPEND, l->value, - list_copy( L0, value ) ); + for ( ; vars_iter != vars_end; vars_iter = list_next( vars_iter ) ) + t->settings = addsettings( t->settings, VAR_APPEND, list_item( vars_iter ), + list_copy( L0, value ) ); } list_free( vars ); list_free( targets ); @@ -3049,15 +3070,15 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) LIST * targets = stack_pop( s ); LIST * value = stack_pop( s ); LIST * vars = stack_pop( s ); - LIST * ts; - for ( ts = targets; ts; ts = list_next( ts ) ) + LISTITER iter = list_begin( targets ), end = list_end( targets ); + for ( ; iter != end; iter = list_next( iter ) ) { - TARGET * t = bindtarget( ts->value ); - LIST * l; + TARGET * t = bindtarget( list_item( iter ) ); + LISTITER vars_iter = list_begin( vars ), vars_end = list_end( vars ); - for ( l = vars; l; l = list_next( l ) ) - t->settings = addsettings( t->settings, VAR_DEFAULT, l->value, - list_copy( L0, value ) ); + for ( ; vars_iter != vars_end; vars_iter = list_next( vars_iter ) ) + t->settings = addsettings( t->settings, VAR_DEFAULT, list_item( vars_iter ), + list_copy( L0, value ) ); } list_free( vars ); list_free( targets ); @@ -3090,8 +3111,9 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) { LIST * value = stack_pop( s ); LIST * vars = stack_pop( s ); - for( r = vars; r; r = list_next( r ) ) - function_set_named_variable( function, frame, r->value, list_copy( L0, value ) ); + LISTITER iter = list_begin( vars ), end = list_end( vars ); + for( ; iter != end; iter = list_next( iter ) ) + function_set_named_variable( function, frame, list_item( iter ), list_copy( L0, value ) ); list_free( vars ); stack_push( s, value ); break; @@ -3101,8 +3123,9 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) { LIST * value = stack_pop( s ); LIST * vars = stack_pop( s ); - for( r = vars; r; r = list_next( r ) ) - function_append_named_variable( function, frame, r->value, list_copy( L0, value ) ); + LISTITER iter = list_begin( vars ), end = list_end( vars ); + for( ; iter != end; iter = list_next( iter ) ) + function_append_named_variable( function, frame, list_item( iter ), list_copy( L0, value ) ); list_free( vars ); stack_push( s, value ); break; @@ -3112,8 +3135,9 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) { LIST * value = stack_pop( s ); LIST * vars = stack_pop( s ); - for( r = vars; r; r = list_next( r ) ) - function_default_named_variable( function, frame, r->value, list_copy( L0, value ) ); + LISTITER iter = list_begin( vars ), end = list_end( vars ); + for( ; iter != end; iter = list_next( iter ) ) + function_default_named_variable( function, frame, list_item( iter ), list_copy( L0, value ) ); list_free( vars ); stack_push( s, value ); break; @@ -3199,9 +3223,10 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) LIST * vars = stack_pop( s ); int n = expand_modifiers( s, code->arg ); LIST * result = L0; - for( l = vars; l; l = list_next( l ) ) + LISTITER iter = list_begin( vars ), end = list_end( vars ); + for( ; iter != end; iter = list_next( iter ) ) { - stack_push( s, function_get_named_variable( function, frame, l->value ) ); + stack_push( s, function_get_named_variable( function, frame, list_item( iter ) ) ); result = list_append( result, apply_modifiers( s, n ) ); list_free( stack_pop( s ) ); } @@ -3217,9 +3242,10 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) { LIST * vars = stack_pop( s ); LIST * result = L0; - for( l = vars; l; l = list_next( l ) ) + LISTITER iter = list_begin( vars ), end = list_end( vars ); + for( ; iter != end; iter = list_next( iter ) ) { - stack_push( s, function_get_named_variable( function, frame, l->value ) ); + stack_push( s, function_get_named_variable( function, frame, list_item( iter ) ) ); result = list_append( result, apply_subscript( s ) ); list_free( stack_pop( s ) ); } @@ -3236,10 +3262,11 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) LIST * r = stack_pop( s ); int n = expand_modifiers( s, code->arg ); LIST * result = L0; + LISTITER iter = list_begin( vars ), end = list_end( vars ); stack_push( s, r ); - for( l = vars; l; l = list_next( l ) ) + for( ; iter != end; iter = list_next( iter ) ) { - stack_push( s, function_get_named_variable( function, frame, l->value ) ); + stack_push( s, function_get_named_variable( function, frame, list_item( iter ) ) ); result = list_append( result, apply_subscript_and_modifiers( s, n ) ); list_free( stack_pop( s ) ); } @@ -3261,7 +3288,8 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) int i; for( i = 0; i < code->arg; ++i ) { - items[i].elem = items[i].saved = stack_pos[i]; + items[i].saved = stack_pos[i]; + items[i].elem = list_begin( items[i].saved ); } result = expand( items, code->arg ); stack_deallocate( s, buffer_size ); @@ -3277,9 +3305,9 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) { LIST * nt = stack_pop( s ); - if ( nt ) + if ( !list_empty( nt ) ) { - TARGET * t = bindtarget( nt->value ); + TARGET * t = bindtarget( list_front( nt ) ); list_free( nt ); /* DWA 2001/10/22 - Perforce Jam cleared the arguments here, which @@ -3315,7 +3343,7 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) LIST * module_name = stack_pop( s ); module_t * outer_module = frame->module; - frame->module = module_name ? bindmodule( module_name->value ) : root_module(); + frame->module = !list_empty( module_name ) ? bindmodule( list_front( module_name ) ) : root_module(); list_free( module_name ); @@ -3366,7 +3394,7 @@ LIST * function_run( FUNCTION * function_, FRAME * frame, STACK * s ) FILE * out_file = 0; string_new( buf ); combine_strings( s, code->arg, buf ); - out = object_str( stack_top( s )->value ); + out = object_str( list_front( stack_top( s ) ) ); /* For stdout/stderr we will create a temp file and generate * a command that outputs the content as needed. diff --git a/src/engine/hcache.c b/src/engine/hcache.c index 93b3dd354..0f967b96b 100644 --- a/src/engine/hcache.c +++ b/src/engine/hcache.c @@ -78,9 +78,9 @@ static const char * cache_name( void ) { LIST * hcachevar = var_get( root_module(), constant_HCACHEFILE ); - if ( hcachevar ) + if ( !list_empty( hcachevar ) ) { - TARGET * t = bindtarget( hcachevar->value ); + TARGET * t = bindtarget( list_front( hcachevar ) ); pushsettings( root_module(), t->settings ); /* Do not expect the cache file to be generated, so pass 0 as the @@ -91,8 +91,7 @@ static const char * cache_name( void ) t->boundname = search( t->name, &t->time, 0, 0 ); popsettings( root_module(), t->settings ); - if ( hcachevar ) - name = object_copy( t->boundname ); + name = object_copy( t->boundname ); } } return name ? object_str( name ) : 0; @@ -108,9 +107,9 @@ static int cache_maxage( void ) { int age = 100; LIST * var = var_get( root_module(), constant_HCACHEMAXAGE ); - if ( var ) + if ( !list_empty( var ) ) { - age = atoi( object_str( var->value ) ); + age = atoi( object_str( list_front( var ) ) ); if ( age < 0 ) age = 0; } @@ -241,7 +240,7 @@ void hcache_init() cachedata.age = atoi( object_str( age_str ) ) + 1; count = atoi( object_str( includes_count_str ) ); - for ( l = 0, i = 0; i < count; ++i ) + for ( l = L0, i = 0; i < count; ++i ) { OBJECT * s = read_netstring( f ); if ( !s ) @@ -262,7 +261,7 @@ void hcache_init() } count = atoi( object_str( hdrscan_count_str ) ); - for ( l = 0, i = 0; i < count; ++i ) + for ( l = L0, i = 0; i < count; ++i ) { OBJECT * s = read_netstring( f ); if ( !s ) @@ -329,7 +328,7 @@ void hcache_done() c = hcachelist; for ( c = hcachelist; c; c = c->next ) { - LIST * l; + LISTITER iter, end; char time_str[ 30 ]; char age_str[ 30 ]; char includes_count_str[ 30 ]; @@ -350,11 +349,13 @@ void hcache_done() write_netstring( f, time_str ); write_netstring( f, age_str ); write_netstring( f, includes_count_str ); - for ( l = c->includes; l; l = list_next( l ) ) - write_netstring( f, object_str( l->value ) ); + for ( iter = list_begin( c->includes ), end = list_end( c->includes ); + iter != end; iter = list_next( iter ) ) + write_netstring( f, object_str( list_item( iter ) ) ); write_netstring( f, hdrscan_count_str ); - for ( l = c->hdrscan; l; l = list_next( l ) ) - write_netstring( f, object_str( l->value ) ); + for ( iter = list_begin( c->hdrscan ), end = list_end( c->hdrscan ); + iter != end; iter = list_next( iter ) ) + write_netstring( f, object_str( list_item( iter ) ) ); fputs( "\n", f ); ++header_count; } @@ -394,19 +395,21 @@ LIST * hcache( TARGET * t, int rec, regexp * re[], LIST * hdrscan ) if ( c->time == t->time ) { LIST *l1 = hdrscan, *l2 = c->hdrscan; - while ( l1 && l2 ) + LISTITER iter1 = list_begin( l1 ), end1 = list_end( l1 ), + iter2 = list_begin( l2 ), end2 = list_end( l2 ); + while ( iter1 != end1 && iter2 != end2 ) { - if (l1->value != l2->value) + if ( list_item( iter1 ) != list_item( iter2 ) ) { - l1 = NULL; + iter1 = end1; } else { - l1 = list_next( l1 ); - l2 = list_next( l2 ); + iter1 = list_next( iter1 ); + iter2 = list_next( iter2 ); } } - if ( l1 || l2 ) + if ( iter1 != end1 || iter2 != end2 ) { if (DEBUG_HEADER) printf( "HDRSCAN out of date in cache for %s\n", @@ -422,8 +425,8 @@ LIST * hcache( TARGET * t, int rec, regexp * re[], LIST * hdrscan ) list_free( c->includes ); list_free( c->hdrscan ); - c->includes = 0; - c->hdrscan = 0; + c->includes = L0; + c->hdrscan = L0; } else { @@ -432,7 +435,7 @@ LIST * hcache( TARGET * t, int rec, regexp * re[], LIST * hdrscan ) object_str( t->boundname ) ); c->age = 0; ++hits; - l = list_copy( 0, c->includes ); + l = list_copy( L0, c->includes ); return l; } } @@ -443,8 +446,8 @@ LIST * hcache( TARGET * t, int rec, regexp * re[], LIST * hdrscan ) object_str( t->boundname ) ); list_free( c->includes ); list_free( c->hdrscan ); - c->includes = 0; - c->hdrscan = 0; + c->includes = L0; + c->hdrscan = L0; } } else @@ -461,12 +464,12 @@ LIST * hcache( TARGET * t, int rec, regexp * re[], LIST * hdrscan ) /* 'c' points at the cache entry. Its out of date. */ - l = headers1( 0, t->boundname, rec, re ); + l = headers1( L0, t->boundname, rec, re ); c->time = t->time; c->age = 0; - c->includes = list_copy( 0, l ); - c->hdrscan = list_copy( 0, hdrscan ); + c->includes = list_copy( L0, l ); + c->hdrscan = list_copy( L0, hdrscan ); return l; } diff --git a/src/engine/headers.c b/src/engine/headers.c index f060cbbf7..92b7be1af 100644 --- a/src/engine/headers.c +++ b/src/engine/headers.c @@ -62,25 +62,28 @@ headers( TARGET * t ) LIST * hdrscan; LIST * hdrrule; #ifndef OPT_HEADER_CACHE_EXT - LIST * headlist = 0; + LIST * headlist = L0; #endif regexp * re[ MAXINC ]; int rec = 0; + LISTITER iter, end; - if ( !( hdrscan = var_get( root_module(), constant_HDRSCAN ) ) ) + hdrscan = var_get( root_module(), constant_HDRSCAN ); + if ( list_empty( hdrscan ) ) return; - if ( !( hdrrule = var_get( root_module(), constant_HDRRULE ) ) ) + hdrrule = var_get( root_module(), constant_HDRRULE ); + if ( list_empty( hdrrule ) ) return; if ( DEBUG_HEADER ) printf( "header scan %s\n", object_str( t->name ) ); /* Compile all regular expressions in HDRSCAN */ - while ( ( rec < MAXINC ) && hdrscan ) + iter = list_begin( hdrscan ), end = list_end( hdrscan ); + for ( ; ( rec < MAXINC ) && iter != end; iter = list_next( iter ) ) { - re[ rec++ ] = regex_compile( hdrscan->value ); - hdrscan = list_next( hdrscan ); + re[ rec++ ] = regex_compile( list_item( iter ) ); } /* Doctor up call to HDRRULE rule */ @@ -101,7 +104,7 @@ headers( TARGET * t ) * $(<) */ lol_add( frame->args, list_new( L0, object_copy( t->boundname ) ) ); - list_free( evaluate_rule( hdrrule->value, frame ) ); + list_free( evaluate_rule( list_front( hdrrule ), frame ) ); } /* Clean up. */ diff --git a/src/engine/jam.c b/src/engine/jam.c index 29c26f89f..7b13f4948 100644 --- a/src/engine/jam.c +++ b/src/engine/jam.c @@ -521,7 +521,7 @@ int main( int argc, char * * argv, char * * arg_environ ) } } - if (!targets_to_update()) + if ( list_empty( targets_to_update() ) ) { mark_target_for_updating( constant_all ); } @@ -569,12 +569,12 @@ int main( int argc, char * * argv, char * * arg_environ ) { LIST *p = L0; p = var_get ( root_module(), constant_PARALLELISM ); - if ( p ) + if ( !list_empty( p ) ) { - int j = atoi( object_str( p->value ) ); + int j = atoi( object_str( list_front( p ) ) ); if ( j == -1 ) { - printf( "Invalid value of PARALLELISM: %s\n", object_str( p->value ) ); + printf( "Invalid value of PARALLELISM: %s\n", object_str( list_front( p ) ) ); } else { @@ -587,9 +587,9 @@ int main( int argc, char * * argv, char * * arg_environ ) { LIST *p = L0; p = var_get( root_module(), constant_KEEP_GOING ); - if ( p ) + if ( !list_empty( p ) ) { - int v = atoi( object_str( p->value ) ); + int v = atoi( object_str( list_front( p ) ) ); if ( v == 0 ) globs.quitquick = 1; else @@ -602,14 +602,15 @@ int main( int argc, char * * argv, char * * arg_environ ) PROFILE_ENTER( MAIN_MAKE ); LIST * targets = targets_to_update(); - if (targets) + if ( !list_empty( targets ) ) { int targets_count = list_length( targets ); + LISTITER iter = list_begin( targets ), end = list_end( targets ); OBJECT * * targets2 = (OBJECT * *) BJAM_MALLOC( targets_count * sizeof( OBJECT * ) ); int n = 0; - for ( ; targets; targets = list_next( targets ) ) - targets2[ n++ ] = targets->value; + for ( ; iter != end; iter = list_next( iter ) ) + targets2[ n++ ] = list_item( iter ); status |= make( targets_count, targets2, anyhow ); BJAM_FREE( (void *)targets2 ); } diff --git a/src/engine/lists.c b/src/engine/lists.c index c291ed082..5ce3d5267 100644 --- a/src/engine/lists.c +++ b/src/engine/lists.c @@ -7,25 +7,63 @@ # include "jam.h" # include "object.h" # include "lists.h" +# include "assert.h" /* * lists.c - maintain lists of objects * - * This implementation essentially uses a singly linked list, but - * guarantees that the head element of every list has a valid pointer - * to the tail of the list, so the new elements can efficiently and - * properly be appended to the end of a list. - * - * To avoid massive allocation, list_free() just tacks the whole freed - * chain onto freelist and list_new() looks on freelist first for an - * available list struct. list_free() does not free the strings in the - * chain: it lazily lets list_new() do so. - * * 08/23/94 (seiwald) - new list_append() * 09/07/00 (seiwald) - documented lol_*() functions */ -static LIST *freelist = 0; /* junkpile for list_free() */ +struct freelist_node { struct freelist_node *next; }; + +static struct freelist_node *freelist[32]; /* junkpile for list_free() */ + +static unsigned get_bucket( unsigned size ) +{ + unsigned bucket = 0; + while ( size > ( 1u << bucket ) ) ++bucket; + return bucket; +} + +static LIST * list_alloc( unsigned size ) +{ + unsigned bucket = get_bucket( size ); + if ( freelist[ bucket ] ) + { + struct freelist_node * result = freelist[ bucket ]; + freelist[ bucket ] = result->next; + return (LIST *)result; + } + else + { + return (LIST *)BJAM_MALLOC( sizeof( LIST ) + ( 1u << bucket ) * sizeof( OBJECT * ) ); + } +} + +static void list_dealloc( LIST * l ) +{ + unsigned size = list_length( l ); + unsigned bucket; + struct freelist_node * node = (struct freelist_node *)l; + + if ( size == 0 ) return; + + bucket = get_bucket( size );; + +#ifdef BJAM_NO_MEM_CACHE + + BJAM_FREE( node ); + +#else + + node->next = freelist[ bucket ]; + freelist[ bucket ] = node; + +#endif + +} /* * list_append() - append a list onto another one, returning total @@ -33,60 +71,66 @@ static LIST *freelist = 0; /* junkpile for list_free() */ LIST * list_append( LIST * l, LIST * nl ) { - if ( !nl ) + if ( list_empty( nl ) ) { /* Just return l */ } - else if ( !l ) + else if ( list_empty( l ) ) { l = nl; } else { - /* Graft two non-empty lists. */ - l->tail->next = nl; - l->tail = nl->tail; + l = list_copy( l, nl ); + list_free( nl ); } return l; } +LISTITER list_begin( LIST * l ) +{ + if ( l ) + return (LISTITER)( (char *)l + sizeof(LIST) ); + else + return 0; +} + +LISTITER list_end( LIST * l ) +{ + if ( l ) + return list_begin( l ) + l->impl.size; + else + return 0; +} + /* * list_new() - tack a string onto the end of a list of strings */ LIST * list_new( LIST * head, OBJECT * value ) { - LIST * l; + unsigned int size = list_length( head ); + unsigned int i; if ( DEBUG_LISTS ) printf( "list > %s <\n", object_str( value ) ); - /* Get list struct from freelist, if one available. */ - /* Otherwise allocate. */ - /* If from freelist, must free string first */ - - if ( freelist ) + /* If the size is a power of 2, reallocate. */ + if ( size == 0 ) { - l = freelist; - object_free( l->value ); - freelist = freelist->next; + head = list_alloc( 1 ); } - else + else if ( ( ( size - 1 ) & size ) == 0 ) { - l = (LIST *)BJAM_MALLOC( sizeof( LIST ) ); + LIST * l = list_alloc( size + 1 ); + memcpy( l, head, sizeof( LIST ) + size * sizeof( OBJECT * ) ); + list_dealloc( head ); + head = l; } - /* If first on chain, head points here. */ - /* If adding to chain, tack us on. */ - /* Tail must point to this new, last element. */ - - if ( !head ) head = l; - else head->tail->next = l; - head->tail = l; - l->next = 0; - - l->value = value; + list_begin( head )[ size ] = value; + head->impl.size = size + 1; return head; } @@ -98,23 +142,64 @@ LIST * list_new( LIST * head, OBJECT * value ) LIST * list_copy( LIST * l, LIST * nl ) { - for ( ; nl; nl = list_next( nl ) ) - l = list_new( l, object_copy( nl->value ) ); + int l_size = list_length( l ); + int nl_size = list_length( nl ); + int size = l_size + nl_size; + unsigned bucket; + int i; + + if ( size == 0 ) return L0; + + bucket = get_bucket( size ); + if ( bucket == 0 || l_size <= ( 1u << (bucket - 1) ) ) + { + LIST * result = list_alloc( size ); + memcpy( list_begin( result ), list_begin( l ), l_size * sizeof( OBJECT * ) ); + list_dealloc( l ); + l = result; + } + + l->impl.size = size; + for ( i = 0; i < nl_size; ++i ) + { + list_begin( l )[ i + l_size ] = object_copy( list_begin( nl )[ i ] ); + } return l; } +LIST * list_copy_range( LIST *l, LISTITER first, LISTITER last ) +{ + if ( first == last ) + { + return L0; + } + else + { + int size = last - first; + LIST * result = list_alloc( size ); + LISTITER dest = list_begin( result ); + result->impl.size = size; + for ( ; first != last; ++first, ++dest ) + { + *dest = object_copy( *first ); + } + return result; + } +} + + /* * list_sublist() - copy a subset of a list of strings. */ LIST * list_sublist( LIST * l, int start, int count ) { - LIST * nl = 0; - for ( ; l && start--; l = list_next( l ) ); - for ( ; l && count--; l = list_next( l ) ) - nl = list_new( nl, object_copy( l->value ) ); - return nl; + int end = start + count; + int size = list_length( l ); + if ( start >= size ) return L0; + if ( end > size ) end = size; + return list_copy_range( l, list_begin( l ) + start, list_begin( l ) + end ); } @@ -130,29 +215,15 @@ LIST * list_sort( LIST * l ) { int len; int ii; - OBJECT * * objects; - LIST * listp; - LIST * result = 0; + LIST * result; if ( !l ) return L0; len = list_length( l ); - objects = (OBJECT * *)BJAM_MALLOC( len * sizeof(OBJECT*) ); + result = list_copy( L0, l ); - listp = l; - for ( ii = 0; ii < len; ++ii ) - { - objects[ ii ] = listp->value; - listp = listp->next; - } - - qsort( objects, len, sizeof( OBJECT * ), str_ptr_compare ); - - for ( ii = 0; ii < len; ++ii ) - result = list_append( result, list_new( 0, object_copy( objects[ ii ] ) ) ); - - BJAM_FREE( objects ); + qsort( list_begin( result ), len, sizeof( OBJECT * ), str_ptr_compare ); return result; } @@ -164,24 +235,15 @@ LIST * list_sort( LIST * l ) void list_free( LIST * head ) { -#ifdef BJAM_NO_MEM_CACHE - LIST *l, *tmp; - for( l = head; l; ) + if ( !list_empty( head ) ) { - object_free( l->value ); - l->value = 0; - tmp = l; - l = l->next; - BJAM_FREE( tmp ); + LISTITER iter = list_begin( head ), end = list_end( head ); + for ( ; iter != end; iter = list_next( iter ) ) + { + object_free( list_item( iter ) ); + } + list_dealloc( head ); } -#else - /* Just tack onto freelist. */ - if ( head ) - { - head->tail->next = freelist; - freelist = head; - } -#endif } @@ -191,40 +253,64 @@ void list_free( LIST * head ) LIST * list_pop_front( LIST * l ) { - LIST * result = l->next; - if ( result ) + unsigned size = list_length( l ); + assert( size != 0 ); + --size; + object_free( list_front( l ) ); + + if ( size == 0 ) { - result->tail = l->tail; - l->next = L0; - l->tail = l; + list_dealloc( l ); + return L0; + } + else if ( ( ( size - 1 ) & size ) == 0 ) + { + LIST * nl = list_alloc( size ); + nl->impl.size = size; + memcpy( list_begin( nl ), list_begin( l ) + 1, size * sizeof( OBJECT * ) ); + list_dealloc( l ); + return nl; + } + else + { + l->impl.size = size; + memmove( list_begin( l ), list_begin( l ) + 1, size * sizeof( OBJECT * ) ); + return l; } - list_free( l ); - return result; } LIST * list_reverse( LIST * l ) { - LIST * result = L0; - for ( ; l; l = l->next ) + int size = list_length( l ); + if ( size == 0 ) return L0; + else { - result = list_append( list_new(L0, object_copy( l->value ) ), result ); + LIST * result = list_alloc( size ); + int i; + result->impl.size = size; + for ( i = 0; i < size; ++i ) + { + list_begin( result )[ i ] = object_copy( list_begin( l )[ size - i - 1 ] ); + } + return result; } - return result; } int list_cmp( LIST * t, LIST * s ) { int status = 0; + LISTITER t_it = list_begin( t ), t_end = list_end( t ); + LISTITER s_it = list_begin( s ), s_end = list_end( s ); - while ( !status && ( t || s ) ) + while ( !status && ( t_it != t_end || s_it != s_end ) ) { - const char *st = t ? object_str( t->value ) : ""; - const char *ss = s ? object_str( s->value ) : ""; + const char *st = t_it != t_end ? object_str( list_item( t_it ) ) : ""; + const char *ss = s_it != s_end ? object_str( list_item( s_it ) ) : ""; status = strcmp( st, ss ); - t = t ? list_next( t ) : t; - s = s ? list_next( s ) : s; + t_it = t_it != t_end ? list_next( t_it ) : t_it; + s_it = s_it != s_end ? list_next( s_it ) : s_it; } return status; @@ -232,9 +318,10 @@ int list_cmp( LIST * t, LIST * s ) int list_is_sublist( LIST * sub, LIST * l ) { - for ( ; sub; sub = sub->next ) + LISTITER iter = list_begin( sub ), end = list_end( sub ); + for ( ; iter != end; iter = list_next( iter ) ) { - if ( !list_in( l, sub->value ) ) + if ( !list_in( l, list_item( iter ) ) ) return 0; } return 1; @@ -246,12 +333,14 @@ int list_is_sublist( LIST * sub, LIST * l ) void list_print( LIST * l ) { - LIST * p = 0; - for ( ; l; p = l, l = list_next( l ) ) - if ( p ) - printf( "%s ", object_str( p->value ) ); - if ( p ) - printf( "%s", object_str( p->value ) ); + LISTITER iter = list_begin( l ), end = list_end( l ); + if ( iter != end ) + { + printf( "%s", object_str( list_item( iter ) ) ); + iter = list_next( iter ); + for ( ; iter != end; iter = list_next( iter ) ) + printf( " %s", object_str( list_item( iter ) ) ); + } } @@ -261,16 +350,18 @@ void list_print( LIST * l ) int list_length( LIST * l ) { - int n = 0; - for ( ; l; l = list_next( l ), ++n ); - return n; + if ( l ) + return l->impl.size; + else + return 0; } int list_in( LIST * l, OBJECT * value ) { - for ( ; l; l = l->next ) - if ( object_equal( l->value, value ) ) + LISTITER iter = list_begin( l ), end = list_end( l ); + for ( ; iter != end; iter = list_next( iter ) ) + if ( object_equal( list_item( iter ), value ) ) return 1; return 0; } @@ -278,15 +369,16 @@ int list_in( LIST * l, OBJECT * value ) LIST * list_unique( LIST * sorted_list ) { - LIST * result = 0; - LIST * last_added = 0; + LIST * result = L0; + OBJECT * last_added = 0; - for ( ; sorted_list; sorted_list = sorted_list->next ) + LISTITER iter = list_begin( sorted_list ), end = list_end( sorted_list ); + for ( ; iter != end; iter = list_next( iter ) ) { - if ( !last_added || !object_equal( sorted_list->value, last_added->value ) ) + if ( !last_added || !object_equal( list_item( iter ), last_added ) ) { - result = list_new( result, object_copy( sorted_list->value ) ); - last_added = sorted_list; + result = list_new( result, object_copy( list_item( iter ) ) ); + last_added = list_item( iter ); } } return result; @@ -294,14 +386,18 @@ LIST * list_unique( LIST * sorted_list ) void list_done() { - LIST *l, *tmp; - for( l = freelist; l; ) + int i; + int total = 0; + for ( i = 0; i < sizeof( freelist ) / sizeof( freelist[ 0 ] ); ++i ) { - object_free( l->value ); - l->value = 0; - tmp = l; - l = l->next; - BJAM_FREE( tmp ); + struct freelist_node *l, *tmp; + int bytes; + for( l = freelist[ i ]; l; ) + { + tmp = l; + l = l->next; + BJAM_FREE( tmp ); + } } } @@ -346,7 +442,7 @@ void lol_free( LOL * lol ) LIST * lol_get( LOL * lol, int i ) { - return i < lol->count ? lol->list[ i ] : 0; + return i < lol->count ? lol->list[ i ] : L0; } @@ -371,10 +467,11 @@ void lol_print( LOL * lol ) PyObject *list_to_python(LIST *l) { PyObject *result = PyList_New(0); + LISTITER iter = list_begin( l ), end = list_end( l ); - for (; l; l = l->next) + for (; iter != end; iter = list_next( iter ) ) { - PyObject* s = PyString_FromString(object_str(l->value)); + PyObject* s = PyString_FromString(object_str(list_item(iter))); PyList_Append(result, s); Py_DECREF(s); } @@ -384,7 +481,7 @@ PyObject *list_to_python(LIST *l) LIST *list_from_python(PyObject *l) { - LIST * result = 0; + LIST * result = L0; Py_ssize_t i, n; n = PySequence_Size(l); diff --git a/src/engine/lists.h b/src/engine/lists.h index 0a1595af7..290d285a8 100644 --- a/src/engine/lists.h +++ b/src/engine/lists.h @@ -58,11 +58,14 @@ typedef struct _list LIST; struct _list { - LIST *next; - LIST *tail; /* only valid in head node */ - OBJECT *value; /* private copy */ + union { + int size; + OBJECT *align; + } impl; }; +typedef OBJECT * * LISTITER; + /* * LOL - list of LISTs */ @@ -78,6 +81,7 @@ struct _lol { LIST * list_append( LIST *l, LIST *nl ); LIST * list_copy( LIST *l, LIST *nl ); +LIST * list_copy_range( LIST *l, LISTITER first, LISTITER last ); void list_free( LIST *head ); LIST * list_new( LIST *head, OBJECT *string ); void list_print( LIST *l ); @@ -92,7 +96,12 @@ int list_cmp( LIST * lhs, LIST * rhs ); int list_is_sublist( LIST * sub, LIST * l ); void list_done(); -# define list_next( l ) ((l)->next) +LISTITER list_begin( LIST * ); +LISTITER list_end( LIST * ); +# define list_next( it ) ((it) + 1) +# define list_item( it ) (*(it)) +# define list_empty( l ) ( (l) == L0 ) +# define list_front( l ) list_item( list_begin( l ) ) # define L0 ((LIST *)0) diff --git a/src/engine/make.c b/src/engine/make.c index d6677d952..9bed18096 100644 --- a/src/engine/make.c +++ b/src/engine/make.c @@ -319,9 +319,9 @@ void make0 #ifdef OPT_SEMAPHORE { LIST * var = var_get( root_module(), constant_JAM_SEMAPHORE ); - if ( var ) + if ( !list_empty( var ) ) { - TARGET * semaphore = bindtarget( var->value ); + TARGET * semaphore = bindtarget( list_front( var ) ); semaphore->progress = T_MAKE_SEMAPHORE; t->semaphore = semaphore; } @@ -793,7 +793,7 @@ static TARGETS * make0sort( TARGETS * chain ) } -static LIST * targets_to_update_ = 0; +static LIST * targets_to_update_ = L0; void mark_target_for_updating( OBJECT * target ) @@ -811,5 +811,5 @@ LIST * targets_to_update() void clear_targets_to_update() { list_free( targets_to_update_ ); - targets_to_update_ = 0; + targets_to_update_ = L0; } diff --git a/src/engine/make1.c b/src/engine/make1.c index 1e80b5a74..2d66c22e1 100644 --- a/src/engine/make1.c +++ b/src/engine/make1.c @@ -532,7 +532,7 @@ static void make1c( state * pState ) ( !( cmd->rule->actions->flags & RULE_QUIETLY ) && DEBUG_MAKE ) ) { rule_name = object_str( cmd->rule->name ); - target = object_str( lol_get( &cmd->args, 0 )->value ); + target = object_str( list_front( lol_get( &cmd->args, 0 ) ) ); if ( globs.noexec ) out_action( rule_name, target, cmd->buf->value, "", "", EXIT_OK ); } @@ -696,7 +696,7 @@ static void call_timing_rule( TARGET * target, timing_info * time ) timing_rule = var_get( root_module(), constant_TIMING_RULE ); popsettings( root_module(), target->settings ); - if ( timing_rule ) + if ( !list_empty( timing_rule ) ) { /* rule timing-rule ( args * : target : start end user system ) */ @@ -705,7 +705,7 @@ static void call_timing_rule( TARGET * target, timing_info * time ) frame_init( frame ); /* args * :: $(__TIMING_RULE__[2-]) */ - lol_add( frame->args, list_copy( L0, timing_rule->next ) ); + lol_add( frame->args, list_copy_range( timing_rule, list_next( list_begin( timing_rule ) ), list_end( timing_rule ) ) ); /* target :: the name of the target */ lol_add( frame->args, list_new( L0, object_copy( target->name ) ) ); @@ -718,7 +718,7 @@ static void call_timing_rule( TARGET * target, timing_info * time ) outf_double( time->system ) ) ); /* Call the rule. */ - evaluate_rule( timing_rule->value, frame ); + evaluate_rule( list_front( timing_rule ), frame ); /* Clean up. */ frame_free( frame ); @@ -747,7 +747,7 @@ static void call_action_rule action_rule = var_get( root_module(), constant_ACTION_RULE ); popsettings( root_module(), target->settings ); - if ( action_rule ) + if ( !list_empty( action_rule ) ) { /* rule action-rule ( args * : @@ -760,7 +760,7 @@ static void call_action_rule frame_init( frame ); /* args * :: $(__ACTION_RULE__[2-]) */ - lol_add( frame->args, list_copy( L0, action_rule->next ) ); + lol_add( frame->args, list_copy_range( action_rule, list_next( list_begin( action_rule ) ), list_end( action_rule ) ) ); /* target :: the name of the target */ lol_add( frame->args, list_new( L0, object_copy( target->name ) ) ); @@ -782,7 +782,7 @@ static void call_action_rule lol_add( frame->args, L0 ); /* Call the rule. */ - evaluate_rule( action_rule->value, frame ); + evaluate_rule( list_front( action_rule ), frame ); /* Clean up. */ frame_free( frame ); @@ -872,16 +872,17 @@ static void make1d( state * pState ) if (status != EXEC_CMD_OK) { LIST * targets = lol_get( &cmd->args, 0 ); - for ( ; targets; targets = list_next( targets ) ) + LISTITER iter = list_begin( targets ), end = list_end( targets ); + for ( ; iter != end; iter = list_next( iter ) ) { int need_unlink = 1; - TARGET* t = bindtarget ( targets->value ); + TARGET* t = bindtarget ( list_item( iter ) ); if (t->flags & T_FLAG_PRECIOUS) { need_unlink = 0; } - if (need_unlink && !unlink( object_str( targets->value ) ) ) - printf( "...removing %s\n", object_str( targets->value ) ); + if (need_unlink && !unlink( object_str( list_item( iter ) ) ) ) + printf( "...removing %s\n", object_str( list_item( iter ) ) ); } } @@ -934,7 +935,7 @@ static void swap_settings static CMD * make1cmds( TARGET * t ) { CMD * cmds = 0; - LIST * shell = 0; + LIST * shell = L0; module_t * settings_module = 0; TARGET * settings_target = 0; ACTIONS * a0; @@ -980,14 +981,14 @@ static CMD * make1cmds( TARGET * t ) /* If doing only updated (or existing) sources, but none have been * updated (or exist), skip this action. */ - if ( !ns && ( actions->flags & ( RULE_NEWSRCS | RULE_EXISTING ) ) ) + if ( list_empty( ns ) && ( actions->flags & ( RULE_NEWSRCS | RULE_EXISTING ) ) ) { list_free( nt ); continue; } swap_settings( &settings_module, &settings_target, rule->module, t ); - if ( !shell ) + if ( list_empty( shell ) ) { shell = var_get( rule->module, constant_JAMSHELL ); /* shell is per-target */ } @@ -1098,11 +1099,11 @@ static LIST * make1list( LIST * l, TARGETS * targets, int flags ) /* Prohibit duplicates for RULE_TOGETHER. */ if ( flags & RULE_TOGETHER ) { - LIST * m; - for ( m = l; m; m = m->next ) - if ( object_equal( m->value, t->boundname ) ) + LISTITER iter = list_begin( l ), end = list_end( l ); + for ( ; iter != end; iter = list_next( iter ) ) + if ( object_equal( list_item( iter ), t->boundname ) ) break; - if ( m ) + if ( iter != end ) continue; } @@ -1122,14 +1123,16 @@ static SETTINGS * make1settings( struct module_t * module, LIST * vars ) { SETTINGS * settings = 0; - for ( ; vars; vars = list_next( vars ) ) + LISTITER vars_iter = list_begin( vars ), vars_end = list_end( vars ); + for ( ; vars_iter != vars_end; vars_iter = list_next( vars_iter ) ) { - LIST * l = var_get( module, vars->value ); - LIST * nl = 0; + LIST * l = var_get( module, list_item( vars_iter ) ); + LIST * nl = L0; + LISTITER iter = list_begin( l ), end = list_end( l ); - for ( ; l; l = list_next( l ) ) + for ( ; iter != end; iter = list_next( iter ) ) { - TARGET * t = bindtarget( l->value ); + TARGET * t = bindtarget( list_item( iter ) ); /* Make sure the target is bound. */ if ( t->binding == T_BIND_UNBOUND ) @@ -1140,7 +1143,7 @@ static SETTINGS * make1settings( struct module_t * module, LIST * vars ) } /* Add to settings chain. */ - settings = addsettings( settings, VAR_SET, vars->value, nl ); + settings = addsettings( settings, VAR_SET, list_item( vars_iter ), nl ); } return settings; diff --git a/src/engine/modules.c b/src/engine/modules.c index 31bda93ae..bf12e287f 100644 --- a/src/engine/modules.c +++ b/src/engine/modules.c @@ -152,15 +152,17 @@ void import_module( LIST * module_names, module_t * target_module ) PROFILE_ENTER( IMPORT_MODULE ); struct hash * h; + LISTITER iter, end; if ( !target_module->imported_modules ) target_module->imported_modules = hashinit( sizeof( char * ), "imported" ); h = target_module->imported_modules; - for ( ; module_names; module_names = module_names->next ) + iter = list_begin( module_names ), end = list_end( module_names ); + for ( ; iter != end; iter = list_next( iter ) ) { int found; - OBJECT * s = module_names->value; + OBJECT * s = list_item( iter ); OBJECT * * ss = (OBJECT * *)hash_insert( h, s, &found ); if( !found ) { diff --git a/src/engine/modules/order.c b/src/engine/modules/order.c index d25744c26..5aa992454 100644 --- a/src/engine/modules/order.c +++ b/src/engine/modules/order.c @@ -15,8 +15,9 @@ LIST *add_pair( FRAME *frame, int flags ) { LIST* arg = lol_get( frame->args, 0 ); + LISTITER iter = list_begin( arg ), end = list_end( arg ); - var_set(frame->module, arg->value, list_copy(0, arg->next), VAR_APPEND); + var_set( frame->module, list_item( iter ), list_copy_range( arg, list_next( iter ), end ), VAR_APPEND ); return L0; } @@ -27,8 +28,9 @@ LIST *add_pair( FRAME *frame, int flags ) int list_index(LIST* list, OBJECT* value) { int result = 0; - for(; list; list = list->next, ++result) { - if (object_equal(list->value, value)) + LISTITER iter = list_begin(list), end = list_end(list); + for(; iter != end; iter = list_next(iter), ++result) { + if (object_equal(list_item(iter), value)) return result; } return -1; @@ -76,10 +78,10 @@ void topological_sort(int** graph, int num_vertices, int* result) LIST *order( FRAME *frame, int flags ) { - LIST* arg = lol_get( frame->args, 0 ); - LIST* tmp; - LIST* result = 0; + LIST* arg = lol_get( frame->args, 0 ); + LIST* result = L0; int src; + LISTITER iter = list_begin(arg), end = list_end(arg); /* We need to create a graph of order dependencies between the passed objects. We assume that there are no duplicates @@ -89,15 +91,16 @@ LIST *order( FRAME *frame, int flags ) int** graph = (int**)BJAM_CALLOC(length, sizeof(int*)); int* order = (int*)BJAM_MALLOC((length+1)*sizeof(int)); - for(tmp = arg, src = 0; tmp; tmp = tmp->next, ++src) { + for(src = 0; iter != end; iter = list_next(iter), ++src) { /* For all object this one depend upon, add elements to 'graph' */ - LIST* dependencies = var_get(frame->module, tmp->value); + LIST* dependencies = var_get(frame->module, list_item(iter)); int index = 0; + LISTITER dep_iter = list_begin(dependencies), dep_end = list_end(dependencies); graph[src] = (int*)BJAM_CALLOC(list_length(dependencies)+1, sizeof(int)); - for(; dependencies; dependencies = dependencies->next) { - int dst = list_index(arg, dependencies->value); + for(; dep_iter != dep_end; dep_iter = list_next(dep_iter)) { + int dst = list_index(arg, list_item(dep_iter)); if (dst != -1) graph[src][index++] = dst; } @@ -110,9 +113,9 @@ LIST *order( FRAME *frame, int flags ) int index = length-1; for(; index >= 0; --index) { int i; - tmp = arg; - for (i = 0; i < order[index]; ++i, tmp = tmp->next); - result = list_new(result, object_copy(tmp->value)); + iter = list_begin(arg), end = list_end(arg); + for (i = 0; i < order[index]; ++i, iter = list_next(iter)); + result = list_new(result, object_copy(list_item(iter))); } } diff --git a/src/engine/modules/path.c b/src/engine/modules/path.c index cfa9b0b8c..b92454304 100644 --- a/src/engine/modules/path.c +++ b/src/engine/modules/path.c @@ -11,10 +11,10 @@ LIST *path_exists( FRAME *frame, int flags ) LIST* l = lol_get( frame->args, 0 ); time_t time; - timestamp(l->value, &time); + timestamp(list_front(l), &time); if (time != 0) { - return list_new(0, object_new("true")); + return list_new(L0, object_new("true")); } else { diff --git a/src/engine/modules/property-set.c b/src/engine/modules/property-set.c index 45d108ab2..d3fb9aba5 100644 --- a/src/engine/modules/property-set.c +++ b/src/engine/modules/property-set.c @@ -44,15 +44,15 @@ rule create ( raw-properties * ) LIST *property_set_create( FRAME *frame, int flags ) { LIST* properties = lol_get( frame->args, 0 ); - LIST* sorted = 0; + LIST* sorted = L0; #if 0 LIST* order_sensitive = 0; #endif LIST* unique; - LIST* tmp; LIST* val; string var[1]; OBJECT* name; + LISTITER iter, end; #if 0 /* Sort all properties which are not order sensitive */ @@ -77,25 +77,26 @@ LIST *property_set_create( FRAME *frame, int flags ) string_new(var); string_append(var, ".ps."); - for(tmp = unique; tmp; tmp = tmp->next) { - string_append(var, object_str( tmp->value )); + iter = list_begin( unique ), end = list_end( unique ); + for( ; iter != end; iter = list_next( iter ) ) { + string_append(var, object_str( list_item( iter ) )); string_push_back(var, '-'); } name = object_new(var->value); val = var_get(frame->module, name); - if (val == 0) + if (list_empty(val)) { OBJECT* rulename = object_new("new"); val = call_rule(rulename, frame, - list_append(list_new(0, object_new("property-set")), unique), 0); + list_append(list_new(L0, object_new("property-set")), unique), 0); object_free(rulename); - var_set(frame->module, name, list_copy(0, val), VAR_SET); + var_set(frame->module, name, list_copy(L0, val), VAR_SET); } else { list_free(unique); - val = list_copy(0, val); + val = list_copy(L0, val); } object_free(name); diff --git a/src/engine/modules/regex.c b/src/engine/modules/regex.c index 257bf0f47..b3d690913 100644 --- a/src/engine/modules/regex.c +++ b/src/engine/modules/regex.c @@ -33,18 +33,19 @@ LIST *regex_transform( FRAME *frame, int flags ) int* indices = 0; int size; int* p; - LIST* result = 0; + LIST* result = L0; string buf[1]; string_new(buf); - if (indices_list) + if (!list_empty(indices_list)) { + LISTITER iter = list_begin(indices_list), end = list_end(indices_list); size = list_length(indices_list); indices = (int*)BJAM_MALLOC(size*sizeof(int)); - for(p = indices; indices_list; indices_list = indices_list->next) + for(p = indices; iter != end; iter = list_next(iter)) { - *p++ = atoi(object_str(indices_list->value)); + *p++ = atoi(object_str(list_item(iter))); } } else @@ -56,11 +57,12 @@ LIST *regex_transform( FRAME *frame, int flags ) { /* Result is cached and intentionally never freed */ - regexp *re = regex_compile( pattern->value ); + regexp *re = regex_compile( list_front( pattern ) ); - for(; l; l = l->next) + LISTITER iter = list_begin( l ), end = list_end( l ); + for( ; iter != end; iter = list_next( iter ) ) { - if( regexec( re, object_str( l->value ) ) ) + if( regexec( re, object_str( list_item( iter ) ) ) ) { int i = 0; for(; i < size; ++i) diff --git a/src/engine/modules/sequence.c b/src/engine/modules/sequence.c index 714432ac5..83ab5c92f 100644 --- a/src/engine/modules/sequence.c +++ b/src/engine/modules/sequence.c @@ -17,17 +17,21 @@ LIST *sequence_select_highest_ranked( FRAME *frame, int flags ) LIST* elements = lol_get( frame->args, 0 ); LIST* rank = lol_get( frame->args, 1 ); + LISTITER iter, end, elements_iter, elements_end; - LIST* result = 0; + LIST* result = L0; LIST* tmp; int highest_rank = -1; - for (tmp = rank; tmp; tmp = tmp->next) - highest_rank = max(highest_rank, atoi(object_str(tmp->value))); - - for (; rank; rank = rank->next, elements = elements->next) - if (atoi(object_str(rank->value)) == highest_rank) - result = list_new(result, object_copy(elements->value)); + iter = list_begin(rank), end = list_end(rank); + for (; iter != end; iter = list_next(iter)) + highest_rank = max(highest_rank, atoi(object_str(list_item(iter)))); + + iter = list_begin(rank), end = list_end(rank); + elements_iter = list_begin(elements), elements_end = list_end(elements); + for (; iter != end; iter = list_next(iter), elements_iter = list_next(elements_iter)) + if (atoi(object_str(list_item(iter))) == highest_rank) + result = list_new(result, object_copy(list_item(elements_iter))); return result; } diff --git a/src/engine/modules/set.c b/src/engine/modules/set.c index 3c4125874..a56a04bfd 100644 --- a/src/engine/modules/set.c +++ b/src/engine/modules/set.c @@ -23,11 +23,12 @@ LIST *set_difference( FRAME *frame, int flags ) LIST* b = lol_get( frame->args, 0 ); LIST* a = lol_get( frame->args, 1 ); - LIST* result = 0; - for(; b; b = b->next) + LIST* result = L0; + LISTITER iter = list_begin( b ), end = list_end( b ); + for( ; iter != end; iter = list_next( iter ) ) { - if (!list_in(a, b->value)) - result = list_new(result, object_copy(b->value)); + if (!list_in(a, list_item(iter))) + result = list_new(result, object_copy(list_item(iter))); } return result; } diff --git a/src/engine/rules.c b/src/engine/rules.c index 55d351367..357d4e5bd 100644 --- a/src/engine/rules.c +++ b/src/engine/rules.c @@ -237,8 +237,9 @@ void touch_target( OBJECT * t ) TARGETS * targetlist( TARGETS * chain, LIST * target_names ) { - for ( ; target_names; target_names = list_next( target_names ) ) - chain = targetentry( chain, bindtarget( target_names->value ) ); + LISTITER iter = list_begin( target_names ), end = list_end( target_names ); + for ( ; iter != end; iter = list_next( iter ) ) + chain = targetentry( chain, bindtarget( list_item( iter ) ) ); return chain; } @@ -404,7 +405,7 @@ SETTINGS * copysettings( SETTINGS * head ) SETTINGS * copy = 0; SETTINGS * v; for ( v = head; v; v = v->next ) - copy = addsettings( copy, VAR_SET, v->symbol, list_copy( 0, v->value ) ); + copy = addsettings( copy, VAR_SET, v->symbol, list_copy( L0, v->value ) ); return copy; } diff --git a/src/engine/search.c b/src/engine/search.c index 186c32728..101c8f5bd 100644 --- a/src/engine/search.c +++ b/src/engine/search.c @@ -40,7 +40,7 @@ void call_bind_rule ) { LIST * bind_rule = var_get( root_module(), constant_BINDRULE ); - if ( bind_rule ) + if ( !list_empty( bind_rule ) ) { OBJECT * target = object_copy( target_ ); OBJECT * boundname = object_copy( boundname_ ); @@ -55,7 +55,7 @@ void call_bind_rule lol_add( frame->args, list_new( L0, boundname ) ); if ( lol_get( frame->args, 1 ) ) - list_free( evaluate_rule( bind_rule->value, frame ) ); + list_free( evaluate_rule( list_front( bind_rule ), frame ) ); /* Clean up */ frame_free( frame ); @@ -117,11 +117,12 @@ search( f->f_grist.ptr = 0; f->f_grist.len = 0; - if ( ( varlist = var_get( root_module(), constant_LOCATE ) ) ) + varlist = var_get( root_module(), constant_LOCATE ); + if ( !list_empty( varlist ) ) { OBJECT * key; - f->f_root.ptr = object_str( varlist->value ); - f->f_root.len = strlen( object_str( varlist->value ) ); + f->f_root.ptr = object_str( list_front( varlist ) ); + f->f_root.len = strlen( object_str( list_front( varlist ) ) ); path_build( f, buf, 1 ); @@ -135,17 +136,18 @@ search( object_free( key ); found = 1; } - else if ( varlist = var_get( root_module(), constant_SEARCH ) ) + else if ( varlist = var_get( root_module(), constant_SEARCH ), !list_empty( varlist ) ) { - while ( varlist ) + LISTITER iter = list_begin( varlist ), end = list_end( varlist ); + for ( ; iter != end; iter = list_next( iter ) ) { BINDING * ba; file_info_t *ff; OBJECT * key; OBJECT * test_path; - f->f_root.ptr = object_str( varlist->value ); - f->f_root.len = strlen( object_str( varlist->value ) ); + f->f_root.ptr = object_str( list_item( iter ) ); + f->f_root.len = strlen( object_str( list_item( iter ) ) ); string_truncate( buf, 0 ); path_build( f, buf, 1 ); @@ -180,8 +182,6 @@ search( } } object_free( key ); - - varlist = list_next( varlist ); } } diff --git a/src/engine/subst.c b/src/engine/subst.c index 4ca404dc3..270c619a1 100644 --- a/src/engine/subst.c +++ b/src/engine/subst.c @@ -40,26 +40,27 @@ LIST * builtin_subst( FRAME * frame, int flags ) { LIST* result = L0; LIST* arg1 = lol_get( frame->args, 0 ); + LISTITER iter = list_begin( arg1 ), end = list_end( arg1 ); - if ( arg1 && list_next(arg1) && list_next(list_next(arg1)) ) + if ( iter != end && list_next( iter ) != end && list_next( list_next( iter ) ) != end ) { - const char* source = object_str( arg1->value ); - OBJECT * pattern = list_next(arg1)->value; + const char* source = object_str( list_item( iter ) ); + OBJECT * pattern = list_item( list_next( iter ) ); regexp* repat = regex_compile( pattern ); if ( regexec( repat, (char*)source) ) { - LIST* subst = list_next(arg1); + LISTITER subst = list_next( iter ); - while ((subst = list_next(subst)) != L0) + while ( ( subst = list_next( subst ) ) != end ) { # define BUFLEN 4096 char buf[BUFLEN + 1]; - const char* in = object_str( subst->value ); + const char* in = object_str( list_item( subst ) ); char* out = buf; - for ( in = object_str( subst->value ); *in && out < buf + BUFLEN; ++in ) + for ( ; *in && out < buf + BUFLEN; ++in ) { if ( *in == '\\' || *in == '$' ) { diff --git a/src/engine/variable.c b/src/engine/variable.c index 459f794fa..0cc26d084 100644 --- a/src/engine/variable.c +++ b/src/engine/variable.c @@ -160,7 +160,7 @@ void var_defines( struct module_t * module, char * const * e, int preprocess ) } -static LIST * saved_var = 0; +static LIST * saved_var = L0; /* * var_get() - get value of a user defined symbol. @@ -170,7 +170,7 @@ static LIST * saved_var = 0; LIST * var_get( struct module_t * module, OBJECT * symbol ) { - LIST * result = 0; + LIST * result = L0; #ifdef OPT_AT_FILES /* Some "fixed" variables... */ if ( object_equal( symbol, constant_TMPDIR ) ) @@ -246,7 +246,7 @@ void var_set( struct module_t * module, OBJECT * symbol, LIST * value, int flag case VAR_DEFAULT: /* Set only if unset */ - if ( !v->value ) + if ( list_empty( v->value ) ) v->value = value; else list_free( value ); @@ -286,7 +286,7 @@ static VARIABLE * var_enter( struct module_t * module, OBJECT * symbol ) if ( !found ) { v->symbol = object_copy( symbol ); - v->value = 0; + v->value = L0; } return v; @@ -320,7 +320,7 @@ static void delete_var_( void * xvar, void * data ) void var_done( struct module_t * module ) { list_free( saved_var ); - saved_var = 0; + saved_var = L0; hashenumerate( module->variables, delete_var_, (void *)0 ); hashdone( module->variables ); } diff --git a/src/engine/w32_getreg.c b/src/engine/w32_getreg.c index 8f67e058c..c4d58e1c0 100644 --- a/src/engine/w32_getreg.c +++ b/src/engine/w32_getreg.c @@ -59,7 +59,7 @@ static HKEY get_key(char const** path) LIST * builtin_system_registry( FRAME * frame, int flags ) { - char const* path = object_str( lol_get(frame->args, 0)->value ); + char const* path = object_str( list_front( lol_get(frame->args, 0) ) ); LIST* result = L0; HKEY key = get_key(&path); @@ -71,10 +71,10 @@ LIST * builtin_system_registry( FRAME * frame, int flags ) DWORD type; BYTE data[MAX_REGISTRY_DATA_LENGTH]; DWORD len = sizeof(data); - LIST const* const field = lol_get(frame->args, 1); + LIST * const field = lol_get(frame->args, 1); if ( ERROR_SUCCESS == - RegQueryValueEx(key, field ? object_str( field->value ) : 0, 0, &type, data, &len) ) + RegQueryValueEx(key, field ? object_str( list_front( field ) ) : 0, 0, &type, data, &len) ) { switch (type) { @@ -186,8 +186,8 @@ static LIST* get_value_names(HKEY key, char const* path) LIST * builtin_system_registry_names( FRAME * frame, int flags ) { - char const* path = object_str( lol_get(frame->args, 0)->value ); - char const* result_type = object_str( lol_get(frame->args, 1)->value ); + char const* path = object_str( list_front( lol_get(frame->args, 0) ) ); + char const* result_type = object_str( list_front( lol_get(frame->args, 1) ) ); HKEY key = get_key(&path);