X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/3b0fced0a85b3f40e7138340dfb1ddcc630d2d07..b2252602473b26e442a9c96b4f28dc089deb19f4:/include/xbt/dynar.h diff --git a/include/xbt/dynar.h b/include/xbt/dynar.h index 87b501c655..4ca03cc365 100644 --- a/include/xbt/dynar.h +++ b/include/xbt/dynar.h @@ -1,6 +1,6 @@ /* dynar - a generic dynamic array */ -/* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team. +/* Copyright (c) 2004-2007, 2009-2015. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -11,7 +11,7 @@ #include /* memcpy */ -#include "xbt/misc.h" /* SG_BEGIN_DECL */ +#include "xbt/base.h" /* SG_BEGIN_DECL */ #include "xbt/function_types.h" SG_BEGIN_DECL() @@ -52,6 +52,12 @@ SG_BEGIN_DECL() * \skip end_of_doxygen * \until } * + * Note that if you use dynars to store pointed data, the + * xbt_dynar_search(), xbt_dynar_search_or_negative() and + * xbt_dynar_member() won't be for you. Instead of comparing + * your pointed elements, they compare the pointer to them. See + * the documentation of xbt_dynar_search() for more info. + * */ /** @defgroup XBT_dynar_cons Dynar constructor and destructor * @ingroup XBT_dynar @@ -64,8 +70,6 @@ typedef struct xbt_dynar_s *xbt_dynar_t; XBT_PUBLIC(xbt_dynar_t) xbt_dynar_new(const unsigned long elm_size, void_f_pvoid_t const free_f); -XBT_PUBLIC(xbt_dynar_t) xbt_dynar_new_sync(const unsigned long elm_size, - void_f_pvoid_t const free_f); XBT_PUBLIC(void) xbt_dynar_free(xbt_dynar_t * dynar); XBT_PUBLIC(void) xbt_dynar_free_voidp(void *dynar); XBT_PUBLIC(void) xbt_dynar_free_container(xbt_dynar_t * dynar); @@ -92,6 +96,9 @@ XBT_PUBLIC(void) xbt_dynar_insert_at(xbt_dynar_t const dynar, const int idx, const void *src); XBT_PUBLIC(void) xbt_dynar_remove_at(xbt_dynar_t const dynar, const int idx, void *const dst); +XBT_PUBLIC(void) xbt_dynar_remove_n_at(xbt_dynar_t const dynar, + const unsigned int n, const int idx); + XBT_PUBLIC(unsigned int) xbt_dynar_search(xbt_dynar_t const dynar, void *elem); XBT_PUBLIC(signed int) xbt_dynar_search_or_negative(xbt_dynar_t const dynar, void *const elem); @@ -197,19 +204,14 @@ XBT_PUBLIC(void *) xbt_dynar_pop_ptr(xbt_dynar_t const dynar); * DynArr during the traversal. To remove elements, use the * xbt_dynar_cursor_rm() function. * - * Do not call these functions directly, but only the xbt_dynar_foreach macro. - * - * For synchronized dynars, the dynar will be locked during the whole - * loop and it will get unlocked automatically if you traverse all - * elements. If you want to break the loop before the end, make sure - * to call xbt_dynar_cursor_unlock() before the break; + * Do not call these function directly, but only within the xbt_dynar_foreach + * macro. * * @{ */ 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: @@ -217,23 +219,19 @@ XBT_PUBLIC(void) xbt_dynar_cursor_unlock(xbt_dynar_t dynar); * - sending such beasts over the network */ -#include "xbt/synchro_core.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, +_xbt_dynar_cursor_first(const xbt_dynar_t dynar _XBT_GNUC_UNUSED, 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); + /* iterating over a NULL dynar is a no-op (but we don't want to have uninitialized counters) */ //XBT_DEBUG("Set cursor on %p to the first position", (void *) dynar); *cursor = 0; @@ -243,11 +241,11 @@ static XBT_INLINE int _xbt_dynar_cursor_get(const xbt_dynar_t dynar, unsigned int idx, void *const dst) { + if (!dynar) /* iterating over a NULL dynar is a no-op */ + return FALSE; if (idx >= dynar->used) { //XBT_DEBUG("Cursor on %p already on last elem", (void *) dynar); - if (dynar->mutex) /* unlock */ - xbt_mutex_release(dynar->mutex); return FALSE; } // XBT_DEBUG("Cash out cursor on %p at %u", (void *) dynar, *idx); @@ -267,7 +265,7 @@ _xbt_dynar_cursor_get(const xbt_dynar_t dynar, * @param _data * @hideinitializer * - * \note An example of usage: + * Here is an example of usage: * \code xbt_dynar_t dyn; unsigned int cpt; @@ -276,12 +274,27 @@ xbt_dynar_foreach (dyn,cpt,str) { printf("Seen %s\n",str); } \endcode + * + * Note that underneath, that's a simple for loop with no real black + * magic involved. It's perfectly safe to interrupt a foreach with a + * break or a return statement. */ #define xbt_dynar_foreach(_dynar,_cursor,_data) \ for (_xbt_dynar_cursor_first(_dynar,&(_cursor)) ; \ - _xbt_dynar_cursor_get(_dynar,_cursor,&_data) ; \ + _xbt_dynar_cursor_get(_dynar,_cursor,&_data) ; \ (_cursor)++ ) +#ifndef __cplusplus +#define xbt_dynar_foreach_ptr(_dynar,_cursor,_ptr) \ + for (_xbt_dynar_cursor_first(_dynar,&(_cursor)) ; \ + (_ptr = _cursor < _dynar->used ? xbt_dynar_get_ptr(_dynar,_cursor) : NULL) ; \ + (_cursor)++ ) +#else +#define xbt_dynar_foreach_ptr(_dynar,_cursor,_ptr) \ + for (_xbt_dynar_cursor_first(_dynar,&(_cursor)) ; \ + (_ptr = _cursor < _dynar->used ? (decltype(_ptr)) xbt_dynar_get_ptr(_dynar,_cursor) : NULL) ; \ + (_cursor)++ ) +#endif /** @} */ SG_END_DECL()