Defer deletion of buffers to after the event loop

We can have SelectionList in flights on the buffer, so mark
it for deletion by moving it in a buffer trash, and effectively
delete it later, at a point where there should not be any further
access to it.
This commit is contained in:
Maxime Coste 2014-08-12 19:24:09 +01:00
parent e2b3dd0ca4
commit 2204b7cf06
7 changed files with 34 additions and 5 deletions

View File

@ -41,6 +41,14 @@ void BufferManager::unregister_buffer(Buffer& buffer)
return; return;
} }
} }
for (auto it = m_buffer_trash.begin(); it != m_buffer_trash.end(); ++it)
{
if (*it == &buffer)
{
m_buffer_trash.erase(it);
return;
}
}
kak_assert(false); kak_assert(false);
} }
@ -52,7 +60,9 @@ void BufferManager::delete_buffer(Buffer& buffer)
{ {
if (ClientManager::has_instance()) if (ClientManager::has_instance())
ClientManager::instance().ensure_no_client_uses_buffer(buffer); ClientManager::instance().ensure_no_client_uses_buffer(buffer);
delete it->get();
m_buffers.erase(it);
m_buffer_trash.emplace_back(&buffer);
return; return;
} }
} }
@ -121,4 +131,10 @@ CandidateList BufferManager::complete_buffer_name(StringView prefix,
return result.empty() ? subsequence_result : result; return result.empty() ? subsequence_result : result;
} }
void BufferManager::clear_buffer_trash()
{
while (not m_buffer_trash.empty())
delete m_buffer_trash.back().get();
}
} }

View File

@ -37,8 +37,10 @@ public:
CandidateList complete_buffer_name(StringView prefix, CandidateList complete_buffer_name(StringView prefix,
ByteCount cursor_pos = -1); ByteCount cursor_pos = -1);
void clear_buffer_trash();
private: private:
BufferList m_buffers; BufferList m_buffers;
BufferList m_buffer_trash;
}; };
} }

View File

@ -39,6 +39,9 @@ public:
Context& context() { return m_input_handler.context(); } Context& context() { return m_input_handler.context(); }
const Context& context() const { return m_input_handler.context(); } const Context& context() const { return m_input_handler.context(); }
InputHandler& input_handler() { return m_input_handler; }
const InputHandler& input_handler() const { return m_input_handler; }
void change_buffer(Buffer& buffer); void change_buffer(Buffer& buffer);
const String& get_env_var(const String& name) const; const String& get_env_var(const String& name) const;

View File

@ -170,6 +170,12 @@ void ClientManager::redraw_clients() const
client->redraw_ifn(); client->redraw_ifn();
} }
void ClientManager::clear_mode_trashes() const
{
for (auto& client : m_clients)
client->input_handler().clear_mode_trash();
}
CandidateList ClientManager::complete_client_name(StringView prefix, CandidateList ClientManager::complete_client_name(StringView prefix,
ByteCount cursor_pos) const ByteCount cursor_pos) const
{ {

View File

@ -34,6 +34,7 @@ public:
void add_free_window(std::unique_ptr<Window>&& window, SelectionList selections); void add_free_window(std::unique_ptr<Window>&& window, SelectionList selections);
void redraw_clients() const; void redraw_clients() const;
void clear_mode_trashes() const;
Client* get_client_ifp(const String& name); Client* get_client_ifp(const String& name);
Client& get_client(const String& name); Client& get_client(const String& name);

View File

@ -1,5 +1,4 @@
#include "event_manager.hh" #include "event_manager.hh"
#include "client_manager.hh"
#include <poll.h> #include <poll.h>
@ -82,9 +81,6 @@ void EventManager::handle_next_events()
if (timer->next_date() <= now) if (timer->next_date() <= now)
timer->run(); timer->run();
} }
if (ClientManager::has_instance())
ClientManager::instance().redraw_clients();
} }
void EventManager::force_signal(int fd) void EventManager::force_signal(int fd)

View File

@ -421,7 +421,12 @@ int kakoune(const ParametersParser& parser)
create_local_client(init_command); create_local_client(init_command);
while (not terminate and (not client_manager.empty() or daemon)) while (not terminate and (not client_manager.empty() or daemon))
{
event_manager.handle_next_events(); event_manager.handle_next_events();
client_manager.clear_mode_trashes();
buffer_manager.clear_buffer_trash();
client_manager.redraw_clients();
}
{ {
Context empty_context; Context empty_context;