relative pointers
authorgtsouk <gtsouk@cslab.ece.ntua.gr>
Sat, 18 Feb 2012 16:34:35 +0000 (18:34 +0200)
committergtsouk <gtsouk@cslab.ece.ntua.gr>
Sat, 18 Feb 2012 16:34:35 +0000 (18:34 +0200)
xseg/sys/Makefile
xseg/sys/util.h
xseg/xq/Makefile
xseg/xq/xq.c
xseg/xq/xq.h
xseg/xseg/xseg.h

index 16000b2..b5df8e3 100644 (file)
@@ -8,7 +8,7 @@ endif
 
 KDIR := /lib/modules/$(shell uname -r)/build
 PWD := $(shell pwd)
-EXTRA_CFLAGS += -I$(BASE) -DRELATIVE_POINTERS
+EXTRA_CFLAGS += -I$(BASE)
 
 xseg-objs := xsegmod.o xq.k.o xseg.k.o 
 obj-m += xsegbd.o xsegdev.o xseg.o
index 8b023b0..fbb9549 100644 (file)
@@ -26,4 +26,34 @@ void xq_mfree(void *ptr);
 #define LOGMSG(...) xseg_snprintf(__xseg_errbuf, 4096, FMTARG("%s: ", __func__, ## __VA_ARGS__, "")), \
                     __xseg_errbuf[4095] = 0, __xseg_log(__xseg_errbuf)
 
+typedef uint64_t xpointer;
+
+#define None ((xqindex)-1)
+#define Null ((xpointer)-1)
+
+
+#define XPTR_TYPE(ptrtype)     \
+       union {                 \
+               ptrtype *t;     \
+               xpointer x;     \
+       }
+
+#define XPTRI(xptraddr)                \
+       (       (xpointer)(unsigned long)(xptraddr) +   \
+               (xptraddr)->x                           )
+
+#define XPTRISET(xptraddr, ptrval)     \
+       ((xptraddr)->x  =       (xpointer)(ptrval) -                    \
+                               (xpointer)(unsigned long)(xptraddr)     )
+
+#define XPTR(xptraddr)         \
+       (       (typeof((xptraddr)->t))                         \
+               (unsigned long)                                 \
+               (       (xpointer)(unsigned long)(xptraddr) +   \
+                       (xptraddr)->x           )               )
+
+#define XPTRSET(xptraddr, ptrval)      \
+       ((xptraddr)->x  =       (xpointer)(unsigned long)(ptrval) -     \
+                               (xpointer)(unsigned long)(xptraddr)     )
+
 #endif
index 31e741f..5c136c3 100644 (file)
@@ -18,10 +18,10 @@ xq_lock_test: xq_lock_test.c xq.o $(BASE)/sys/xseg_user.o
        $(CC) $(CFLAGS) -o $@ $< xq.o $(BASE)/sys/xseg_user.o -lrt -lm -ldl -lpthread
 
 xq.o: xq.c xq.h xq_lock.h
-       $(CC) $(CFLAGS) -DRELATIVE_POINTERS -c -o $@ $<
+       $(CC) $(CFLAGS) -c -o $@ $<
 
 xq.pic.o: xq.c xq.h xq_lock.h
-       $(CC) $(CFLAGS) -DRELATIVE_POINTERS -fPIC -c -o $@ $<
+       $(CC) $(CFLAGS) -fPIC -c -o $@ $<
 
 clean:
        rm -f xq_lock_test xq_test xq.o xq.pic.o
index 6bc3530..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,7 +16,7 @@ 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);
 }
@@ -38,7 +30,7 @@ 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);
@@ -50,7 +42,7 @@ 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;
@@ -99,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)
@@ -144,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;
@@ -158,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;
@@ -186,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;
@@ -198,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;
@@ -226,7 +218,7 @@ xqindex xq_append_tails(struct xq *xq,
        mask = xq->size -1;
        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;
@@ -240,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;
@@ -266,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;
@@ -278,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;
@@ -303,8 +295,8 @@ 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];
@@ -316,9 +308,6 @@ out:
        return ret;
 }
 
-#undef PTR
-#undef PTRDEF
-
 #ifdef __KERNEL__
 #include <linux/module.h>
 #include <xq/xq_exports.h>
index f93eeb6..03910d8 100644 (file)
@@ -1,17 +1,16 @@
 #ifndef _XQ_H
 #define _XQ_H
 
-typedef unsigned int xqindex;
+#include <sys/util.h>
 
-#define None (xqindex)-1
+typedef uint32_t xqindex;
 
-#include <sys/util.h>
 #include "xq_lock.h"
 
 struct xq {
         struct xq_lock lock;
         xqindex head, tail;
-        xqindex *queue;
+        XPTR_TYPE(xqindex) queue;
         xqindex size;
 };
 
index b338d96..1dd907a 100644 (file)
@@ -145,12 +145,13 @@ struct xseg_task {
 #define XF_FUA    (1 << 2)
 
 /* STATES */
-#define XS_SERVED   (1 << 0)
-#define XS_ERROR    (1 << 1)
+#define XS_SERVED      (1 << 0)
+#define XS_FAILED      (1 << 1)
 
-#define XS_ACCEPTED (1 << 2)
-#define XS_PENDING  (2 << 2)
-#define XS_FINISHED (3 << 2)
+#define XS_ACCEPTED    (1 << 2)
+#define XS_PENDING     (2 << 2)
+#define XS_SERVING     (3 << 2)
+#define XS_CONCLUDED   (3 << 2)
 
 struct xseg_request {
        xserial serial;
@@ -174,7 +175,7 @@ struct xseg_request {
 
 struct xseg_shared {
        uint64_t flags;
-       char (*peer_types)[XSEG_TNAMESIZE];
+       char (*peer_types)[XSEG_TNAMESIZE]; /* alignment? */
        uint32_t nr_peer_types;
 };