win: keep a prev pointer

Save us some iteration when updating the window stack

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2019-04-03 20:48:46 +01:00
parent a68903b9ef
commit 166ec55778
No known key found for this signature in database
GPG Key ID: 37C999F617EA1A47
3 changed files with 44 additions and 45 deletions

View File

@ -666,31 +666,27 @@ static void restack_win(session_t *ps, win *w, xcb_window_t new_above) {
rc_region_unref(&w->next->reg_ignore); rc_region_unref(&w->next->reg_ignore);
} }
win **prev = NULL, **prev_old = NULL; win **prev = NULL, *tmp_w;
HASH_FIND_INT(ps->windows, &new_above, tmp_w);
bool found = false; if (new_above && !tmp_w) {
for (prev = &ps->window_stack; *prev; prev = &(*prev)->next) {
if ((*prev)->id == new_above && (*prev)->state != WSTATE_DESTROYING) {
found = true;
break;
}
}
if (new_above && !found) {
log_error("(%#010x, %#010x): Failed to found new above window.", log_error("(%#010x, %#010x): Failed to found new above window.",
w->id, new_above); w->id, new_above);
return; return;
} }
prev = tmp_w->prev;
for (prev_old = &ps->window_stack; *prev_old; prev_old = &(*prev_old)->next) { // Unlink from old position
if ((*prev_old) == w) { *w->prev = w->next;
break; if (w->next) {
} w->next->prev = w->prev;
} }
// Link to new position
*prev_old = w->next;
w->next = *prev; w->next = *prev;
*prev = w; if (w->next) {
w->next->prev = &w->next;
}
w->prev = prev;
*w->prev = w;
// add damage for this window // add damage for this window
add_damage_from_win(ps, w); add_damage_from_win(ps, w);
@ -2132,8 +2128,8 @@ static session_t *session_init(int argc, char **argv, Display *dpy,
free(reply); free(reply);
log_trace("Initial stack:"); log_trace("Initial stack:");
for (win *c = ps->window_stack; c; c = c->next) { WIN_STACK_ITER(ps, w) {
log_trace("%#010x \"%s\"", c->id, c->name); log_trace("%#010x \"%s\"", w->id, w->name);
} }
} }

View File

@ -57,7 +57,7 @@
* Clear leader cache of all windows. * Clear leader cache of all windows.
*/ */
static inline void clear_cache_win_leaders(session_t *ps) { static inline void clear_cache_win_leaders(session_t *ps) {
for (win *w = ps->window_stack; w; w = w->next) { WIN_STACK_ITER(ps, w) {
w->cache_leader = XCB_NONE; w->cache_leader = XCB_NONE;
} }
} }
@ -1027,14 +1027,18 @@ void add_win(session_t *ps, xcb_window_t id, xcb_window_t prev) {
// Find window insertion point // Find window insertion point
win **p = NULL; win **p = NULL;
if (prev) { if (prev) {
for (p = &ps->window_stack; *p; p = &(*p)->next) { win *w = NULL;
if ((*p)->id == prev && (*p)->state != WSTATE_DESTROYING) HASH_FIND_INT(ps->windows, &prev, w);
break; assert(w);
} p = w->prev;
} else { } else {
p = &ps->window_stack; p = &ps->window_stack;
} }
new->next = *p; new->next = *p;
if (new->next) {
new->next->prev = &new->next;
}
new->prev = p;
*p = new; *p = new;
HASH_ADD_INT(ps->windows, id, new); HASH_ADD_INT(ps->windows, id, new);
@ -1466,7 +1470,6 @@ static void finish_unmap_win(session_t *ps, win **_w) {
static void finish_destroy_win(session_t *ps, win **_w) { static void finish_destroy_win(session_t *ps, win **_w) {
win *w = *_w; win *w = *_w;
win **prev = NULL;
if (w->state != WSTATE_UNMAPPED) { if (w->state != WSTATE_UNMAPPED) {
// Only UNMAPPED state has window resources freed, otherwise // Only UNMAPPED state has window resources freed, otherwise
@ -1487,30 +1490,28 @@ static void finish_destroy_win(session_t *ps, win **_w) {
} }
log_trace("Trying to destroy (%#010x)", w->id); log_trace("Trying to destroy (%#010x)", w->id);
for (prev = &ps->window_stack; *prev; prev = &(*prev)->next) { *w->prev = w->next;
if (w == *prev) { if (w->next) {
log_trace("Found (%#010x \"%s\")", w->id, w->name); w->next->prev = w->prev;
*prev = w->next; }
if (w == ps->active_win) { if (w == ps->active_win) {
ps->active_win = NULL; ps->active_win = NULL;
} }
free_win_res(ps, w); free_win_res(ps, w);
// Drop w from all prev_trans to avoid accessing freed memory in // Drop w from all prev_trans to avoid accessing freed memory in
// repair_win() // repair_win()
// TODO there can only be one prev_trans pointing to w // TODO there can only be one prev_trans pointing to w
for (win *w2 = ps->window_stack; w2; w2 = w2->next) { WIN_STACK_ITER(ps, w2) {
if (w == w2->prev_trans) { if (w == w2->prev_trans) {
w2->prev_trans = NULL; w2->prev_trans = NULL;
}
}
free(w);
*_w = NULL;
return;
} }
} }
free(w);
*_w = NULL;
return;
log_warn("Destroyed window is not in window list"); log_warn("Destroyed window is not in window list");
assert(false); assert(false);
} }

View File

@ -137,6 +137,8 @@ struct win {
void *shadow_image; void *shadow_image;
/// Pointer to the next lower window in window stack. /// Pointer to the next lower window in window stack.
win *next; win *next;
/// Pointer to a linked-list pointer that points to this window.
win **prev;
/// Pointer to the next higher window to paint. /// Pointer to the next higher window to paint.
win *prev_trans; win *prev_trans;
// TODO rethink reg_ignore // TODO rethink reg_ignore