int
main(int argc, char **argv)
{
	struct node *tmp, *ins;
	int i, max, min;

	SPLAY_INIT(&root);

	for (i = 0; i < ITER; i++) {
		tmp = malloc(sizeof(struct node));
		if (tmp == NULL) err(1, "malloc");
		do {
			tmp->key = arc4random_uniform(MAX-MIN);
			tmp->key += MIN;
		} while (SPLAY_FIND(tree, &root, tmp) != NULL);
		if (i == 0)
			max = min = tmp->key;
		else {
			if (tmp->key > max)
				max = tmp->key;
			if (tmp->key < min)
				min = tmp->key;
		}
		if (SPLAY_INSERT(tree, &root, tmp) != NULL)
			errx(1, "SPLAY_INSERT failed");
	}

	ins = SPLAY_MIN(tree, &root);
	if (ins->key != min)
		errx(1, "min does not match");
	tmp = ins;
	ins = SPLAY_MAX(tree, &root);
	if (ins->key != max)
		errx(1, "max does not match");

	if (SPLAY_REMOVE(tree, &root, tmp) != tmp)
		errx(1, "SPLAY_REMOVE failed");

	for (i = 0; i < ITER - 1; i++) {
		tmp = SPLAY_ROOT(&root);
		if (tmp == NULL)
			errx(1, "SPLAY_ROOT error");
		if (SPLAY_REMOVE(tree, &root, tmp) != tmp)
			errx(1, "SPLAY_REMOVE error");
		free(tmp);
	}

	exit(0);
}
Exemple #2
0
int
cmd_unbind_key_exec(struct cmd *self, unused struct cmd_ctx *ctx)
{
	struct args		*args = self->args;
	struct key_binding	*bd;
	int			 key;

	if (args_has(args, 'a')) {
		while (!SPLAY_EMPTY(&key_bindings)) {
			bd = SPLAY_ROOT(&key_bindings);
			SPLAY_REMOVE(key_bindings, &key_bindings, bd);
			cmd_list_free(bd->cmdlist);
			xfree(bd);
		}
		return (0);
	}

	key = key_string_lookup_string(args->argv[0]);
	if (key == KEYC_NONE) {
		ctx->error(ctx, "unknown key: %s", args->argv[0]);
		return (-1);
	}

	if (args_has(args, 't'))
		return (cmd_unbind_key_table(self, ctx, key));

	if (!args_has(args, 'n'))
		key |= KEYC_PREFIX;
	key_bindings_remove(key);
	return (0);
}
Exemple #3
0
struct policy *
systrace_findpolicy_wildcard(const char *name)
{
    struct policy tmp, *res;
    static char path[PATH_MAX], lookup[PATH_MAX];

    if (strlcpy(path, name, sizeof(path)) >= sizeof(path))
        errx(1, "%s: path name overflow", __func__);

    strlcpy(lookup, "*/", sizeof(lookup));
    strlcat(lookup, basename(path), sizeof(lookup));

    tmp.name = lookup;
    res = SPLAY_FIND(policytree, &policyroot, &tmp);
    if (res == NULL)
        return (NULL);

    /* we found the wildcarded policy; now remove it and bind it */
    SPLAY_REMOVE(policytree, &policyroot, res);

    free((char *)res->name);
    if ((res->name = strdup(name)) == NULL)
        err(1, "%s: strdup", __func__);

    SPLAY_INSERT(policytree, &policyroot, res);
    return (res);
}
Exemple #4
0
int
dict_iterfrom(struct dict *d, void **hdl, const char *kfrom, const char **k,
    void **data)
{
	struct dictentry *curr = *hdl, key;

	if (curr == NULL) {
		if (kfrom == NULL)
			curr = SPLAY_MIN(_dict, &d->dict);
		else {
			key.key = kfrom;
			curr = SPLAY_FIND(_dict, &d->dict, &key);
			if (curr == NULL) {
				SPLAY_INSERT(_dict, &d->dict, &key);
				curr = SPLAY_NEXT(_dict, &d->dict, &key);
				SPLAY_REMOVE(_dict, &d->dict, &key);
			}
		}
	} else
		curr = SPLAY_NEXT(_dict, &d->dict, curr);

	if (curr) {
		*hdl = curr;
		if (k)
			*k = curr->key;
		if (data)
			*data = curr->data;
		return (1);
	}

	return (0);
}
Exemple #5
0
void
systrace_cleanpolicy(struct policy *policy)
{
    struct filter *filter;
    struct policy_syscall *pflq;
    int i;

    /* Set the default policy to ask */
    for (i = 0; i < INTERCEPT_MAXSYSCALLNR; i++)
        policy->kerneltable[i] = ICPOLICY_ASK;

    while ((filter = TAILQ_FIRST(&policy->prefilters)) != NULL) {
        TAILQ_REMOVE(&policy->prefilters, filter, policy_next);
        filter_free(filter);
    }

    while ((filter = TAILQ_FIRST(&policy->filters)) != NULL) {
        TAILQ_REMOVE(&policy->filters, filter, policy_next);
        filter_free(filter);
    }

    while ((pflq = SPLAY_ROOT(&policy->pflqs)) != NULL) {
        SPLAY_REMOVE(syscalltree, &policy->pflqs, pflq);

        while ((filter = TAILQ_FIRST(&pflq->flq)) != NULL) {
            TAILQ_REMOVE(&pflq->flq, filter, next);
            filter_free(filter);
        }

        free(pflq);
    }
}
Exemple #6
0
int
tree_iterfrom(struct tree *t, void **hdl, uint64_t k, uint64_t *id, void **data)
{
	struct treeentry *curr = *hdl, key;

	if (curr == NULL) {
		if (k == 0)
			curr = SPLAY_MIN(_tree, &t->tree);
		else {
			key.id = k;
			curr = SPLAY_FIND(_tree, &t->tree, &key);
			if (curr == NULL) {
				SPLAY_INSERT(_tree, &t->tree, &key);
				curr = SPLAY_NEXT(_tree, &t->tree, &key);
				SPLAY_REMOVE(_tree, &t->tree, &key);
			}
		}
	} else
		curr = SPLAY_NEXT(_tree, &t->tree, curr);

	if (curr) {
		*hdl = curr;
		if (id)
			*id = curr->id;
		if (data)
			*data = curr->data;
		return (1);
	}

	return (0);
}
Exemple #7
0
void
personality_free(struct personality *pers)
{
	SPLAY_REMOVE(perstree, &personalities, pers);

	free(pers->name);
	free(pers);
}
Exemple #8
0
void
systrace_freepolicy(struct policy *policy)
{
    if (policy->flags & POLICY_CHANGED) {
        if (systrace_writepolicy(policy) == -1)
            fprintf(stderr, "Failed to write policy for %s\n",
                    policy->name);
    }

    systrace_cleanpolicy(policy);

    SPLAY_REMOVE(policytree, &policyroot, policy);
    if (policy->policynr != -1)
        SPLAY_REMOVE(polnrtree, &polnrroot, policy);

    free((char *)policy->name);
    free(policy);
}
void
key_bindings_remove(int key)
{
	struct key_binding	*bd;

	if ((bd = key_bindings_lookup(key)) == NULL)
		return;
	SPLAY_REMOVE(key_bindings, &key_bindings, bd);
	SPLAY_INSERT(key_bindings, &dead_key_bindings, bd);
}
Exemple #10
0
void
filter_free(struct filtertree *filters)
{
	struct filter *filter;

	while ((filter = SPLAY_ROOT(filters)) != NULL) {
		SPLAY_REMOVE(filtertree, filters, filter);
		free(filter);
	}
	free(filters);
}
Exemple #11
0
void
tree_merge(struct tree *dst, struct tree *src)
{
	struct treeentry	*entry;

	while (!SPLAY_EMPTY(src)) {
		entry = SPLAY_ROOT(src);
		SPLAY_REMOVE(tree, src, entry);
		if (SPLAY_INSERT(tree, dst, entry))
			errx(1, "tree_merge: duplicate");
	}
}
Exemple #12
0
static void
mta_route_free(struct mta_route *route)
{
	log_trace(TRACE_MTA, "mta: freeing %s", mta_route_to_text(route));
	SPLAY_REMOVE(mta_route_tree, &routes, route);
	free(route->hostname);
	if (route->cert)
		free(route->cert);
	if (route->auth)
		free(route->auth);
	free(route);
}
void
key_bindings_clean(void)
{
	struct key_binding	*bd;

	while (!SPLAY_EMPTY(&dead_key_bindings)) {
		bd = SPLAY_ROOT(&dead_key_bindings);
		SPLAY_REMOVE(key_bindings, &dead_key_bindings, bd);
		cmd_list_free(bd->cmdlist);
		xfree(bd);
	}
}
Exemple #14
0
void
session_destroy(struct session *s, const char * reason)
{
	uint32_t msgid;

	log_debug("smtp: %p: deleting session: %s", s, reason);

	if (s->s_flags & F_ZOMBIE)
		goto finalize;

	log_debug("session_destroy: s->datafp = %p", s->datafp);
	if (s->datafp != NULL)
		fclose(s->datafp);

	if (s->s_msg.id != 0 && s->s_state != S_DONE) {
		msgid = evpid_to_msgid(s->s_msg.id);
		imsg_compose_event(env->sc_ievs[PROC_QUEUE],
		    IMSG_QUEUE_REMOVE_MESSAGE, 0, 0, -1, &msgid, sizeof(msgid));
	}

	if (s->s_io.ssl) {
		if (s->s_l->flags & F_SMTPS)
			if (s->s_flags & F_SECURE)
				stat_decrement("smtp.smtps", 1);
		if (s->s_l->flags & F_STARTTLS)
			if (s->s_flags & F_SECURE)
				stat_decrement("smtp.tls", 1);
	}

	event_del(&s->s_ev); /* in case something was scheduled */
	io_clear(&s->s_io);
	iobuf_clear(&s->s_iobuf);

	/* resume when session count decreases to 95% */
	stat_decrement("smtp.session", 1);

	/* If the session is waiting for an imsg, do not kill it now, since
	 * the id must still be valid.
	 */
	if (s->s_flags & F_WAITIMSG) {
		s->s_flags = F_ZOMBIE;
		return;
	}

    finalize:

	smtp_destroy(s);

	SPLAY_REMOVE(sessiontree, &env->sc_sessions, s);
	bzero(s, sizeof(*s));
	free(s);
}
void i915_gem_context_close(struct drm_device *dev, struct drm_file *file)
{
	struct drm_i915_file_private *file_priv = file->driver_priv;
	struct i915_ctx_handle *han, *nxt;

	for (han = SPLAY_MIN(i915_ctx_tree, &file_priv->ctx_tree); han != NULL;
	    han = nxt) {
		nxt = SPLAY_NEXT(i915_ctx_tree, &file_priv->ctx_tree, han);
		context_idr_cleanup(han->handle, han->ctx, NULL);
		SPLAY_REMOVE(i915_ctx_tree, &file_priv->ctx_tree, han);
		free(han, M_DRM);
	}
}
Exemple #16
0
void
aux_free(void *arg)
{
	struct aux *aux = arg;
	struct auxtree *tree = &aux->tree;
	struct auxkey *key;

	while ((key = SPLAY_ROOT(tree)) != NULL) {
		SPLAY_REMOVE(auxtree, tree, key);
		free(key);
	}
	free(arg);
}
Exemple #17
0
void
dict_merge(struct dict *dst, struct dict *src)
{
	struct dictentry	*entry;

	while (!SPLAY_EMPTY(&src->dict)) {
		entry = SPLAY_ROOT(&src->dict);
		SPLAY_REMOVE(_dict, &src->dict, entry);
		if (SPLAY_INSERT(_dict, &dst->dict, entry))
			errx(1, "dict_merge: duplicate");
	}
	dst->count += src->count;
	src->count = 0;
}
Exemple #18
0
void
options_remove(struct options *oo, const char *name)
{
	struct options_entry	*o;

	if ((o = options_find1(oo, name)) == NULL)
		return;

	SPLAY_REMOVE(options_tree, &oo->tree, o);
	xfree(o->name);
	if (o->type == OPTIONS_STRING)
		xfree(o->value.string);
	xfree(o);
}
Exemple #19
0
void
options_free(struct options *oo)
{
	struct options_entry	*o;

	while (!SPLAY_EMPTY(&oo->tree)) {
		o = SPLAY_ROOT(&oo->tree);
		SPLAY_REMOVE(options_tree, &oo->tree, o);
		xfree(o->name);
		if (o->type == OPTIONS_STRING)
			xfree(o->value.string);
		xfree(o);
	}
}
Exemple #20
0
void
mode_key_free_trees(void)
{
	const struct mode_key_table	*mtab;
	struct mode_key_binding		*mbind;

	for (mtab = mode_key_tables; mtab->name != NULL; mtab++) {
		while (!SPLAY_EMPTY(mtab->tree)) {
			mbind = SPLAY_ROOT(mtab->tree);
			SPLAY_REMOVE(mode_key_tree, mtab->tree, mbind);
			xfree(mbind);
		}
	}
}
Exemple #21
0
void
tree_merge(struct tree *dst, struct tree *src)
{
	struct treeentry	*entry;

	while (!SPLAY_EMPTY(&src->tree)) {
		entry = SPLAY_ROOT(&src->tree);
		SPLAY_REMOVE(_tree, &src->tree, entry);
		if (SPLAY_INSERT(_tree, &dst->tree, entry))
			errx(1, "tree_merge: duplicate");
	}
	dst->count += src->count;
	src->count = 0;
}
Exemple #22
0
void
purge_config(uint8_t what)
{
	struct listener	*l;
	struct map	*m;
	struct rule	*r;
	struct ssl	*s;
	struct mapel	*me;

	if (what & PURGE_LISTENERS) {
		while ((l = TAILQ_FIRST(env->sc_listeners)) != NULL) {
			TAILQ_REMOVE(env->sc_listeners, l, entry);
			free(l);
		}
		free(env->sc_listeners);
		env->sc_listeners = NULL;
	}
	if (what & PURGE_MAPS) {
		while ((m = TAILQ_FIRST(env->sc_maps)) != NULL) {
			TAILQ_REMOVE(env->sc_maps, m, m_entry);
			while ((me = TAILQ_FIRST(&m->m_contents))) {
				TAILQ_REMOVE(&m->m_contents, me, me_entry);
				free(me);
			}
			free(m);
		}
		free(env->sc_maps);
		env->sc_maps = NULL;
	}
	if (what & PURGE_RULES) {
		while ((r = TAILQ_FIRST(env->sc_rules)) != NULL) {
			TAILQ_REMOVE(env->sc_rules, r, r_entry);
			free(r);
		}
		free(env->sc_rules);
		env->sc_rules = NULL;
	}
	if (what & PURGE_SSL) {
		while ((s = SPLAY_ROOT(env->sc_ssl)) != NULL) {
			SPLAY_REMOVE(ssltree, env->sc_ssl, s);
			free(s->ssl_cert);
			free(s->ssl_key);
			free(s);
		}
		free(env->sc_ssl);
		env->sc_ssl = NULL;
	}
}
Exemple #23
0
void *
tree_pop(struct tree *t, uint64_t id)
{
	struct treeentry	key, *entry;
	void			*data;

	key.id = id;
	if ((entry = SPLAY_FIND(tree, t, &key)) == NULL)
		return (NULL);

	data = entry->data;
	SPLAY_REMOVE(tree, t, entry);
	free(entry);

	return (data);
}
Exemple #24
0
void *
tree_xpop(struct tree *t, uint64_t id)
{
	struct treeentry	key, *entry;
	void			*data;

	key.id = id;
	if ((entry = SPLAY_FIND(tree, t, &key)) == NULL)
		errx(1, "tree_xpop(%p, 0x%016" PRIx64 ")", t, id);

	data = entry->data;
	SPLAY_REMOVE(tree, t, entry);
	free(entry);

	return (data);
}
Exemple #25
0
int
tree_poproot(struct tree *t, uint64_t *id, void **data)
{
	struct treeentry	*entry;

	entry = SPLAY_ROOT(t);
	if (entry == NULL)
		return (0);
	if (id)
		*id = entry->id;
	if (data)
		*data = entry->data;
	SPLAY_REMOVE(tree, t, entry);
	free(entry);
	return (1);
}
Exemple #26
0
void
options_remove(struct options *oo, const char *name)
{
	struct options_entry	*o;

	if ((o = options_find1(oo, name)) == NULL)
		return;

	SPLAY_REMOVE(options_tree, &oo->tree, o);
	xfree(o->name);
	if (o->type == OPTIONS_STRING)
		xfree(o->str);
	else if (o->type == OPTIONS_DATA)
		o->freefn(o->data);
	xfree(o);
}
Exemple #27
0
int
dict_poproot(struct dict *d, void **data)
{
	struct dictentry	*entry;

	entry = SPLAY_ROOT(&d->dict);
	if (entry == NULL)
		return (0);
	if (data)
		*data = entry->data;
	SPLAY_REMOVE(_dict, &d->dict, entry);
	free(entry);
	d->count -= 1;

	return (1);
}
Exemple #28
0
void
options_free(struct options *oo)
{
	struct options_entry	*o;

	while (!SPLAY_EMPTY(&oo->tree)) {
		o = SPLAY_ROOT(&oo->tree);
		SPLAY_REMOVE(options_tree, &oo->tree, o);
		xfree(o->name);
		if (o->type == OPTIONS_STRING)
			xfree(o->str);
		else if (o->type == OPTIONS_DATA)
			o->freefn(o->data);
		xfree(o);
	}
}
static void do_destroy(struct i915_hw_context *ctx)
{
	struct drm_device *dev = ctx->obj->base.dev;
	struct drm_i915_private *dev_priv = dev->dev_private;

	if (ctx->file_priv) {
		struct i915_ctx_handle *han, find;
		find.handle = ctx->id;
		han = SPLAY_FIND(i915_ctx_tree, &ctx->file_priv->ctx_tree, &find);
		if (han != NULL)
			SPLAY_REMOVE(i915_ctx_tree, &ctx->file_priv->ctx_tree, han);
	} else
		BUG_ON(ctx != dev_priv->ring[RCS].default_context);

	drm_gem_object_unreference(&ctx->obj->base);
	kfree(ctx);
}
Exemple #30
0
void
waitq_run(void *tag, void *result)
{
	struct waitq	*wq, key;
	struct waiter	*w;

	key.tag = tag;
	wq = SPLAY_FIND(waitqtree, &waitqs, &key);
	SPLAY_REMOVE(waitqtree, &waitqs, wq);

	while((w = TAILQ_FIRST(&wq->waiters))) {
		TAILQ_REMOVE(&wq->waiters, w, entry);
		w->cb(tag, w->arg, result);
		free(w);
	}
	free(wq);
}