+/**
+ * Adds the operation to the ready queue and starts the 'process_rq_task'
+ *
+ * @param op the operation to be queued
+ */
+static void
+rq_add (struct GNUNET_TESTBED_Operation *op)
+{
+ struct ReadyQueueEntry *rq_entry;
+
+ GNUNET_assert (NULL == op->rq_entry);
+ rq_entry = GNUNET_new (struct ReadyQueueEntry);
+ rq_entry->op = op;
+ GNUNET_CONTAINER_DLL_insert_tail (rq_head, rq_tail, rq_entry);
+ op->rq_entry = rq_entry;
+ if (NULL == process_rq_task_id)
+ process_rq_task_id = GNUNET_SCHEDULER_add_now (&process_rq_task, NULL);
+}
+
+
+/**
+ * Checks if the given operation queue is empty or not
+ *
+ * @param opq the operation queue
+ * @return GNUNET_YES if the given operation queue has no operations; GNUNET_NO
+ * otherwise
+ */
+static int
+is_queue_empty (struct OperationQueue *opq)
+{
+ if ( (NULL != opq->wq_head)
+ || (NULL != opq->rq_head)
+ || (NULL != opq->aq_head)
+ || (NULL != opq->nq_head) )
+ return GNUNET_NO;
+ return GNUNET_YES;
+}
+
+
+/**
+ * Checks if the given operation queue has enough resources to provide for the
+ * operation of the given queue entry. It also checks if any inactive
+ * operations are to be released in order to accommodate the needed resources
+ * and returns them as an array.
+ *
+ * @param opq the operation queue to check for resource accommodation
+ * @param entry the operation queue entry whose operation's resources are to be
+ * accommodated
+ * @param ops_ pointer to return the array of operations which are to be released
+ * in order to accommodate the new operation. Can be NULL
+ * @param n_ops_ the number of operations in ops_
+ * @return GNUNET_YES if the given entry's operation can be accommodated in this
+ * queue. GNUNET_NO if it cannot be accommodated; ops_ and n_ops_ will
+ * be set to NULL and 0 respectively.
+ */
+static int
+decide_capacity (struct OperationQueue *opq,
+ struct QueueEntry *entry,
+ struct GNUNET_TESTBED_Operation ***ops_,
+ unsigned int *n_ops_)
+{
+ struct QueueEntry **evict_entries;
+ struct GNUNET_TESTBED_Operation **ops;
+ struct GNUNET_TESTBED_Operation *op;
+ unsigned int n_ops;
+ unsigned int n_evict_entries;
+ unsigned int need;
+ unsigned int max;
+ int deficit;
+ int rval;
+
+ GNUNET_assert (NULL != (op = entry->op));
+ GNUNET_assert (0 < (need = entry->nres));
+ ops = NULL;
+ n_ops = 0;
+ evict_entries = NULL;
+ n_evict_entries = 0;
+ rval = GNUNET_YES;
+ if (OPERATION_QUEUE_TYPE_ADAPTIVE == opq->type)
+ {
+ GNUNET_assert (NULL != opq->fctx);
+ GNUNET_assert (opq->max_active >= opq->overload);
+ max = opq->max_active - opq->overload;
+ }
+ else
+ max = opq->max_active;
+ if (opq->active > max)
+ {
+ rval = GNUNET_NO;
+ goto ret;
+ }
+ if ((opq->active + need) <= max)
+ goto ret;
+ deficit = need - (max - opq->active);
+ for (entry = opq->nq_head;
+ (0 < deficit) && (NULL != entry);
+ entry = entry->next)
+ {
+ GNUNET_array_append (evict_entries, n_evict_entries, entry);
+ deficit -= entry->nres;
+ }
+ if (0 < deficit)
+ {
+ rval = GNUNET_NO;
+ goto ret;
+ }
+ for (n_ops = 0; n_ops < n_evict_entries;)
+ {
+ op = evict_entries[n_ops]->op;
+ GNUNET_array_append (ops, n_ops, op); /* increments n-ops */
+ }
+
+ ret:
+ GNUNET_free_non_null (evict_entries);
+ if (NULL != ops_)
+ *ops_ = ops;
+ else
+ GNUNET_free (ops);
+ if (NULL != n_ops_)
+ *n_ops_ = n_ops;
+ return rval;
+}
+
+
+/**
+ * Merges an array of operations into another, eliminating duplicates. No
+ * ordering is guaranteed.
+ *
+ * @param old the array into which the merging is done.
+ * @param n_old the number of operations in old array
+ * @param new the array from which operations are to be merged
+ * @param n_new the number of operations in new array
+ */
+static void
+merge_ops (struct GNUNET_TESTBED_Operation ***old,
+ unsigned int *n_old,
+ struct GNUNET_TESTBED_Operation **new,
+ unsigned int n_new)
+{
+ struct GNUNET_TESTBED_Operation **cur;
+ unsigned int i;
+ unsigned int j;
+ unsigned int n_cur;
+
+ GNUNET_assert (NULL != old);
+ n_cur = *n_old;
+ cur = *old;
+ for (i = 0; i < n_new; i++)
+ {
+ for (j = 0; j < *n_old; j++)
+ {
+ if (new[i] == cur[j])
+ break;
+ }
+ if (j < *n_old)
+ continue;
+ GNUNET_array_append (cur, n_cur, new[j]);
+ }
+ *old = cur;
+ *n_old = n_cur;
+}
+
+
+