diff mbox

Clean index map on exit

Message ID EB3C2705-3925-49DD-8DB2-07BAE9E60A05@gmx.net (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Hannes Weisbach April 11, 2014, 4:58 p.m. UTC
This patch adds the function idm_free() to free all entries of an index
map. A call to this function is added in the ucma_cleanup destructor.
The ucma_idm struct index_map is cleaned.

Signed-off-by: Hannes Weisbach <hannes_weisbach@gmx.net>
---
 src/cma.c     | 1 +
 src/indexer.c | 8 ++++++++
 src/indexer.h | 1 +
 3 files changed, 10 insertions(+)

Comments

Yann Droneaud April 11, 2014, 5:06 p.m. UTC | #1
Le vendredi 11 avril 2014 à 18:58 +0200, Hannes Weisbach a écrit :
> This patch adds the function idm_free() to free all entries of an index
> map. A call to this function is added in the ucma_cleanup destructor.
> The ucma_idm struct index_map is cleaned.
> 

Please explain the purpose: what's your goal ?

> Signed-off-by: Hannes Weisbach <hannes_weisbach@gmx.net>
> ---
>  src/cma.c     | 1 +
>  src/indexer.c | 8 ++++++++
>  src/indexer.h | 1 +
>  3 files changed, 10 insertions(+)
> 
> diff --git a/src/cma.c b/src/cma.c
> index 0cf4203..a533bf9 100644
> --- a/src/cma.c
> +++ b/src/cma.c
> @@ -139,6 +139,7 @@ static void ucma_cleanup(void)
>  			ibv_close_device(cma_dev_array[cma_dev_cnt].verbs);
>  		}
>  
> +		idm_free(&ucma_idm);
>  		fastlock_destroy(&idm_lock);
>  		free(cma_dev_array);
>  		cma_dev_cnt = 0;
> diff --git a/src/indexer.c b/src/indexer.c
> index c8e8bce..4d1fd77 100644
> --- a/src/indexer.c
> +++ b/src/indexer.c
> @@ -164,3 +164,11 @@ void *idm_clear(struct index_map *idm, int index)
>  	entry[idx_entry_index(index)] = NULL;
>  	return item;
>  }
> +
> +void idm_free(struct index_map *idm)
> +{
> +	size_t i;
> +	for (i = 0; i < IDX_ARRAY_SIZE; i++) {
> +	free(idm->array[i]);
> +	}
> +}

Is the array always full ? is the maximum index of the array
IDX_ARRAY_SIZE or could it be lower ?

And what about idm, is it free()'d somewhere else ?

> diff --git a/src/indexer.h b/src/indexer.h
> index 0c5f388..829d46b 100644
> --- a/src/indexer.h
> +++ b/src/indexer.h
> @@ -89,6 +89,7 @@ struct index_map
>  
>  int idm_set(struct index_map *idm, int index, void *item);
>  void *idm_clear(struct index_map *idm, int index);
> +void idm_free(struct index_map *idm);
>  
>  static inline void *idm_at(struct index_map *idm, int index)
>  {

Regards.
Hannes Weisbach April 11, 2014, 5:55 p.m. UTC | #2
> 
> Is the array always full ?
No, but it has to be zero-initialized and free(NULL) is fine.

idm_set() does:
if (!idm->array[idx_array_index(index)]) {
		if (idm_grow(idm, index) < 0)
(idm_grow does calloc().)

So I concluded unused entries have to be zero already and I don't put
a new constraint on struct index_map.

> is the maximum index of the array
> IDX_ARRAY_SIZE or could it be lower ?
It's fixed:
struct index_map
{
	void **array[IDX_ARRAY_SIZE];
};

> And what about idm, is it free()'d somewhere else ?
The owner has to take care of its struct index_map.  I can only free
the resources the map itself holds.
For what it's worth: all struct index_maps are declared statically.

Again, thank you for your quick feedback.

Best regards,
Hannes
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/src/cma.c b/src/cma.c
index 0cf4203..a533bf9 100644
--- a/src/cma.c
+++ b/src/cma.c
@@ -139,6 +139,7 @@  static void ucma_cleanup(void)
 			ibv_close_device(cma_dev_array[cma_dev_cnt].verbs);
 		}
 
+		idm_free(&ucma_idm);
 		fastlock_destroy(&idm_lock);
 		free(cma_dev_array);
 		cma_dev_cnt = 0;
diff --git a/src/indexer.c b/src/indexer.c
index c8e8bce..4d1fd77 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -164,3 +164,11 @@  void *idm_clear(struct index_map *idm, int index)
 	entry[idx_entry_index(index)] = NULL;
 	return item;
 }
+
+void idm_free(struct index_map *idm)
+{
+	size_t i;
+	for (i = 0; i < IDX_ARRAY_SIZE; i++) {
+	free(idm->array[i]);
+	}
+}
diff --git a/src/indexer.h b/src/indexer.h
index 0c5f388..829d46b 100644
--- a/src/indexer.h
+++ b/src/indexer.h
@@ -89,6 +89,7 @@  struct index_map
 
 int idm_set(struct index_map *idm, int index, void *item);
 void *idm_clear(struct index_map *idm, int index);
+void idm_free(struct index_map *idm);
 
 static inline void *idm_at(struct index_map *idm, int index)
 {