[PATCH] Add pre global constructors hook

Aurelio Remonda aurelio.remonda at tallertechnologies.com
Wed Nov 18 15:06:30 UTC 2015


Having an optional function that could be called prior to the construction of any global
objects would be very useful.
This is being discussed in the C++Embedded mailing list, in this thread:
http://www.open-std.org/pipermail/embedded/2014-November/000226.html

For example, we would like to use this function to set an error handler for
operator new, so that the handler is called if we run out of memory during
global objects construction.

This patch adds a pre_global_ctors function which is called right before the
construction of global objects. The default implementation of this function is empty, 
and the function itself is declared with the 'weak' linker attribute. 
Users can then provide their own implementation of pre_global_ctors, which will override
the default one.

Any feedback or suggestions is welcome.
Best regards,Aurelio.

---
 cpukit/score/src/threadglobalconstruction.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/cpukit/score/src/threadglobalconstruction.c b/cpukit/score/src/threadglobalconstruction.c
index 05a8613..3efc61c 100644
--- a/cpukit/score/src/threadglobalconstruction.c
+++ b/cpukit/score/src/threadglobalconstruction.c
@@ -46,6 +46,13 @@
   #define EXECUTE_GLOBAL_CONSTRUCTORS
 #endif
 
+/* 
+ * pre_globals_ctors is always called before global constructors.
+ * the default implementation does nothing, and it has the weak
+ * attribute so users can override it. 
+ */
+extern __attribute__((weak)) void pre_global_ctors(){/**/}
+
 void *_Thread_Global_construction( void )
 {
   Thread_Control *executing;
@@ -57,6 +64,7 @@ void *_Thread_Global_construction( void )
    *  in any configuration I know of and it generates a warning on every
    *  RTEMS target configuration.  --joel (12 May 2007)
    */
+  pre_global_ctors();
   INIT_NAME();
 #endif
 
-- 
1.9.1



More information about the devel mailing list