Some hopefully good changes to get IOs off of halloc

This commit is contained in:
ridiculousfish 2012-02-09 18:43:36 -08:00
parent 646240fc54
commit e5ff5f7484
10 changed files with 44 additions and 96 deletions

View file

@ -113,11 +113,6 @@ int debug_level=1;
*/ */
static struct winsize termsize; static struct winsize termsize;
/**
String buffer used by the wsetlocale function
*/
static string_buffer_t *setlocale_buff=0;
void show_stackframe() void show_stackframe()
{ {
@ -485,20 +480,26 @@ wchar_t **strv2wcsv( const char **in )
return res; return res;
} }
wcstring format_string(const wchar_t *format, ...) wcstring format_string(const wchar_t *format, ...)
{ {
va_list va; va_list va;
va_start( va, format ); va_start( va, format );
string_buffer_t buffer; wcstring result = vformat_string(format, va);
sb_init(&buffer);
sb_vprintf(&buffer, format, va);
wcstring result = (wchar_t *)buffer.buff;
sb_destroy(&buffer);
va_end( va ); va_end( va );
return result; return result;
} }
wcstring vformat_string(const wchar_t *format, va_list va_orig)
{
string_buffer_t buffer;
sb_init(&buffer);
sb_vprintf(&buffer, format, va_orig);
wcstring result = (wchar_t *)buffer.buff;
sb_destroy(&buffer);
return result;
}
wchar_t *wcsvarname( const wchar_t *str ) wchar_t *wcsvarname( const wchar_t *str )
{ {
while( *str ) while( *str )

View file

@ -361,6 +361,7 @@ public:
void append_path_component(wcstring &path, const wcstring &component); void append_path_component(wcstring &path, const wcstring &component);
wcstring format_string(const wchar_t *format, ...); wcstring format_string(const wchar_t *format, ...);
wcstring vformat_string(const wchar_t *format, va_list va_orig);
/** /**
Returns a newly allocated wide character string array equivalent of Returns a newly allocated wide character string array equivalent of

View file

@ -950,11 +950,11 @@ void exec( parser_t &parser, job_t *j )
{ {
if( j->io ) if( j->io )
{ {
j->io = io_add( io_duplicate( j, parser.block_io), j->io ); j->io = io_add( io_duplicate(parser.block_io), j->io );
} }
else else
{ {
j->io=io_duplicate( j, parser.block_io); j->io=io_duplicate(parser.block_io);
} }
} }

16
io.cpp
View file

@ -48,8 +48,6 @@ Utilities for io redirection.
#include "common.h" #include "common.h"
#include "io.h" #include "io.h"
#include "halloc.h"
void io_buffer_read( io_data_t *d ) void io_buffer_read( io_data_t *d )
{ {
@ -195,23 +193,15 @@ io_data_t *io_remove( io_data_t *list, io_data_t *element )
return list; return list;
} }
io_data_t *io_duplicate( void *context, io_data_t *l ) io_data_t *io_duplicate( io_data_t *l )
{ {
io_data_t *res; io_data_t *res;
if( l == 0 ) if( l == 0 )
return 0; return 0;
res = (io_data_t *)halloc( context, sizeof( io_data_t) ); res = new io_data_t(*l);
res->next=io_duplicate(l->next );
if( !res )
{
DIE_MEM();
}
memcpy( res, l, sizeof(io_data_t ));
res->next=io_duplicate( context, l->next );
return res; return res;
} }

16
io.h
View file

@ -11,7 +11,7 @@ enum io_mode
; ;
/** Represents an FD redirection */ /** Represents an FD redirection */
typedef struct io_data struct io_data_t
{ {
/** Type of redirect */ /** Type of redirect */
int io_mode; int io_mode;
@ -41,8 +41,7 @@ typedef struct io_data
buffer_t *out_buffer; buffer_t *out_buffer;
/** Whether to close old_fd for IO_FD */ /** Whether to close old_fd for IO_FD */
int close_old; int close_old;
} param2 } param2;
;
/** /**
Set to true if this is an input io redirection Set to true if this is an input io redirection
@ -50,9 +49,10 @@ typedef struct io_data
int is_input; int is_input;
/** Pointer to the next IO redirection */ /** Pointer to the next IO redirection */
struct io_data *next; io_data_t *next;
}
io_data_t; io_data_t() : next(NULL) { }
};
/** /**
@ -66,9 +66,9 @@ io_data_t *io_add( io_data_t *first_chain, io_data_t *decond_chain );
io_data_t *io_remove( io_data_t *list, io_data_t *element ); io_data_t *io_remove( io_data_t *list, io_data_t *element );
/** /**
Make a copy of the specified chain of redirections. Uses halloc. Make a copy of the specified chain of redirections. Uses operator new.
*/ */
io_data_t *io_duplicate( void *context, io_data_t *l ); io_data_t *io_duplicate( io_data_t *l );
/** /**
Return the last io redirection in the chain for the specified file descriptor. Return the last io redirection in the chain for the specified file descriptor.

View file

@ -361,7 +361,6 @@ parser_t::parser_t(enum parser_type_t type) :
parser_type(type), parser_type(type),
error_code(0), error_code(0),
err_pos(0), err_pos(0),
err_buff(NULL),
current_tokenizer(NULL), current_tokenizer(NULL),
lineinfo(NULL), lineinfo(NULL),
current_tokenizer_pos(0), current_tokenizer_pos(0),
@ -602,17 +601,11 @@ void parser_t::error( int ec, int p, const wchar_t *str, ... )
CHECK( str, ); CHECK( str, );
if( !err_buff )
err_buff = sb_halloc( global_context );
sb_clear( err_buff );
error_code = ec; error_code = ec;
err_pos = p; err_pos = p;
va_start( va, str ); va_start( va, str );
err_buff = vformat_string(str, va);
sb_vprintf( err_buff, str, va );
va_end( va ); va_end( va );
} }
@ -744,11 +737,11 @@ void parser_t::print_errors( string_buffer_t *target, const wchar_t *prefix )
CHECK( target, ); CHECK( target, );
CHECK( prefix, ); CHECK( prefix, );
if( error_code && err_buff ) if( error_code && ! err_buff.empty() )
{ {
int tmp; int tmp;
sb_printf( target, L"%ls: %ls\n", prefix, (wchar_t *)err_buff->buff ); sb_printf( target, L"%ls: %ls\n", prefix, err_buff.c_str() );
tmp = current_tokenizer_pos; tmp = current_tokenizer_pos;
current_tokenizer_pos = err_pos; current_tokenizer_pos = err_pos;
@ -764,9 +757,9 @@ void parser_t::print_errors( string_buffer_t *target, const wchar_t *prefix )
*/ */
void parser_t::print_errors_stderr() void parser_t::print_errors_stderr()
{ {
if( error_code && err_buff ) if( error_code && ! err_buff.empty() )
{ {
debug( 0, L"%ls", (wchar_t *)err_buff->buff ); debug( 0, L"%ls", err_buff.c_str() );
int tmp; int tmp;
tmp = current_tokenizer_pos; tmp = current_tokenizer_pos;
@ -1415,7 +1408,7 @@ void parser_t::parse_job_argument_list( process_t *p,
case TOK_REDIRECT_NOCLOB: case TOK_REDIRECT_NOCLOB:
{ {
int type = tok_last_type( tok ); int type = tok_last_type( tok );
io_data_t *new_io; std::auto_ptr<io_data_t> new_io;
wcstring target; wcstring target;
bool has_target = false; bool has_target = false;
wchar_t *end; wchar_t *end;
@ -1440,9 +1433,7 @@ void parser_t::parse_job_argument_list( process_t *p,
break; break;
} }
new_io = (io_data_t *)halloc( j, sizeof(io_data_t) ); new_io.reset(new io_data_t);
if( !new_io )
DIE_MEM();
errno = 0; errno = 0;
new_io->fd = wcstol( tok_last( tok ), new_io->fd = wcstol( tok_last( tok ),
@ -1557,7 +1548,7 @@ void parser_t::parse_job_argument_list( process_t *p,
} }
} }
j->io = io_add( j->io, new_io ); j->io = io_add( j->io, new_io.release() );
} }
break; break;

View file

@ -289,7 +289,7 @@ class parser_t {
int err_pos; int err_pos;
/** Description of last error */ /** Description of last error */
string_buffer_t *err_buff; wcstring err_buff;
/** Pointer to the current tokenizer */ /** Pointer to the current tokenizer */
tokenizer *current_tokenizer; tokenizer *current_tokenizer;

View file

@ -443,46 +443,6 @@ bool path_can_get_cdpath(const wcstring &in) {
return result; return result;
} }
wchar_t *path_get_config( void *context)
{
int done = 0;
wcstring res;
const env_var_t xdg_dir = env_get_string( L"XDG_CONFIG_HOME" );
if( ! xdg_dir.missing() )
{
res = xdg_dir + L"/fish";
if( !create_directory( res.c_str() ) )
{
done = 1;
}
}
else
{
const env_var_t home = env_get_string( L"HOME" );
if( ! home.missing() )
{
res = home + L"/.config/fish";
if( !create_directory( res.c_str() ) )
{
done = 1;
}
}
}
if( done )
{
wchar_t *result = wcsdup(res.c_str());
halloc_register_function( context, &free, result );
return result;
}
else
{
debug( 0, _(L"Unable to create a configuration directory for fish. Your personal settings will not be saved. Please set the $XDG_CONFIG_HOME variable to a directory where the current user has write access." ));
return 0;
}
}
bool path_get_config(wcstring &path) bool path_get_config(wcstring &path)
{ {

5
path.h
View file

@ -18,10 +18,9 @@
Returns the user configuration directory for fish. If the directory Returns the user configuration directory for fish. If the directory
or one of it's parents doesn't exist, they are first created. or one of it's parents doesn't exist, they are first created.
\param context the halloc context to use for memory allocations \param path The directory as an out param
\return 0 if the no configuration directory can be located or created, the directory path otherwise. \return whether the directory was returned successfully
*/ */
wchar_t *path_get_config( void *context);
bool path_get_config(wcstring &path); bool path_get_config(wcstring &path);
/** /**

8
proc.h
View file

@ -297,6 +297,12 @@ class job_t
~job_t() { ~job_t() {
if (first_process != NULL) if (first_process != NULL)
delete first_process; delete first_process;
io_data_t *data = this->io;
while (data) {
io_data_t *tmp = data->next;
delete data;
data = tmp;
}
} }
@ -336,7 +342,7 @@ class job_t
const int job_id; const int job_id;
/** /**
List of all IO redirections for this job List of all IO redirections for this job. This linked list is allocated via new, and owned by the object, which should delete them.
*/ */
io_data_t *io; io_data_t *io;