Merge branch 'maint'
[paraslash.git] / chunk_queue.c
index fa31718590dd73fad6f034d69ce2a8d7fc299e86..3f5ac1d929262c116f6c7822f67f0ceb8cf1e7f6 100644 (file)
@@ -1,11 +1,13 @@
 /*
- * Copyright (C) 2007-2009 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2007-2010 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file chunk_queue.c Queuing functions for paraslash senders. */
 
+#include <regex.h>
+
 #include "para.h"
 #include "list.h"
 #include "afh.h"
@@ -80,16 +82,46 @@ struct queued_chunk *cq_peek(struct chunk_queue *cq)
 /**
  * Remove the current chunk from the queue.
  *
- * \param cq The chunk to remove.
+ * \param cq The queue to remove from.
  */
 void cq_dequeue(struct chunk_queue *cq)
 {
        struct queued_chunk *qc = cq_peek(cq);
        assert(qc);
+       assert(cq->num_pending >= qc->num_bytes);
+       cq->num_pending -= qc->num_bytes;
        list_del(&qc->node);
        free(qc);
 }
 
+/**
+ * Force to add a chunk to the given queue.
+ *
+ * \param cq See \ref cq_enqueue.
+ * \param buf See \ref cq_enqueue.
+ * \param num_bytes See \ref cq_enqueue.
+ *
+ * If queuing the given buffer would result in exceeding the maximal queue
+ * size, buffers are dropped from the beginning of the queue. Note that this
+ * function still might fail.
+ *
+ * \return Standard.
+ */
+int cq_force_enqueue(struct chunk_queue *cq, const char *buf, size_t num_bytes)
+{
+       int ret;
+
+       if (num_bytes > cq->max_pending)
+               return -E_QUEUE;
+       for (;;) {
+               ret = cq_enqueue(cq, buf, num_bytes);
+               if (ret >= 0)
+                       return ret;
+               cq_dequeue(cq);
+       }
+       /* never reached */
+}
+
 /**
  * Change the number of bytes sent for the current queued chunk.
  *