X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/c20d392fa3f8d901885cb8c04d37230b4bdf1ef2..a04cda2a492c0549ff94b6dfc41b150d9e0abe61:/src/xbt/dynar.c diff --git a/src/xbt/dynar.c b/src/xbt/dynar.c index 1d58b67a86..30eafd0162 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.c @@ -98,7 +98,7 @@ static XBT_INLINE const unsigned long new_length = new_size * elmsize; char *const new_data = (char *) xbt_malloc0(elmsize * new_size); - DEBUG3("expend %p from %lu to %lu elements", (void *) dynar, + DEBUG3("expand %p from %lu to %lu elements", (void *) dynar, (unsigned long) old_size, nb); if (old_data) { @@ -390,6 +390,28 @@ XBT_INLINE void *xbt_dynar_get_ptr(const xbt_dynar_t dynar, return res; } +XBT_INLINE void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar, + const unsigned long idx) +{ + + void *res; + _dynar_lock(dynar); + _sanity_check_dynar(dynar); + + _xbt_dynar_expand(dynar, idx + 1); + + if (idx >= dynar->used) { + _xbt_clear_mem(((char * const)dynar->data) + dynar->used * dynar->elmsize, + (idx + 1 - dynar->used)*dynar->elmsize); + dynar->used = idx + 1; + } + + _dynar_unlock(dynar); + + res = _xbt_dynar_elm(dynar, idx); + + return res; +} static void XBT_INLINE /* not synchronized */ _xbt_dynar_set(xbt_dynar_t dynar, @@ -397,18 +419,19 @@ _xbt_dynar_set(xbt_dynar_t dynar, { _sanity_check_dynar(dynar); - _sanity_check_idx(idx); _xbt_dynar_expand(dynar, idx + 1); if (idx >= dynar->used) { + _xbt_clear_mem(((char * const)dynar->data) + dynar->used * dynar->elmsize, + (idx + 1 - dynar->used)*dynar->elmsize); dynar->used = idx + 1; } _xbt_dynar_put_elm(dynar, idx, src); } -/** @brief Set the Nth element of a dynar (expended if needed). Previous value at this position is NOT freed +/** @brief Set the Nth element of a dynar (expanded if needed). Previous value at this position is NOT freed * * \param dynar information dealer * \param idx index of the slot we want to modify @@ -425,7 +448,7 @@ XBT_INLINE void xbt_dynar_set(xbt_dynar_t dynar, const int idx, _dynar_unlock(dynar); } -/** @brief Set the Nth element of a dynar (expended if needed). Previous value is freed +/** @brief Set the Nth element of a dynar (expanded if needed). Previous value is freed * * \param dynar * \param idx @@ -441,7 +464,6 @@ xbt_dynar_replace(xbt_dynar_t dynar, { _dynar_lock(dynar); _sanity_check_dynar(dynar); - _sanity_check_idx(idx); if (idx < dynar->used && dynar->free_f) { void *const old_object = _xbt_dynar_elm(dynar, idx); @@ -496,7 +518,7 @@ void *xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, const int idx) return res; } -/** @brief Set the Nth dynar's element, expending the dynar and sliding the previous values to the right +/** @brief Set the Nth dynar's element, expanding the dynar and sliding the previous values to the right * * Set the Nth element of a dynar, expanding the dynar if needed, and * moving the previously existing value and all subsequent ones to one @@ -709,6 +731,7 @@ XBT_INLINE void xbt_dynar_cursor_unlock(xbt_dynar_t dynar) /** @brief Sorts a dynar according to the function compar_fn * + * \param dynar the dynar to sort * \param compar_fn comparison function of type (int (compar_fn*) (void*) (void*)). * * Remark: if the elements stored in the dynar are structures, the compar_fn @@ -725,6 +748,22 @@ XBT_INLINE void xbt_dynar_sort(xbt_dynar_t dynar, _dynar_unlock(dynar); } +/** @brief Transform a dynar into a NULL terminated array + * + * \param dynar the dynar to transform + */ +XBT_INLINE void * xbt_dynar_to_array (xbt_dynar_t dynar) +{ + void * res; + void * last = xbt_new0(char,dynar->elmsize); + xbt_dynar_push(dynar, last); + free(last); + dynar->used--; + res = dynar->data; + free(dynar); + return res; +} + /* * Return 0 if d1 and d2 are equal and 1 if not equal */ @@ -734,16 +773,29 @@ XBT_INLINE int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2, int i ; int size; if((!d1) && (!d2)) return 0; - if((!d1) || (!d2)) return 1; - if((d1->elmsize)!=(d2->elmsize)) return 1; // xbt_die - if(xbt_dynar_length(d1) != xbt_dynar_length(d2)) return 1; + if((!d1) || (!d2)) + { + DEBUG2("NULL dynar d1=%p d2=%p",d1,d2); + return 1; + } + if((d1->elmsize)!=(d2->elmsize)) + { + DEBUG2("Size of elmsize d1=%ld d2=%ld",d1->elmsize,d2->elmsize); + return 1; // xbt_die + } + if(xbt_dynar_length(d1) != xbt_dynar_length(d2)) + { + DEBUG2("Size of dynar d1=%ld d2=%ld",xbt_dynar_length(d1),xbt_dynar_length(d2)); + return 1; + } size = xbt_dynar_length(d1); for(i=0;i