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

Author: Philippe Gerum <r...@xenomai.org>
Date:   Wed Oct 23 15:37:19 2013 +0200

boilerplate/ancillaries: introduce simple name generator

---

 include/boilerplate/ancillaries.h |   16 ++++++++++++++++
 lib/boilerplate/ancillaries.c     |   18 ++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/include/boilerplate/ancillaries.h 
b/include/boilerplate/ancillaries.h
index 1330762..fc04f2f 100644
--- a/include/boilerplate/ancillaries.h
+++ b/include/boilerplate/ancillaries.h
@@ -29,6 +29,19 @@ extern pthread_mutex_t __printlock;
 struct error_frame;
 struct cleanup_block;
 
+struct name_generator {
+       const char *radix;
+       int length;
+       int serial;
+};
+
+#define DEFINE_NAME_GENERATOR(__name, __radix, __type, __member)       \
+       struct name_generator __name = {                                \
+               .radix = __radix,                                       \
+               .length = sizeof ((__type *)0)->__member,               \
+               .serial = 1,                                            \
+       }
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -51,6 +64,9 @@ void warning(const char *fmt, ...);
 
 const char *symerror(int errnum);
 
+char *generate_name(char *buf, const char *radix,
+                   struct name_generator *ngen);
+
 void error_hook(struct error_frame *ef);
 
 void boilerplate_init(void);
diff --git a/lib/boilerplate/ancillaries.c b/lib/boilerplate/ancillaries.c
index 51dd39b..9b5dc48 100644
--- a/lib/boilerplate/ancillaries.c
+++ b/lib/boilerplate/ancillaries.c
@@ -22,7 +22,9 @@
 #include <stdarg.h>
 #include <pthread.h>
 #include <unistd.h>
+#include <string.h>
 #include <errno.h>
+#include "nocore/atomic.h"
 #include "boilerplate/lock.h"
 #include "boilerplate/time.h"
 #include "boilerplate/ancillaries.h"
@@ -167,6 +169,22 @@ void warning(const char *fmt, ...)
        va_end(ap);
 }
 
+char *generate_name(char *buf, const char *radix,
+                   struct name_generator *ngen)
+{
+       int len = ngen->length - 1, tag;
+
+       if (radix && *radix) {
+               strncpy(buf, radix, len);
+               buf[len] = '\0';
+       } else {
+               tag = atomic_add_fetch(ngen->serial, 1);
+               snprintf(buf, len, "%s@%d", ngen->radix, tag);
+       }
+
+       return buf;
+}
+
 #ifdef __XENO_DEBUG__
 
 int __check_cancel_type(const char *locktype)


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

Reply via email to