Ejemplo n.º 1
0
TEST_F(GQueueTest, popHeadLink)
{
	int testData1 = 42;
	int testData2 = 1337;

	g_queue_push_tail(queue, &testData1);
	g_queue_push_tail(queue, &testData2);

	GList *head = g_queue_pop_head_link(queue);
	ASSERT_TRUE(head != NULL) << "popped queue element should not be NULL";
	ASSERT_EQ(&testData1, head->data) << "popped queue head should have correct value";
	ASSERT_TRUE(head->next == NULL) << "poppoed queue head should not have a next element";
	ASSERT_TRUE(head->prev == NULL) << "poppoed queue head should not have a next element";
	ASSERT_TRUE(queue->head != NULL) << "queue head should not be NULL after removing one element";
	ASSERT_EQ(queue->head, queue->tail) << "queue tail should be equal to head after removing one element";
	ASSERT_TRUE(queue->head->next == NULL) << "queue element should not have a next element";
	ASSERT_TRUE(queue->head->prev == NULL) << "queue element should not have a previous element";
	ASSERT_EQ(1, queue->length) << "queue should have one element left after popping one";
	g_list_free(head);

	head = g_queue_pop_head_link(queue);
	ASSERT_TRUE(head != NULL) << "popped queue element should not be NULL";
	ASSERT_EQ(&testData2, head->data) << "popped queue head should have correct value";
	ASSERT_TRUE(head->next == NULL) << "poppoed queue head should not have a next element";
	ASSERT_TRUE(head->prev == NULL) << "poppoed queue head should not have a next element";
	ASSERT_TRUE(queue->head == NULL) << "queue head should be NULL after popping another element";
	ASSERT_TRUE(queue->tail == NULL) << "queue tail should be NULL after popping another element";
	ASSERT_EQ(0, queue->length) << "queue length should be zero after popping another element";
	g_list_free(head);

	head = g_queue_pop_head_link(queue);
	ASSERT_TRUE(head == NULL) << "popped queue element of empty queue should be NULL";
}
Ejemplo n.º 2
0
/* steal the first chunk from in and append it to out, return number of bytes stolen */
goffset li_chunkqueue_steal_chunk(liChunkQueue *out, liChunkQueue *in) {
	liChunk *c;
	goffset length;
	GList *l = g_queue_pop_head_link(&in->queue);
	if (!l) return 0;
	g_queue_push_tail_link(&out->queue, l);

	c = (liChunk*) l->data;
	length = li_chunk_length(c);
	in->bytes_out += length;
	in->length -= length;
	out->bytes_in += length;
	out->length += length;
	if (in->limit != out->limit) {
		if (c->type == STRING_CHUNK) {
			cqlimit_update(out, c->data.str->len);
			cqlimit_update(in, - (goffset)c->data.str->len);
		} else if (c->type == MEM_CHUNK) {
			cqlimit_update(out, c->mem->len);
			cqlimit_update(in, - (goffset)c->mem->len);
		} else if (c->type == BUFFER_CHUNK) {
			cqlimit_update(out, c->data.buffer.length);
			cqlimit_update(in, - (goffset)c->data.buffer.length);
		}
	}
	return length;
}
Ejemplo n.º 3
0
static void job_queue_run(liJobQueue* jq, int loops) {
	int i;

	for (i = 0; i < loops; i++) {
		GQueue *q = &jq->queue;
		GList *l;
		liJob *job;
		guint todo = q->length;

		INC_GEN(jq);

		if (0 == todo) return;

		while ((todo-- > 0) && (NULL != (l = g_queue_pop_head_link(q)))) {
			job = LI_CONTAINER_OF(l, liJob, link);
			job->generation = jq->generation;
			job->link.data = NULL;

			job->callback(job);
		}
	}

	if (jq->queue.length > 0) {
		/* make sure we will run again soon */
		li_event_timer_once(&jq->queue_watcher, 0);
	}
}
/**
 * rtp_jitter_buffer_flush:
 * @jbuf: an #RTPJitterBuffer
 * @free_func: function to free each item
 * @user_data: user data passed to @free_func
 *
 * Flush all packets from the jitterbuffer.
 */
void
rtp_jitter_buffer_flush (RTPJitterBuffer * jbuf, GFunc free_func,
    gpointer user_data)
{
  GList *item;

  g_return_if_fail (jbuf != NULL);
  g_return_if_fail (free_func != NULL);

  while ((item = g_queue_pop_head_link (jbuf->packets)))
    free_func ((RTPJitterBufferItem *) item, user_data);
}
Ejemplo n.º 5
0
static void         explorer_prune_history  (Explorer* self, int max_nodes)
{
    while (self->history_queue->length > max_nodes) {
	GList* link = g_queue_pop_head_link(self->history_queue);
	if (!link)
	    break;

	if (link == self->history_current_link)
	    self->history_current_link = NULL;

	history_node_free(link->data);
	g_list_free_1(link);
    }
}
Ejemplo n.º 6
0
static void throttle_pool_rearm(liWorker *wrk, liThrottlePool *pool, guint now) {
	liThrottlePoolWorkerState *wpool = &pool->workers[wrk->ndx];
	guint last = g_atomic_int_get((gint*) &pool->last_rearm);
	guint time_diff = now - last;

	if (G_UNLIKELY(time_diff >= LI_THROTTLE_GRANULARITY)) {
		g_mutex_lock(pool->rearm_mutex);
			/* check again */
			last = g_atomic_int_get((gint*) &pool->last_rearm);
			time_diff = now - last;
			if (G_LIKELY(time_diff >= LI_THROTTLE_GRANULARITY)) {
				S_throttle_pool_rearm_workers(pool, wrk->srv->worker_count, time_diff);
				g_atomic_int_set((gint*) &pool->last_rearm, now);
			}
		g_mutex_unlock(pool->rearm_mutex);
	}

	if (G_UNLIKELY(wpool->last_rearm < last)) {
		/* distribute wpool->magazine */
		GList *lnk;
		guint connections = wpool->connections;
		gint magazine = g_atomic_int_get(&wpool->magazine);
		gint supply = magazine / connections;
		g_atomic_int_add(&wpool->magazine, -supply * connections);
		wpool->last_rearm = now;

		throttle_debug("throttle_pool_rearm: distribute supply %i on each of %i connections\n",
			supply, connections);

		if (0 == supply) return;

		g_atomic_int_set((gint*) &wpool->connections, 0);
		while (NULL != (lnk = g_queue_pop_head_link(&wpool->waiting))) {
			liThrottlePoolState *pstate = LI_CONTAINER_OF(lnk, liThrottlePoolState, pool_link);
			pstate->magazine += supply;
			lnk->data = NULL;
		}
	}
}
Ejemplo n.º 7
0
GList *
tracker_priority_queue_pop_node (TrackerPriorityQueue *queue,
                                 gint                 *priority_out)
{
	PrioritySegment *segment;
	GList *node;

	g_return_val_if_fail (queue != NULL, NULL);

	node = g_queue_peek_head_link (&queue->queue);

	if (!node) {
		/* No elements in queue */
		return NULL;
	}

	segment = &g_array_index (queue->segments, PrioritySegment, 0);
	g_assert (segment->first_elem == node);

	if (priority_out) {
		*priority_out = segment->priority;
	}

	if (segment->last_elem == node) {
		/* It is the last element of the first
		 * segment, remove segment as well.
		 */
		g_array_remove_index (queue->segments, 0);
	} else {

		/* Move segments first element forward */
		segment->first_elem = segment->first_elem->next;
	}

	return g_queue_pop_head_link (&queue->queue);
}
Ejemplo n.º 8
0
/* steal up to length bytes from in and put them into out, return number of bytes stolen */
goffset li_chunkqueue_steal_len(liChunkQueue *out, liChunkQueue *in, goffset length) {
	liChunk *c, *cnew;
	GList* l;
	goffset bytes = 0, meminbytes = 0, memoutbytes = 0;
	goffset we_have;

	while ( (NULL != (c = li_chunkqueue_first_chunk(in))) && length > 0 ) {
		we_have = li_chunk_length(c);
		if (!we_have) { /* remove empty chunks */
			if (c->type == STRING_CHUNK) meminbytes -= c->data.str->len;
			else if (c->type == MEM_CHUNK) meminbytes -= c->mem->len;
			else if (c->type == BUFFER_CHUNK) meminbytes -= c->data.buffer.length;
			chunk_free(in, c);
			continue;
		}
		if (we_have <= length) { /* move complete chunk */
			l = g_queue_pop_head_link(&in->queue);
			g_queue_push_tail_link(&out->queue, l);
			bytes += we_have;
			if (c->type == STRING_CHUNK) {
				meminbytes -= c->data.str->len;
				memoutbytes += c->data.str->len;
			} else if (c->type == MEM_CHUNK) {
				meminbytes -= c->mem->len;
				memoutbytes += c->mem->len;
			} else if (c->type == BUFFER_CHUNK) {
				meminbytes -= c->data.buffer.length;
				memoutbytes += c->data.buffer.length;
			}
			length -= we_have;
		} else { /* copy first part of a chunk */
			cnew = chunk_new();
			switch (c->type) {
			case UNUSED_CHUNK: /* impossible, has length 0 */
				/* remove "empty" chunks */
				chunk_free(in, c);
				chunk_free(NULL, cnew);
				continue;
			case STRING_CHUNK: /* change type to MEM_CHUNK, as we copy it anyway */
				cnew->type = MEM_CHUNK;
				cnew->mem = g_byte_array_sized_new(length);
				g_byte_array_append(cnew->mem, (guint8*) c->data.str->str + c->offset, length);
				memoutbytes += length;
				break;
			case MEM_CHUNK:
				cnew->type = MEM_CHUNK;
				cnew->mem = g_byte_array_sized_new(length);
				g_byte_array_append(cnew->mem, (guint8*) c->mem->data + c->offset, length);
				memoutbytes += length;
				break;
			case FILE_CHUNK:
				cnew->type = FILE_CHUNK;
				li_chunkfile_acquire(c->data.file.file);
				cnew->data.file.file = c->data.file.file;
				cnew->data.file.start = c->data.file.start + c->offset;
				cnew->data.file.length = length;
				break;
			case BUFFER_CHUNK:
				cnew->type = BUFFER_CHUNK;
				li_buffer_acquire(c->data.buffer.buffer);
				cnew->data.buffer.buffer = c->data.buffer.buffer;
				cnew->data.buffer.offset = c->data.buffer.offset + c->offset;
				cnew->data.buffer.length = length;
				memoutbytes += length;
				break;
			}
			c->offset += length;
			bytes += length;
			length = 0;
			g_queue_push_tail_link(&out->queue, &cnew->cq_link);
		}
	}

	in->bytes_out += bytes;
	in->length -= bytes;
	out->bytes_in += bytes;
	out->length += bytes;
	cqlimit_update(out, memoutbytes);
	cqlimit_update(in, meminbytes);

	return bytes;
}
Ejemplo n.º 9
0
void
sql_reserved_query_rebuild(sql_reserved_query *srq, gint max_query_num)
{
    GQueue *gq = NULL;
    GList *travel_list = NULL;
    gint i = 0, remove_item_num = 0, time_interval = 0;
    reserved_query_item *rm_rqi = NULL;
    time_t cur_time  = time(NULL);

    g_assert(max_query_num >= 0);

    g_rw_lock_writer_lock(&srq->rq_lock);

    if (srq == NULL) goto exit;
    if (srq->ht_reserved_query == NULL) goto exit ;

    remove_item_num = g_queue_get_length(srq->gq_reserved_short_query);
    remove_item_num += g_queue_get_length(srq->gq_reserved_long_query);
    remove_item_num -= max_query_num;

    if (remove_item_num <= 0) goto exit;

    /* 4 roll lru */
    for(i = 0; i < 4; i++)
    {
        gq = (i%2 == 0) ? srq->gq_reserved_short_query : srq->gq_reserved_long_query;

        if (g_queue_get_length(gq) == 0) continue;

        travel_list = g_queue_peek_head_link(gq);
        while (travel_list != NULL)
        {
            rm_rqi = (reserved_query_item *)travel_list->data;
            g_assert(rm_rqi->list_pos == travel_list);

            if (i < 2 && max_query_num != 0)
            {
               time_interval = cur_time - rm_rqi->item_last_access_time;
               if (time_interval < g_atomic_int_get(&srq->freq_time_window))
               {
                   break;
               }
            }
#ifdef FILTER_DEBUG
            g_debug("[reserved query][lru remove][%s]", rm_rqi->item_rewrite->str);
#endif
            travel_list =  g_queue_pop_head_link(gq);
            g_hash_table_remove(srq->ht_reserved_query, rm_rqi->item_rewrite_md5->str);
            g_list_free(travel_list);
            travel_list = NULL;

            if (remove_item_num-- == 0) goto exit;

            travel_list = g_queue_peek_head_link(gq);
        }
    }

exit :

#ifdef FILTER_DEBUG
    g_queue_travel(srq->gq_reserved_long_query);
    g_queue_travel(srq->gq_reserved_short_query);
#endif

    g_rw_lock_writer_unlock(&srq->rq_lock);
    return ;
}
Ejemplo n.º 10
0
int main()
{
  GQueue *q;
  GList *node;
  gpointer data;

  q = g_queue_new ();

  g_assert (g_queue_is_empty (q) == TRUE);

  g_queue_push_head (q, GINT_TO_POINTER (2));
  g_assert (g_queue_peek_head (q) == GINT_TO_POINTER (2));
  g_assert (g_queue_is_empty (q) == FALSE);
  g_assert (g_list_length (q->head) == 1);
  g_assert (q->head == q->tail);
  g_queue_push_head (q, GINT_TO_POINTER (1));
  g_assert (q->head->next == q->tail);
  g_assert (q->tail->prev == q->head);
  g_assert (g_list_length (q->head) == 2);
  g_assert (q->tail->data == GINT_TO_POINTER (2));
  g_assert (q->head->data == GINT_TO_POINTER (1));
  g_queue_push_tail (q, GINT_TO_POINTER (3));
  g_assert (g_list_length (q->head) == 3);
  g_assert (q->head->data == GINT_TO_POINTER (1));
  g_assert (q->head->next->data == GINT_TO_POINTER (2));
  g_assert (q->head->next->next == q->tail);
  g_assert (q->head->next == q->tail->prev);
  g_assert (q->tail->data == GINT_TO_POINTER (3));
  g_queue_push_tail (q, GINT_TO_POINTER (4));
  g_assert (g_list_length (q->head) == 4);
  g_assert (q->head->data == GINT_TO_POINTER (1));
  g_assert (g_queue_peek_tail (q) == GINT_TO_POINTER (4));
  g_queue_push_tail (q, GINT_TO_POINTER (5));
  g_assert (g_list_length (q->head) == 5);

  g_assert (g_queue_is_empty (q) == FALSE);

  g_assert (q->length == 5);
  g_assert (q->head->prev == NULL);
  g_assert (q->head->data == GINT_TO_POINTER (1));
  g_assert (q->head->next->data == GINT_TO_POINTER (2));
  g_assert (q->head->next->next->data == GINT_TO_POINTER (3));
  g_assert (q->head->next->next->next->data == GINT_TO_POINTER (4));
  g_assert (q->head->next->next->next->next->data == GINT_TO_POINTER (5));
  g_assert (q->head->next->next->next->next->next == NULL);
  g_assert (q->head->next->next->next->next == q->tail);
  g_assert (q->tail->data == GINT_TO_POINTER (5));
  g_assert (q->tail->prev->data == GINT_TO_POINTER (4));
  g_assert (q->tail->prev->prev->data == GINT_TO_POINTER (3));
  g_assert (q->tail->prev->prev->prev->data == GINT_TO_POINTER (2));
  g_assert (q->tail->prev->prev->prev->prev->data == GINT_TO_POINTER (1));
  g_assert (q->tail->prev->prev->prev->prev->prev == NULL);
  g_assert (q->tail->prev->prev->prev->prev == q->head);
  g_assert (g_queue_peek_tail (q) == GINT_TO_POINTER (5));
  g_assert (g_queue_peek_head (q) == GINT_TO_POINTER (1));

  g_assert (g_queue_pop_head (q) == GINT_TO_POINTER (1));
  g_assert (g_list_length (q->head) == 4 && q->length == 4);
  g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (5));
  g_assert (g_list_length (q->head) == 3);
  g_assert (g_queue_pop_head_link (q)->data == GINT_TO_POINTER (2));
  g_assert (g_list_length (q->head) == 2);
  g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (4));
  g_assert (g_list_length (q->head) == 1);
  g_assert (g_queue_pop_head_link (q)->data == GINT_TO_POINTER (3));
  g_assert (g_list_length (q->head) == 0);
  g_assert (g_queue_pop_tail (q) == NULL);
  g_assert (g_list_length (q->head) == 0);
  g_assert (g_queue_pop_head (q) == NULL);
  g_assert (g_list_length (q->head) == 0);

  g_assert (g_queue_is_empty (q) == TRUE);

  /************************/

  g_queue_push_head (q, GINT_TO_POINTER (1));
  g_assert (g_list_length (q->head) == 1 && 1 == q->length);
  g_queue_push_head (q, GINT_TO_POINTER (2));
  g_assert (g_list_length (q->head) == 2 && 2 == q->length);
  g_queue_push_head (q, GINT_TO_POINTER (3));
  g_assert (g_list_length (q->head) == 3 && 3 == q->length);
  g_queue_push_head (q, GINT_TO_POINTER (4));
  g_assert (g_list_length (q->head) == 4 && 4 == q->length);
  g_queue_push_head (q, GINT_TO_POINTER (5));
  g_assert (g_list_length (q->head) == 5 && 5 == q->length);

  g_assert (g_queue_pop_head (q) == GINT_TO_POINTER (5));
  g_assert (g_list_length (q->head) == 4);
  node = q->tail;
  g_assert (node == g_queue_pop_tail_link (q));
  g_assert (g_list_length (q->head) == 3);
  data = q->head->data;
  g_assert (data == g_queue_pop_head (q));
  g_assert (g_list_length (q->head) == 2);
  g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (2));
  g_assert (g_list_length (q->head) == 1);
  g_assert (q->head == q->tail);
  g_assert (g_queue_pop_tail (q) == GINT_TO_POINTER (3));
  g_assert (g_list_length (q->head) == 0);
  g_assert (g_queue_pop_head (q) == NULL);
  g_assert (g_queue_pop_head_link (q) == NULL);
  g_assert (g_list_length (q->head) == 0);
  g_assert (g_queue_pop_tail_link (q) == NULL);
  g_assert (g_list_length (q->head) == 0);

  g_queue_free (q);

  return 0;
}
Ejemplo n.º 11
0
static void
_eventd_nd_notification_refresh_list(EventdPluginContext *context, EventdNdQueue *queue)
{
    if ( queue->more_notification != NULL )
    {
        g_queue_pop_tail_link(queue->queue);
        queue->more_notification->visible = FALSE;
    }

    while ( ( g_queue_get_length(queue->queue) < queue->limit ) && ( ! g_queue_is_empty(queue->wait_queue) ) )
    {
        GList *link;
        link = g_queue_pop_head_link(queue->wait_queue);
        if ( queue->reverse )
            g_queue_push_tail_link(queue->queue, link);
        else
            g_queue_push_head_link(queue->queue, link);

        EventdNdNotification *self = link->data;
        gint timeout;
        timeout = eventd_nd_style_get_bubble_timeout(self->style);
        if ( timeout > 0 )
            self->timeout = g_timeout_add_full(G_PRIORITY_DEFAULT, timeout, _eventd_nd_event_timedout, self, NULL);
        self->visible = TRUE;
    }

    if ( queue->more_indicator )
    {
        if ( ! g_queue_is_empty(queue->wait_queue) )
        {
            if ( queue->more_notification == NULL )
                queue->more_notification = eventd_nd_notification_new(context, NULL, context->style);
            else
            {
                _eventd_nd_notification_update(queue->more_notification, NULL);
                g_queue_push_tail_link(queue->queue, queue->more_notification->link);
            }
            queue->more_notification->visible = TRUE;
        }
        else if ( queue->more_notification != NULL )
            eventd_nd_notification_free(queue->more_notification);
    }

    gpointer data = NULL;
    if ( context->backend->move_begin != NULL )
        data = context->backend->move_begin(context->backend->context, g_queue_get_length(queue->queue));

    gboolean right, center, bottom;
    right = ( queue->anchor == EVENTD_ND_ANCHOR_TOP_RIGHT ) || ( queue->anchor == EVENTD_ND_ANCHOR_BOTTOM_RIGHT );
    center = ( queue->anchor == EVENTD_ND_ANCHOR_TOP ) || ( queue->anchor == EVENTD_ND_ANCHOR_BOTTOM );
    bottom = ( queue->anchor == EVENTD_ND_ANCHOR_BOTTOM_LEFT ) || ( queue->anchor == EVENTD_ND_ANCHOR_BOTTOM ) || ( queue->anchor == EVENTD_ND_ANCHOR_BOTTOM_RIGHT );

    gint bx, by;
    bx = queue->margin_x;
    by = queue->margin_y;
    if ( center )
        bx = context->geometry.w;
    else if ( right )
        bx = context->geometry.w - bx;
    if ( bottom )
        by = context->geometry.h - by;
    GList *self_;
    for ( self_ = g_queue_peek_head_link(queue->queue) ; self_ != NULL ; self_ = g_list_next(self_) )
    {
        EventdNdNotification *self = self_->data;

        if ( bottom )
            by -= self->border_size.height;

        gint x, y;
        x = center ? ( ( bx / 2 ) - ( self->border_size.width / 2 ) ) : right ? ( bx - self->border_size.width ) : bx;
        y = by;
        x -= self->offset.x;
        y -= self->offset.y;
        context->backend->move_surface(self->surface, x, y, data);

        if ( bottom )
            by -= queue->spacing;
        else
            by += self->border_size.height + queue->spacing;
    }

    if ( context->backend->move_end != NULL )
        context->backend->move_end(context->backend->context, data);
}