X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/7e81b0b0e989779327ccf6917a358bc5c71ac83b..6e5cfd7ff86900354c20502af95ee5f751492753:/src/xbt/dynar.c diff --git a/src/xbt/dynar.c b/src/xbt/dynar.c index 51d207b14a..87d4aa8ac8 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.c @@ -86,27 +86,18 @@ static XBT_INLINE const unsigned long old_size = dynar->size; if (nb > old_size) { - char *const old_data = (char *) dynar->data; - + void *const old_data = dynar->data; const unsigned long elmsize = dynar->elmsize; + const unsigned long old_length = old_size * elmsize; - const unsigned long used = dynar->used; - const unsigned long used_length = used * elmsize; - - const unsigned long new_size = - nb > (2 * (old_size + 1)) ? nb : (2 * (old_size + 1)); + const unsigned long expand = 2 * (old_size + 1); + const unsigned long new_size = (nb > expand ? nb : expand); const unsigned long new_length = new_size * elmsize; - char *const new_data = (char *) xbt_malloc0(elmsize * new_size); + void *const new_data = xbt_realloc(old_data, new_length); - XBT_DEBUG("expand %p from %lu to %lu elements", (void *) dynar, - (unsigned long) old_size, nb); - - if (old_data) { - memcpy(new_data, old_data, used_length); - free(old_data); - } + XBT_DEBUG("expand %p from %lu to %lu elements", dynar, old_size, new_size); - _xbt_clear_mem(new_data + used_length, new_length - used_length); + _xbt_clear_mem((char *)new_data + old_length, new_length - old_length); dynar->size = new_size; dynar->data = new_data; @@ -583,7 +574,8 @@ int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem) TRY { xbt_dynar_search(dynar, elem); - } CATCH(e) { + } + CATCH(e) { if (e.category == not_found_error) { xbt_ex_free(e); return 0; @@ -745,8 +737,11 @@ XBT_INLINE void xbt_dynar_sort(xbt_dynar_t dynar, _dynar_lock(dynar); +#ifdef HAVE_MERGESORT + mergesort(dynar->data, dynar->used, dynar->elmsize, compar_fn); +#else qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn); - +#endif _dynar_unlock(dynar); } @@ -1329,7 +1324,7 @@ static void pusher_f(void *a) static void poper_f(void *a) { xbt_dynar_t d = (xbt_dynar_t) a; - int i; + volatile int i; int data; xbt_ex_t e;