\r
#include "queue.h"\r
\r
-#define DEBUG(x) x\r
-/*#define DEBUG(x)*/\r
+/*#define DEBUG(x) x*/\r
+#define DEBUG(x)\r
+#define DEBUG_ERR(x) x\r
+/*#define DEBUG_ERR(x)*/\r
\r
#define PQ_START_SIZE 10\r
#define AT_START 0\r
sv_setnv(HeVAL(entry), new_priority);\r
}\r
\r
+/*\r
+pq_move_items - moves items around.\r
+\r
+This encapsulates the old calls to memmove(), providing a single place\r
+to add error checking.\r
+*/\r
+static void\r
+pq_move_items(poe_queue *pq, int target, int src, int count) {\r
+\r
+ DEBUG_ERR(\r
+ {\r
+ int die = 0;\r
+ if (src < pq->start) {\r
+ fprintf(stderr, "src %d less than start %d\n", src, pq->start);\r
+ ++die;\r
+ }\r
+ if (src + count > pq->end) {\r
+ fprintf(stderr, "src %d + count %d beyond end %d\n", src, count, pq->end);\r
+ ++die;\r
+ }\r
+ if (target < 0) {\r
+ fprintf(stderr, "target %d < 0\n", target);\r
+ ++die;\r
+ }\r
+ if (target + count > pq->alloc) {\r
+ fprintf(stderr, "target %d + count %d > alloc\n", target, count, pq->alloc);\r
+ ++die;\r
+ }\r
+ if (die) *(char *)0 = '\0';\r
+ }\r
+ )\r
+ memmove(pq->entries + target, pq->entries + src, count * sizeof(pq_entry));\r
+}\r
+\r
/*\r
pq_realloc - make space at the front of back of the queue.\r
\r
int count = pq->end - pq->start;\r
\r
DEBUG( fprintf(stderr, "pq_realloc((%d, %d, %d), %d)\n", pq->start, pq->end, pq->alloc, at_end) );\r
- pq_dump(pq);\r
if (count * 3 / 2 < pq->alloc) {\r
/* 33 % or more space available, use some of it */\r
int new_start;\r
new_start = (pq->alloc - count) * 2 / 3;\r
}\r
DEBUG( fprintf(stderr, " moving start to %d\n", new_start) );\r
- memmove(pq->entries + new_start, pq->entries + pq->start,\r
- count * sizeof(pq_entry));\r
+ pq_move_items(pq, new_start, pq->start, count);\r
pq->start = new_start;\r
pq->end = new_start + count;\r
}\r
if (!at_end) {\r
int new_start = (new_alloc - count) * 2 / 3;\r
DEBUG( fprintf(stderr, " moving start to %d\n", new_start) );\r
- memmove(pq->entries + new_start, pq->entries + pq->start,\r
- count * sizeof(pq_entry));\r
+ pq_move_items(pq, new_start, pq->start, count);\r
pq->start = new_start;\r
pq->end = new_start + count;\r
}\r
}\r
- pq_dump(pq);\r
DEBUG( fprintf(stderr, " final: %d %d %d\n", pq->start, pq->end, pq->alloc) );\r
}\r
\r
i += pq->start - old_start;\r
}\r
\r
- memmove(pq->entries + i + 1, pq->entries + i, (pq->end - i) * sizeof(pq_entry));\r
+ pq_move_items(pq, i+1, i, pq->end - i);\r
++pq->end;\r
fill_at = i;\r
}\r
pq_realloc(pq, AT_START);\r
i += pq->start;\r
}\r
- memmove(pq->entries + pq->start - 1, pq->entries + pq->start,\r
- (i - pq->start) * sizeof(pq_entry));\r
+ pq_move_items(pq, pq->start-1, pq->start, i - pq->start);\r
--pq->start;\r
fill_at = i-1;\r
}\r
--pq->end;\r
}\r
else {\r
- memmove(pq->entries + index, pq->entries + index + 1,\r
- sizeof(pq_entry) * (pq->end - index - 1));\r
+ pq_move_items(pq, index, index+1, pq->end - index - 1);\r
--pq->end;\r
}\r
+ DEBUG( fprintf(stderr, "removed (%d, %p (%d))\n", id, removed->payload,\r
+ SvREFCNT(removed->payload)) );\r
\r
return 1;\r
}\r
\r
if (insert_at < index) {\r
DEBUG( fprintf(stderr, " - insert_at < index\n") );\r
- memmove(pq->entries + insert_at + 1, pq->entries + insert_at,\r
- sizeof(pq_entry) * (index - insert_at));\r
+ pq_move_items(pq, insert_at + 1, insert_at, index - insert_at);\r
pq->entries[insert_at] = saved;\r
}\r
else {\r
DEBUG( fprintf(stderr, " - insert_at > index\n") );\r
--insert_at;\r
- memmove(pq->entries + index, pq->entries + index + 1,\r
- sizeof(pq_entry) * (insert_at - index));\r
+ pq_move_items(pq, index, index + 1, insert_at - index);\r
pq->entries[insert_at] = saved;\r
}\r
}\r
\r
if (insert_at < index) {\r
DEBUG( fprintf(stderr, " - insert_at < index\n") );\r
- memmove(pq->entries + insert_at + 1, pq->entries + insert_at,\r
- sizeof(pq_entry) * (index - insert_at));\r
+ pq_move_items(pq, insert_at + 1, insert_at, index - insert_at);\r
pq->entries[insert_at] = saved;\r
}\r
else {\r
DEBUG( fprintf(stderr, " - insert_at > index\n") );\r
--insert_at;\r
- memmove(pq->entries + index, pq->entries + index + 1,\r
- sizeof(pq_entry) * (insert_at - index));\r
+ pq_move_items(pq, index, index + 1, insert_at - index);\r
pq->entries[insert_at] = saved;\r
}\r
}\r