From 6e95d8018bccb24c4600c551ef6900eae41c0faa Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Fri, 27 Dec 2019 17:59:21 +0100 Subject: [PATCH] [sonar] Constify pointer and reference parameters in dicts and dynars. --- include/xbt/dict.h | 5 ++++- include/xbt/dynar.h | 3 ++- src/xbt/dict_cursor.c | 2 +- src/xbt/dict_elm.c | 4 ++-- src/xbt/dict_private.h | 4 ++-- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/xbt/dict.h b/include/xbt/dict.h index e36d2b9558..73b6e624f9 100644 --- a/include/xbt/dict.h +++ b/include/xbt/dict.h @@ -45,6 +45,7 @@ SG_BEGIN_DECL /** @brief Dictionary data type (opaque structure) */ typedef struct s_xbt_dict *xbt_dict_t; +typedef const struct s_xbt_dict* const_xbt_dict_t; typedef struct s_xbt_dictelm *xbt_dictelm_t; typedef struct s_xbt_dictelm { char *key; @@ -124,8 +125,10 @@ struct s_xbt_dict_cursor { /** @brief Cursor on dictionaries (opaque type) */ typedef struct s_xbt_dict_cursor *xbt_dict_cursor_t; +typedef const struct s_xbt_dict_cursor* const_xbt_dict_cursor_t; -static inline xbt_dictelm_t xbt_dict_cursor_get_elm(xbt_dict_cursor_t cursor) { +static inline xbt_dictelm_t xbt_dict_cursor_get_elm(const_xbt_dict_cursor_t cursor) +{ return cursor->current; } diff --git a/include/xbt/dynar.h b/include/xbt/dynar.h index 46fa9c825c..c90aa68921 100644 --- a/include/xbt/dynar.h +++ b/include/xbt/dynar.h @@ -63,6 +63,7 @@ SG_BEGIN_DECL */ /** @brief Dynar data type (opaque type) */ typedef struct xbt_dynar_s *xbt_dynar_t; +typedef const struct xbt_dynar_s* const_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 void xbt_dynar_init(xbt_dynar_t dynar, const unsigned long elmsize, void_f_pvoid_t const free_f); @@ -201,7 +202,7 @@ typedef struct xbt_dynar_s { void_f_pvoid_t free_f; } s_xbt_dynar_t; -static inline int _xbt_dynar_cursor_get(const xbt_dynar_t dynar, unsigned int idx, void* const dst) +static 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 0; diff --git a/src/xbt/dict_cursor.c b/src/xbt/dict_cursor.c index ca96b1c6bf..b364d32b3e 100644 --- a/src/xbt/dict_cursor.c +++ b/src/xbt/dict_cursor.c @@ -45,7 +45,7 @@ inline void xbt_dict_cursor_free(xbt_dict_cursor_t * cursor) /* * Sanity check to see if the head contains something */ -static inline void __cursor_not_null(xbt_dict_cursor_t cursor) +static inline void __cursor_not_null(const_xbt_dict_cursor_t cursor) { xbt_assert(cursor, "Null cursor"); } diff --git a/src/xbt/dict_elm.c b/src/xbt/dict_elm.c index 9c5bc3b6e6..6295905249 100644 --- a/src/xbt/dict_elm.c +++ b/src/xbt/dict_elm.c @@ -25,7 +25,7 @@ xbt_dictelm_t xbt_dictelm_new(const char* key, int key_len, unsigned int hash_co return element; } -void xbt_dictelm_free(xbt_dict_t dict, xbt_dictelm_t element) +void xbt_dictelm_free(const_xbt_dict_t dict, xbt_dictelm_t element) { if (element) { char *key = element->key; @@ -40,7 +40,7 @@ void xbt_dictelm_free(xbt_dict_t dict, xbt_dictelm_t element) } } -void xbt_dictelm_set_data(xbt_dict_t dict, xbt_dictelm_t element, void* data) +void xbt_dictelm_set_data(const_xbt_dict_t dict, xbt_dictelm_t element, void* data) { void_f_pvoid_t free_f = dict->free_f; if (free_f && element->content) diff --git a/src/xbt/dict_private.h b/src/xbt/dict_private.h index 15a5d69d7c..6aa9c95761 100644 --- a/src/xbt/dict_private.h +++ b/src/xbt/dict_private.h @@ -34,8 +34,8 @@ XBT_PRIVATE void * dict_elm_mallocator_new_f(void); /*####[ Function prototypes ]################################################*/ XBT_PRIVATE xbt_dictelm_t xbt_dictelm_new(const char* key, int key_len, unsigned int hash_code, void* content); -XBT_PRIVATE void xbt_dictelm_free(xbt_dict_t dict, xbt_dictelm_t element); -XBT_PRIVATE void xbt_dictelm_set_data(xbt_dict_t dict, xbt_dictelm_t element, void* data); +XBT_PRIVATE void xbt_dictelm_free(const_xbt_dict_t dict, xbt_dictelm_t element); +XBT_PRIVATE void xbt_dictelm_set_data(const_xbt_dict_t dict, xbt_dictelm_t element, void* data); SG_END_DECL -- 2.20.1