Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix parameter passing through makecontext (again).
[simgrid.git] / src / simix / smx_context_sysv.c
index 5c607b8..e5b66e1 100644 (file)
@@ -7,9 +7,11 @@
   * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include <stdarg.h>
+
 #include "smx_context_sysv_private.h"
 #include "xbt/parmap.h"
 #include "simix/private.h"
+#include "gras_config.h"
 
 #ifdef HAVE_VALGRIND_VALGRIND_H
 #  include <valgrind/valgrind.h>
@@ -32,6 +34,21 @@ smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
 
 static void smx_ctx_sysv_wrapper(int count, ...);
 
+/* This is a bit paranoid about SIZEOF_VOIDP not being a multiple of SIZEOF_INT,
+ * but it doesn't harm. */
+#define CTX_ADDR_LEN (SIZEOF_VOIDP / SIZEOF_INT + !!(SIZEOF_VOIDP % SIZEOF_INT))
+union u_ctx_addr {
+  void *addr;
+  int intv[CTX_ADDR_LEN];
+};
+#if (CTX_ADDR_LEN == 1)
+#  define CTX_ADDR_SPLIT(u) (u).intv[0]
+#elif (CTX_ADDR_LEN == 2)
+#  define CTX_ADDR_SPLIT(u) (u).intv[0], (u).intv[1]
+#else
+#  error Your architecture is not supported yet
+#endif
+
 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
 {
   smx_ctx_base_factory_init(factory);
@@ -71,7 +88,7 @@ smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
                                   void_pfn_smxprocess_t cleanup_func,
                                   void *data)
 {
-  uintptr_t ctx_addr;
+  union u_ctx_addr ctx_addr;
   smx_ctx_sysv_t context =
       (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
                                                                  code,
@@ -102,21 +119,9 @@ smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
                                 ((char *) context->uc.uc_stack.ss_sp) +
                                 context->uc.uc_stack.ss_size);
 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
-    ctx_addr = (uintptr_t)context;
-    /* This switch select a case base on a static value: the compiler optimizes it out */
-    /* It could be replaced by a set of #ifdef/#else/#endif blocks */
-    switch(sizeof(uintptr_t) / sizeof(int)) {
-      case 1:
-        makecontext(&((smx_ctx_sysv_t) context)->uc, (void (*)())smx_ctx_sysv_wrapper,
-                    2, 1, (int)ctx_addr);
-        break;
-      case 2:
-        makecontext(&((smx_ctx_sysv_t) context)->uc, (void (*)())smx_ctx_sysv_wrapper,
-                    3, 2, (int)(ctx_addr >> (8 * sizeof(int))), (int)(ctx_addr));
-        break;
-      default:
-        THROW_IMPOSSIBLE;
-    }
+    ctx_addr.addr = context;
+    makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
+                CTX_ADDR_LEN, CTX_ADDR_SPLIT(ctx_addr));
   }else{
     maestro_context = context;
   }
@@ -157,20 +162,21 @@ void smx_ctx_sysv_stop(smx_context_t context)
   smx_ctx_sysv_suspend(context);
 }
 
-void smx_ctx_sysv_wrapper(int count, ...)
+void smx_ctx_sysv_wrapper(int first, ...)
 { 
-  uintptr_t ctx_addr = 0;
-  va_list ap;
+  union u_ctx_addr ctx_addr;
   smx_ctx_sysv_t context;
-  int i;
 
-  va_start(ap, count);
-  for(i = 0; i < count; i++) {
-     ctx_addr <<= 8*sizeof(int);
-     ctx_addr |= (uintptr_t)va_arg(ap, int);
+  ctx_addr.intv[0] = first;
+  if (CTX_ADDR_LEN > 1) {
+    va_list ap;
+    int i;
+    va_start(ap, first);
+    for(i = 1; i < CTX_ADDR_LEN; i++)
+      ctx_addr.intv[i] = va_arg(ap, int);
+    va_end(ap);
   }
-  va_end(ap);
-  context = (smx_ctx_sysv_t)ctx_addr;
+  context = ctx_addr.addr;
   (context->super.code) (context->super.argc, context->super.argv);
 
   smx_ctx_sysv_stop((smx_context_t) context);