#ifndef _XBT_DYNAR_H
#define _XBT_DYNAR_H
+#include <string.h> /* memcpy */
#include "xbt/misc.h" /* SG_BEGIN_DECL */
#include "xbt/function_types.h"
* @{
*/
-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
*/
#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)++ )
/** @} */
*##
*/
-#include "xbt/dynar_private.h"
static void gras_datadesc_dynar_cb(gras_datadesc_type_t typedesc,
gras_cbps_t vars, void *data)
{
/* 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));
#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.
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
*
+++ /dev/null
-/* $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 */