move qa/ to tools/
[archipelago] / xseg / xq / xq.c
index e0bba20..b65bbbc 100644 (file)
@@ -1,13 +1,5 @@
 #include <xq/xq.h>
 
-#ifdef RELATIVE_POINTERS
-#define PTR(p, f) ((typeof((p)->f))((unsigned long)(p) + (unsigned long)(p)->f))
-#define PTRSET(p, f, v) ((p)->f = (void *)((unsigned long)(v) - (unsigned long)(p)))
-#else
-#define PTR(p, f) (p)->f
-#define PTRSET(p, f, v) ((p)->f = (v))
-#endif
-
 static inline int __snap(xqindex size)
 {
        if (!size)
@@ -16,7 +8,7 @@ static inline int __snap(xqindex size)
 }
 
 void xq_free(struct xq *xq) {
-       xq_mfree((void *)PTR(xq, queue));
+       xq_mfree((void *)XPTR(&xq->queue));
        memset(xq, 0, sizeof(struct xq));
 }
 
@@ -24,8 +16,9 @@ void xq_init_empty(struct xq *xq, xqindex size, void *mem)
 {
        xq->head = 1;
        xq->tail = 0;
-       PTRSET(xq, queue, mem);
+       XPTRSET(&xq->queue, mem);
        xq->size = __snap(size);
+       xq_release(&xq->lock);
 }
 
 void xq_init_map(struct xq *xq,
@@ -37,10 +30,11 @@ void xq_init_map(struct xq *xq,
        xqindex t, *qmem = mem;
        xq->head = count + 1;
        xq->tail = 0;
-       PTRSET(xq, queue, qmem);
+       XPTRSET(&xq->queue, qmem);
        xq->size = __snap(size);
        for (t = 0; t < count; t++)
                qmem[t] = mapfn(t);
+       xq_release(&xq->lock);
 }
 
 void xq_init_seq(struct xq *xq, xqindex size, xqindex count, void *mem)
@@ -48,10 +42,11 @@ void xq_init_seq(struct xq *xq, xqindex size, xqindex count, void *mem)
        xqindex t, *qmem = mem;
        xq->head = count + 1;
        xq->tail = 0;
-       PTRSET(xq, queue, qmem);
+       XPTRSET(&xq->queue, qmem);
        xq->size = __snap(size);
        for (t = 0; t < count; t++)
                qmem[t] = t;
+       xq_release(&xq->lock);
 }
 
 xqindex *xq_alloc_empty(struct xq *xq, xqindex size)
@@ -96,7 +91,7 @@ xqindex xq_count(struct xq *xq)
 
 xqindex xq_element(struct xq *xq, xqindex index)
 {
-       return PTR(xq, queue)[index & (xq->size - 1)];
+       return XPTR(&xq->queue)[index & (xq->size - 1)];
 }
 
 void xq_print(struct xq *xq)
@@ -141,7 +136,7 @@ xqindex xq_append_heads(struct xq *xq,
        mask = xq->size -1;
        head = __xq_append_head(xq, nr);
        for (i = 0; i < nr; i++)
-               PTR(xq, queue)[(head + i) & mask] = heads[i];
+               XPTR(&xq->queue)[(head + i) & mask] = heads[i];
 out:
        xq_release(&xq->lock);
        return serial;
@@ -155,7 +150,7 @@ xqindex xq_append_head(struct xq *xq, xqindex xqi)
                serial = None;
                goto out;
        }
-       PTR(xq, queue)[__xq_append_head(xq, 1) & (xq->size -1)] = xqi;
+       XPTR(&xq->queue)[__xq_append_head(xq, 1) & (xq->size -1)] = xqi;
 out:
        xq_release(&xq->lock);
        return serial;
@@ -163,9 +158,9 @@ out:
 
 xqindex __xq_pop_head(struct xq *xq, xqindex nr)
 {
-       xqindex head = xq->head;
-       xq->head = head - nr;
-       return head - nr;
+       xqindex head = xq->head - nr;
+       xq->head = head;
+       return head;
 }
 
 xqindex xq_pop_heads(struct xq *xq,
@@ -183,7 +178,7 @@ xqindex xq_pop_heads(struct xq *xq,
        mask = xq->size -1;
        head = __xq_pop_head(xq, nr);
        for (i = 0; i < nr; i++)
-               heads[i] = PTR(xq, queue)[(head - i) & mask];
+               heads[i] = XPTR(&xq->queue)[(head - i) & mask];
 out:
        xq_release(&xq->lock);
        return serial;
@@ -195,7 +190,7 @@ xqindex xq_pop_head(struct xq *xq)
        (void)xq_acquire(&xq->lock, 1);
        if (!xq_count(xq))
                goto out;
-       value = PTR(xq, queue)[__xq_pop_head(xq, 1) & (xq->size -1)];
+       value = XPTR(&xq->queue)[__xq_pop_head(xq, 1) & (xq->size -1)];
 out:
        xq_release(&xq->lock);
        return value;
@@ -203,9 +198,9 @@ out:
 
 xqindex __xq_append_tail(struct xq *xq, xqindex nr)
 {
-       xqindex tail = xq->tail;
-       xq->tail = tail - nr;
-       return tail;
+       xqindex tail = xq->tail - nr;
+       xq->tail = tail;
+       return tail + 1;
 }
 
 xqindex xq_append_tails(struct xq *xq,
@@ -221,9 +216,9 @@ xqindex xq_append_tails(struct xq *xq,
        }
 
        mask = xq->size -1;
-       tail = __xq_append_tail(xq, nr);
+       tail = __xq_append_tail(xq, nr) + nr -1;
        for (i = 0; i < nr; i++)
-               PTR(xq, queue)[(tail - i) & mask] = tails[i];
+               XPTR(&xq->queue)[(tail - i) & mask] = tails[i];
 out:
        xq_release(&xq->lock);
        return serial;
@@ -237,7 +232,7 @@ xqindex xq_append_tail(struct xq *xq, xqindex xqi)
                serial = None;
                goto out;
        }
-       PTR(xq, queue)[__xq_append_tail(xq, 1) & (xq->size -1)] = xqi;
+       XPTR(&xq->queue)[__xq_append_tail(xq, 1) & (xq->size -1)] = xqi;
 out:
        xq_release(&xq->lock);
        return serial;
@@ -247,7 +242,7 @@ xqindex __xq_pop_tail(struct xq *xq, xqindex nr)
 {
        xqindex tail = xq->tail;
        xq->tail = tail + nr;
-       return tail + 1;
+       return tail +1;
 }
 
 xqindex xq_pop_tails(struct xq *xq, xqindex nr, xqindex *tails)
@@ -263,7 +258,7 @@ xqindex xq_pop_tails(struct xq *xq, xqindex nr, xqindex *tails)
        mask = xq->size -1;
        tail = __xq_pop_tail(xq, nr);
        for (i = 0; i < nr; i++)
-               tails[i] = PTR(xq, queue)[(tail + i) & mask];
+               tails[i] = XPTR(&xq->queue)[(tail + i) & mask];
 out:
        xq_release(&xq->lock);
        return serial;
@@ -275,7 +270,7 @@ xqindex xq_pop_tail(struct xq *xq)
        (void)xq_acquire(&xq->lock, 1);
        if (!xq_count(xq))
                goto out;
-       value = PTR(xq, queue)[__xq_pop_tail(xq, 1) & (xq->size -1)];
+       value = XPTR(&xq->queue)[__xq_pop_tail(xq, 1) & (xq->size -1)];
 out:
        xq_release(&xq->lock);
        return value;
@@ -300,11 +295,11 @@ int xq_head_to_tail(struct xq *headq, struct xq *tailq, xqindex nr)
        tmask = tailq->size -1;
        head = __xq_pop_head(headq, nr);
        tail = __xq_append_tail(tailq, nr);
-       hq = PTR(headq, queue);
-       tq = PTR(tailq, queue);
+       hq = XPTR(&headq->queue);
+       tq = XPTR(&tailq->queue);
 
        for (i = 0; i < nr; i++)
-               tq[(tail - i) & tmask] = hq[(head - i) & hmask];
+               tq[(tail + i) & tmask] = hq[(head + i) & hmask];
 
        ret = 0;
 out:
@@ -313,9 +308,6 @@ out:
        return ret;
 }
 
-#undef PTR
-#undef PTRDEF
-
 #ifdef __KERNEL__
 #include <linux/module.h>
 #include <xq/xq_exports.h>