mirror of
https://github.com/neovim/neovim.git
synced 2025-09-23 03:28:33 +00:00
refactor(map): remove extra-allocating map_new/map_free functions
Note: the reason for removing them is not that there after this refactor is no use of them, but rather that having them available is an anti-pattern: they manange an _extra_ heap allocation which has nothing to do with the functionality of the map itself (khash manages the real buffers internally). In case there happens to be a reason to allocate the map structure itself later, this should be made explicit using xcalloc/xfree calls.
This commit is contained in:
@@ -152,11 +152,10 @@ static VTermScreenCallbacks vterm_screen_callbacks = {
|
||||
.sb_popline = term_sb_pop,
|
||||
};
|
||||
|
||||
static PMap(ptr_t) *invalidated_terminals;
|
||||
static PMap(ptr_t) invalidated_terminals = MAP_INIT;
|
||||
|
||||
void terminal_init(void)
|
||||
{
|
||||
invalidated_terminals = pmap_new(ptr_t)();
|
||||
time_watcher_init(&main_loop, &refresh_timer, NULL);
|
||||
// refresh_timer_cb will redraw the screen which can call vimscript
|
||||
refresh_timer.events = multiqueue_new_child(main_loop.events);
|
||||
@@ -167,8 +166,10 @@ void terminal_teardown(void)
|
||||
time_watcher_stop(&refresh_timer);
|
||||
multiqueue_free(refresh_timer.events);
|
||||
time_watcher_close(&refresh_timer, NULL);
|
||||
pmap_free(ptr_t)(invalidated_terminals);
|
||||
invalidated_terminals = NULL;
|
||||
pmap_destroy(ptr_t)(&invalidated_terminals);
|
||||
// terminal_destroy might be called after terminal_teardown is invoked
|
||||
// make sure it is in an empty, valid state
|
||||
pmap_init(ptr_t, &invalidated_terminals);
|
||||
}
|
||||
|
||||
// public API {{{
|
||||
@@ -525,14 +526,12 @@ void terminal_destroy(Terminal *term)
|
||||
}
|
||||
|
||||
if (!term->refcount) {
|
||||
// might be destroyed after terminal_teardown is invoked
|
||||
if (invalidated_terminals
|
||||
&& pmap_has(ptr_t)(invalidated_terminals, term)) {
|
||||
if (pmap_has(ptr_t)(&invalidated_terminals, term)) {
|
||||
// flush any pending changes to the buffer
|
||||
block_autocmds();
|
||||
refresh_terminal(term);
|
||||
unblock_autocmds();
|
||||
pmap_del(ptr_t)(invalidated_terminals, term);
|
||||
pmap_del(ptr_t)(&invalidated_terminals, term);
|
||||
}
|
||||
for (size_t i = 0; i < term->sb_current; i++) {
|
||||
xfree(term->sb_buffer[i]);
|
||||
@@ -869,7 +868,7 @@ static int term_sb_push(int cols, const VTermScreenCell *cells, void *data)
|
||||
}
|
||||
|
||||
memcpy(sbrow->cells, cells, sizeof(cells[0]) * c);
|
||||
pmap_put(ptr_t)(invalidated_terminals, term, NULL);
|
||||
pmap_put(ptr_t)(&invalidated_terminals, term, NULL);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -910,7 +909,7 @@ static int term_sb_pop(int cols, VTermScreenCell *cells, void *data)
|
||||
}
|
||||
|
||||
xfree(sbrow);
|
||||
pmap_put(ptr_t)(invalidated_terminals, term, NULL);
|
||||
pmap_put(ptr_t)(&invalidated_terminals, term, NULL);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -1212,7 +1211,7 @@ static void invalidate_terminal(Terminal *term, int start_row, int end_row)
|
||||
term->invalid_end = MAX(term->invalid_end, end_row);
|
||||
}
|
||||
|
||||
pmap_put(ptr_t)(invalidated_terminals, term, NULL);
|
||||
pmap_put(ptr_t)(&invalidated_terminals, term, NULL);
|
||||
if (!refresh_pending) {
|
||||
time_watcher_start(&refresh_timer, refresh_timer_cb, REFRESH_DELAY, 0);
|
||||
refresh_pending = true;
|
||||
@@ -1254,10 +1253,10 @@ static void refresh_timer_cb(TimeWatcher *watcher, void *data)
|
||||
void *stub; (void)(stub);
|
||||
// don't process autocommands while updating terminal buffers
|
||||
block_autocmds();
|
||||
map_foreach(invalidated_terminals, term, stub, {
|
||||
map_foreach(&invalidated_terminals, term, stub, {
|
||||
refresh_terminal(term);
|
||||
});
|
||||
pmap_clear(ptr_t)(invalidated_terminals);
|
||||
pmap_clear(ptr_t)(&invalidated_terminals);
|
||||
unblock_autocmds();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user