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

Author: Philippe Gerum <r...@xenomai.org>
Date:   Sun Apr 20 11:22:29 2014 +0200

cobalt/kernel: introduce knamecpy()

---

 include/cobalt/kernel/ancillaries.h |   13 +++++++++++++
 include/cobalt/kernel/registry.h    |    6 ------
 kernel/cobalt/clock.c               |    4 ++--
 kernel/cobalt/heap.c                |    2 +-
 kernel/cobalt/posix/thread.c        |    3 +--
 kernel/cobalt/sched.c               |    4 ++--
 kernel/cobalt/timer.c               |    9 ++-------
 7 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/include/cobalt/kernel/ancillaries.h 
b/include/cobalt/kernel/ancillaries.h
index 88c0afb..312c181 100644
--- a/include/cobalt/kernel/ancillaries.h
+++ b/include/cobalt/kernel/ancillaries.h
@@ -20,6 +20,8 @@
 #define _COBALT_KERNEL_ANCILLARIES_H
 
 #include <linux/kernel.h>
+#include <linux/string.h>
+#include <cobalt/uapi/kernel/limits.h>
 
 #define ksformat(__dst, __len, __fmt, __args...)                       \
        ({                                                              \
@@ -49,4 +51,15 @@
                kvasprintf(GFP_KERNEL, __fmt, __ap);                    \
        })
 
+void __knamecpy_requires_character_array_as_destination(void);
+
+#define knamecpy(__dst, __src)                                         \
+       ({                                                              \
+               if (!__builtin_types_compatible_p(typeof(__dst), char[])) \
+                       __knamecpy_requires_character_array_as_destination();   
\
+               strncpy((__dst), __src, sizeof(__dst) - 1);             \
+               __dst[sizeof(__dst) - 1] = '\0';                        \
+               __dst;                                                  \
+        })
+       
 #endif /* !_COBALT_KERNEL_ANCILLARIES_H */
diff --git a/include/cobalt/kernel/registry.h b/include/cobalt/kernel/registry.h
index 9c80352..d0109c5 100644
--- a/include/cobalt/kernel/registry.h
+++ b/include/cobalt/kernel/registry.h
@@ -50,12 +50,6 @@ struct xnobject {
        struct list_head link;
 };
 
-static inline void xnobject_copy_name(char *dst, const char *src)
-{
-       strncpy(dst, src ?: "", XNOBJECT_NAME_LEN-1);
-       dst[XNOBJECT_NAME_LEN-1] = '\0';
-}
-
 int xnregistry_init(void);
 
 void xnregistry_cleanup(void);
diff --git a/kernel/cobalt/clock.c b/kernel/cobalt/clock.c
index 71fae16..a2553d1 100644
--- a/kernel/cobalt/clock.c
+++ b/kernel/cobalt/clock.c
@@ -385,8 +385,8 @@ static int clock_vfile_next(struct 
xnvfile_snapshot_iterator *it, void *data)
        p->timeout = xntimer_get_timeout(timer);
        p->interval = xntimer_get_interval(timer);
        p->status = timer->status;
-       strncpy(p->handler, timer->handler_name, 16)[16] = '\0';
-       xnobject_copy_name(p->name, timer->name);
+       knamecpy(p->handler, timer->handler_name);
+       knamecpy(p->name, timer->name);
 
        return 1;
 }
diff --git a/kernel/cobalt/heap.c b/kernel/cobalt/heap.c
index dcc7129..f267976 100644
--- a/kernel/cobalt/heap.c
+++ b/kernel/cobalt/heap.c
@@ -142,7 +142,7 @@ static int vfile_next(struct xnvfile_snapshot_iterator *it, 
void *data)
        p->usable_mem = xnheap_usable_mem(heap);
        p->used_mem = xnheap_used_mem(heap);
        p->page_size = xnheap_page_size(heap);
-       strncpy(p->label, heap->label, sizeof(p->label));
+       knamecpy(p->label, heap->label);
 
        return 1;
 }
diff --git a/kernel/cobalt/posix/thread.c b/kernel/cobalt/posix/thread.c
index 1d547cf..cd5376b 100644
--- a/kernel/cobalt/posix/thread.c
+++ b/kernel/cobalt/posix/thread.c
@@ -1056,8 +1056,7 @@ int cobalt_thread_set_name_np(unsigned long pth, const 
char __user *u_name)
 
        xnlock_put_irqrestore(&nklock, s);
 
-       strncpy(p->comm, name, sizeof(p->comm));
-       p->comm[sizeof(p->comm) - 1] = '\0';
+       knamecpy(p->comm, name);
        put_task_struct(p);
 
        return 0;
diff --git a/kernel/cobalt/sched.c b/kernel/cobalt/sched.c
index 61d0580..41aed20 100644
--- a/kernel/cobalt/sched.c
+++ b/kernel/cobalt/sched.c
@@ -908,8 +908,8 @@ static int vfile_schedlist_next(struct 
xnvfile_snapshot_iterator *it,
        memcpy(p->name, thread->name, sizeof(p->name));
        p->cprio = thread->cprio;
        p->state = xnthread_state_flags(thread);
-       xnobject_copy_name(p->sched_class, thread->sched_class->name);
-       xnobject_copy_name(p->personality, thread->personality->name);
+       knamecpy(p->sched_class, thread->sched_class->name);
+       knamecpy(p->personality, thread->personality->name);
        period = xnthread_get_period(thread);
        timeout = xnthread_get_timeout(thread, priv->start_time);
        /*
diff --git a/kernel/cobalt/timer.c b/kernel/cobalt/timer.c
index e493ac6..ac3aa9f 100644
--- a/kernel/cobalt/timer.c
+++ b/kernel/cobalt/timer.c
@@ -347,13 +347,8 @@ void __xntimer_init(struct xntimer *timer,
 #ifdef CONFIG_XENO_OPT_EXTCLOCK
        timer->tracker = clock;
 #endif
-       if (!xnsched_current_thread() || !xnsched_root_p())
-               ksformat(timer->name, XNOBJECT_NAME_LEN, "%d/%s",
-                        current->pid, current->comm);
-       else
-               xnobject_copy_name(timer->name,
-                                  xnsched_current_thread()->name);
-
+       ksformat(timer->name, XNOBJECT_NAME_LEN, "%d/%s",
+                current->pid, current->comm);
        xntimer_reset_stats(timer);
        xnlock_get_irqsave(&nklock, s);
        list_add_tail(&timer->next_stat, &clock->statq);


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://www.xenomai.org/mailman/listinfo/xenomai-git

Reply via email to