Make event_t.arguments into a vector instead of an auto_ptr<vector>.

Yay for less indirection and less code! The resulting event_t structure is two pointers larger, but cuts out an indirection and allocation.
This commit is contained in:
Jan Kanis 2012-12-20 10:52:44 +01:00
parent af3059ab2a
commit 71233ee894
4 changed files with 30 additions and 66 deletions

24
env.cpp
View file

@ -415,12 +415,10 @@ static void universal_callback(fish_message_type_t type,
mark_changed_exported(); mark_changed_exported();
event_t ev = event_t::variable_event(name); event_t ev = event_t::variable_event(name);
ev.arguments.reset(new wcstring_list_t()); ev.arguments.push_back(L"VARIABLE");
ev.arguments->push_back(L"VARIABLE"); ev.arguments.push_back(str);
ev.arguments->push_back(str); ev.arguments.push_back(name);
ev.arguments->push_back(name);
event_fire(&ev); event_fire(&ev);
ev.arguments.reset(NULL);
} }
if (name) if (name)
@ -979,15 +977,13 @@ int env_set(const wcstring &key, const wchar_t *val, int var_mode)
if (!is_universal) if (!is_universal)
{ {
event_t ev = event_t::variable_event(key); event_t ev = event_t::variable_event(key);
ev.arguments.reset(new wcstring_list_t); ev.arguments.push_back(L"VARIABLE");
ev.arguments->push_back(L"VARIABLE"); ev.arguments.push_back(L"SET");
ev.arguments->push_back(L"SET"); ev.arguments.push_back(key);
ev.arguments->push_back(key);
// debug( 1, L"env_set: fire events on variable %ls", key ); // debug( 1, L"env_set: fire events on variable %ls", key );
event_fire(&ev); event_fire(&ev);
// debug( 1, L"env_set: return from event firing" ); // debug( 1, L"env_set: return from event firing" );
ev.arguments.reset(NULL);
} }
react_to_variable_change(key); react_to_variable_change(key);
@ -1066,14 +1062,12 @@ int env_remove(const wcstring &key, int var_mode)
if (try_remove(first_node, key.c_str(), var_mode)) if (try_remove(first_node, key.c_str(), var_mode))
{ {
event_t ev = event_t::variable_event(key); event_t ev = event_t::variable_event(key);
ev.arguments.reset(new wcstring_list_t); ev.arguments.push_back(L"VARIABLE");
ev.arguments->push_back(L"VARIABLE"); ev.arguments.push_back(L"ERASE");
ev.arguments->push_back(L"ERASE"); ev.arguments.push_back(key);
ev.arguments->push_back(key);
event_fire(&ev); event_fire(&ev);
ev.arguments.reset(NULL);
erased = 1; erased = 1;
} }
} }

View file

@ -137,22 +137,6 @@ static int event_match(const event_t *classv, const event_t *instance)
} }
/**
Create an identical copy of an event. Use deep copying, i.e. make
duplicates of any strings used as well.
*/
static event_t *event_copy(const event_t *event, int copy_arguments)
{
event_t *e = new event_t(*event);
e->arguments.reset(new wcstring_list_t);
if (copy_arguments && event->arguments.get() != NULL)
{
*(e->arguments) = *(event->arguments);
}
return e;
}
/** /**
Test if specified event is blocked Test if specified event is blocked
@ -307,10 +291,10 @@ void event_add_handler(const event_t *event)
event_t *e; event_t *e;
CHECK(event,); CHECK(event,);
if(debug_level >= 3) if(debug_level >= 3)
debug(3, "register: %ls\n", event_type_str(event).c_str()); debug(3, "register: %ls\n", event_type_str(event).c_str());
e = event_copy(event, 0); e = new event_t(*event);
if (e->type == EVENT_SIGNAL) if (e->type == EVENT_SIGNAL)
{ {
@ -506,11 +490,11 @@ static void event_fire_internal(const event_t *event)
*/ */
wcstring buffer = criterion->function_name; wcstring buffer = criterion->function_name;
if (event->arguments.get()) if (! event->arguments.empty())
{ {
for (j=0; j< event->arguments->size(); j++) for (j=0; j < event->arguments.size(); j++)
{ {
wcstring arg_esc = escape_string(event->arguments->at(j), 1); wcstring arg_esc = escape_string(event->arguments.at(j), 1);
buffer += L" "; buffer += L" ";
buffer += arg_esc; buffer += arg_esc;
} }
@ -566,7 +550,7 @@ static void event_fire_delayed()
event_t *e = blocked.at(i); event_t *e = blocked.at(i);
if (event_is_blocked(e)) if (event_is_blocked(e))
{ {
new_blocked.push_back(e); new_blocked.push_back(new event_t(*e));
} }
else else
{ {
@ -592,7 +576,6 @@ static void event_fire_delayed()
Set up Set up
*/ */
event_t e = event_t::signal_event(0); event_t e = event_t::signal_event(0);
e.arguments.reset(new wcstring_list_t(1)); //one element
lst = &sig_list[1-active_list]; lst = &sig_list[1-active_list];
if (lst->overflow) if (lst->overflow)
@ -606,10 +589,10 @@ static void event_fire_delayed()
for (int i=0; i < lst->count; i++) for (int i=0; i < lst->count; i++)
{ {
e.param1.signal = lst->signal[i]; e.param1.signal = lst->signal[i];
e.arguments->at(0) = sig2wcs(e.param1.signal); e.arguments.at(0) = sig2wcs(e.param1.signal);
if (event_is_blocked(&e)) if (event_is_blocked(&e))
{ {
blocked.push_back(event_copy(&e, 1)); blocked.push_back(new event_t(e));
} }
else else
{ {
@ -617,8 +600,6 @@ static void event_fire_delayed()
} }
} }
e.arguments.reset(NULL);
} }
} }
@ -657,7 +638,7 @@ void event_fire(event_t *event)
{ {
if (event_is_blocked(event)) if (event_is_blocked(event))
{ {
blocked.push_back(event_copy(event, 1)); blocked.push_back(new event_t(*event));
} }
else else
{ {
@ -696,7 +677,7 @@ void event_fire_generic(const wchar_t *name, wcstring_list_t *args)
event_t ev(EVENT_GENERIC); event_t ev(EVENT_GENERIC);
ev.str_param1 = name; ev.str_param1 = name;
if (args) if (args)
ev.arguments.reset(new wcstring_list_t(*args)); ev.arguments = *args;
event_fire(&ev); event_fire(&ev);
} }
@ -720,14 +701,4 @@ event_t event_t::generic_event(const wcstring &str)
event.str_param1 = str; event.str_param1 = str;
return event; return event;
} }
event_t::event_t(const event_t &x) :
type(x.type),
param1(x.param1),
str_param1(x.str_param1),
function_name(x.function_name)
{
const wcstring_list_t *ptr = x.arguments.get();
if (ptr)
arguments.reset(new wcstring_list_t(*ptr));
}

View file

@ -84,12 +84,13 @@ struct event_t
event_fire. In all other situations, the value of this variable event_fire. In all other situations, the value of this variable
is ignored. is ignored.
*/ */
std::auto_ptr<wcstring_list_t> arguments; wcstring_list_t arguments;
event_t(int t) : type(t), param1(), str_param1(), function_name(), arguments() { } event_t(int t) : type(t), param1(), str_param1(), function_name(), arguments() { }
/** Copy constructor */ /** default copy constructor */
event_t(const event_t &x); //event_t(const event_t &x);
static event_t signal_event(int sig); static event_t signal_event(int sig);
static event_t variable_event(const wcstring &str); static event_t variable_event(const wcstring &str);

View file

@ -162,7 +162,6 @@ static std::vector<int> interactive_stack;
void proc_init() void proc_init()
{ {
proc_push_interactive(0); proc_push_interactive(0);
event.arguments.reset(new wcstring_list_t);
} }
@ -194,7 +193,6 @@ void job_free(job_t * j)
void proc_destroy() void proc_destroy()
{ {
event.arguments.reset(NULL);
job_list_t &jobs = parser_t::principal_parser().job_list(); job_list_t &jobs = parser_t::principal_parser().job_list();
while (! jobs.empty()) while (! jobs.empty())
{ {
@ -603,11 +601,11 @@ void proc_fire_event(const wchar_t *msg, int type, pid_t pid, int status)
event.type=type; event.type=type;
event.param1.pid = pid; event.param1.pid = pid;
event.arguments->push_back(msg); event.arguments.push_back(msg);
event.arguments->push_back(to_string<int>(pid)); event.arguments.push_back(to_string<int>(pid));
event.arguments->push_back(to_string<int>(status)); event.arguments.push_back(to_string<int>(status));
event_fire(&event); event_fire(&event);
event.arguments->resize(0); event.arguments.resize(0);
} }
int job_reap(bool interactive) int job_reap(bool interactive)