Module: xenomai-forge
Branch: master
Commit: a759d045871bdcdb8378b91f431b8822b7492f72
URL:    
http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=a759d045871bdcdb8378b91f431b8822b7492f72

Author: Philippe Gerum <r...@xenomai.org>
Date:   Sun Nov 27 17:21:54 2011 +0100

alchemy: factor out object lookup routines

---

 lib/alchemy/alarm.c    |   11 +-------
 lib/alchemy/buffer.c   |   33 +-------------------------
 lib/alchemy/cond.c     |   19 +--------------
 lib/alchemy/event.c    |   33 +-------------------------
 lib/alchemy/heap.c     |   33 +-------------------------
 lib/alchemy/internal.h |   60 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/alchemy/mutex.c    |   19 +--------------
 lib/alchemy/pipe.c     |   21 +---------------
 lib/alchemy/queue.c    |   33 +-------------------------
 lib/alchemy/sem.c      |   19 +--------------
 lib/alchemy/task.c     |    8 ++----
 11 files changed, 74 insertions(+), 215 deletions(-)

diff --git a/lib/alchemy/alarm.c b/lib/alchemy/alarm.c
index 785e04a..4dc6ab3 100644
--- a/lib/alchemy/alarm.c
+++ b/lib/alchemy/alarm.c
@@ -36,20 +36,13 @@ static struct alchemy_alarm *get_alchemy_alarm(RT_ALARM 
*alarm, int *err_r)
 {
        struct alchemy_alarm *acb;
 
-       if (alarm == NULL || ((intptr_t)alarm & (sizeof(intptr_t)-1)) != 0)
+       if (bad_pointer(alarm))
                goto bad_handle;
 
        acb = (struct alchemy_alarm *)alarm->handle;
-       if (acb == NULL || ((intptr_t)acb & (sizeof(intptr_t)-1)) != 0)
+       if (bad_pointer(acb) || timerobj_lock(&acb->tmobj))
                goto bad_handle;
 
-       if (acb->magic != alarm_magic)
-               goto bad_handle;
-
-       if (timerobj_lock(&acb->tmobj))
-               goto bad_handle;
-
-       /* Recheck under lock. */
        if (acb->magic == alarm_magic)
                return acb;
 bad_handle:
diff --git a/lib/alchemy/buffer.c b/lib/alchemy/buffer.c
index 32a922d..5447d71 100644
--- a/lib/alchemy/buffer.c
+++ b/lib/alchemy/buffer.c
@@ -32,38 +32,7 @@ static struct alchemy_namegen buffer_namegen = {
        .length = sizeof ((struct alchemy_buffer *)0)->name,
 };
 
-static struct alchemy_buffer *get_alchemy_buffer(RT_BUFFER *bf,
-                                                struct syncstate *syns, int 
*err_r)
-{
-       struct alchemy_buffer *bcb;
-
-       if (bf == NULL || ((intptr_t)bf & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       bcb = mainheap_deref(bf->handle, struct alchemy_buffer);
-       if (bcb == NULL || ((intptr_t)bcb & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       if (bcb->magic != buffer_magic)
-               goto bad_handle;
-
-       if (syncobj_lock(&bcb->sobj, syns))
-               goto bad_handle;
-
-       /* Recheck under lock. */
-       if (bcb->magic == buffer_magic)
-               return bcb;
-bad_handle:
-       *err_r = -EINVAL;
-
-       return NULL;
-}
-
-static inline void put_alchemy_buffer(struct alchemy_buffer *bcb,
-                                     struct syncstate *syns)
-{
-       syncobj_unlock(&bcb->sobj, syns);
-}
+DEFINE_SYNC_LOOKUP(buffer, RT_BUFFER);
 
 static void buffer_finalize(struct syncobj *sobj)
 {
diff --git a/lib/alchemy/cond.c b/lib/alchemy/cond.c
index ce8fd38..baeaa5e 100644
--- a/lib/alchemy/cond.c
+++ b/lib/alchemy/cond.c
@@ -37,24 +37,7 @@ static struct alchemy_namegen cond_namegen = {
        .length = sizeof ((struct alchemy_cond *)0)->name,
 };
 
-static struct alchemy_cond *find_alchemy_cond(RT_COND *cond, int *err_r)
-{
-       struct alchemy_cond *ccb;
-
-       if (cond == NULL || ((intptr_t)cond & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       ccb = mainheap_deref(cond->handle, struct alchemy_cond);
-       if (ccb == NULL || ((intptr_t)ccb & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       if (ccb->magic == cond_magic)
-               return ccb;
-bad_handle:
-       *err_r = -EINVAL;
-
-       return NULL;
-}
+DEFINE_LOOKUP_PRIVATE(cond, RT_COND);
 
 int rt_cond_create(RT_COND *cond, const char *name)
 {
diff --git a/lib/alchemy/event.c b/lib/alchemy/event.c
index bfac429..c637290 100644
--- a/lib/alchemy/event.c
+++ b/lib/alchemy/event.c
@@ -32,38 +32,7 @@ static struct alchemy_namegen event_namegen = {
        .length = sizeof ((struct alchemy_event *)0)->name,
 };
 
-static struct alchemy_event *get_alchemy_event(RT_EVENT *event,
-                                              struct syncstate *syns, int 
*err_r)
-{
-       struct alchemy_event *evcb;
-
-       if (event == NULL || ((intptr_t)event & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       evcb = mainheap_deref(event->handle, struct alchemy_event);
-       if (evcb == NULL || ((intptr_t)evcb & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       if (evcb->magic != event_magic)
-               goto bad_handle;
-
-       if (syncobj_lock(&evcb->sobj, syns))
-               goto bad_handle;
-
-       /* Recheck under lock. */
-       if (evcb->magic == event_magic)
-               return evcb;
-bad_handle:
-       *err_r = -EINVAL;
-
-       return NULL;
-}
-
-static inline void put_alchemy_event(struct alchemy_event *evcb,
-                                    struct syncstate *syns)
-{
-       syncobj_unlock(&evcb->sobj, syns);
-}
+DEFINE_SYNC_LOOKUP(event, RT_EVENT);
 
 static void event_finalize(struct syncobj *sobj)
 {
diff --git a/lib/alchemy/heap.c b/lib/alchemy/heap.c
index 72b3793..96e42ee 100644
--- a/lib/alchemy/heap.c
+++ b/lib/alchemy/heap.c
@@ -32,38 +32,7 @@ static struct alchemy_namegen heap_namegen = {
        .length = sizeof ((struct alchemy_heap *)0)->name,
 };
 
-static struct alchemy_heap *get_alchemy_heap(RT_HEAP *heap,
-                                            struct syncstate *syns, int *err_r)
-{
-       struct alchemy_heap *hcb;
-
-       if (heap == NULL || ((intptr_t)heap & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       hcb = mainheap_deref(heap->handle, struct alchemy_heap);
-       if (hcb == NULL || ((intptr_t)hcb & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       if (hcb->magic != heap_magic)
-               goto bad_handle;
-
-       if (syncobj_lock(&hcb->sobj, syns))
-               goto bad_handle;
-
-       /* Recheck under lock. */
-       if (hcb->magic == heap_magic)
-               return hcb;
-bad_handle:
-       *err_r = -EINVAL;
-
-       return NULL;
-}
-
-static inline void put_alchemy_heap(struct alchemy_heap *hcb,
-                                   struct syncstate *syns)
-{
-       syncobj_unlock(&hcb->sobj, syns);
-}
+DEFINE_SYNC_LOOKUP(heap, RT_HEAP);
 
 static void heap_finalize(struct syncobj *sobj)
 {
diff --git a/lib/alchemy/internal.h b/lib/alchemy/internal.h
index 60d8e34..de3675c 100644
--- a/lib/alchemy/internal.h
+++ b/lib/alchemy/internal.h
@@ -27,6 +27,66 @@ struct alchemy_namegen {
        int serial;
 };
 
+#define DEFINE_SYNC_LOOKUP(__name, __dsctype)                          \
+static inline struct alchemy_ ## __name *                              \
+get_alchemy_ ## __name(__dsctype *desc,                                        
\
+                      struct syncstate *syns, int *err_r)              \
+{                                                                      \
+       struct alchemy_ ## __name *cb;                                  \
+                                                                       \
+       if (bad_pointer(desc)) {                                        \
+               *err_r = -EINVAL;                                       \
+               return NULL;                                            \
+       }                                                               \
+                                                                       \
+       cb = mainheap_deref(desc->handle, struct alchemy_ ## __name);   \
+       if (bad_pointer(cb)) {                                          \
+               *err_r = -EINVAL;                                       \
+               return NULL;                                            \
+       }                                                               \
+                                                                       \
+       if (syncobj_lock(&cb->sobj, syns) ||                            \
+           cb->magic != __name ## _magic) {                            \
+               *err_r = -EINVAL;                                       \
+               return NULL;                                            \
+       }                                                               \
+                                                                       \
+       return cb;                                                      \
+}                                                                      \
+                                                                       \
+static inline                                                          \
+void put_alchemy_ ## __name(struct alchemy_ ## __name *cb,             \
+                           struct syncstate *syns)                     \
+{                                                                      \
+       syncobj_unlock(&cb->sobj, syns);                                \
+}
+
+#define __DEFINE_LOOKUP(__scope, __name, __dsctype)                    \
+__scope struct alchemy_ ## __name *                                    \
+find_alchemy_ ## __name(__dsctype *desc, int *err_r)                   \
+{                                                                      \
+       struct alchemy_ ## __name *cb;                                  \
+                                                                       \
+       if (bad_pointer(desc)) {                                        \
+               *err_r = -EINVAL;                                       \
+               return NULL;                                            \
+       }                                                               \
+                                                                       \
+       cb = mainheap_deref(desc->handle, struct alchemy_ ## __name);   \
+       if (bad_pointer(cb) || cb->magic != __name ## _magic) {         \
+               *err_r = -EINVAL;                                       \
+               return NULL;                                            \
+       }                                                               \
+                                                                       \
+       return cb;                                                      \
+}                                                                      \
+
+#define DEFINE_LOOKUP_PRIVATE(__name, __dsctype)                       \
+       __DEFINE_LOOKUP(static inline, __name, __dsctype)
+
+#define DEFINE_LOOKUP(__name, __dsctype)                               \
+       __DEFINE_LOOKUP(, __name, __dsctype)
+
 struct syncluster;
 
 char *alchemy_build_name(char *buf, const char *name,
diff --git a/lib/alchemy/mutex.c b/lib/alchemy/mutex.c
index 4e111c2..aedc330 100644
--- a/lib/alchemy/mutex.c
+++ b/lib/alchemy/mutex.c
@@ -37,24 +37,7 @@ static struct alchemy_namegen mutex_namegen = {
        .length = sizeof ((struct alchemy_mutex *)0)->name,
 };
 
-struct alchemy_mutex *find_alchemy_mutex(RT_MUTEX *mutex, int *err_r)
-{
-       struct alchemy_mutex *mcb;
-
-       if (mutex == NULL || ((intptr_t)mutex & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       mcb = mainheap_deref(mutex->handle, struct alchemy_mutex);
-       if (mcb == NULL || ((intptr_t)mcb & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       if (mcb->magic == mutex_magic)
-               return mcb;
-bad_handle:
-       *err_r = -EINVAL;
-
-       return NULL;
-}
+DEFINE_LOOKUP(mutex, RT_MUTEX);
 
 int rt_mutex_create(RT_MUTEX *mutex, const char *name)
 {
diff --git a/lib/alchemy/pipe.c b/lib/alchemy/pipe.c
index e3bfbab..db93d3b 100644
--- a/lib/alchemy/pipe.c
+++ b/lib/alchemy/pipe.c
@@ -36,24 +36,7 @@ static struct alchemy_namegen pipe_namegen = {
        .length = sizeof ((struct alchemy_pipe *)0)->name,
 };
 
-static struct alchemy_pipe *find_alchemy_pipe(RT_PIPE *pipe, int *err_r)
-{
-       struct alchemy_pipe *pcb;
-
-       if (pipe == NULL || ((intptr_t)pipe & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       pcb = mainheap_deref(pipe->handle, struct alchemy_pipe);
-       if (pcb == NULL || ((intptr_t)pcb & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       if (pcb->magic == pipe_magic)
-               return pcb;
-bad_handle:
-       *err_r = -EINVAL;
-
-       return NULL;
-}
+DEFINE_LOOKUP_PRIVATE(pipe, RT_PIPE);
 
 int rt_pipe_create(RT_PIPE *pipe,
                   const char *name, int minor, size_t poolsize)
@@ -212,7 +195,7 @@ static ssize_t do_write_pipe(RT_PIPE *pipe,
        struct alchemy_pipe *pcb;
        struct service svc;
        ssize_t ret;
-       int err;
+       int err = 0;
 
        COPPERPLATE_PROTECT(svc);
 
diff --git a/lib/alchemy/queue.c b/lib/alchemy/queue.c
index d78da8b..65c5164 100644
--- a/lib/alchemy/queue.c
+++ b/lib/alchemy/queue.c
@@ -32,38 +32,7 @@ static struct alchemy_namegen queue_namegen = {
        .length = sizeof ((struct alchemy_queue *)0)->name,
 };
 
-static struct alchemy_queue *get_alchemy_queue(RT_QUEUE *queue,
-                                              struct syncstate *syns, int 
*err_r)
-{
-       struct alchemy_queue *qcb;
-
-       if (queue == NULL || ((intptr_t)queue & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       qcb = mainheap_deref(queue->handle, struct alchemy_queue);
-       if (qcb == NULL || ((intptr_t)qcb & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       if (qcb->magic != queue_magic)
-               goto bad_handle;
-
-       if (syncobj_lock(&qcb->sobj, syns))
-               goto bad_handle;
-
-       /* Recheck under lock. */
-       if (qcb->magic == queue_magic)
-               return qcb;
-bad_handle:
-       *err_r = -EINVAL;
-
-       return NULL;
-}
-
-static inline void put_alchemy_queue(struct alchemy_queue *qcb,
-                                    struct syncstate *syns)
-{
-       syncobj_unlock(&qcb->sobj, syns);
-}
+DEFINE_SYNC_LOOKUP(queue, RT_QUEUE);
 
 static void queue_finalize(struct syncobj *sobj)
 {
diff --git a/lib/alchemy/sem.c b/lib/alchemy/sem.c
index 375e2b9..0d5dd3c 100644
--- a/lib/alchemy/sem.c
+++ b/lib/alchemy/sem.c
@@ -32,24 +32,7 @@ static struct alchemy_namegen sem_namegen = {
        .length = sizeof ((struct alchemy_sem *)0)->name,
 };
 
-static struct alchemy_sem *find_alchemy_sem(RT_SEM *sem, int *err_r)
-{
-       struct alchemy_sem *scb;
-
-       if (sem == NULL || ((intptr_t)sem & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       scb = mainheap_deref(sem->handle, struct alchemy_sem);
-       if (scb == NULL || ((intptr_t)scb & (sizeof(intptr_t)-1)) != 0)
-               goto bad_handle;
-
-       if (scb->magic == sem_magic)
-               return scb;
-bad_handle:
-       *err_r = -EINVAL;
-
-       return NULL;
-}
+DEFINE_LOOKUP_PRIVATE(sem, RT_SEM);
 
 static void sem_finalize(struct semobj *smobj)
 {
diff --git a/lib/alchemy/task.c b/lib/alchemy/task.c
index 81b16f4..f418cb7 100644
--- a/lib/alchemy/task.c
+++ b/lib/alchemy/task.c
@@ -52,17 +52,15 @@ static void delete_tcb(struct alchemy_task *tcb);
 static struct alchemy_task *find_alchemy_task(RT_TASK *task, int *err_r)
 {
        struct alchemy_task *tcb;
-       unsigned int magic;
 
-       if (task == NULL || ((intptr_t)task & (sizeof(intptr_t)-1)) != 0)
+       if (bad_pointer(task))
                goto bad_handle;
 
        tcb = mainheap_deref(task->handle, struct alchemy_task);
-       if (tcb == NULL || ((intptr_t)tcb & (sizeof(intptr_t)-1)) != 0)
+       if (bad_pointer(tcb))
                goto bad_handle;
 
-       magic = threadobj_get_magic(&tcb->thobj);
-       if (magic == task_magic)
+       if (threadobj_get_magic(&tcb->thobj) == task_magic)
                return tcb;
 bad_handle:
        *err_r = -EINVAL;


_______________________________________________
Xenomai-git mailing list
Xenomai-git@gna.org
https://mail.gna.org/listinfo/xenomai-git

Reply via email to