X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/092910cd23c789eb83b53efb69e85ae58ed3b1d0..0c0857127aadbff3d4d1d317ca35b0c50621663a:/src/xbt/dynar.c diff --git a/src/xbt/dynar.c b/src/xbt/dynar.c index 16a5f00e12..1010a3754a 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.c @@ -9,9 +9,9 @@ under the terms of the license (GNU LGPL) which comes with this package. */ #include "gras_private.h" +#include - -GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dynar,GRAS); +GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dynar,gros,"Dynamic arrays"); struct gras_dynar_s { size_t size; @@ -27,28 +27,26 @@ struct gras_dynar_s { #define __sanity_check_idx(idx) \ gras_assert1(idx >= 0, \ "dynar idx(=%d) < 0", \ - idx) + (int) (idx)) #define __check_inbound_idx(dynar, idx) \ gras_assert2(idx < dynar->used, \ - "dynar is not that long. You asked %d, but it's only %d long", \ - idx, dynar->used) + "dynar is not that long. You asked %d, but it's only %lu long", \ + (int) (idx), (unsigned long) dynar->used) #define __check_sloppy_inbound_idx(dynar, idx) \ gras_assert2(idx <= dynar->used, \ - "dynar is not that long. You asked %d, but it's only %d long", \ - idx, dynar->used) + "dynar is not that long. You asked %d, but it's only %lu long", \ + (int) (idx), (unsigned long) dynar->used) #define __check_populated_dynar(dynar) \ - gras_assert0(dynar->used, \ - "dynar contains nothing") - + gras_assert1(dynar->used, \ + "dynar %p contains nothing",(void*)dynar) -static inline -void -_gras_clear_mem(void * const ptr, - const size_t length) { +static _GRAS_INLINE +void _gras_clear_mem(void * const ptr, + const size_t length) { memset(ptr, 0, length); } -static inline +static _GRAS_INLINE gras_error_t _gras_dynar_expand(gras_dynar_t * const dynar, const int nb) { @@ -66,16 +64,16 @@ _gras_dynar_expand(gras_dynar_t * const dynar, const size_t new_size = nb > (2*(old_size+1)) ? nb : (2*(old_size+1)); const size_t new_length = new_size*elmsize; - char * const new_data = calloc(1, elmsize*new_size); + char * const new_data = gras_malloc0(elmsize*new_size); - DEBUG3("expend %p from %d to %d elements", dynar, old_size, nb); + DEBUG3("expend %p from %lu to %d elements", (void*)dynar, (unsigned long)old_size, nb); if (!new_data) RAISE_MALLOC; if (old_data) { memcpy(new_data, old_data, used_length); _gras_clear_mem(old_data, old_length); - free(old_data); + gras_free(old_data); } _gras_clear_mem(new_data + used_length, new_length - used_length); @@ -87,7 +85,7 @@ _gras_dynar_expand(gras_dynar_t * const dynar, return errcode; } -static inline +static _GRAS_INLINE void * _gras_dynar_elm(const gras_dynar_t * const dynar, const size_t idx) { @@ -97,7 +95,7 @@ _gras_dynar_elm(const gras_dynar_t * const dynar, return data + idx*elmsize; } -static inline +static _GRAS_INLINE void _gras_dynar_get_elm(void * const dst, const gras_dynar_t * const dynar, @@ -108,7 +106,7 @@ _gras_dynar_get_elm(void * const dst, memcpy(dst, elm, elmsize); } -static inline +static _GRAS_INLINE void _gras_dynar_put_elm(const gras_dynar_t * const dynar, const size_t idx, @@ -137,7 +135,7 @@ gras_dynar_new(gras_dynar_t ** const p_dynar, gras_error_t errcode = no_error; gras_dynar_t *dynar = NULL; - if (!(dynar = calloc(1, sizeof(gras_dynar_t)))) + if (!(dynar = gras_new0(gras_dynar_t,1))) RAISE_MALLOC; dynar->size = 0; @@ -164,12 +162,12 @@ gras_dynar_free_container(gras_dynar_t * const dynar) { if (dynar->data) { _gras_clear_mem(dynar->data, dynar->size); - free(dynar->data); + gras_free(dynar->data); } _gras_clear_mem(dynar, sizeof(gras_dynar_t)); - free(dynar); + gras_free(dynar); } } @@ -184,13 +182,14 @@ gras_dynar_reset(gras_dynar_t * const dynar) { __sanity_check_dynar(dynar); + DEBUG1("Reset the dynar %p",(void*)dynar); if (dynar->free) { gras_dynar_map(dynar, dynar->free); } if (dynar->data) { _gras_clear_mem(dynar->data, dynar->size); - free(dynar->data); + gras_free(dynar->data); } dynar->size = 0; @@ -219,9 +218,9 @@ gras_dynar_free(gras_dynar_t * const dynar) { * * Returns the count of elements in a dynar */ -size_t +unsigned long gras_dynar_length(const gras_dynar_t * const dynar) { - return (dynar ? dynar->used : (size_t)0); + return (dynar ? (unsigned long) dynar->used : (unsigned long)0); } /** @@ -419,6 +418,7 @@ gras_dynar_pop(gras_dynar_t * const dynar, void * const dst) { __sanity_check_dynar(dynar); __check_populated_dynar(dynar); + DEBUG1("Pop %p",(void*)dynar); gras_dynar_remove_at(dynar, dynar->used-1, dst); } @@ -497,7 +497,7 @@ gras_dynar_cursor_first(const gras_dynar_t * const dynar, int * const cursor) { __sanity_check_dynar(dynar); - DEBUG1("Set cursor on %p to the first position",dynar); + DEBUG1("Set cursor on %p to the first position",(void*)dynar); *cursor = 0; } @@ -530,10 +530,10 @@ gras_dynar_cursor_get(const gras_dynar_t * const dynar, const int idx = *cursor; if (idx >= dynar->used) { - DEBUG1("Cursor on %p already on last elem",dynar); + DEBUG1("Cursor on %p already on last elem",(void*)dynar); return FALSE; } - DEBUG2("Cash out cursor on %p at %d",dynar,idx); + DEBUG2("Cash out cursor on %p at %d",(void*)dynar,idx); _gras_dynar_get_elm(dst, dynar, idx); } @@ -546,10 +546,27 @@ gras_dynar_cursor_get(const gras_dynar_t * const dynar, * @dynar: * @cursor: * - * Remove the entry pointed by the cursor, for use in the middle of a foreach + * Remove (free) the entry pointed by the cursor, for use in the middle of a foreach */ void gras_dynar_cursor_rm(gras_dynar_t * dynar, int * const cursor) { - - gras_dynar_remove_at(dynar,(*cursor)--,NULL); + void *dst; + + if (dynar->elmsize > sizeof(void*)) { + DEBUG0("Elements too big to fit into a pointer"); + if (dynar->free) { + dst=gras_malloc(dynar->elmsize); + gras_dynar_remove_at(dynar,(*cursor)--,dst); + (dynar->free)(dst); + gras_free(dst); + } else { + DEBUG0("Ok, we dont care about the element when no free function"); + gras_dynar_remove_at(dynar,(*cursor)--,NULL); + } + + } else { + gras_dynar_remove_at(dynar,(*cursor)--,&dst); + if (dynar->free) + (dynar->free)(dst); + } }