[Xenomai-git] Philippe Gerum : debug: use safe memory allocator during early code execution

2011-11-10 Thread GIT version control
Module: xenomai-gch
Branch: for-forge
Commit: 7bf542cd58eee99e80ee6c65166bb89550d435c3
URL:
http://git.xenomai.org/?p=xenomai-gch.git;a=commit;h=7bf542cd58eee99e80ee6c65166bb89550d435c3

Author: Philippe Gerum r...@xenomai.org
Date:   Tue Nov  8 07:30:27 2011 +0100

debug: use safe memory allocator during early code execution

When a failure happens while initializing the internal real-time
allocator (e.g. heapobj_init() in phsared mode not being able to
obtain a shared memory segment), calling xnmalloc() to get memory for
the backtrace records is a really bad idea.

This patch makes sure to use the safe malloc/free calls in the debug
support code until the base system has fully initialized.

---

 include/copperplate/debug.h |3 +++
 lib/copperplate/debug.c |   34 --
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/include/copperplate/debug.h b/include/copperplate/debug.h
index 2811e2b..375b2c0 100644
--- a/include/copperplate/debug.h
+++ b/include/copperplate/debug.h
@@ -75,6 +75,8 @@ char *__get_error_buf(size_t *sizep);
 
 int debug_pkg_init(void);
 
+void debug_pkg_activate(void);
+
 #ifdef __cplusplus
 }
 #endif
@@ -122,6 +124,7 @@ struct backtrace_data {
})
 
 #define debug_pkg_init()   ({ 0; })
+#define debug_pkg_activate()   ({ 0; })
 
 #endif /* !__XENO_DEBUG__ */
 
diff --git a/lib/copperplate/debug.c b/lib/copperplate/debug.c
index fce9455..eb80296 100644
--- a/lib/copperplate/debug.c
+++ b/lib/copperplate/debug.c
@@ -35,6 +35,30 @@ static struct backtrace_data main_btd = {
.name = main,
 };
 
+static void *safe_malloc(size_t size)
+{
+   return malloc(size);
+}
+
+static void safe_free(void *p)
+{
+   free(p);
+}
+
+static void *fast_malloc(size_t size)
+{
+   return xnmalloc(size);
+}
+
+static void fast_free(void *p)
+{
+   xnfree(p);
+}
+
+static void *(*do_malloc)(size_t size) = safe_malloc;
+
+static void (*do_free)(void *p) = safe_free;
+
 void __debug(struct threadobj *thobj, const char *fmt, ...)
 {
va_list ap;
@@ -57,7 +81,7 @@ void backtrace_log(int retval, const char *fn,
 
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, state);
 
-   ef = xnmalloc(sizeof(*ef));
+   ef = do_malloc(sizeof(*ef));
if (ef == NULL)
goto out;
 
@@ -93,7 +117,7 @@ static void flush_backtrace(struct backtrace_data *btd)
 
for (ef = btd-inner; ef; ef = nef) {
nef = ef-next;
-   xnfree(ef);
+   do_free(ef);
}
 
btd-inner = NULL;
@@ -181,3 +205,9 @@ int debug_pkg_init(void)
__RT(pthread_mutex_init(main_btd.lock, NULL));
return -pthread_key_create(btkey, NULL);
 }
+
+void debug_pkg_activate(void)
+{
+   do_malloc = fast_malloc;
+   do_free = fast_free;
+}


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


[Xenomai-git] Philippe Gerum : debug: use safe memory allocator during early code execution

2011-11-07 Thread GIT version control
Module: xenomai-forge
Branch: master
Commit: 7bf542cd58eee99e80ee6c65166bb89550d435c3
URL:
http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=7bf542cd58eee99e80ee6c65166bb89550d435c3

Author: Philippe Gerum r...@xenomai.org
Date:   Tue Nov  8 07:30:27 2011 +0100

debug: use safe memory allocator during early code execution

When a failure happens while initializing the internal real-time
allocator (e.g. heapobj_init() in phsared mode not being able to
obtain a shared memory segment), calling xnmalloc() to get memory for
the backtrace records is a really bad idea.

This patch makes sure to use the safe malloc/free calls in the debug
support code until the base system has fully initialized.

---

 include/copperplate/debug.h |3 +++
 lib/copperplate/debug.c |   34 --
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/include/copperplate/debug.h b/include/copperplate/debug.h
index 2811e2b..375b2c0 100644
--- a/include/copperplate/debug.h
+++ b/include/copperplate/debug.h
@@ -75,6 +75,8 @@ char *__get_error_buf(size_t *sizep);
 
 int debug_pkg_init(void);
 
+void debug_pkg_activate(void);
+
 #ifdef __cplusplus
 }
 #endif
@@ -122,6 +124,7 @@ struct backtrace_data {
})
 
 #define debug_pkg_init()   ({ 0; })
+#define debug_pkg_activate()   ({ 0; })
 
 #endif /* !__XENO_DEBUG__ */
 
diff --git a/lib/copperplate/debug.c b/lib/copperplate/debug.c
index fce9455..eb80296 100644
--- a/lib/copperplate/debug.c
+++ b/lib/copperplate/debug.c
@@ -35,6 +35,30 @@ static struct backtrace_data main_btd = {
.name = main,
 };
 
+static void *safe_malloc(size_t size)
+{
+   return malloc(size);
+}
+
+static void safe_free(void *p)
+{
+   free(p);
+}
+
+static void *fast_malloc(size_t size)
+{
+   return xnmalloc(size);
+}
+
+static void fast_free(void *p)
+{
+   xnfree(p);
+}
+
+static void *(*do_malloc)(size_t size) = safe_malloc;
+
+static void (*do_free)(void *p) = safe_free;
+
 void __debug(struct threadobj *thobj, const char *fmt, ...)
 {
va_list ap;
@@ -57,7 +81,7 @@ void backtrace_log(int retval, const char *fn,
 
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, state);
 
-   ef = xnmalloc(sizeof(*ef));
+   ef = do_malloc(sizeof(*ef));
if (ef == NULL)
goto out;
 
@@ -93,7 +117,7 @@ static void flush_backtrace(struct backtrace_data *btd)
 
for (ef = btd-inner; ef; ef = nef) {
nef = ef-next;
-   xnfree(ef);
+   do_free(ef);
}
 
btd-inner = NULL;
@@ -181,3 +205,9 @@ int debug_pkg_init(void)
__RT(pthread_mutex_init(main_btd.lock, NULL));
return -pthread_key_create(btkey, NULL);
 }
+
+void debug_pkg_activate(void)
+{
+   do_malloc = fast_malloc;
+   do_free = fast_free;
+}


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