Example #1
0
int tl_event_node_insert(struct tl_event_node * node) {
    struct tl_event_node * insertPos;
    tl_manage_t tm;

    assert(node);
    assert(node->m_event.m_tl);
    assert(node->m_event.m_tl->m_manage);

    tm = node->m_event.m_tl->m_manage;

    insertPos = TAILQ_FIRST(&tm->m_event_queue);
    while(insertPos != TAILQ_END(&tm->m_event_queue)
          && insertPos->m_execute_time <= node->m_execute_time)
    {
        insertPos = TAILQ_NEXT(insertPos, m_next);
    }

    if (insertPos == TAILQ_END(&tm->m_event_queue)) {
        TAILQ_INSERT_TAIL(&tm->m_event_queue, node, m_next);
    }
    else {
        TAILQ_INSERT_BEFORE(insertPos, node, m_next);
    }

    node->m_state = tl_event_node_state_in_event_queue;

    return 0;
}
Example #2
0
void
pmcstat_image_link(struct pmcstat_process *pp, struct pmcstat_image *image,
    uintfptr_t start)
{
	struct pmcstat_pcmap *pcm, *pcmnew;
	uintfptr_t offset;

	assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN &&
	    image->pi_type != PMCSTAT_IMAGE_INDETERMINABLE);

	if ((pcmnew = malloc(sizeof(*pcmnew))) == NULL)
		err(EX_OSERR, "ERROR: Cannot create a map entry");

	/*
	 * Adjust the map entry to only cover the text portion
	 * of the object.
	 */

	offset = start - image->pi_vaddr;
	pcmnew->ppm_lowpc  = image->pi_start + offset;
	pcmnew->ppm_highpc = image->pi_end + offset;
	pcmnew->ppm_image  = image;

	assert(pcmnew->ppm_lowpc < pcmnew->ppm_highpc);

	/* Overlapped mmap()'s are assumed to never occur. */
	TAILQ_FOREACH(pcm, &pp->pp_map, ppm_next)
	    if (pcm->ppm_lowpc >= pcmnew->ppm_highpc)
		    break;

	if (pcm == NULL)
		TAILQ_INSERT_TAIL(&pp->pp_map, pcmnew, ppm_next);
	else
		TAILQ_INSERT_BEFORE(pcm, pcmnew, ppm_next);
}
Example #3
0
void
_waitq_insert(pthread_t pthread)
{
	pthread_t tid;

	/*
	 * Make some assertions when debugging is enabled:
	 */
	_PQ_ASSERT_INACTIVE("_waitq_insert: pq_active");
	_PQ_SET_ACTIVE();
	_PQ_ASSERT_NOT_QUEUED(pthread, "_waitq_insert: Already in queue");

	if (pthread->wakeup_time.tv_sec == -1)
		TAILQ_INSERT_TAIL(&_waitingq, pthread, pqe);
	else {
		tid = TAILQ_FIRST(&_waitingq);
		while ((tid != NULL) && (tid->wakeup_time.tv_sec != -1) && 
		    ((tid->wakeup_time.tv_sec < pthread->wakeup_time.tv_sec) ||
		    ((tid->wakeup_time.tv_sec == pthread->wakeup_time.tv_sec) &&
		    (tid->wakeup_time.tv_nsec <= pthread->wakeup_time.tv_nsec))))
			tid = TAILQ_NEXT(tid, pqe);
		if (tid == NULL)
			TAILQ_INSERT_TAIL(&_waitingq, pthread, pqe);
		else
			TAILQ_INSERT_BEFORE(tid, pthread, pqe);
	}
	pthread->flags |= PTHREAD_FLAGS_IN_WAITQ;

	_PQ_CLEAR_ACTIVE();
}
Example #4
0
static void
timer_list_insert_into_list(struct timer_list *tlist, struct timer_list_entry *new_entry)
{
	struct timer_list_entry *entry;

	/*
	 * This can overflow and it's not a problem
	 */
	new_entry->expire_time = new_entry->epoch + PR_MillisecondsToInterval(new_entry->interval);

	entry = TAILQ_FIRST(&tlist->list);
	while (entry != NULL) {
		if (timer_list_entry_cmp(entry, new_entry, new_entry->epoch) > 0) {
			/*
			 * Insert new entry right before current entry
			 */
			TAILQ_INSERT_BEFORE(entry, new_entry, entries);

			break;
		}

		entry = TAILQ_NEXT(entry, entries);
	}

	if (entry == NULL) {
		TAILQ_INSERT_TAIL(&tlist->list, new_entry, entries);
	}
}
Example #5
0
static inline void put_entry_d_sorted(int row, int col, double value, size_t pos, queue_d *queue, cstuff_DirectionMap *obj, entry_d **waiting) {
	entry_d *entry, *entry2, *entry3;

	entry = waiting[pos];
	if (entry) {
		TAILQ_REMOVE(queue, entry, hook);
	} else {
		entry = get_entry_d(row, col, value);
		waiting[pos] = entry;
	}

	entry->row = row;
	entry->col = col;
	entry->value = value;
	obj->map->values[pos] = value;

	if (TAILQ_EMPTY(queue) || TAILQ_LAST(queue, _queue_d)->value <= value) {
		TAILQ_INSERT_TAIL(queue, entry, hook);
	} else if (TAILQ_FIRST(queue)->value > value) {
		TAILQ_INSERT_HEAD(queue, entry, hook);
	} else {
		entry2 = TAILQ_LAST(queue, _queue_d);
		TAILQ_FOREACH_REVERSE(entry3, queue, _queue_d, hook) {
			if (entry3->value > value) {
				entry2 = entry3;
			} else {
				break;
			}
		}
		TAILQ_INSERT_BEFORE(entry2, entry, hook);
	}
}
Example #6
0
static JSBool 
js_item_moveBefore(JSContext *cx, JSObject *obj,
		   uintN argc, jsval *argv, jsval *rval)
{
  js_item_t *ji = JS_GetPrivate(cx, obj);
  js_item_t *before;

  if(argc >= 1 && JSVAL_IS_OBJECT(argv[0]) &&
     !JSVAL_IS_NULL(argv[0]) && 
     JS_GetClass(cx, JSVAL_TO_OBJECT(argv[0])) == &item_class) {
    before = JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));
  } else {
    before = NULL;
  }

  TAILQ_REMOVE(&ji->ji_model->jm_items, ji, ji_link);
  if(before)
    TAILQ_INSERT_BEFORE(before, ji, ji_link);
  else
    TAILQ_INSERT_TAIL(&ji->ji_model->jm_items, ji, ji_link);

  prop_move(ji->ji_root, before ? before->ji_root : NULL);
  *rval = JSVAL_VOID;
  return JS_TRUE;
}
Example #7
0
static void
pq_insert_prio_list(pq_queue_t *pq, int prio)
{
	pq_list_t *pql;

	/*
	 * Make some assertions when debugging is enabled:
	 */
	PQ_ASSERT_ACTIVE(pq, "pq_insert_prio_list: pq_active");

	/*
	 * The priority queue is in descending priority order.  Start at
	 * the beginning of the queue and find the list before which the
	 * new list should be inserted.
	 */
	pql = TAILQ_FIRST(&pq->pq_queue);
	while ((pql != NULL) && (pql->pl_prio > prio))
		pql = TAILQ_NEXT(pql, pl_link);

	/* Insert the list: */
	if (pql == NULL)
		TAILQ_INSERT_TAIL(&pq->pq_queue, &pq->pq_lists[prio], pl_link);
	else
		TAILQ_INSERT_BEFORE(pql, &pq->pq_lists[prio], pl_link);

	/* Mark this list as being in the queue: */
	pq->pq_lists[prio].pl_queued = 1;
}
Example #8
0
/* Add/insert a new component to the module search path */
static void
addpath(struct pathhead *pathq, char *path, int force, int insert)
{
	struct pathentry *pe, *pskip;
	char pathbuf[MAXPATHLEN+1];
	size_t len;
	static unsigned added = 0;
	unsigned i;

	/*
	 * If the path exists, use it; otherwise, take the user-specified
	 * path at face value - may be a removed directory.
	 */
	if (realpath(path, pathbuf) == NULL)
		strlcpy(pathbuf, path, sizeof(pathbuf));

	len = strlen(pathbuf);
#ifdef NEED_SLASHTERM
	/* slash-terminate, because the kernel linker said so. */
	if ((len == 0) || (pathbuf[len-1] != '/')) {
		if (len == sizeof(pathbuf) - 1)
			errx(1, "path too long: %s", pathbuf);
		pathbuf[len] = '/';
	}
#else  /* NEED_SLASHTERM */
	/* remove a terminating slash if present */
	if ((len > 0) && (pathbuf[len-1] == '/'))
		pathbuf[--len] = '\0';
#endif /* NEED_SLASHTERM */

	/* is it already in there? */
	TAILQ_FOREACH(pe, pathq, next)
		if (!strcmp(pe->path, pathbuf))
			break;
	if (pe != NULL) {
		if (force)
			return;
		errx(1, "already in the module search path: %s", pathbuf);
	}
	
	/* OK, allocate and add it. */
	if (((pe = malloc(sizeof(*pe))) == NULL) ||
	    ((pe->path = strdup(pathbuf)) == NULL)) {
		errno = ENOMEM;
		err(1, "allocating path component");
	}
	if (!insert) {
		TAILQ_INSERT_TAIL(pathq, pe, next);
	} else {
		for (i = 0, pskip = TAILQ_FIRST(pathq); i < added; i++)
			pskip = TAILQ_NEXT(pskip, next);
		if (pskip != NULL)
			TAILQ_INSERT_BEFORE(pskip, pe, next);
		else
			TAILQ_INSERT_TAIL(pathq, pe, next);
		added++;
	}
	changed = 1;
}
Example #9
0
File: net.c Project: jdiego/kore
void
net_send_queue(struct connection *c, void *data, u_int32_t len,
    struct spdy_stream *s, int before)
{
	u_int8_t		*d;
	struct netbuf		*nb;
	u_int32_t		avail;

	kore_debug("net_send_queue(%p, %p, %d, %p, %d)",
	    c, data, len, s, before);

	d = data;
	if (before == NETBUF_LAST_CHAIN) {
		nb = TAILQ_LAST(&(c->send_queue), netbuf_head);
		if (nb != NULL && !(nb->flags & NETBUF_IS_STREAM) &&
		    nb->stream == s && nb->b_len < nb->m_len) {
			avail = nb->m_len - nb->b_len;
			if (len < avail) {
				memcpy(nb->buf + nb->b_len, d, len);
				nb->b_len += len;
				return;
			} else if (len > avail) {
				memcpy(nb->buf + nb->b_len, d, avail);
				nb->b_len += avail;

				len -= avail;
				d += avail;
				if (len == 0)
					return;
			}
		}
	}

	nb = kore_pool_get(&nb_pool);
	nb->flags = 0;
	nb->cb = NULL;
	nb->owner = c;
	nb->s_off = 0;
	nb->stream = s;
	nb->b_len = len;
	nb->type = NETBUF_SEND;

	if (nb->b_len < NETBUF_SEND_PAYLOAD_MAX)
		nb->m_len = NETBUF_SEND_PAYLOAD_MAX;
	else
		nb->m_len = nb->b_len;

	nb->buf = kore_malloc(nb->m_len);
	if (len > 0)
		memcpy(nb->buf, d, nb->b_len);

	if (before == NETBUF_BEFORE_CHAIN) {
		TAILQ_INSERT_BEFORE(c->snb, nb, list);
	} else {
		TAILQ_INSERT_TAIL(&(c->send_queue), nb, list);
	}
}
/* 
 * Insertion is O(n) due to the priority scan, but optimises to O(1)
 * if all priorities are identical.
 *
 * MPSAFE
 */
eventhandler_tag
eventhandler_register(struct eventhandler_list *list, const char *name, 
		      void *func, void *arg, int priority)
{
    struct eventhandler_entry_generic	*eg;
    struct eventhandler_entry		*ep;
    
    lwkt_gettoken(&evlist_token);

    /*
     * find/create the list as needed
     */
    while (list == NULL) {
	list = eventhandler_find_list(name);
	if (list)
		break;
	list = kmalloc(sizeof(struct eventhandler_list) + strlen(name) + 1,
		       M_EVENTHANDLER, M_INTWAIT);
	if (eventhandler_find_list(name)) {
	    kfree(list, M_EVENTHANDLER);
	    list = NULL;
	} else {
	    list->el_flags = 0;
	    list->el_name = (char *)list + sizeof(struct eventhandler_list);
	    strcpy(list->el_name, name);
	    TAILQ_INSERT_HEAD(&eventhandler_lists, list, el_link);
	}
    }

    if (!(list->el_flags & EHE_INITTED)) {
	TAILQ_INIT(&list->el_entries);
	list->el_flags = EHE_INITTED;
    }
    
    /* allocate an entry for this handler, populate it */
    eg = kmalloc(sizeof(struct eventhandler_entry_generic),
		M_EVENTHANDLER, M_INTWAIT);
    eg->func = func;
    eg->ee.ee_arg = arg;
    eg->ee.ee_priority = priority;
    
    /* sort it into the list */
    for (ep = TAILQ_FIRST(&list->el_entries);
	 ep != NULL; 
	 ep = TAILQ_NEXT(ep, ee_link)) {
	if (eg->ee.ee_priority < ep->ee_priority) {
	    TAILQ_INSERT_BEFORE(ep, &eg->ee, ee_link);
	    break;
	}
    }
    if (ep == NULL)
	TAILQ_INSERT_TAIL(&list->el_entries, &eg->ee, ee_link);
    lwkt_reltoken(&evlist_token);

    return(&eg->ee);
}
Example #11
0
static bool vm_map_insert_entry(vm_map_t *vm_map, vm_map_entry_t *entry) {
  rw_assert(&vm_map->rwlock, RW_WLOCKED);
  if (!SPLAY_INSERT(vm_map_tree, &vm_map->tree, entry)) {
    vm_map_entry_t *next = SPLAY_NEXT(vm_map_tree, &vm_map->tree, entry);
    if (next)
      TAILQ_INSERT_BEFORE(next, entry, map_list);
    else
      TAILQ_INSERT_TAIL(&vm_map->list, entry, map_list);
    vm_map->nentries++;
    return true;
  }
  return false;
}
Example #12
0
u_int16_t
_name2id(struct n2id_labels *head, const char *name)
{
	struct n2id_label	*label, *p = NULL;
	u_int16_t		 new_id = 1;

	if (!name[0]) {
		errno = EINVAL;
		return (0);
	}

	TAILQ_FOREACH(label, head, entry)
		if (strcmp(name, label->name) == 0) {
			label->ref++;
			return (label->id);
		}

	/*
	 * to avoid fragmentation, we do a linear search from the beginning
	 * and take the first free slot we find. if there is none or the list
	 * is empty, append a new entry at the end.
	 */

	if (!TAILQ_EMPTY(head))
		for (p = TAILQ_FIRST(head); p != NULL &&
		    p->id == new_id; p = TAILQ_NEXT(p, entry))
			new_id = p->id + 1;

	if (new_id > IDVAL_MAX) {
		errno = ERANGE;
		return (0);
	}

	if ((label = calloc(1, sizeof(struct n2id_label))) == NULL)
		return (0);
	if ((label->name = strdup(name)) == NULL) {
		free(label);
		return (0);
	}
	label->id = new_id;
	label->ref++;

	if (p != NULL)	/* insert new entry before p */
		TAILQ_INSERT_BEFORE(p, label, entry);
	else		/* either list empty or no free slot in between */
		TAILQ_INSERT_TAIL(head, label, entry);

	return (label->id);
}
Example #13
0
/*
 * Insert an address entry in the bridge address TAILQ starting to search
 * for its place from the position of the first bridge address for the bridge
 * interface. Update the first bridge address if necessary.
 */
static void
bridge_addrs_insert_at(struct tp_entries *headp,
	struct tp_entry *ta, struct tp_entry **f_tpa)
{
	struct tp_entry *t1;

	assert(f_tpa != NULL);

	for (t1 = *f_tpa;
	    t1 != NULL && ta->sysindex == t1->sysindex;
	    t1 = TAILQ_NEXT(t1, tp_e)) {
		if (bridge_compare_macs(ta->tp_addr, t1->tp_addr) < 0) {
			TAILQ_INSERT_BEFORE(t1, ta, tp_e);
			if (*f_tpa == t1)
				(*f_tpa) = ta;
			return;
		}
	}

	if (t1 == NULL)
		TAILQ_INSERT_TAIL(headp, ta, tp_e);
	else
		TAILQ_INSERT_BEFORE(t1, ta, tp_e);
}
Example #14
0
/*
 * sleepq_insert:
 *
 *	Insert an LWP into the sleep queue, optionally sorting by priority.
 */
static void
sleepq_insert(sleepq_t *sq, lwp_t *l, syncobj_t *sobj)
{

	if ((sobj->sobj_flag & SOBJ_SLEEPQ_SORTED) != 0) {
		lwp_t *l2;
		const int pri = lwp_eprio(l);

		TAILQ_FOREACH(l2, sq, l_sleepchain) {
			if (lwp_eprio(l2) < pri) {
				TAILQ_INSERT_BEFORE(l2, l, l_sleepchain);
				return;
			}
		}
	}
Example #15
0
int nr_ice_candidate_pair_insert(nr_ice_cand_pair_head *head,nr_ice_cand_pair *pair)
  {
    nr_ice_cand_pair *c1;

    c1=TAILQ_FIRST(head);
    while(c1){
      if(c1->priority < pair->priority){
        TAILQ_INSERT_BEFORE(c1,pair,entry);
        break;
      }

      c1=TAILQ_NEXT(c1,entry);
    }
    if(!c1) TAILQ_INSERT_TAIL(head,pair,entry);

    return(0);
  }
Example #16
0
/*
 * 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();
}
Example #17
0
File: move.c Project: Chr1stoph/i3
/*
 * 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);
}
Example #18
0
/*
 * Insert an entry in the bridge interface TAILQ. Keep the
 * TAILQ sorted by the bridge's interface name.
 */
static void
bridge_ifs_insert(struct bridge_ifs *headp,
	struct bridge_if *b)
{
	struct bridge_if *temp;

	if ((temp = TAILQ_FIRST(headp)) == NULL ||
	    strcmp(b->bif_name, temp->bif_name) < 0) {
		TAILQ_INSERT_HEAD(headp, b, b_if);
		return;
	}

	TAILQ_FOREACH(temp, headp, b_if)
		if(strcmp(b->bif_name, temp->bif_name) < 0)
			TAILQ_INSERT_BEFORE(temp, b, b_if);

	TAILQ_INSERT_TAIL(headp, b, b_if);
}
Example #19
0
/*
 * Enqueue a waiting thread to a queue in descending priority order.
 */
static inline void
mutex_queue_enq(pthread_mutex_t mutex, pthread_t pthread)
{
	pthread_t tid = TAILQ_LAST(&mutex->m_queue, mutex_head);

	PTHREAD_ASSERT_NOT_IN_SYNCQ(pthread);
	/*
	 * For the common case of all threads having equal priority,
	 * we perform a quick check against the priority of the thread
	 * at the tail of the queue.
	 */
	if ((tid == NULL) || (pthread->active_priority <= tid->active_priority))
		TAILQ_INSERT_TAIL(&mutex->m_queue, pthread, sqe);
	else {
		tid = TAILQ_FIRST(&mutex->m_queue);
		while (pthread->active_priority <= tid->active_priority)
			tid = TAILQ_NEXT(tid, sqe);
		TAILQ_INSERT_BEFORE(tid, pthread, sqe);
	}
	pthread->flags |= PTHREAD_FLAGS_IN_MUTEXQ;
}
/*
 * Add an fd to preserve.
 */
int
add_preserved_fd(struct preserved_fd_list *pfds, int fd)
{
    struct preserved_fd *pfd, *pfd_new;
    debug_decl(add_preserved_fd, SUDO_DEBUG_UTIL)

    pfd_new = emalloc(sizeof(*pfd));
    pfd_new->lowfd = fd;
    pfd_new->highfd = fd;
    pfd_new->flags = fcntl(fd, F_GETFD);
    if (pfd_new->flags == -1) {
        efree(pfd_new);
        debug_return_int(-1);
    }

    TAILQ_FOREACH(pfd, pfds, entries) {
        if (fd == pfd->highfd) {
            /* already preserved */
            sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
                              "fd %d already preserved", fd);
            efree(pfd_new);
            break;
        }
        if (fd < pfd->highfd) {
            TAILQ_INSERT_BEFORE(pfd, pfd_new, entries);
            sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
                              "preserving fd %d", fd);
            break;
        }
    }
    if (pfd == NULL) {
        TAILQ_INSERT_TAIL(pfds, pfd_new, entries);
        sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
                          "preserving fd %d", fd);
    }

    debug_return_int(0);
}
Example #21
0
static int
open_dsts(struct _citrus_iconv_std_dst_list *dl,
    const struct _esdb_charset *ec, const struct _esdb *dbdst)
{
	struct _citrus_iconv_std_dst *sd, *sdtmp;
	unsigned long norm;
	int i, ret;

	sd = malloc(sizeof(*sd));
	if (sd == NULL)
		return (errno);

	for (i = 0; i < dbdst->db_num_charsets; i++) {
		ret = open_csmapper(&sd->sd_mapper, ec->ec_csname,
		    dbdst->db_charsets[i].ec_csname, &norm);
		if (ret == 0) {
			sd->sd_csid = dbdst->db_charsets[i].ec_csid;
			sd->sd_norm = norm;
			/* insert this mapper by sorted order. */
			TAILQ_FOREACH(sdtmp, dl, sd_entry) {
				if (sdtmp->sd_norm > norm) {
					TAILQ_INSERT_BEFORE(sdtmp, sd,
					    sd_entry);
					sd = NULL;
					break;
				}
			}
			if (sd)
				TAILQ_INSERT_TAIL(dl, sd, sd_entry);
			sd = malloc(sizeof(*sd));
			if (sd == NULL) {
				ret = errno;
				close_dsts(dl);
				return (ret);
			}
		} else if (ret != ENOENT) {
Example #22
0
/*
 * Enqueue a waiting thread to a condition queue in descending priority
 * order.
 */
static inline void
cond_queue_enq(pthread_cond_t cond, struct pthread *pthread)
{
	struct pthread *tid = TAILQ_LAST(&cond->c_queue, cond_head);

	THR_ASSERT(!THR_IN_SYNCQ(pthread),
	    "cond_queue_enq: thread already queued!");

	/*
	 * For the common case of all threads having equal priority,
	 * we perform a quick check against the priority of the thread
	 * at the tail of the queue.
	 */
	if ((tid == NULL) || (pthread->active_priority <= tid->active_priority))
		TAILQ_INSERT_TAIL(&cond->c_queue, pthread, sqe);
	else {
		tid = TAILQ_FIRST(&cond->c_queue);
		while (pthread->active_priority <= tid->active_priority)
			tid = TAILQ_NEXT(tid, sqe);
		TAILQ_INSERT_BEFORE(tid, pthread, sqe);
	}
	THR_CONDQ_SET(pthread);
	pthread->data.cond = cond;
}
Example #23
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);
    }
}
Example #24
0
struct ip4_range_list_element *
ip4_range_list_insert_range(ip4_range_list_t *list, const struct ip4_range *range, struct ip4_range_list_element *hint) {
	struct ip4_range_list_element *element;
	struct ip4_range_list_element *tmp_element;
	struct ip4_range_list_element *new_element;

	if (list==NULL || range==NULL) { return NULL; }

	if (TAILQ_EMPTY(list)) {
		new_element = malloc(sizeof(struct ip4_range_list_element));
		if (new_element == NULL) {
			fprintf(stderr, "Allocation error\n");
			exit(-1);
		}
		new_element->range = *range;
		TAILQ_INSERT_HEAD(list, new_element, ip4_range_list_links);
		return new_element;
	}
	
	tmp_element = TAILQ_LAST(list, ip4_range_list);
	if (range->start >= tmp_element->range.start) {
			/* Overlapping or adjacent */
			if (range->start <= tmp_element->range.end+1) {
				tmp_element->range.end = MAX(range->end, tmp_element->range.end);
				return tmp_element;
			}
			
			/* Separate */
			
			new_element = malloc(sizeof(struct ip4_range_list_element));
			if (new_element == NULL) {
				fprintf(stderr, "Allocation error\n");
				exit(-1);
				/* NOTREACHED */
			}
			new_element->range = *range;
			TAILQ_INSERT_TAIL(list, new_element, ip4_range_list_links);
			return new_element;
	}	
	
	if (hint != NULL) {
		element = hint;
	}
	else {
		element = TAILQ_FIRST(list);
	}
	
	for (;element != NULL; element = TAILQ_NEXT(element, ip4_range_list_links)) {
		/* Insert before */
		if (range->start <= element->range.start) {
			if (range->end+1 >= element->range.start) {
				element->range.start = range->start;
				element->range.end = MAX(range->end, element->range.end);
				return element;
			}
			
			new_element = malloc(sizeof(struct ip4_range_list_element));
			if (new_element == NULL) {
				fprintf(stderr, "Allocation error\n");
				exit(-1);
				/* NOTREACHED */
			}
			new_element->range = *range;
			TAILQ_INSERT_BEFORE(element, new_element, ip4_range_list_links);
			return new_element;
		}
	}
	fprintf(stderr, "*dies*\n");
	exit(-1);
	/* NOTREACHED */
}
Example #25
0
/*
 * Find the correct table entry for the given variable. If non exists,
 * create one.
 */
static struct entry *
table_find(struct tabwork *work, const struct asn_oid *var)
{
	struct entry *e, *e1;
	struct work *w, *w1;
	u_int i, p, j;
	size_t len;
	u_char *ptr;
	struct asn_oid oid;

	/* get index */
	asn_slice_oid(&oid, var, work->descr->table.len + 2, var->len);

	e = TAILQ_FIRST(work->table);
	w = TAILQ_FIRST(&work->worklist);
	while (e != NULL) {
		if (asn_compare_oid(&w->index, &oid) == 0)
			return (e);
		e = TAILQ_NEXT(e, link);
		w = TAILQ_NEXT(w, link);
	}

	/* Not found create new one */
	if ((e = malloc(work->descr->entry_size)) == NULL) {
		seterr(&snmp_client, "no memory for table entry");
		return (NULL);
	}
	if ((w = malloc(sizeof(*w))) == NULL) {
		seterr(&snmp_client, "no memory for table entry");
		free(e);
		return (NULL);
	}
	w->index = oid;
	memset(e, 0, work->descr->entry_size);

	/* decode index */
	p = work->descr->table.len + 2;
	for (i = 0; i < work->descr->index_size; i++) {
		switch (work->descr->entries[i].syntax) {

		  case SNMP_SYNTAX_INTEGER:
			if (var->len < p + 1) {
				seterr(&snmp_client, "bad index: need integer");
				goto err;
			}
			if (var->subs[p] > INT32_MAX) {
				seterr(&snmp_client,
				    "bad index: integer too large");
				goto err;
			}
			*(int32_t *)(void *)((u_char *)e +
			    work->descr->entries[i].offset) = var->subs[p++];
			break;

		  case SNMP_SYNTAX_OCTETSTRING:
			if (var->len < p + 1) {
				seterr(&snmp_client,
				    "bad index: need string length");
				goto err;
			}
			len = var->subs[p++];
			if (var->len < p + len) {
				seterr(&snmp_client,
				    "bad index: string too short");
				goto err;
			}
			if ((ptr = malloc(len + 1)) == NULL) {
				seterr(&snmp_client,
				    "no memory for index string");
				goto err;
			}
			for (j = 0; j < len; j++) {
				if (var->subs[p] > UCHAR_MAX) {
					seterr(&snmp_client,
					    "bad index: char too large");
					free(ptr);
					goto err;
				}
				ptr[j] = var->subs[p++];
			}
			ptr[j] = '\0';
			*(u_char **)(void *)((u_char *)e +
			    work->descr->entries[i].offset) = ptr;
			*(size_t *)(void *)((u_char *)e +
			    work->descr->entries[i].offset + sizeof(u_char *))
			    = len;
			break;

		  case SNMP_SYNTAX_OID:
			if (var->len < p + 1) {
				seterr(&snmp_client,
				    "bad index: need oid length");
				goto err;
			}
			oid.len = var->subs[p++];
			if (var->len < p + oid.len) {
				seterr(&snmp_client,
				    "bad index: oid too short");
				goto err;
			}
			for (j = 0; j < oid.len; j++)
				oid.subs[j] = var->subs[p++];
			*(struct asn_oid *)(void *)((u_char *)e +
			    work->descr->entries[i].offset) = oid;
			break;

		  case SNMP_SYNTAX_IPADDRESS:
			if (var->len < p + 4) {
				seterr(&snmp_client,
				    "bad index: need ip-address");
				goto err;
			}
			for (j = 0; j < 4; j++) {
				if (var->subs[p] > 0xff) {
					seterr(&snmp_client,
					    "bad index: ipaddress too large");
					goto err;
				}
				((u_char *)e +
				    work->descr->entries[i].offset)[j] =
				    var->subs[p++];
			}
			break;

		  case SNMP_SYNTAX_GAUGE:
			if (var->len < p + 1) {
				seterr(&snmp_client,
				    "bad index: need unsigned");
				goto err;
			}
			if (var->subs[p] > UINT32_MAX) {
				seterr(&snmp_client,
				    "bad index: unsigned too large");
				goto err;
			}
			*(uint32_t *)(void *)((u_char *)e +
			    work->descr->entries[i].offset) = var->subs[p++];
			break;

		  case SNMP_SYNTAX_COUNTER:
		  case SNMP_SYNTAX_TIMETICKS:
		  case SNMP_SYNTAX_COUNTER64:
		  case SNMP_SYNTAX_NULL:
		  case SNMP_SYNTAX_NOSUCHOBJECT:
		  case SNMP_SYNTAX_NOSUCHINSTANCE:
		  case SNMP_SYNTAX_ENDOFMIBVIEW:
			abort();
		}
		e->found |= (uint64_t)1 << i;
	}

	/* link into the correct place */
	e1 = TAILQ_FIRST(work->table);
	w1 = TAILQ_FIRST(&work->worklist);
	while (e1 != NULL) {
		if (asn_compare_oid(&w1->index, &w->index) > 0)
			break;
		e1 = TAILQ_NEXT(e1, link);
		w1 = TAILQ_NEXT(w1, link);
	}
	if (e1 == NULL) {
		TAILQ_INSERT_TAIL(work->table, e, link);
		TAILQ_INSERT_TAIL(&work->worklist, w, link);
	} else {
		TAILQ_INSERT_BEFORE(e1, e, link);
		TAILQ_INSERT_BEFORE(w1, w, link);
	}

	return (e);

  err:
	/*
	 * Error happend. Free all octet string index parts and the entry
	 * itself.
	 */
	for (i = 0; i < work->descr->index_size; i++) {
		if (work->descr->entries[i].syntax == SNMP_SYNTAX_OCTETSTRING &&
		    (e->found & ((uint64_t)1 << i)))
			free(*(void **)(void *)((u_char *)e +
			    work->descr->entries[i].offset));
	}
	free(e);
	free(w);
	return (NULL);
}
Example #26
0
/* Insert a device into a predefined position in VMBUS bus */
void
vmbus_insert_device(struct rte_vmbus_device *exist_vmbus_dev,
		      struct rte_vmbus_device *new_vmbus_dev)
{
	TAILQ_INSERT_BEFORE(exist_vmbus_dev, new_vmbus_dev, next);
}
Example #27
0
/*
 * manual --
 *	Search the manuals for the pages.
 */
static int
manual(const char *page, TAG *tag, glob_t *pg)
{
	ENTRY *ep, *e_sufp, *e_tag;
	TAG *missp, *sufp;
	int anyfound, cnt, found;
	char *p, buf[MAXPATHLEN];

	anyfound = 0;
	buf[0] = '*';

	/* Expand the search path. */
	if (f_all != f_where) {
		e_tag = tag == NULL ? NULL : TAILQ_FIRST(&tag->list);
		while (e_tag != NULL) {
			if (glob(e_tag->s, GLOB_BRACE | GLOB_NOSORT,
			    NULL, pg)) {
				/* No GLOB_NOMATCH here due to {arch,}. */
				warn("globbing directories");
				(void)cleanup(0);
				exit(1);
			}
			for (cnt = 0; cnt < pg->gl_pathc; cnt++) {
				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
				    (ep->s = strdup(pg->gl_pathv[cnt])) ==
						NULL) {
					warn(NULL);
					(void)cleanup(0);
					exit(1);
				}
				TAILQ_INSERT_BEFORE(e_tag, ep, q);
			}
			ep = e_tag;
			e_tag = TAILQ_NEXT(e_tag, q);
			free(ep->s);
			TAILQ_REMOVE(&tag->list, ep, q);
			free(ep);
			globfree(pg);
			pg->gl_pathc = 0;
		}
	}

	/* For each element in the list... */
	e_tag = tag == NULL ? NULL : TAILQ_FIRST(&tag->list);
	for (; e_tag != NULL; e_tag = TAILQ_NEXT(e_tag, q)) {
		(void)snprintf(buf, sizeof(buf), "%s/%s.*", e_tag->s, page);
		switch (glob(buf, GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT,
		    NULL, pg)) {
		case (0):
			break;
		case (GLOB_NOMATCH):
			continue;
		default:
			warn("globbing files");
			(void)cleanup(0);
			exit(1);
		}
		if (pg->gl_matchc == 0)
			continue;

		/* Find out if it's really a man page. */
		for (cnt = pg->gl_pathc - pg->gl_matchc;
		    cnt < pg->gl_pathc; ++cnt) {

			if (!f_all || !f_where) {
				check_companion(pg->gl_pathv + cnt, tag);
				if (*pg->gl_pathv[cnt] == '\0')
					continue;
			}

			/*
			 * Try the _suffix key words first.
			 *
			 * XXX
			 * Older versions of man.conf didn't have the suffix
			 * key words, it was assumed that everything was a .0.
			 * We just test for .0 first, it's fast and probably
			 * going to hit.
			 */
			(void)snprintf(buf, sizeof(buf), "*/%s.0", page);
			if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
				goto next;

			e_sufp = (sufp = getlist("_suffix")) == NULL ?
			    NULL : TAILQ_FIRST(&sufp->list);
			for (found = 0;
			    e_sufp != NULL; e_sufp = TAILQ_NEXT(e_sufp, q)) {
				(void)snprintf(buf,
				     sizeof(buf), "*/%s%s", page, e_sufp->s);
				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
					found = 1;
					break;
				}
			}
			if (found)
				goto next;

			/* Try the _build key words next. */
			e_sufp = (sufp = getlist("_build")) == NULL ?
			    NULL : TAILQ_FIRST(&sufp->list);
			for (found = 0;
			    e_sufp != NULL; e_sufp = TAILQ_NEXT(e_sufp, q)) {
				for (p = e_sufp->s;
				    *p != '\0' && !isspace(*p); ++p);
				if (*p == '\0')
					continue;
				*p = '\0';
				(void)snprintf(buf,
				     sizeof(buf), "*/%s%s", page, e_sufp->s);
				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
					if (!f_where)
						build_page(p + 1,
						    &pg->gl_pathv[cnt]);
					*p = ' ';
					found = 1;
					break;
				}
				*p = ' ';
			}
			if (found) {
next:				anyfound = 1;
				if (!f_all && !f_where) {
					/* Delete any other matches. */
					while (++cnt< pg->gl_pathc)
						pg->gl_pathv[cnt] = "";
					break;
				}
				continue;
			}

			/* It's not a man page, forget about it. */
			pg->gl_pathv[cnt] = "";
		}

		if (anyfound && !f_all && !f_where)
			break;
	}

	/* If not found, enter onto the missing list. */
	if (!anyfound) {
		sigset_t osigs;

		sigprocmask(SIG_BLOCK, &blocksigs, &osigs);

		if ((missp = getlist("_missing")) == NULL)
			missp = addlist("_missing");
		if ((ep = malloc(sizeof(ENTRY))) == NULL ||
		    (ep->s = strdup(page)) == NULL) {
			warn(NULL);
			(void)cleanup(0);
			exit(1);
		}
		TAILQ_INSERT_TAIL(&missp->list, ep, q);
		sigprocmask(SIG_SETMASK, &osigs, NULL);
	}
	return (anyfound);
}
Example #28
0
static inline lagopus_chrono_t
s_do_sched(lagopus_callout_task_t t) {
  lagopus_result_t ret = -1LL;
  lagopus_result_t r;
  lagopus_callout_task_t e;

  s_lock_task_q();
  {

    s_lock_task(t);
    {
      if (likely(t->m_is_in_timed_q == false)) {

        /*
         * Firstly (re-)compute the next exeution time of this
         * task.
         */
        if (t->m_is_first == false && t->m_do_repeat == true) {
          t->m_next_abstime = t->m_last_abstime + t->m_interval_time;
        } else {
          WHAT_TIME_IS_IT_NOW_IN_NSEC(t->m_last_abstime);
          t->m_next_abstime = t->m_last_abstime + t->m_initial_delay_time;
        }

        /*
         * Then insert the task into the Q.
         */
        e = TAILQ_FIRST(&s_chrono_tsk_q);
        if (e != NULL) {
          if (e->m_next_abstime > t->m_next_abstime) {
            TAILQ_INSERT_HEAD(&s_chrono_tsk_q, t, m_entry);
          } else {
            for (;
                 e != NULL && e->m_next_abstime <= t->m_next_abstime;
                 e = TAILQ_NEXT(e, m_entry)) {
              ;
            }
            if (e != NULL) {
              TAILQ_INSERT_BEFORE(e, t, m_entry);
            } else {
              TAILQ_INSERT_TAIL(&s_chrono_tsk_q, t, m_entry);
            }
          }
        } else {
          TAILQ_INSERT_TAIL(&s_chrono_tsk_q, t, m_entry);
        }

        (void)s_set_task_state_in_table(t, TASK_STATE_ENQUEUED);
        t->m_status = TASK_STATE_ENQUEUED;
        t->m_is_in_timed_q = true;

        ret = t->m_next_abstime;

        /*
         * Fianlly wake the master scheduler.
         */
        lagopus_msg_debug(4, "wake the master scheduler up.\n");

        /*
         * TODO
         *
         *	Re-optimize forcible wake up by timed task submission
         *	timing and times. See also
         *	callout.c:s_start_callout_main_loop()
         */

        r = lagopus_bbq_wakeup(&s_urgent_tsk_q, 0LL);
        if (unlikely(r != LAGOPUS_RESULT_OK)) {
          lagopus_perror(r);
          lagopus_msg_error("can't wake the callout task master "
                            "scheduler up.\n");
        }
      }
    }
    s_unlock_task(t);

  }
  s_unlock_task_q();

  return ret;
}
Example #29
0
/* -----------------------------------------------------------------------------
attach L2TP interface dlil layer
----------------------------------------------------------------------------- */
int l2tp_wan_attach(void *rfc, struct ppp_link **link)
{
    int 		ret;	
    struct l2tp_wan  	*wan, *wan1;
    struct ppp_link  	*lk;
    u_short 		unit;
	
	lck_mtx_assert(ppp_domain_mutex, LCK_MTX_ASSERT_OWNED);

    // Note : we allocate/find number/insert in queue in that specific order
    // because of funnels and race condition issues

    MALLOC(wan, struct l2tp_wan *, sizeof(struct l2tp_wan), M_TEMP, M_WAITOK);
    if (!wan)
        return ENOMEM;

    bzero(wan, sizeof(struct l2tp_wan));

	// find a unit and where to insert it, keep the list ordered
	unit = 0;
    wan1 = TAILQ_FIRST(&l2tp_wan_head);
    while (wan1) {
    	if (wan1->link.lk_unit > unit)
            break;

		unit = wan1->link.lk_unit + 1;
        wan1 = TAILQ_NEXT(wan1, next);
    }

	if (wan1)
		TAILQ_INSERT_BEFORE(wan1, wan, next);
	else
		TAILQ_INSERT_TAIL(&l2tp_wan_head, wan, next);
		
    lk = (struct ppp_link *) wan;
    
    // it's time now to register our brand new link
    lk->lk_name 	= (u_char*)L2TP_NAME;
    lk->lk_mtu 		= L2TP_MTU;
    lk->lk_mru 		= L2TP_MTU;;
    lk->lk_type 	= PPP_TYPE_L2TP;
    lk->lk_hdrlen 	= 80;	// ??? 
	l2tp_rfc_command(rfc, L2TP_CMD_GETBAUDRATE, &lk->lk_baudrate);
    lk->lk_ioctl 	= l2tp_wan_ioctl;
    lk->lk_output 	= l2tp_wan_output;
    lk->lk_unit 	= unit;
    lk->lk_support 	= 0;
    wan->rfc = rfc;

    ret = ppp_link_attach((struct ppp_link *)wan);
    if (ret) {
        IOLog("L2TP_wan_attach, error = %d, (ld = %p)\n", ret, wan);
        TAILQ_REMOVE(&l2tp_wan_head, wan, next);
        FREE(wan, M_TEMP);
        return ret;
    }
    
    //IOLog("L2TP_wan_attach, link index = %d, (ld = %p)\n", lk->lk_index, lk);

    *link = lk;
    
    return 0;
}