Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Inline the functions used in xbt_dynar_foreach
authormquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Thu, 20 Aug 2009 13:53:32 +0000 (13:53 +0000)
committermquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Thu, 20 Aug 2009 13:53:32 +0000 (13:53 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@6617 48e7efb5-ca39-0410-a469-dd3cf9ba447f

include/xbt/dynar.h
src/gras/DataDesc/ddt_create.c
src/gras/Transport/transport.c
src/xbt/dynar.c
src/xbt/dynar_private.h [deleted file]

index 6d7b090..351a3e8 100644 (file)
@@ -10,6 +10,7 @@
 #ifndef _XBT_DYNAR_H
 #define _XBT_DYNAR_H
 
+#include <string.h> /* memcpy */
 #include "xbt/misc.h"           /* SG_BEGIN_DECL */
 #include "xbt/function_types.h"
 
@@ -181,17 +182,57 @@ XBT_PUBLIC(void *) xbt_dynar_pop_ptr(xbt_dynar_t const dynar);
  *  @{
  */
 
-XBT_PUBLIC(void) _xbt_dynar_cursor_first(const xbt_dynar_t dynar,
-                                         unsigned int *const cursor);
-XBT_PUBLIC(void) _xbt_dynar_cursor_step(const xbt_dynar_t dynar,
-                                        unsigned int *const cursor);
-XBT_PUBLIC(int) _xbt_dynar_cursor_get(const xbt_dynar_t dynar,
-                                      unsigned int *const cursor,
-                                      void *whereto);
 XBT_PUBLIC(void) xbt_dynar_cursor_rm(xbt_dynar_t dynar,
                                      unsigned int *const cursor);
 XBT_PUBLIC(void) xbt_dynar_cursor_unlock(xbt_dynar_t dynar);
 
+/* do not use this structure internals directly, but use the public interface
+ * This was made public to allow:
+ *  - the inlining of the foreach elements
+ *  - sending such beasts over the network
+ */
+
+#include "xbt/synchro.h"
+typedef struct xbt_dynar_s {
+  unsigned long size;
+  unsigned long used;
+  unsigned long elmsize;
+  void *data;
+  void_f_pvoid_t free_f;
+  xbt_mutex_t mutex;
+} s_xbt_dynar_t;
+
+static XBT_INLINE void
+_xbt_dynar_cursor_first(const xbt_dynar_t dynar, unsigned int *const cursor)
+{
+  /* don't test for dynar!=NULL. The segfault would tell us */
+  if (dynar->mutex)  /* ie _dynar_lock(dynar) but not public */
+    xbt_mutex_acquire(dynar->mutex);
+
+  //DEBUG1("Set cursor on %p to the first position", (void *) dynar);
+  *cursor = 0;
+}
+
+static XBT_INLINE int
+_xbt_dynar_cursor_get(const xbt_dynar_t dynar,
+                      unsigned int idx, void *const dst)
+{
+
+  if (idx >= dynar->used) {
+    //DEBUG1("Cursor on %p already on last elem", (void *) dynar);
+    if (dynar->mutex) /* unlock */
+      xbt_mutex_release(dynar->mutex);
+    return FALSE;
+  }
+  //  DEBUG2("Cash out cursor on %p at %u", (void *) dynar, *idx);
+
+  memcpy(dst, ((char*)dynar->data) + idx * dynar->elmsize, dynar->elmsize);
+
+  return TRUE;
+}
+
+
+
 /** @brief Iterates over the whole dynar. 
  * 
  *  @param _dynar what to iterate over
@@ -211,8 +252,8 @@ xbt_dynar_foreach (dyn,cpt,str) {
  */
 #define xbt_dynar_foreach(_dynar,_cursor,_data) \
        for (_xbt_dynar_cursor_first(_dynar,&(_cursor))      ; \
-           _xbt_dynar_cursor_get(_dynar,&(_cursor),&_data) ; \
-            _xbt_dynar_cursor_step(_dynar,&(_cursor))         )
+           _xbt_dynar_cursor_get(_dynar,_cursor,&_data) ; \
+            (_cursor)++         )
 
 /** @} */
 
index f893efe..7cfff63 100644 (file)
@@ -632,7 +632,6 @@ gras_datadesc_ref_pop_arr(gras_datadesc_type_t element_type)
  *##
  */
 
-#include "xbt/dynar_private.h"
 static void gras_datadesc_dynar_cb(gras_datadesc_type_t typedesc,
                                    gras_cbps_t vars, void *data)
 {
index bb1b568..1b758cf 100644 (file)
@@ -359,7 +359,7 @@ void gras_socket_close(gras_socket_t sock)
     /* FIXME: Cannot get the dynar mutex, because it can be already locked */
 //              _xbt_dynar_foreach(sockets,cursor,sock_iter) {
     for (cursor = 0; cursor < xbt_dynar_length(sockets); cursor++) {
-      _xbt_dynar_cursor_get(sockets, &cursor, &sock_iter);
+      _xbt_dynar_cursor_get(sockets, cursor, &sock_iter);
       if (sock == sock_iter) {
         DEBUG2("remove sock cursor %d dize %lu\n", cursor,
                xbt_dynar_length(sockets));
index b6af152..0b59641 100644 (file)
@@ -15,9 +15,6 @@
 #include "xbt/dynar.h"
 #include <sys/types.h>
 
-#include "xbt/dynar_private.h"  /* type definition, which we share with the
-                                   code in charge of sending this across the net */
-
 /* IMPLEMENTATION NOTE ON SYNCHRONIZATION: every functions which name is prefixed by _
  * assumes that the dynar is already locked if we have to.
  * Other functions (public ones) check for this.
@@ -659,67 +656,14 @@ static void _dynar_map(const xbt_dynar_t dynar, void_f_pvoid_t const op)
 void xbt_dynar_map(const xbt_dynar_t dynar, void_f_pvoid_t const op)
 {
 
-  _dynar_lock(dynar);
   _sanity_check_dynar(dynar);
+  _dynar_lock(dynar);
 
   _dynar_map(dynar, op);
 
   _dynar_unlock(dynar);
 }
 
-/** @brief Put the cursor at the begining of the dynar.
- *
- * Actually, the cursor is set one step before the begining, so that you
- * can iterate over the dynar with a for loop.
- *
- * @warning Do not call this function directly, but only through xbt_dynar_foreach.
- */
-void
-_xbt_dynar_cursor_first(const xbt_dynar_t dynar, unsigned int *const cursor)
-{
-
-  _dynar_lock(dynar);
-  DEBUG1("Set cursor on %p to the first position", (void *) dynar);
-  *cursor = 0;
-}
-
-/** @brief Move the cursor to the next value
- *
- * @warning Do not call this function directly, but only through xbt_dynar_foreach.
- */
-void
-_xbt_dynar_cursor_step(const xbt_dynar_t dynar, unsigned int *const cursor)
-{
-
-  (*cursor)++;
-}
-
-/** @brief Get the data currently pointed by the cursor
- *
- * @warning Do not call this function directly, but only through xbt_dynar_foreach.
- */
-int
-_xbt_dynar_cursor_get(const xbt_dynar_t dynar,
-                      unsigned int *const cursor, void *const dst)
-{
-
-  _sanity_check_dynar(dynar);
-  {
-
-    const unsigned long idx = *cursor;
-
-    if (idx >= dynar->used) {
-      DEBUG1("Cursor on %p already on last elem", (void *) dynar);
-      _dynar_unlock(dynar);
-      return FALSE;
-    }
-    DEBUG2("Cash out cursor on %p at %lu", (void *) dynar, idx);
-
-    _xbt_dynar_get_elm(dst, dynar, idx);
-  }
-  return TRUE;
-
-}
 
 /** @brief Removes and free the entry pointed by the cursor
  *
diff --git a/src/xbt/dynar_private.h b/src/xbt/dynar_private.h
deleted file mode 100644 (file)
index 0a9bdfe..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-/* $Id$ */
-
-/* a generic DYNamic ARray implementation.                                  */
-
-/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
-
-/* This program is free software; you can redistribute it and/or modify it
- * under the terms of the license (GNU LGPL) which comes with this package. */
-
-#ifndef DYNAR_PRIVATE_H
-#define DYNAR_PRIVATE_H
-
-#include "xbt/synchro.h"
-typedef struct xbt_dynar_s {
-  unsigned long size;
-  unsigned long used;
-  unsigned long elmsize;
-  void *data;
-  void_f_pvoid_t free_f;
-  xbt_mutex_t mutex;
-} s_xbt_dynar_t;
-
-#endif /* DYNAR_PRIVATE_H */