/* * Very simple allocator for GART memory, working on a static range * already mapped into each client's address space. */ struct drm_mem * drm_split_block(struct drm_heap *heap, struct drm_mem *p, int start, int size, struct drm_file *file_priv) { /* Maybe cut off the start of an existing block */ if (start > p->start) { struct drm_mem *newblock = drm_alloc(sizeof(*newblock)); if (newblock == NULL) goto out; newblock->start = start; newblock->size = p->size - (start - p->start); newblock->file_priv = NULL; TAILQ_INSERT_AFTER(heap, p, newblock, link); p->size -= newblock->size; p = newblock; } /* Maybe cut off the end of an existing block */ if (size < p->size) { struct drm_mem *newblock = drm_alloc(sizeof(*newblock)); if (newblock == NULL) goto out; newblock->start = start + size; newblock->size = p->size - size; newblock->file_priv = NULL; TAILQ_INSERT_AFTER(heap, p, newblock, link); p->size = size; } out: /* Our block is in the middle */ p->file_priv = file_priv; return p; }
int skiplist_insert(skiplist_t *skl, void *key, size_t klen, void *value) { skl_item_wrapper_t **path = calloc(skl->num_layers, sizeof(skl_item_wrapper_t *)); skl_item_t *prev_item = skiplist_search_internal(skl, key, klen, path); if (prev_item && skl->cmp_keys_cb(prev_item->key, prev_item->klen, key, klen) == 0) { // we have found an item with the same key, let's just update the value if (skl->free_value_cb) skl->free_value_cb(prev_item->value); prev_item->value = value; free(path); return 1; } // create a new item skl_item_t *new_item = calloc(1, sizeof(skl_item_t)); new_item->key = calloc(1, klen); memcpy(new_item->key, key, klen); new_item->klen = klen; new_item->value = value; new_item->layer_check = calloc(skl->num_layers, sizeof(char)); new_item->wrappers = calloc(skl->num_layers, sizeof(skl_item_wrapper_t)); // initialize the list wrappers new_item->wrappers[0].data = new_item; // always insert the new item to the tail list if (prev_item) TAILQ_INSERT_AFTER(&skl->layers[0], &prev_item->wrappers[0], &new_item->wrappers[0], next); else TAILQ_INSERT_TAIL(&skl->layers[0], &new_item->wrappers[0], next); new_item->layer_check[0] = 1; int i = 0; int coin = -1; while (++i < skl->num_layers) { new_item->wrappers[i].data = new_item; if (coin != 0) coin = (random()%100 > skl->probability); if (coin) { // then insert it to the upper layers as well if we got the chance if (path[i]) TAILQ_INSERT_AFTER(&skl->layers[i], path[i], &new_item->wrappers[i], next); else TAILQ_INSERT_TAIL(&skl->layers[i], &new_item->wrappers[i], next); new_item->layer_check[i] = 1; } } free(path); skl->count++; return 0; }
void xpc_array_set_value(xpc_object_t xarray, size_t index, xpc_object_t value) { struct xpc_object *xo, *xotmp, *xotmp2; struct xpc_array_head *arr; size_t i; xo = xarray; arr = &xo->xo_array; i = 0; if (index == XPC_ARRAY_APPEND) return xpc_array_append_value(xarray, value); if (index >= (size_t)xo->xo_size) return; TAILQ_FOREACH_SAFE(xotmp, arr, xo_link, xotmp2) { if (i++ == index) { TAILQ_INSERT_AFTER(arr, (struct xpc_object *)value, xotmp, xo_link); TAILQ_REMOVE(arr, xotmp, xo_link); xpc_retain(value); free(xotmp); break; } } }
int32_t provider_register_sd(int32_t fd) { extern profile_t sd_profile_descriptor; extern profile_t bgd_profile_descriptor; provider_p sd = calloc(1, sizeof(*sd)); provider_p bgd = calloc(1, sizeof(*bgd)); if (sd == NULL || bgd == NULL) { if (sd != NULL) free(sd); if (bgd != NULL) free(bgd); return (-1); } sd->profile = &sd_profile_descriptor; bgd->handle = 0; sd->fd = fd; TAILQ_INSERT_HEAD(&providers, sd, provider_next); bgd->profile = &bgd_profile_descriptor; bgd->handle = 1; sd->fd = fd; TAILQ_INSERT_AFTER(&providers, sd, bgd, provider_next); change_state ++; return (0); }
short filter_evaluate(struct intercept_tlq *tls, struct filterq *fls, struct intercept_pid *icpid) { struct filter *filter, *last = NULL; short action; TAILQ_FOREACH(filter, fls, next) { action = filter->match_action; if (filter_predicate(icpid, &filter->match_predicate) && filter_match(icpid, tls, filter->logicroot)) { /* Profile feedback optimization */ filter->match_count++; if (last != NULL && last->match_action == action && last->match_flags == filter->match_flags && filter->match_count > last->match_count) { TAILQ_REMOVE(fls, last, next); TAILQ_INSERT_AFTER(fls, filter, last, next); } if (action == ICPOLICY_NEVER) action = filter->match_error; icpid->uflags = filter->match_flags; /* Policy requests privilege elevation */ if (filter->elevate.e_flags) icpid->elevate = &filter->elevate; return (action); } /* Keep track of last processed filtered in a group */ last = filter; }
struct ct_op * ct_add_operation_after(struct ct_global_state *state, struct ct_op *after, ct_op_cb *start, ct_op_complete_cb *complete, void *args) { struct ct_op *op; op = e_calloc(1, sizeof(*op)); op->op_start = start; op->op_complete = complete; op->op_args = args; TAILQ_INSERT_AFTER(&state->ct_operations, after, op, op_link); return (op); }
/* * append_subdirs -- * Iterate the searchlist and append section and machine * subdirectories as needed. */ static void append_subdirs(TAG *t, const char *machine) { TAG *tsub; ENTRY *eold, *elast, *enew, *esub; char *slashp; eold = elast = TAILQ_FIRST(&t->list); while (eold) { /* * Section subdirectories *not* ending in a slash * only get the machine suffix: They already had * the {cat,man}N part in the configuration file * or got it in parse_path(). */ if (section && eold->s[strlen(eold->s)-1] != '/') { (void)snprintf(gbuf, sizeof(gbuf), "%s{/%s,}", eold->s, machine); free(eold->s); if ((eold->s = strdup(gbuf)) == NULL) err(1, NULL); eold = elast = TAILQ_NEXT(eold, q); continue; } /* * Without a section, expand each entry using the * subdir list, then drop the original entry. */ esub = (tsub = getlist("_subdir")) == NULL ? NULL : TAILQ_FIRST(&tsub->list); while (esub) { slashp = eold->s[strlen(eold->s)-1] == '/' ? "" : "/"; (void)snprintf(gbuf, sizeof(gbuf), "%s%s%s{/%s,}", eold->s, slashp, esub->s, machine); if ((enew = malloc(sizeof(ENTRY))) == NULL || (enew->s = strdup(gbuf)) == NULL) err(1, NULL); TAILQ_INSERT_AFTER(&t->list, elast, enew, q); elast = enew; esub = TAILQ_NEXT(esub, q); } elast = TAILQ_NEXT(elast, q); TAILQ_REMOVE(&t->list, eold, q); eold = elast; } }
/* * parse_path -- * Split the -M or -m argument or the MANPATH variable at colons, * and insert the parts into the searchlist. */ static void parse_path(TAG *t, const char *path) { ENTRY *eplast = NULL, *ep; char *p, *slashp, *path_copy; if ((path_copy = strdup(path)) == NULL) err(1, NULL); while ((p = strsep(&path_copy, ":")) != NULL) { /* Skip empty fields */ if (*p == '\0') continue; if ((ep = malloc(sizeof(ENTRY))) == NULL) err(1, NULL); /* * Bring section specific paths to the same format as * used in the configuration file, ending in /{cat,man}N. */ if (section) { slashp = p[strlen(p) - 1] == '/' ? "" : "/"; (void)snprintf(gbuf, sizeof(gbuf), "%s%s{cat,man}%s", p, slashp, t->s); if ((ep->s = strdup(gbuf)) == NULL) err(1, NULL); } /* Without a section, subdirs will be appended later. */ else if ((ep->s = strdup(p)) == NULL) err(1, NULL); /* * Even in case of -M, inserting in front is fine: * We have just cleared the list. */ if (eplast) TAILQ_INSERT_AFTER(&t->list, eplast, ep, q); else TAILQ_INSERT_HEAD(&t->list, ep, q); eplast = ep; } free(path_copy); }
/* * MPSAFE */ void systimer_add(systimer_t info) { struct globaldata *gd = mycpu; KKASSERT((info->flags & SYSTF_ONQUEUE) == 0); crit_enter(); if (info->gd == gd) { systimer_t scan1; systimer_t scan2; scan1 = TAILQ_FIRST(&gd->gd_systimerq); if (scan1 == NULL || (int)(scan1->time - info->time) > 0) { cputimer_intr_reload(info->time - sys_cputimer->count()); TAILQ_INSERT_HEAD(&gd->gd_systimerq, info, node); } else { scan2 = TAILQ_LAST(&gd->gd_systimerq, systimerq); for (;;) { if (scan1 == NULL) { TAILQ_INSERT_TAIL(&gd->gd_systimerq, info, node); break; } if ((int)(scan1->time - info->time) > 0) { TAILQ_INSERT_BEFORE(scan1, info, node); break; } if ((int)(scan2->time - info->time) <= 0) { TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2, info, node); break; } scan1 = TAILQ_NEXT(scan1, node); scan2 = TAILQ_PREV(scan2, systimerq, node); } } info->flags = (info->flags | SYSTF_ONQUEUE) & ~SYSTF_IPIRUNNING; info->queue = &gd->gd_systimerq; } else { #ifdef SMP KKASSERT((info->flags & SYSTF_IPIRUNNING) == 0); info->flags |= SYSTF_IPIRUNNING; lwkt_send_ipiq(info->gd, (ipifunc1_t)systimer_add, info); #else panic("systimer_add: bad gd in info %p", info); #endif } crit_exit(); }
/* * This function detaches 'con' from its parent and inserts it either before or * after 'target'. * */ static void insert_con_into(Con *con, Con *target, position_t position) { Con *parent = target->parent; /* We need to preserve the old con->parent. While it might still be used to * insert the entry before/after it, we call the on_remove_child callback * afterwards which might then close the con if it is empty. */ Con *old_parent = con->parent; con_detach(con); con_fix_percent(con->parent); /* When moving to a workspace, we respect the user’s configured * workspace_layout */ if (parent->type == CT_WORKSPACE) { Con *split = workspace_attach_to(parent); if (split != parent) { DLOG("Got a new split con, using that one instead\n"); con->parent = split; con_attach(con, split, false); DLOG("attached\n"); con->percent = 0.0; con_fix_percent(split); con = split; DLOG("ok, continuing with con %p instead\n", con); con_detach(con); } } con->parent = parent; if (position == BEFORE) { TAILQ_INSERT_BEFORE(target, con, nodes); TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused); } else if (position == AFTER) { TAILQ_INSERT_AFTER(&(parent->nodes_head), target, con, nodes); TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused); } /* Pretend the con was just opened with regards to size percent values. * Since the con is moved to a completely different con, the old value * does not make sense anyways. */ con->percent = 0.0; con_fix_percent(parent); CALL(old_parent, on_remove_child); }
/* * Insert a bridge address in the bridge addresses list. */ static void bridge_addrs_bif_insert(struct tp_entries *headp, struct tp_entry *te, struct tp_entry **f_tpa) { struct tp_entry *temp; if (*f_tpa != NULL) bridge_addrs_insert_at(headp, te, f_tpa); else { temp = bridge_addrs_find_pos(headp, te->sysindex); if (temp == NULL) TAILQ_INSERT_HEAD(headp, te, tp_e); else TAILQ_INSERT_AFTER(headp, temp, te, tp_e); *f_tpa = te; } }
/* * Seek sort for disks. * * Sort all requests in a single queue while keeping * track of the current position of the disk with last_offset. * See above for details. */ void bioq_disksort(struct bio_queue_head *head, struct bio *bp) { struct bio *cur, *prev = NULL; uoff_t key = bioq_bio_key(head, bp); cur = TAILQ_FIRST(&head->queue); if (head->insert_point) cur = head->insert_point; while (cur != NULL && key >= bioq_bio_key(head, cur)) { prev = cur; cur = TAILQ_NEXT(cur, bio_queue); } if (prev == NULL) TAILQ_INSERT_HEAD(&head->queue, bp, bio_queue); else TAILQ_INSERT_AFTER(&head->queue, prev, bp, bio_queue); }
/* * Insert new SACK hole into scoreboard. */ static struct sackhole * tcp_sackhole_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end, struct sackhole *after) { struct sackhole *hole; /* Allocate a new SACK hole. */ hole = tcp_sackhole_alloc(tp, start, end); if (hole == NULL) return NULL; hole->rxmit_start = tcp_now; /* Insert the new SACK hole into scoreboard */ if (after != NULL) TAILQ_INSERT_AFTER(&tp->snd_holes, after, hole, scblink); else TAILQ_INSERT_TAIL(&tp->snd_holes, hole, scblink); /* Update SACK hint. */ if (tp->sackhint.nexthole == NULL) tp->sackhint.nexthole = hole; return(hole); }
/* * Seek sort for disks. * * Sort all requests in a single queue while keeping * track of the current position of the disk with last_offset. * See above for details. */ void bioq_disksort(struct bio_queue_head *head, struct bio *bp) { struct bio *cur, *prev; uoff_t key; if ((bp->bio_flags & BIO_ORDERED) != 0) { /* * Ordered transactions can only be dispatched * after any currently queued transactions. They * also have barrier semantics - no transactions * queued in the future can pass them. */ bioq_insert_tail(head, bp); return; } prev = NULL; key = bioq_bio_key(head, bp); cur = TAILQ_FIRST(&head->queue); if (head->insert_point) { prev = head->insert_point; cur = TAILQ_NEXT(head->insert_point, bio_queue); } while (cur != NULL && key >= bioq_bio_key(head, cur)) { prev = cur; cur = TAILQ_NEXT(cur, bio_queue); } if (prev == NULL) TAILQ_INSERT_HEAD(&head->queue, bp, bio_queue); else TAILQ_INSERT_AFTER(&head->queue, prev, bp, bio_queue); }
static enum cmd_retval cmd_join_pane_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = self->args; struct cmd_find_state *current = &item->shared->current; struct session *dst_s; struct winlink *src_wl, *dst_wl; struct window *src_w, *dst_w; struct window_pane *src_wp, *dst_wp; char *cause; int size, percentage, dst_idx; enum layout_type type; struct layout_cell *lc; int not_same_window, flags; if (self->entry == &cmd_join_pane_entry) not_same_window = 1; else not_same_window = 0; dst_s = item->target.s; dst_wl = item->target.wl; dst_wp = item->target.wp; dst_w = dst_wl->window; dst_idx = dst_wl->idx; server_unzoom_window(dst_w); src_wl = item->source.wl; src_wp = item->source.wp; src_w = src_wl->window; server_unzoom_window(src_w); if (not_same_window && src_w == dst_w) { cmdq_error(item, "can't join a pane to its own window"); return (CMD_RETURN_ERROR); } if (!not_same_window && src_wp == dst_wp) { cmdq_error(item, "source and target panes must be different"); return (CMD_RETURN_ERROR); } type = LAYOUT_TOPBOTTOM; if (args_has(args, 'h')) type = LAYOUT_LEFTRIGHT; size = -1; if (args_has(args, 'l')) { size = args_strtonum(args, 'l', 0, INT_MAX, &cause); if (cause != NULL) { cmdq_error(item, "size %s", cause); free(cause); return (CMD_RETURN_ERROR); } } else if (args_has(args, 'p')) { percentage = args_strtonum(args, 'p', 0, 100, &cause); if (cause != NULL) { cmdq_error(item, "percentage %s", cause); free(cause); return (CMD_RETURN_ERROR); } if (type == LAYOUT_TOPBOTTOM) size = (dst_wp->sy * percentage) / 100; else size = (dst_wp->sx * percentage) / 100; } if (args_has(args, 'b')) flags = SPAWN_BEFORE; else flags = 0; lc = layout_split_pane(dst_wp, type, size, flags); if (lc == NULL) { cmdq_error(item, "create pane failed: pane too small"); return (CMD_RETURN_ERROR); } layout_close_pane(src_wp); window_lost_pane(src_w, src_wp); TAILQ_REMOVE(&src_w->panes, src_wp, entry); src_wp->window = dst_w; TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry); layout_assign_pane(lc, src_wp); recalculate_sizes(); server_redraw_window(src_w); server_redraw_window(dst_w); if (!args_has(args, 'd')) { window_set_active_pane(dst_w, src_wp, 1); session_select(dst_s, dst_idx); cmd_find_from_session(current, dst_s, 0); server_redraw_session(dst_s); } else server_status_session(dst_s); if (window_count_panes(src_w) == 0) server_kill_window(src_w); else notify_window("window-layout-changed", src_w); notify_window("window-layout-changed", dst_w); return (CMD_RETURN_NORMAL); }
int cmd_join_pane_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct session *dst_s; struct winlink *src_wl, *dst_wl; struct window *src_w, *dst_w; struct window_pane *src_wp, *dst_wp; char *cause; int size, percentage, dst_idx; enum layout_type type; struct layout_cell *lc; dst_wl = cmd_find_pane(ctx, args_get(args, 't'), &dst_s, &dst_wp); if (dst_wl == NULL) return (-1); dst_w = dst_wl->window; dst_idx = dst_wl->idx; src_wl = cmd_find_pane(ctx, args_get(args, 's'), NULL, &src_wp); if (src_wl == NULL) return (-1); src_w = src_wl->window; if (src_w == dst_w) { ctx->error(ctx, "can't join a pane to its own window"); return (-1); } type = LAYOUT_TOPBOTTOM; if (args_has(args, 'h')) type = LAYOUT_LEFTRIGHT; size = -1; if (args_has(args, 'l')) { size = args_strtonum(args, 'l', 0, INT_MAX, &cause); if (cause != NULL) { ctx->error(ctx, "size %s", cause); xfree(cause); return (-1); } } else if (args_has(args, 'p')) { percentage = args_strtonum(args, 'p', 0, 100, &cause); if (cause != NULL) { ctx->error(ctx, "percentage %s", cause); xfree(cause); return (-1); } if (type == LAYOUT_TOPBOTTOM) size = (dst_wp->sy * percentage) / 100; else size = (dst_wp->sx * percentage) / 100; } if ((lc = layout_split_pane(dst_wp, type, size)) == NULL) { ctx->error(ctx, "create pane failed: pane too small"); return (-1); } layout_close_pane(src_wp); if (src_w->active == src_wp) { src_w->active = TAILQ_PREV(src_wp, window_panes, entry); if (src_w->active == NULL) src_w->active = TAILQ_NEXT(src_wp, entry); } TAILQ_REMOVE(&src_w->panes, src_wp, entry); if (window_count_panes(src_w) == 0) server_kill_window(src_w); src_wp->window = dst_w; TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry); layout_assign_pane(lc, src_wp); recalculate_sizes(); server_redraw_window(src_w); server_redraw_window(dst_w); if (!args_has(args, 'd')) { window_set_active_pane(dst_w, src_wp); session_select(dst_s, dst_idx); server_redraw_session(dst_s); } else server_status_session(dst_s); return (0); }
/* * Do some sanity checks and then reparent the window. * */ void manage_window(xcb_window_t window, xcb_get_window_attributes_cookie_t cookie, bool needs_to_be_mapped) { xcb_drawable_t d = {window}; xcb_get_geometry_cookie_t geomc; xcb_get_geometry_reply_t *geom; xcb_get_window_attributes_reply_t *attr = NULL; xcb_get_property_cookie_t wm_type_cookie, strut_cookie, state_cookie, utf8_title_cookie, title_cookie, class_cookie, leader_cookie, transient_cookie, role_cookie, startup_id_cookie, wm_hints_cookie, wm_normal_hints_cookie, motif_wm_hints_cookie; geomc = xcb_get_geometry(conn, d); /* Check if the window is mapped (it could be not mapped when intializing and calling manage_window() for every window) */ if ((attr = xcb_get_window_attributes_reply(conn, cookie, 0)) == NULL) { DLOG("Could not get attributes\n"); xcb_discard_reply(conn, geomc.sequence); return; } if (needs_to_be_mapped && attr->map_state != XCB_MAP_STATE_VIEWABLE) { xcb_discard_reply(conn, geomc.sequence); goto out; } /* Don’t manage clients with the override_redirect flag */ if (attr->override_redirect) { xcb_discard_reply(conn, geomc.sequence); goto out; } /* Check if the window is already managed */ if (con_by_window_id(window) != NULL) { DLOG("already managed (by con %p)\n", con_by_window_id(window)); xcb_discard_reply(conn, geomc.sequence); goto out; } /* Get the initial geometry (position, size, …) */ if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) == NULL) { DLOG("could not get geometry\n"); goto out; } uint32_t values[1]; /* Set a temporary event mask for the new window, consisting only of * PropertyChange and StructureNotify. We need to be notified of * PropertyChanges because the client can change its properties *after* we * requested them but *before* we actually reparented it and have set our * final event mask. * We need StructureNotify because the client may unmap the window before * we get to re-parent it. * If this request fails, we assume the client has already unmapped the * window between the MapRequest and our event mask change. */ values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY; xcb_void_cookie_t event_mask_cookie = xcb_change_window_attributes_checked(conn, window, XCB_CW_EVENT_MASK, values); if (xcb_request_check(conn, event_mask_cookie) != NULL) { LOG("Could not change event mask, the window probably already disappeared.\n"); goto out; } #define GET_PROPERTY(atom, len) xcb_get_property(conn, false, window, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, len) wm_type_cookie = GET_PROPERTY(A__NET_WM_WINDOW_TYPE, UINT32_MAX); strut_cookie = GET_PROPERTY(A__NET_WM_STRUT_PARTIAL, UINT32_MAX); state_cookie = GET_PROPERTY(A__NET_WM_STATE, UINT32_MAX); utf8_title_cookie = GET_PROPERTY(A__NET_WM_NAME, 128); leader_cookie = GET_PROPERTY(A_WM_CLIENT_LEADER, UINT32_MAX); transient_cookie = GET_PROPERTY(XCB_ATOM_WM_TRANSIENT_FOR, UINT32_MAX); title_cookie = GET_PROPERTY(XCB_ATOM_WM_NAME, 128); class_cookie = GET_PROPERTY(XCB_ATOM_WM_CLASS, 128); role_cookie = GET_PROPERTY(A_WM_WINDOW_ROLE, 128); startup_id_cookie = GET_PROPERTY(A__NET_STARTUP_ID, 512); wm_hints_cookie = xcb_icccm_get_wm_hints(conn, window); wm_normal_hints_cookie = xcb_icccm_get_wm_normal_hints(conn, window); motif_wm_hints_cookie = GET_PROPERTY(A__MOTIF_WM_HINTS, 5 * sizeof(uint64_t)); DLOG("Managing window 0x%08x\n", window); i3Window *cwindow = scalloc(1, sizeof(i3Window)); cwindow->id = window; cwindow->depth = get_visual_depth(attr->visual); /* We need to grab buttons 1-3 for click-to-focus and buttons 1-5 * to allow for mouse bindings using --whole-window to work correctly. */ xcb_grab_button(conn, false, window, XCB_EVENT_MASK_BUTTON_PRESS, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE, XCB_BUTTON_INDEX_ANY, XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */); /* update as much information as possible so far (some replies may be NULL) */ window_update_class(cwindow, xcb_get_property_reply(conn, class_cookie, NULL), true); window_update_name_legacy(cwindow, xcb_get_property_reply(conn, title_cookie, NULL), true); window_update_name(cwindow, xcb_get_property_reply(conn, utf8_title_cookie, NULL), true); window_update_leader(cwindow, xcb_get_property_reply(conn, leader_cookie, NULL)); window_update_transient_for(cwindow, xcb_get_property_reply(conn, transient_cookie, NULL)); window_update_strut_partial(cwindow, xcb_get_property_reply(conn, strut_cookie, NULL)); window_update_role(cwindow, xcb_get_property_reply(conn, role_cookie, NULL), true); bool urgency_hint; window_update_hints(cwindow, xcb_get_property_reply(conn, wm_hints_cookie, NULL), &urgency_hint); border_style_t motif_border_style = BS_NORMAL; window_update_motif_hints(cwindow, xcb_get_property_reply(conn, motif_wm_hints_cookie, NULL), &motif_border_style); xcb_size_hints_t wm_size_hints; if (!xcb_icccm_get_wm_size_hints_reply(conn, wm_normal_hints_cookie, &wm_size_hints, NULL)) memset(&wm_size_hints, '\0', sizeof(xcb_size_hints_t)); xcb_get_property_reply_t *type_reply = xcb_get_property_reply(conn, wm_type_cookie, NULL); xcb_get_property_reply_t *state_reply = xcb_get_property_reply(conn, state_cookie, NULL); xcb_get_property_reply_t *startup_id_reply; startup_id_reply = xcb_get_property_reply(conn, startup_id_cookie, NULL); char *startup_ws = startup_workspace_for_window(cwindow, startup_id_reply); DLOG("startup workspace = %s\n", startup_ws); /* check if the window needs WM_TAKE_FOCUS */ cwindow->needs_take_focus = window_supports_protocol(cwindow->id, A_WM_TAKE_FOCUS); /* read the preferred _NET_WM_WINDOW_TYPE atom */ cwindow->window_type = xcb_get_preferred_window_type(type_reply); /* Where to start searching for a container that swallows the new one? */ Con *search_at = croot; if (xcb_reply_contains_atom(type_reply, A__NET_WM_WINDOW_TYPE_DOCK)) { LOG("This window is of type dock\n"); Output *output = get_output_containing(geom->x, geom->y); if (output != NULL) { DLOG("Starting search at output %s\n", output->name); search_at = output->con; } /* find out the desired position of this dock window */ if (cwindow->reserved.top > 0 && cwindow->reserved.bottom == 0) { DLOG("Top dock client\n"); cwindow->dock = W_DOCK_TOP; } else if (cwindow->reserved.top == 0 && cwindow->reserved.bottom > 0) { DLOG("Bottom dock client\n"); cwindow->dock = W_DOCK_BOTTOM; } else { DLOG("Ignoring invalid reserved edges (_NET_WM_STRUT_PARTIAL), using position as fallback:\n"); if (geom->y < (int16_t)(search_at->rect.height / 2)) { DLOG("geom->y = %d < rect.height / 2 = %d, it is a top dock client\n", geom->y, (search_at->rect.height / 2)); cwindow->dock = W_DOCK_TOP; } else { DLOG("geom->y = %d >= rect.height / 2 = %d, it is a bottom dock client\n", geom->y, (search_at->rect.height / 2)); cwindow->dock = W_DOCK_BOTTOM; } } } DLOG("Initial geometry: (%d, %d, %d, %d)\n", geom->x, geom->y, geom->width, geom->height); Con *nc = NULL; Match *match = NULL; Assignment *assignment; /* TODO: two matches for one container */ /* See if any container swallows this new window */ nc = con_for_window(search_at, cwindow, &match); if (nc == NULL) { /* If not, check if it is assigned to a specific workspace */ if ((assignment = assignment_for(cwindow, A_TO_WORKSPACE))) { DLOG("Assignment matches (%p)\n", match); Con *assigned_ws = workspace_get(assignment->dest.workspace, NULL); nc = con_descend_tiling_focused(assigned_ws); DLOG("focused on ws %s: %p / %s\n", assigned_ws->name, nc, nc->name); if (nc->type == CT_WORKSPACE) nc = tree_open_con(nc, cwindow); else nc = tree_open_con(nc->parent, cwindow); /* set the urgency hint on the window if the workspace is not visible */ if (!workspace_is_visible(assigned_ws)) urgency_hint = true; } else if (startup_ws) { /* If it’s not assigned, but was started on a specific workspace, * we want to open it there */ DLOG("Using workspace on which this application was started (%s)\n", startup_ws); nc = con_descend_tiling_focused(workspace_get(startup_ws, NULL)); DLOG("focused on ws %s: %p / %s\n", startup_ws, nc, nc->name); if (nc->type == CT_WORKSPACE) nc = tree_open_con(nc, cwindow); else nc = tree_open_con(nc->parent, cwindow); } else { /* If not, insert it at the currently focused position */ if (focused->type == CT_CON && con_accepts_window(focused)) { LOG("using current container, focused = %p, focused->name = %s\n", focused, focused->name); nc = focused; } else nc = tree_open_con(NULL, cwindow); } } else { /* M_BELOW inserts the new window as a child of the one which was * matched (e.g. dock areas) */ if (match != NULL && match->insert_where == M_BELOW) { nc = tree_open_con(nc, cwindow); } /* If M_BELOW is not used, the container is replaced. This happens with * "swallows" criteria that are used for stored layouts, in which case * we need to remove that criterion, because they should only be valid * once. */ if (match != NULL && match->insert_where != M_BELOW) { DLOG("Removing match %p from container %p\n", match, nc); TAILQ_REMOVE(&(nc->swallow_head), match, matches); match_free(match); } } DLOG("new container = %p\n", nc); if (nc->window != NULL && nc->window != cwindow) { if (!restore_kill_placeholder(nc->window->id)) { DLOG("Uh?! Container without a placeholder, but with a window, has swallowed this to-be-managed window?!\n"); } else { /* Remove remaining criteria, the first swallowed window wins. */ while (!TAILQ_EMPTY(&(nc->swallow_head))) { Match *first = TAILQ_FIRST(&(nc->swallow_head)); TAILQ_REMOVE(&(nc->swallow_head), first, matches); match_free(first); } } } nc->window = cwindow; x_reinit(nc); nc->border_width = geom->border_width; char *name; sasprintf(&name, "[i3 con] container around %p", cwindow); x_set_name(nc, name); free(name); /* handle fullscreen containers */ Con *ws = con_get_workspace(nc); Con *fs = (ws ? con_get_fullscreen_con(ws, CF_OUTPUT) : NULL); if (fs == NULL) fs = con_get_fullscreen_con(croot, CF_GLOBAL); if (xcb_reply_contains_atom(state_reply, A__NET_WM_STATE_FULLSCREEN)) { /* If this window is already fullscreen (after restarting!), skip * toggling fullscreen, that would drop it out of fullscreen mode. */ if (fs != nc) con_toggle_fullscreen(nc, CF_OUTPUT); fs = NULL; } bool set_focus = false; if (fs == NULL) { DLOG("Not in fullscreen mode, focusing\n"); if (!cwindow->dock) { /* Check that the workspace is visible and on the same output as * the current focused container. If the window was assigned to an * invisible workspace, we should not steal focus. */ Con *current_output = con_get_output(focused); Con *target_output = con_get_output(ws); if (workspace_is_visible(ws) && current_output == target_output) { if (!match || !match->restart_mode) { set_focus = true; } else DLOG("not focusing, matched with restart_mode == true\n"); } else DLOG("workspace not visible, not focusing\n"); } else DLOG("dock, not focusing\n"); } else { DLOG("fs = %p, ws = %p, not focusing\n", fs, ws); /* Insert the new container in focus stack *after* the currently * focused (fullscreen) con. This way, the new container will be * focused after we return from fullscreen mode */ Con *first = TAILQ_FIRST(&(nc->parent->focus_head)); if (first != nc) { /* We only modify the focus stack if the container is not already * the first one. This can happen when existing containers swallow * new windows, for example when restarting. */ TAILQ_REMOVE(&(nc->parent->focus_head), nc, focused); TAILQ_INSERT_AFTER(&(nc->parent->focus_head), first, nc, focused); } } /* set floating if necessary */ bool want_floating = false; if (xcb_reply_contains_atom(type_reply, A__NET_WM_WINDOW_TYPE_DIALOG) || xcb_reply_contains_atom(type_reply, A__NET_WM_WINDOW_TYPE_UTILITY) || xcb_reply_contains_atom(type_reply, A__NET_WM_WINDOW_TYPE_TOOLBAR) || xcb_reply_contains_atom(type_reply, A__NET_WM_WINDOW_TYPE_SPLASH) || xcb_reply_contains_atom(state_reply, A__NET_WM_STATE_MODAL) || (wm_size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE && wm_size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE && wm_size_hints.min_height == wm_size_hints.max_height && wm_size_hints.min_width == wm_size_hints.max_width)) { LOG("This window is a dialog window, setting floating\n"); want_floating = true; } if (xcb_reply_contains_atom(state_reply, A__NET_WM_STATE_STICKY)) nc->sticky = true; FREE(state_reply); FREE(type_reply); if (cwindow->transient_for != XCB_NONE || (cwindow->leader != XCB_NONE && cwindow->leader != cwindow->id && con_by_window_id(cwindow->leader) != NULL)) { LOG("This window is transient for another window, setting floating\n"); want_floating = true; if (config.popup_during_fullscreen == PDF_LEAVE_FULLSCREEN && fs != NULL) { LOG("There is a fullscreen window, leaving fullscreen mode\n"); con_toggle_fullscreen(fs, CF_OUTPUT); } else if (config.popup_during_fullscreen == PDF_SMART && fs != NULL && fs->window != NULL) { i3Window *transient_win = cwindow; while (transient_win != NULL && transient_win->transient_for != XCB_NONE) { if (transient_win->transient_for == fs->window->id) { LOG("This floating window belongs to the fullscreen window (popup_during_fullscreen == smart)\n"); set_focus = true; break; } Con *next_transient = con_by_window_id(transient_win->transient_for); if (next_transient == NULL) break; /* Some clients (e.g. x11-ssh-askpass) actually set * WM_TRANSIENT_FOR to their own window id, so break instead of * looping endlessly. */ if (transient_win == next_transient->window) break; transient_win = next_transient->window; } } } /* dock clients cannot be floating, that makes no sense */ if (cwindow->dock) want_floating = false; /* Plasma windows set their geometry in WM_SIZE_HINTS. */ if ((wm_size_hints.flags & XCB_ICCCM_SIZE_HINT_US_POSITION || wm_size_hints.flags & XCB_ICCCM_SIZE_HINT_P_POSITION) && (wm_size_hints.flags & XCB_ICCCM_SIZE_HINT_US_SIZE || wm_size_hints.flags & XCB_ICCCM_SIZE_HINT_P_SIZE)) { DLOG("We are setting geometry according to wm_size_hints x=%d y=%d w=%d h=%d\n", wm_size_hints.x, wm_size_hints.y, wm_size_hints.width, wm_size_hints.height); geom->x = wm_size_hints.x; geom->y = wm_size_hints.y; geom->width = wm_size_hints.width; geom->height = wm_size_hints.height; } /* Store the requested geometry. The width/height gets raised to at least * 75x50 when entering floating mode, which is the minimum size for a * window to be useful (smaller windows are usually overlays/toolbars/… * which are not managed by the wm anyways). We store the original geometry * here because it’s used for dock clients. */ if (nc->geometry.width == 0) nc->geometry = (Rect){geom->x, geom->y, geom->width, geom->height}; if (motif_border_style != BS_NORMAL) { DLOG("MOTIF_WM_HINTS specifies decorations (border_style = %d)\n", motif_border_style); if (want_floating) { con_set_border_style(nc, motif_border_style, config.default_floating_border_width); } else { con_set_border_style(nc, motif_border_style, config.default_border_width); } } if (want_floating) { DLOG("geometry = %d x %d\n", nc->geometry.width, nc->geometry.height); /* automatically set the border to the default value if a motif border * was not specified */ bool automatic_border = (motif_border_style == BS_NORMAL); floating_enable(nc, automatic_border); } /* explicitly set the border width to the default */ if (nc->current_border_width == -1) { nc->current_border_width = (want_floating ? config.default_floating_border_width : config.default_border_width); } /* to avoid getting an UnmapNotify event due to reparenting, we temporarily * declare no interest in any state change event of this window */ values[0] = XCB_NONE; xcb_change_window_attributes(conn, window, XCB_CW_EVENT_MASK, values); xcb_void_cookie_t rcookie = xcb_reparent_window_checked(conn, window, nc->frame, 0, 0); if (xcb_request_check(conn, rcookie) != NULL) { LOG("Could not reparent the window, aborting\n"); goto geom_out; } values[0] = CHILD_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW; xcb_change_window_attributes(conn, window, XCB_CW_EVENT_MASK, values); xcb_flush(conn); /* Put the client inside the save set. Upon termination (whether killed or * normal exit does not matter) of the window manager, these clients will * be correctly reparented to their most closest living ancestor (= * cleanup) */ xcb_change_save_set(conn, XCB_SET_MODE_INSERT, window); /* Check if any assignments match */ run_assignments(cwindow); /* 'ws' may be invalid because of the assignments, e.g. when the user uses * "move window to workspace 1", but had it assigned to workspace 2. */ ws = con_get_workspace(nc); /* If this window was put onto an invisible workspace (via assignments), we * render this workspace. It wouldn’t be rendered in our normal code path * because only the visible workspaces get rendered. * * By rendering the workspace, we assign proper coordinates (read: not * width=0, height=0) to the window, which is important for windows who * actually use them to position their GUI elements, e.g. rhythmbox. */ if (ws && !workspace_is_visible(ws)) { /* This is a bit hackish: we need to copy the content container’s rect * to the workspace, because calling render_con() on the content * container would also take the shortcut and not render the invisible * workspace at all. However, just calling render_con() on the * workspace isn’t enough either — it needs the rect. */ ws->rect = ws->parent->rect; render_con(ws, true); /* Disable setting focus, otherwise we’d move focus to an invisible * workspace, which we generally prevent (e.g. in * con_move_to_workspace). */ set_focus = false; } render_con(croot, false); /* Send an event about window creation */ ipc_send_window_event("new", nc); if (set_focus && assignment_for(cwindow, A_NO_FOCUS) != NULL) { /* The first window on a workspace should always be focused. We have to * compare with == 1 because the container has already been inserted at * this point. */ if (con_num_children(ws) == 1) { DLOG("This is the first window on this workspace, ignoring no_focus.\n"); } else { DLOG("no_focus was set for con = %p, not setting focus.\n", nc); set_focus = false; } } /* Defer setting focus after the 'new' event has been sent to ensure the * proper window event sequence. */ if (set_focus && !nc->window->doesnt_accept_focus && nc->mapped) { DLOG("Now setting focus.\n"); con_focus(nc); } tree_render(); /* Windows might get managed with the urgency hint already set (Pidgin is * known to do that), so check for that and handle the hint accordingly. * This code needs to be in this part of manage_window() because the window * needs to be on the final workspace first. */ con_set_urgency(nc, urgency_hint); geom_out: free(geom); out: free(attr); return; }
/* * vm_contig_pg_clean: * * Do a thorough cleanup of the specified 'queue', which can be either * PQ_ACTIVE or PQ_INACTIVE by doing a walkthrough. If the page is not * marked dirty, it is shoved into the page cache, provided no one has * currently aqcuired it, otherwise localized action per object type * is taken for cleanup: * * In the OBJT_VNODE case, the whole page range is cleaned up * using the vm_object_page_clean() routine, by specyfing a * start and end of '0'. * * Otherwise if the object is of any other type, the generic * pageout (daemon) flush routine is invoked. */ static void vm_contig_pg_clean(int queue, int count) { vm_object_t object; vm_page_t m, m_tmp; struct vm_page marker; struct vpgqueues *pq = &vm_page_queues[queue]; /* * Setup a local marker */ bzero(&marker, sizeof(marker)); marker.flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER; marker.queue = queue; marker.wire_count = 1; vm_page_queues_spin_lock(queue); TAILQ_INSERT_HEAD(&pq->pl, &marker, pageq); vm_page_queues_spin_unlock(queue); /* * Iterate the queue. Note that the vm_page spinlock must be * acquired before the pageq spinlock so it's easiest to simply * not hold it in the loop iteration. */ while (count-- > 0 && (m = TAILQ_NEXT(&marker, pageq)) != NULL) { vm_page_and_queue_spin_lock(m); if (m != TAILQ_NEXT(&marker, pageq)) { vm_page_and_queue_spin_unlock(m); ++count; continue; } KKASSERT(m->queue == queue); TAILQ_REMOVE(&pq->pl, &marker, pageq); TAILQ_INSERT_AFTER(&pq->pl, m, &marker, pageq); if (m->flags & PG_MARKER) { vm_page_and_queue_spin_unlock(m); continue; } if (vm_page_busy_try(m, TRUE)) { vm_page_and_queue_spin_unlock(m); continue; } vm_page_and_queue_spin_unlock(m); /* * We've successfully busied the page */ if (m->queue - m->pc != queue) { vm_page_wakeup(m); continue; } if (m->wire_count || m->hold_count) { vm_page_wakeup(m); continue; } if ((object = m->object) == NULL) { vm_page_wakeup(m); continue; } vm_page_test_dirty(m); if (m->dirty || (m->flags & PG_NEED_COMMIT)) { vm_object_hold(object); KKASSERT(m->object == object); if (object->type == OBJT_VNODE) { vm_page_wakeup(m); vn_lock(object->handle, LK_EXCLUSIVE|LK_RETRY); vm_object_page_clean(object, 0, 0, OBJPC_SYNC); vn_unlock(((struct vnode *)object->handle)); } else if (object->type == OBJT_SWAP || object->type == OBJT_DEFAULT) { m_tmp = m; vm_pageout_flush(&m_tmp, 1, 0); } else { vm_page_wakeup(m); } vm_object_drop(object); } else if (m->hold_count == 0) { vm_page_cache(m); } else { vm_page_wakeup(m); } } /* * Scrap our local marker */ vm_page_queues_spin_lock(queue); TAILQ_REMOVE(&pq->pl, &marker, pageq); vm_page_queues_spin_unlock(queue); }
static void bt_insseg(vmem_t *vm, bt_t *bt, bt_t *prev) { TAILQ_INSERT_AFTER(&vm->vm_seglist, prev, bt, bt_seglist); }
enum cmd_retval join_pane(struct cmd *self, struct cmd_q *cmdq, int not_same_window) { struct args *args = self->args; struct session *dst_s; struct winlink *src_wl, *dst_wl; struct window *src_w, *dst_w; struct window_pane *src_wp, *dst_wp; char *cause; int size, percentage, dst_idx; enum layout_type type; struct layout_cell *lc; dst_wl = cmd_find_pane(cmdq, args_get(args, 't'), &dst_s, &dst_wp); if (dst_wl == NULL) return (CMD_RETURN_ERROR); dst_w = dst_wl->window; dst_idx = dst_wl->idx; server_unzoom_window(dst_w); src_wl = cmd_find_pane(cmdq, args_get(args, 's'), NULL, &src_wp); if (src_wl == NULL) return (CMD_RETURN_ERROR); src_w = src_wl->window; server_unzoom_window(src_w); if (not_same_window && src_w == dst_w) { cmdq_error(cmdq, "can't join a pane to its own window"); return (CMD_RETURN_ERROR); } if (!not_same_window && src_wp == dst_wp) { cmdq_error(cmdq, "source and target panes must be different"); return (CMD_RETURN_ERROR); } type = LAYOUT_TOPBOTTOM; if (args_has(args, 'h')) type = LAYOUT_LEFTRIGHT; size = -1; if (args_has(args, 'l')) { size = args_strtonum(args, 'l', 0, INT_MAX, &cause); if (cause != NULL) { cmdq_error(cmdq, "size %s", cause); free(cause); return (CMD_RETURN_ERROR); } } else if (args_has(args, 'p')) { percentage = args_strtonum(args, 'p', 0, 100, &cause); if (cause != NULL) { cmdq_error(cmdq, "percentage %s", cause); free(cause); return (CMD_RETURN_ERROR); } if (type == LAYOUT_TOPBOTTOM) size = (dst_wp->sy * percentage) / 100; else size = (dst_wp->sx * percentage) / 100; } lc = layout_split_pane(dst_wp, type, size, args_has(args, 'b')); if (lc == NULL) { cmdq_error(cmdq, "create pane failed: pane too small"); return (CMD_RETURN_ERROR); } layout_close_pane(src_wp); window_lost_pane(src_w, src_wp); TAILQ_REMOVE(&src_w->panes, src_wp, entry); if (window_count_panes(src_w) == 0) server_kill_window(src_w); else notify_window_layout_changed(src_w); src_wp->window = dst_w; TAILQ_INSERT_AFTER(&dst_w->panes, dst_wp, src_wp, entry); layout_assign_pane(lc, src_wp); recalculate_sizes(); server_redraw_window(src_w); server_redraw_window(dst_w); if (!args_has(args, 'd')) { window_set_active_pane(dst_w, src_wp); session_select(dst_s, dst_idx); server_redraw_session(dst_s); } else server_status_session(dst_s); notify_window_layout_changed(dst_w); return (CMD_RETURN_NORMAL); }
/* * Find the offset of the specified name within the given inode. * Returns 0 on success, ENOENT if the entry does not exist, or * EJUSTRETURN if the caller should revert to a linear search. * * If successful, the directory offset is stored in *offp, and a * pointer to a struct buf containing the entry is stored in *bpp. If * prevoffp is non-NULL, the offset of the previous entry within * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry * is the first in a block, the start of the block is used). */ int ufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp, struct buf **bpp, doff_t *prevoffp) { struct dirhash *dh, *dh_next; struct direct *dp; struct vnode *vp; struct buf *bp; doff_t blkoff, bmask, offset, prevoff; int i, slot; if ((dh = ip->i_dirhash) == NULL) return (EJUSTRETURN); /* * Move this dirhash towards the end of the list if it has a * score higher than the next entry, and acquire the dh_mtx. * Optimise the case where it's already the last by performing * an unlocked read of the TAILQ_NEXT pointer. * * In both cases, end up holding just dh_mtx. */ if (TAILQ_NEXT(dh, dh_list) != NULL) { DIRHASHLIST_LOCK(); DIRHASH_LOCK(dh); /* * If the new score will be greater than that of the next * entry, then move this entry past it. With both mutexes * held, dh_next won't go away, but its dh_score could * change; that's not important since it is just a hint. */ if (dh->dh_hash != NULL && (dh_next = TAILQ_NEXT(dh, dh_list)) != NULL && dh->dh_score >= dh_next->dh_score) { DIRHASH_ASSERT(dh->dh_onlist, ("dirhash: not on list")); TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list); TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh, dh_list); } DIRHASHLIST_UNLOCK(); } else { /* Already the last, though that could change as we wait. */ DIRHASH_LOCK(dh); } if (dh->dh_hash == NULL) { DIRHASH_UNLOCK(dh); ufsdirhash_free(ip); return (EJUSTRETURN); } /* Update the score. */ if (dh->dh_score < DH_SCOREMAX) dh->dh_score++; vp = ip->i_vnode; bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1; blkoff = -1; bp = NULL; restart: slot = ufsdirhash_hash(dh, name, namelen); if (dh->dh_seqopt) { /* * Sequential access optimisation. dh_seqoff contains the * offset of the directory entry immediately following * the last entry that was looked up. Check if this offset * appears in the hash chain for the name we are looking for. */ for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY; i = WRAPINCR(i, dh->dh_hlen)) if (offset == dh->dh_seqoff) break; if (offset == dh->dh_seqoff) { /* * We found an entry with the expected offset. This * is probably the entry we want, but if not, the * code below will turn off seqopt and retry. */ slot = i; } else dh->dh_seqopt = 0; } for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY; slot = WRAPINCR(slot, dh->dh_hlen)) { if (offset == DIRHASH_DEL) continue; DIRHASH_UNLOCK(dh); if (offset < 0 || offset >= DIP(ip, size)) panic("ufsdirhash_lookup: bad offset in hash array"); if ((offset & ~bmask) != blkoff) { if (bp != NULL) brelse(bp); blkoff = offset & ~bmask; if (UFS_BUFATOFF(ip, (off_t)blkoff, NULL, &bp) != 0) return (EJUSTRETURN); } dp = (struct direct *)(bp->b_data + (offset & bmask)); if (dp->d_reclen == 0 || dp->d_reclen > DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) { /* Corrupted directory. */ brelse(bp); return (EJUSTRETURN); } if (dp->d_namlen == namelen && memcmp(dp->d_name, name, namelen) == 0) { /* Found. Get the prev offset if needed. */ if (prevoffp != NULL) { if (offset & (DIRBLKSIZ - 1)) { prevoff = ufsdirhash_getprev(dp, offset); if (prevoff == -1) { brelse(bp); return (EJUSTRETURN); } } else prevoff = offset; *prevoffp = prevoff; } /* Check for sequential access, and update offset. */ if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset) dh->dh_seqopt = 1; dh->dh_seqoff = offset + DIRSIZ(0, dp); *bpp = bp; *offp = offset; return (0); } DIRHASH_LOCK(dh); if (dh->dh_hash == NULL) { DIRHASH_UNLOCK(dh); if (bp != NULL) brelse(bp); ufsdirhash_free(ip); return (EJUSTRETURN); } /* * When the name doesn't match in the seqopt case, go back * and search normally. */ if (dh->dh_seqopt) { dh->dh_seqopt = 0; goto restart; } } DIRHASH_UNLOCK(dh); if (bp != NULL) brelse(bp); return (ENOENT); }
/* * Do some sanity checks and then reparent the window. * */ void manage_window(xcb_window_t window, xcb_get_window_attributes_cookie_t cookie, bool needs_to_be_mapped) { xcb_drawable_t d = { window }; xcb_get_geometry_cookie_t geomc; xcb_get_geometry_reply_t *geom; xcb_get_window_attributes_reply_t *attr = NULL; xcb_get_property_cookie_t wm_type_cookie, strut_cookie, state_cookie, utf8_title_cookie, title_cookie, class_cookie, leader_cookie, transient_cookie, role_cookie, startup_id_cookie, wm_hints_cookie; #ifdef USE_ICONS xcb_get_property_cookie_t wm_icon_cookie; #endif geomc = xcb_get_geometry(conn, d); #define FREE_GEOMETRY() do { \ if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) != NULL) \ free(geom); \ } while (0) /* Check if the window is mapped (it could be not mapped when intializing and calling manage_window() for every window) */ if ((attr = xcb_get_window_attributes_reply(conn, cookie, 0)) == NULL) { DLOG("Could not get attributes\n"); FREE_GEOMETRY(); return; } if (needs_to_be_mapped && attr->map_state != XCB_MAP_STATE_VIEWABLE) { FREE_GEOMETRY(); goto out; } /* Don’t manage clients with the override_redirect flag */ if (attr->override_redirect) { FREE_GEOMETRY(); goto out; } /* Check if the window is already managed */ if (con_by_window_id(window) != NULL) { DLOG("already managed (by con %p)\n", con_by_window_id(window)); FREE_GEOMETRY(); goto out; } /* Get the initial geometry (position, size, …) */ if ((geom = xcb_get_geometry_reply(conn, geomc, 0)) == NULL) { DLOG("could not get geometry\n"); goto out; } uint32_t values[1]; /* Set a temporary event mask for the new window, consisting only of * PropertyChange and StructureNotify. We need to be notified of * PropertyChanges because the client can change its properties *after* we * requested them but *before* we actually reparented it and have set our * final event mask. * We need StructureNotify because the client may unmap the window before * we get to re-parent it. * If this request fails, we assume the client has already unmapped the * window between the MapRequest and our event mask change. */ values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY; xcb_void_cookie_t event_mask_cookie = xcb_change_window_attributes_checked(conn, window, XCB_CW_EVENT_MASK, values); if (xcb_request_check(conn, event_mask_cookie) != NULL) { LOG("Could not change event mask, the window probably already disappeared.\n"); goto out; } #define GET_PROPERTY(atom, len) xcb_get_property(conn, false, window, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, len) wm_type_cookie = GET_PROPERTY(A__NET_WM_WINDOW_TYPE, UINT32_MAX); strut_cookie = GET_PROPERTY(A__NET_WM_STRUT_PARTIAL, UINT32_MAX); state_cookie = GET_PROPERTY(A__NET_WM_STATE, UINT32_MAX); utf8_title_cookie = GET_PROPERTY(A__NET_WM_NAME, 128); leader_cookie = GET_PROPERTY(A_WM_CLIENT_LEADER, UINT32_MAX); transient_cookie = GET_PROPERTY(XCB_ATOM_WM_TRANSIENT_FOR, UINT32_MAX); title_cookie = GET_PROPERTY(XCB_ATOM_WM_NAME, 128); class_cookie = GET_PROPERTY(XCB_ATOM_WM_CLASS, 128); role_cookie = GET_PROPERTY(A_WM_WINDOW_ROLE, 128); startup_id_cookie = GET_PROPERTY(A__NET_STARTUP_ID, 512); wm_hints_cookie = xcb_icccm_get_wm_hints(conn, window); #ifdef USE_ICONS wm_icon_cookie = xcb_get_property_unchecked(conn, false, window, A__NET_WM_ICON, XCB_ATOM_CARDINAL, 0, UINT32_MAX); #endif /* TODO: also get wm_normal_hints here. implement after we got rid of xcb-event */ DLOG("Managing window 0x%08x\n", window); i3Window *cwindow = scalloc(sizeof(i3Window)); cwindow->id = window; cwindow->depth = get_visual_depth(attr->visual); /* We need to grab the mouse buttons for click to focus */ xcb_grab_button(conn, false, window, XCB_EVENT_MASK_BUTTON_PRESS, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE, 1 /* left mouse button */, XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */); xcb_grab_button(conn, false, window, XCB_EVENT_MASK_BUTTON_PRESS, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE, 2 /* middle mouse button */, XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */); xcb_grab_button(conn, false, window, XCB_EVENT_MASK_BUTTON_PRESS, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE, 3 /* right mouse button */, XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */); /* update as much information as possible so far (some replies may be NULL) */ window_update_class(cwindow, xcb_get_property_reply(conn, class_cookie, NULL), true); window_update_name_legacy(cwindow, xcb_get_property_reply(conn, title_cookie, NULL), true); window_update_name(cwindow, xcb_get_property_reply(conn, utf8_title_cookie, NULL), true); window_update_leader(cwindow, xcb_get_property_reply(conn, leader_cookie, NULL)); window_update_transient_for(cwindow, xcb_get_property_reply(conn, transient_cookie, NULL)); window_update_strut_partial(cwindow, xcb_get_property_reply(conn, strut_cookie, NULL)); window_update_role(cwindow, xcb_get_property_reply(conn, role_cookie, NULL), true); window_update_hints(cwindow, xcb_get_property_reply(conn, wm_hints_cookie, NULL)); #ifdef USE_ICONS window_update_icon(cwindow, xcb_get_property_reply(conn, wm_icon_cookie, NULL)); #endif xcb_get_property_reply_t *startup_id_reply; startup_id_reply = xcb_get_property_reply(conn, startup_id_cookie, NULL); char *startup_ws = startup_workspace_for_window(cwindow, startup_id_reply); DLOG("startup workspace = %s\n", startup_ws); /* check if the window needs WM_TAKE_FOCUS */ cwindow->needs_take_focus = window_supports_protocol(cwindow->id, A_WM_TAKE_FOCUS); /* Where to start searching for a container that swallows the new one? */ Con *search_at = croot; xcb_get_property_reply_t *reply = xcb_get_property_reply(conn, wm_type_cookie, NULL); if (xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_DOCK)) { LOG("This window is of type dock\n"); Output *output = get_output_containing(geom->x, geom->y); if (output != NULL) { DLOG("Starting search at output %s\n", output->name); search_at = output->con; } /* find out the desired position of this dock window */ if (cwindow->reserved.top > 0 && cwindow->reserved.bottom == 0) { DLOG("Top dock client\n"); cwindow->dock = W_DOCK_TOP; } else if (cwindow->reserved.top == 0 && cwindow->reserved.bottom > 0) { DLOG("Bottom dock client\n"); cwindow->dock = W_DOCK_BOTTOM; } else { DLOG("Ignoring invalid reserved edges (_NET_WM_STRUT_PARTIAL), using position as fallback:\n"); if (geom->y < (search_at->rect.height / 2)) { DLOG("geom->y = %d < rect.height / 2 = %d, it is a top dock client\n", geom->y, (search_at->rect.height / 2)); cwindow->dock = W_DOCK_TOP; } else { DLOG("geom->y = %d >= rect.height / 2 = %d, it is a bottom dock client\n", geom->y, (search_at->rect.height / 2)); cwindow->dock = W_DOCK_BOTTOM; } } } DLOG("Initial geometry: (%d, %d, %d, %d)\n", geom->x, geom->y, geom->width, geom->height); Con *nc = NULL; Match *match = NULL; Assignment *assignment; /* TODO: two matches for one container */ /* See if any container swallows this new window */ nc = con_for_window(search_at, cwindow, &match); if (nc == NULL) { /* If not, check if it is assigned to a specific workspace / output */ if ((assignment = assignment_for(cwindow, A_TO_WORKSPACE | A_TO_OUTPUT))) { DLOG("Assignment matches (%p)\n", match); if (assignment->type == A_TO_WORKSPACE) { nc = con_descend_tiling_focused(workspace_get(assignment->dest.workspace, NULL)); DLOG("focused on ws %s: %p / %s\n", assignment->dest.workspace, nc, nc->name); if (nc->type == CT_WORKSPACE) nc = tree_open_con(nc, cwindow); else nc = tree_open_con(nc->parent, cwindow); } /* TODO: handle assignments with type == A_TO_OUTPUT */ } else if (startup_ws) { /* If it’s not assigned, but was started on a specific workspace, * we want to open it there */ DLOG("Using workspace on which this application was started (%s)\n", startup_ws); nc = con_descend_tiling_focused(workspace_get(startup_ws, NULL)); DLOG("focused on ws %s: %p / %s\n", startup_ws, nc, nc->name); if (nc->type == CT_WORKSPACE) nc = tree_open_con(nc, cwindow); else nc = tree_open_con(nc->parent, cwindow); } else { /* If not, insert it at the currently focused position */ if (focused->type == CT_CON && con_accepts_window(focused)) { LOG("using current container, focused = %p, focused->name = %s\n", focused, focused->name); nc = focused; } else nc = tree_open_con(NULL, cwindow); } } else { /* M_BELOW inserts the new window as a child of the one which was * matched (e.g. dock areas) */ if (match != NULL && match->insert_where == M_BELOW) { nc = tree_open_con(nc, cwindow); } } DLOG("new container = %p\n", nc); nc->window = cwindow; x_reinit(nc); nc->border_width = geom->border_width; char *name; sasprintf(&name, "[i3 con] container around %p", cwindow); x_set_name(nc, name); free(name); Con *ws = con_get_workspace(nc); Con *fs = (ws ? con_get_fullscreen_con(ws, CF_OUTPUT) : NULL); if (fs == NULL) fs = con_get_fullscreen_con(croot, CF_GLOBAL); if (fs == NULL) { DLOG("Not in fullscreen mode, focusing\n"); if (!cwindow->dock) { /* Check that the workspace is visible and on the same output as * the current focused container. If the window was assigned to an * invisible workspace, we should not steal focus. */ Con *current_output = con_get_output(focused); Con *target_output = con_get_output(ws); if (workspace_is_visible(ws) && current_output == target_output) { if (!match || !match->restart_mode) { con_focus(nc); } else DLOG("not focusing, matched with restart_mode == true\n"); } else DLOG("workspace not visible, not focusing\n"); } else DLOG("dock, not focusing\n"); } else { DLOG("fs = %p, ws = %p, not focusing\n", fs, ws); /* Insert the new container in focus stack *after* the currently * focused (fullscreen) con. This way, the new container will be * focused after we return from fullscreen mode */ Con *first = TAILQ_FIRST(&(nc->parent->focus_head)); if (first != nc) { /* We only modify the focus stack if the container is not already * the first one. This can happen when existing containers swallow * new windows, for example when restarting. */ TAILQ_REMOVE(&(nc->parent->focus_head), nc, focused); TAILQ_INSERT_AFTER(&(nc->parent->focus_head), first, nc, focused); } } /* set floating if necessary */ bool want_floating = false; if (xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_DIALOG) || xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_UTILITY) || xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_TOOLBAR) || xcb_reply_contains_atom(reply, A__NET_WM_WINDOW_TYPE_SPLASH)) { LOG("This window is a dialog window, setting floating\n"); want_floating = true; } FREE(reply); if (cwindow->transient_for != XCB_NONE || (cwindow->leader != XCB_NONE && cwindow->leader != cwindow->id && con_by_window_id(cwindow->leader) != NULL)) { LOG("This window is transient for another window, setting floating\n"); want_floating = true; if (config.popup_during_fullscreen == PDF_LEAVE_FULLSCREEN && fs != NULL) { LOG("There is a fullscreen window, leaving fullscreen mode\n"); con_toggle_fullscreen(fs, CF_OUTPUT); } else if (config.popup_during_fullscreen == PDF_SMART && fs != NULL && fs->window != NULL) { i3Window *transient_win = cwindow; while (transient_win != NULL && transient_win->transient_for != XCB_NONE) { if (transient_win->transient_for == fs->window->id) { LOG("This floating window belongs to the fullscreen window (popup_during_fullscreen == smart)\n"); con_focus(nc); break; } Con *next_transient = con_by_window_id(transient_win->transient_for); if (next_transient == NULL) break; transient_win = next_transient->window; } } } /* dock clients cannot be floating, that makes no sense */ if (cwindow->dock) want_floating = false; /* Store the requested geometry. The width/height gets raised to at least * 75x50 when entering floating mode, which is the minimum size for a * window to be useful (smaller windows are usually overlays/toolbars/… * which are not managed by the wm anyways). We store the original geometry * here because it’s used for dock clients. */ nc->geometry = (Rect){ geom->x, geom->y, geom->width, geom->height }; if (want_floating) { DLOG("geometry = %d x %d\n", nc->geometry.width, nc->geometry.height); floating_enable(nc, true); } /* to avoid getting an UnmapNotify event due to reparenting, we temporarily * declare no interest in any state change event of this window */ values[0] = XCB_NONE; xcb_change_window_attributes(conn, window, XCB_CW_EVENT_MASK, values); xcb_void_cookie_t rcookie = xcb_reparent_window_checked(conn, window, nc->frame, 0, 0); if (xcb_request_check(conn, rcookie) != NULL) { LOG("Could not reparent the window, aborting\n"); goto geom_out; } values[0] = CHILD_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW; xcb_change_window_attributes(conn, window, XCB_CW_EVENT_MASK, values); xcb_flush(conn); reply = xcb_get_property_reply(conn, state_cookie, NULL); if (xcb_reply_contains_atom(reply, A__NET_WM_STATE_FULLSCREEN)) con_toggle_fullscreen(nc, CF_OUTPUT); FREE(reply); /* Put the client inside the save set. Upon termination (whether killed or * normal exit does not matter) of the window manager, these clients will * be correctly reparented to their most closest living ancestor (= * cleanup) */ xcb_change_save_set(conn, XCB_SET_MODE_INSERT, window); /* Check if any assignments match */ run_assignments(cwindow); /* 'ws' may be invalid because of the assignments, e.g. when the user uses * "move window to workspace 1", but had it assigned to workspace 2. */ ws = con_get_workspace(nc); /* If this window was put onto an invisible workspace (via assignments), we * render this workspace. It wouldn’t be rendered in our normal code path * because only the visible workspaces get rendered. * * By rendering the workspace, we assign proper coordinates (read: not * width=0, height=0) to the window, which is important for windows who * actually use them to position their GUI elements, e.g. rhythmbox. */ if (ws && !workspace_is_visible(ws)) { /* This is a bit hackish: we need to copy the content container’s rect * to the workspace, because calling render_con() on the content * container would also take the shortcut and not render the invisible * workspace at all. However, just calling render_con() on the * workspace isn’t enough either — it needs the rect. */ ws->rect = ws->parent->rect; render_con(ws, true); } tree_render(); /* Send an event about window creation */ ipc_send_window_new_event(nc); geom_out: free(geom); out: free(attr); return; }
/* * No requirements. */ static int do_vmtotal(SYSCTL_HANDLER_ARGS) { struct vmtotal total; struct vmtotal *totalp; struct vm_object marker; vm_object_t object; long collisions; int burst; bzero(&total, sizeof(total)); totalp = &total; bzero(&marker, sizeof(marker)); marker.type = OBJT_MARKER; collisions = vmobj_token.t_collisions; #if 0 /* * Mark all objects as inactive. */ lwkt_gettoken(&vmobj_token); for (object = TAILQ_FIRST(&vm_object_list); object != NULL; object = TAILQ_NEXT(object,object_list)) { if (object->type == OBJT_MARKER) continue; vm_object_clear_flag(object, OBJ_ACTIVE); } lwkt_reltoken(&vmobj_token); #endif /* * Calculate process statistics. */ allproc_scan(do_vmtotal_callback, totalp); /* * Calculate object memory usage statistics. */ lwkt_gettoken(&vmobj_token); TAILQ_INSERT_HEAD(&vm_object_list, &marker, object_list); burst = 0; for (object = TAILQ_FIRST(&vm_object_list); object != NULL; object = TAILQ_NEXT(object, object_list)) { /* * devices, like /dev/mem, will badly skew our totals. * markers aren't real objects. */ if (object->type == OBJT_MARKER) continue; if (object->type == OBJT_DEVICE) continue; if (object->size >= 0x7FFFFFFF) { /* * Probably unbounded anonymous memory (really * bounded by related vm_map_entry structures which * we do not have access to in this loop). */ totalp->t_vm += object->resident_page_count; } else { /* * It's questionable how useful this is but... */ totalp->t_vm += object->size; } totalp->t_rm += object->resident_page_count; if (object->flags & OBJ_ACTIVE) { totalp->t_avm += object->size; totalp->t_arm += object->resident_page_count; } if (object->shadow_count > 1) { /* shared object */ totalp->t_vmshr += object->size; totalp->t_rmshr += object->resident_page_count; if (object->flags & OBJ_ACTIVE) { totalp->t_avmshr += object->size; totalp->t_armshr += object->resident_page_count; } } /* * Don't waste time unnecessarily */ if (++burst < 25) continue; burst = 0; /* * Don't hog the vmobj_token if someone else wants it. */ TAILQ_REMOVE(&vm_object_list, &marker, object_list); TAILQ_INSERT_AFTER(&vm_object_list, object, &marker, object_list); object = ▮ if (collisions != vmobj_token.t_collisions) { tsleep(&vm_object_list, 0, "breath", 1); collisions = vmobj_token.t_collisions; } else { lwkt_yield(); } } TAILQ_REMOVE(&vm_object_list, &marker, object_list); lwkt_reltoken(&vmobj_token); totalp->t_free = vmstats.v_free_count + vmstats.v_cache_count; return (sysctl_handle_opaque(oidp, totalp, sizeof total, req)); }
int cmd_swap_pane_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct winlink *src_wl, *dst_wl; struct window *src_w, *dst_w; struct window_pane *tmp_wp, *src_wp, *dst_wp; struct layout_cell *src_lc, *dst_lc; u_int sx, sy, xoff, yoff; dst_wl = cmd_find_pane(ctx, args_get(args, 't'), NULL, &dst_wp); if (dst_wl == NULL) return (-1); dst_w = dst_wl->window; if (!args_has(args, 's')) { src_w = dst_w; if (args_has(self->args, 'D')) { src_wp = TAILQ_NEXT(dst_wp, entry); if (src_wp == NULL) src_wp = TAILQ_FIRST(&dst_w->panes); } else if (args_has(self->args, 'U')) { src_wp = TAILQ_PREV(dst_wp, window_panes, entry); if (src_wp == NULL) src_wp = TAILQ_LAST(&dst_w->panes, window_panes); } else return (0); } else { src_wl = cmd_find_pane(ctx, args_get(args, 's'), NULL, &src_wp); if (src_wl == NULL) return (-1); src_w = src_wl->window; } if (src_wp == dst_wp) return (0); tmp_wp = TAILQ_PREV(dst_wp, window_panes, entry); TAILQ_REMOVE(&dst_w->panes, dst_wp, entry); TAILQ_REPLACE(&src_w->panes, src_wp, dst_wp, entry); if (tmp_wp == src_wp) tmp_wp = dst_wp; if (tmp_wp == NULL) TAILQ_INSERT_HEAD(&dst_w->panes, src_wp, entry); else TAILQ_INSERT_AFTER(&dst_w->panes, tmp_wp, src_wp, entry); src_lc = src_wp->layout_cell; dst_lc = dst_wp->layout_cell; src_lc->wp = dst_wp; dst_wp->layout_cell = src_lc; dst_lc->wp = src_wp; src_wp->layout_cell = dst_lc; src_wp->window = dst_w; dst_wp->window = src_w; sx = src_wp->sx; sy = src_wp->sy; xoff = src_wp->xoff; yoff = src_wp->yoff; src_wp->xoff = dst_wp->xoff; src_wp->yoff = dst_wp->yoff; window_pane_resize(src_wp, dst_wp->sx, dst_wp->sy); dst_wp->xoff = xoff; dst_wp->yoff = yoff; window_pane_resize(dst_wp, sx, sy); if (!args_has(self->args, 'd')) { if (src_w != dst_w) { window_set_active_pane(src_w, dst_wp); window_set_active_pane(dst_w, src_wp); } else { tmp_wp = dst_wp; if (!window_pane_visible(tmp_wp)) tmp_wp = src_wp; window_set_active_pane(src_w, tmp_wp); } } else { if (src_w->active == src_wp) window_set_active_pane(src_w, dst_wp); if (dst_w->active == dst_wp) window_set_active_pane(dst_w, src_wp); } if (src_w != dst_w) { if (src_w->last == src_wp) src_w->last = NULL; if (dst_w->last == dst_wp) dst_w->last = NULL; } server_redraw_window(src_w); server_redraw_window(dst_w); return (0); }
void * mem_pos_insert_alloc(mem_buffer_pos_t pos, size_t n) { struct mem_buffer_trunk * trunk; assert(pos); assert(pos->m_buffer); if (n <= 0) return NULL; if (pos->m_trunk == NULL) { return mem_buffer_alloc(pos->m_buffer, n); } if (pos->m_pos_in_trunk == 0) { /*pos at begin of trunk, alloc a new trunk to store data*/ trunk = mem_trunk_alloc(pos->m_buffer->m_default_allocrator, n); if (trunk == NULL) { return NULL; } TAILQ_INSERT_BEFORE(pos->m_trunk, trunk, m_next); pos->m_buffer->m_size += n; trunk->m_size = n; return mem_trunk_data(trunk); } else if (pos->m_trunk->m_capacity >= pos->m_pos_in_trunk + n) { /*new data can store in current trunk*/ char * trunkBegin = (char *)mem_trunk_data(pos->m_trunk); char * result = trunkBegin + pos->m_pos_in_trunk; /*can`t store all data in current buffer, move overflow data to a new trunk*/ ssize_t appendSize = pos->m_trunk->m_size + n - pos->m_trunk->m_capacity; if (appendSize > 0) { trunk = mem_trunk_alloc(pos->m_buffer->m_default_allocrator, appendSize); if (trunk == NULL) { return NULL; } TAILQ_INSERT_AFTER(&pos->m_buffer->m_trunks, pos->m_trunk, trunk, m_next); memcpy( mem_trunk_data(trunk), trunkBegin + pos->m_trunk->m_size - appendSize, appendSize); trunk->m_size = appendSize; pos->m_trunk->m_size -= appendSize; } if (pos->m_pos_in_trunk < pos->m_trunk->m_size) { memmove( trunkBegin + pos->m_pos_in_trunk + n, trunkBegin + pos->m_pos_in_trunk, pos->m_trunk->m_size - pos->m_pos_in_trunk); } else { } pos->m_trunk->m_size += pos->m_trunk->m_size - appendSize + n; pos->m_pos_in_trunk += n; if (pos->m_pos_in_trunk >= pos->m_trunk->m_size) { pos->m_pos_in_trunk = pos->m_pos_in_trunk - pos->m_trunk->m_size; pos->m_trunk = trunk; } assert(pos->m_pos_in_trunk < pos->m_trunk->m_size); assert(pos->m_trunk->m_size <= pos->m_trunk->m_capacity); pos->m_buffer->m_size += n; return result; } else { /*new data can not store in current trunk*/ ssize_t moveSize = pos->m_trunk->m_size - pos->m_pos_in_trunk; trunk = mem_trunk_alloc(pos->m_buffer->m_default_allocrator, n + moveSize); if (trunk == NULL) { return NULL; } TAILQ_INSERT_AFTER(&pos->m_buffer->m_trunks, pos->m_trunk, trunk, m_next); assert(moveSize > 0); memcpy( (char *)mem_trunk_data(trunk) + n, (char *)mem_trunk_data(pos->m_trunk) + pos->m_pos_in_trunk, moveSize); trunk->m_size = n + moveSize; pos->m_trunk->m_size = pos->m_pos_in_trunk; pos->m_trunk = trunk; pos->m_pos_in_trunk = n; pos->m_buffer->m_size += n; return mem_trunk_data(trunk); } }
enum cmd_retval cmd_swap_pane_exec(struct cmd *self, struct cmd_q *cmdq) { #ifdef TMATE_SLAVE return (CMD_RETURN_ERROR); #else struct winlink *src_wl, *dst_wl; struct window *src_w, *dst_w; struct window_pane *tmp_wp, *src_wp, *dst_wp; struct layout_cell *src_lc, *dst_lc; u_int sx, sy, xoff, yoff; dst_w = cmdq->state.tflag.wl->window; dst_wp = cmdq->state.tflag.wp; src_w = cmdq->state.sflag.wl->window; src_wp = cmdq->state.sflag.wp; server_unzoom_window(dst_w); if (args_has(self->args, 'D')) { src_w = dst_w; src_wp = TAILQ_NEXT(dst_wp, entry); if (src_wp == NULL) src_wp = TAILQ_FIRST(&dst_w->panes); } else if (args_has(self->args, 'U')) { src_w = dst_w; src_wp = TAILQ_PREV(dst_wp, window_panes, entry); if (src_wp == NULL) src_wp = TAILQ_LAST(&dst_w->panes, window_panes); } server_unzoom_window(src_w); if (src_wp == dst_wp) return (CMD_RETURN_NORMAL); tmp_wp = TAILQ_PREV(dst_wp, window_panes, entry); TAILQ_REMOVE(&dst_w->panes, dst_wp, entry); TAILQ_REPLACE(&src_w->panes, src_wp, dst_wp, entry); if (tmp_wp == src_wp) tmp_wp = dst_wp; if (tmp_wp == NULL) TAILQ_INSERT_HEAD(&dst_w->panes, src_wp, entry); else TAILQ_INSERT_AFTER(&dst_w->panes, tmp_wp, src_wp, entry); src_lc = src_wp->layout_cell; dst_lc = dst_wp->layout_cell; src_lc->wp = dst_wp; dst_wp->layout_cell = src_lc; dst_lc->wp = src_wp; src_wp->layout_cell = dst_lc; src_wp->window = dst_w; dst_wp->window = src_w; sx = src_wp->sx; sy = src_wp->sy; xoff = src_wp->xoff; yoff = src_wp->yoff; src_wp->xoff = dst_wp->xoff; src_wp->yoff = dst_wp->yoff; window_pane_resize(src_wp, dst_wp->sx, dst_wp->sy); dst_wp->xoff = xoff; dst_wp->yoff = yoff; window_pane_resize(dst_wp, sx, sy); if (!args_has(self->args, 'd')) { if (src_w != dst_w) { window_set_active_pane(src_w, dst_wp); window_set_active_pane(dst_w, src_wp); } else { tmp_wp = dst_wp; if (!window_pane_visible(tmp_wp)) tmp_wp = src_wp; window_set_active_pane(src_w, tmp_wp); } } else { if (src_w->active == src_wp) window_set_active_pane(src_w, dst_wp); if (dst_w->active == dst_wp) window_set_active_pane(dst_w, src_wp); } if (src_w != dst_w) { if (src_w->last == src_wp) src_w->last = NULL; if (dst_w->last == dst_wp) dst_w->last = NULL; } server_redraw_window(src_w); server_redraw_window(dst_w); return (CMD_RETURN_NORMAL); #endif }
static connman_service_t * update_service(GVariant *v, const char *path, connman_service_t *after) { connman_service_t *cs = connman_service_find(path); if(cs == NULL) { GError *err = NULL; GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START , NULL, "net.connman", path, "net.connman.Service", NULL, &err); if(proxy == NULL) { TRACE(TRACE_ERROR, "CONNMAN", "Unable to connect to service %s -- %s", path, err->message); g_error_free(err); return NULL; } cs = calloc(1, sizeof(connman_service_t)); cs->cs_refcount = 1; cs->cs_proxy = proxy; if(after == NULL) { TAILQ_INSERT_HEAD(&connman_services, cs, cs_link); } else { TAILQ_INSERT_AFTER(&connman_services, after, cs, cs_link); } connman_service_t *next = TAILQ_NEXT(cs, cs_link); cs->cs_prop = prop_create_root(path); cs->cs_path = strdup(path); cs->cs_sub = prop_subscribe(0, PROP_TAG_CALLBACK_EVENT, connman_service_event, cs, PROP_TAG_ROOT, cs->cs_prop, PROP_TAG_COURIER, connman_courier, NULL); g_signal_connect(G_OBJECT(cs->cs_proxy), "g-signal", G_CALLBACK(connman_svc_signal), cs); // Insert at correct position if(prop_set_parent_ex(cs->cs_prop, service_nodes, next ? next->cs_prop : NULL, NULL)) abort(); prop_t *m = prop_create(cs->cs_prop, "metadata"); prop_link(prop_create(m, "name"), prop_create(m, "title")); prop_set(cs->cs_prop, "type", PROP_SET_STRING, "network"); } else { // Possibly move TAILQ_REMOVE(&connman_services, cs, cs_link); if(after == NULL) { TAILQ_INSERT_HEAD(&connman_services, cs, cs_link); } else { TAILQ_INSERT_AFTER(&connman_services, after, cs, cs_link); } connman_service_t *next = TAILQ_NEXT(cs, cs_link); prop_move(cs->cs_prop, next ? next->cs_prop : NULL); } // Update metadata prop_set_from_vardict(v, prop_create(cs->cs_prop, "metadata")); GVariant *name = g_variant_lookup_value(v, "Name", NULL); const gchar *val = name ? g_variant_get_string(name, NULL) : NULL; if(val) mystrset(&cs->cs_name, val); return cs; }
/* * Try to reuse a vnode from the free list. This function is somewhat * advisory in that NULL can be returned as a normal case, even if free * vnodes are present. * * The scan is limited because it can result in excessive CPU use during * periods of extreme vnode use. * * NOTE: The returned vnode is not completely initialized. */ static struct vnode * cleanfreevnode(int maxcount) { struct vnode *vp; int count; int trigger = (long)vmstats.v_page_count / (activevnodes * 2 + 1); /* * Try to deactivate some vnodes cached on the active list. */ if (countcachedvnodes(0) < inactivevnodes) goto skip; for (count = 0; count < maxcount * 2; count++) { spin_lock(&vfs_spin); vp = TAILQ_NEXT(&vnode_active_rover, v_list); TAILQ_REMOVE(&vnode_active_list, &vnode_active_rover, v_list); if (vp == NULL) { TAILQ_INSERT_HEAD(&vnode_active_list, &vnode_active_rover, v_list); } else { TAILQ_INSERT_AFTER(&vnode_active_list, vp, &vnode_active_rover, v_list); } if (vp == NULL) { spin_unlock(&vfs_spin); continue; } if ((vp->v_refcnt & VREF_MASK) != 0) { spin_unlock(&vfs_spin); vp->v_act += VACT_INC; if (vp->v_act > VACT_MAX) /* SMP race ok */ vp->v_act = VACT_MAX; continue; } /* * decrement by less if the vnode's object has a lot of * VM pages. XXX possible SMP races. */ if (vp->v_act > 0) { vm_object_t obj; if ((obj = vp->v_object) != NULL && obj->resident_page_count >= trigger) { vp->v_act -= 1; } else { vp->v_act -= VACT_INC; } if (vp->v_act < 0) vp->v_act = 0; spin_unlock(&vfs_spin); continue; } /* * Try to deactivate the vnode. */ if ((atomic_fetchadd_int(&vp->v_refcnt, 1) & VREF_MASK) == 0) atomic_add_int(&mycpu->gd_cachedvnodes, -1); atomic_set_int(&vp->v_refcnt, VREF_FINALIZE); spin_unlock(&vfs_spin); vrele(vp); } skip: /* * Loop trying to lock the first vnode on the free list. * Cycle if we can't. */ for (count = 0; count < maxcount; count++) { spin_lock(&vfs_spin); vp = TAILQ_FIRST(&vnode_inactive_list); if (vp == NULL) { spin_unlock(&vfs_spin); break; } /* * non-blocking vx_get will also ref the vnode on success. */ if (vx_get_nonblock(vp)) { KKASSERT(vp->v_state == VS_INACTIVE); TAILQ_REMOVE(&vnode_inactive_list, vp, v_list); TAILQ_INSERT_TAIL(&vnode_inactive_list, vp, v_list); spin_unlock(&vfs_spin); continue; } /* * Because we are holding vfs_spin the vnode should currently * be inactive and VREF_TERMINATE should still be set. * * Once vfs_spin is released the vnode's state should remain * unmodified due to both the lock and ref on it. */ KKASSERT(vp->v_state == VS_INACTIVE); spin_unlock(&vfs_spin); #ifdef TRACKVNODE if ((u_long)vp == trackvnode) kprintf("cleanfreevnode %p %08x\n", vp, vp->v_flag); #endif /* * Do not reclaim/reuse a vnode while auxillary refs exists. * This includes namecache refs due to a related ncp being * locked or having children, a VM object association, or * other hold users. * * Do not reclaim/reuse a vnode if someone else has a real * ref on it. This can occur if a filesystem temporarily * releases the vnode lock during VOP_RECLAIM. */ if (vp->v_auxrefs || (vp->v_refcnt & ~VREF_FINALIZE) != VREF_TERMINATE + 1) { failed: if (vp->v_state == VS_INACTIVE) { spin_lock(&vfs_spin); if (vp->v_state == VS_INACTIVE) { TAILQ_REMOVE(&vnode_inactive_list, vp, v_list); TAILQ_INSERT_TAIL(&vnode_inactive_list, vp, v_list); } spin_unlock(&vfs_spin); } vx_put(vp); continue; } /* * VINACTIVE and VREF_TERMINATE are expected to both be set * for vnodes pulled from the inactive list, and cannot be * changed while we hold the vx lock. * * Try to reclaim the vnode. */ KKASSERT(vp->v_flag & VINACTIVE); KKASSERT(vp->v_refcnt & VREF_TERMINATE); if ((vp->v_flag & VRECLAIMED) == 0) { if (cache_inval_vp_nonblock(vp)) goto failed; vgone_vxlocked(vp); /* vnode is still VX locked */ } /* * At this point if there are no other refs or auxrefs on * the vnode with the inactive list locked, and we remove * the vnode from the inactive list, it should not be * possible for anyone else to access the vnode any more. * * Since the vnode is in a VRECLAIMED state, no new * namecache associations could have been made and the * vnode should have already been removed from its mountlist. * * Since we hold a VX lock on the vnode it cannot have been * reactivated (moved out of the inactive list). */ KKASSERT(TAILQ_EMPTY(&vp->v_namecache)); spin_lock(&vfs_spin); if (vp->v_auxrefs || (vp->v_refcnt & ~VREF_FINALIZE) != VREF_TERMINATE + 1) { spin_unlock(&vfs_spin); goto failed; } KKASSERT(vp->v_state == VS_INACTIVE); TAILQ_REMOVE(&vnode_inactive_list, vp, v_list); --inactivevnodes; vp->v_state = VS_DYING; spin_unlock(&vfs_spin); /* * Nothing should have been able to access this vp. Only * our ref should remain now. */ atomic_clear_int(&vp->v_refcnt, VREF_TERMINATE|VREF_FINALIZE); KASSERT(vp->v_refcnt == 1, ("vp %p badrefs %08x", vp, vp->v_refcnt)); /* * Return a VX locked vnode suitable for reuse. */ return(vp); } return(NULL); }