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;
/**
String buffer used by the wsetlocale function
*/
static string_buffer_t *setlocale_buff=0;
void show_stackframe()
{
@ -485,20 +480,26 @@ wchar_t **strv2wcsv( const char **in )
return res;
}
wcstring format_string(const wchar_t *format, ...)
{
va_list va;
va_start( va, format );
string_buffer_t buffer;
sb_init(&buffer);
sb_vprintf(&buffer, format, va);
wcstring result = (wchar_t *)buffer.buff;
sb_destroy(&buffer);
wcstring result = vformat_string(format, va);
va_end( va );
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 )
{
while( *str )

View File

@ -361,6 +361,7 @@ public:
void append_path_component(wcstring &path, const wcstring &component);
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

View File

@ -950,11 +950,11 @@ void exec( parser_t &parser, job_t *j )
{
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
{
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 "io.h"
#include "halloc.h"
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;
}
io_data_t *io_duplicate( void *context, io_data_t *l )
io_data_t *io_duplicate( io_data_t *l )
{
io_data_t *res;
if( l == 0 )
return 0;
res = (io_data_t *)halloc( context, sizeof( io_data_t) );
if( !res )
{
DIE_MEM();
}
memcpy( res, l, sizeof(io_data_t ));
res->next=io_duplicate( context, l->next );
res = new io_data_t(*l);
res->next=io_duplicate(l->next );
return res;
}

16
io.h
View File

@ -11,7 +11,7 @@ enum io_mode
;
/** Represents an FD redirection */
typedef struct io_data
struct io_data_t
{
/** Type of redirect */
int io_mode;
@ -41,8 +41,7 @@ typedef struct io_data
buffer_t *out_buffer;
/** Whether to close old_fd for IO_FD */
int close_old;
} param2
;
} param2;
/**
Set to true if this is an input io redirection
@ -50,9 +49,10 @@ typedef struct io_data
int is_input;
/** Pointer to the next IO redirection */
struct io_data *next;
}
io_data_t;
io_data_t *next;
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 );
/**
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.

View File

@ -361,7 +361,6 @@ parser_t::parser_t(enum parser_type_t type) :
parser_type(type),
error_code(0),
err_pos(0),
err_buff(NULL),
current_tokenizer(NULL),
lineinfo(NULL),
current_tokenizer_pos(0),
@ -602,17 +601,11 @@ void parser_t::error( int ec, int p, const wchar_t *str, ... )
CHECK( str, );
if( !err_buff )
err_buff = sb_halloc( global_context );
sb_clear( err_buff );
error_code = ec;
err_pos = p;
va_start( va, str );
sb_vprintf( err_buff, str, va );
err_buff = vformat_string(str, va);
va_end( va );
}
@ -744,11 +737,11 @@ void parser_t::print_errors( string_buffer_t *target, const wchar_t *prefix )
CHECK( target, );
CHECK( prefix, );
if( error_code && err_buff )
if( error_code && ! err_buff.empty() )
{
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;
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()
{
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;
tmp = current_tokenizer_pos;
@ -1415,7 +1408,7 @@ void parser_t::parse_job_argument_list( process_t *p,
case TOK_REDIRECT_NOCLOB:
{
int type = tok_last_type( tok );
io_data_t *new_io;
std::auto_ptr<io_data_t> new_io;
wcstring target;
bool has_target = false;
wchar_t *end;
@ -1440,9 +1433,7 @@ void parser_t::parse_job_argument_list( process_t *p,
break;
}
new_io = (io_data_t *)halloc( j, sizeof(io_data_t) );
if( !new_io )
DIE_MEM();
new_io.reset(new io_data_t);
errno = 0;
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;

View File

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

View File

@ -443,46 +443,6 @@ bool path_can_get_cdpath(const wcstring &in) {
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)
{

5
path.h
View File

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

8
proc.h
View File

@ -297,6 +297,12 @@ class job_t
~job_t() {
if (first_process != NULL)
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;
/**
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;