Просмотр исходного кода

u-a: Add ref counting support to struct alternative

This will allow to properly free an alternative map which contains
duplicated references to the same alternative struct.
Guillem Jover лет назад: 15
Родитель
Сommit
3b2b9aea51
1 измененных файлов с 17 добавлено и 0 удалено
  1. 17 0
      utils/update-alternatives.c

+ 17 - 0
utils/update-alternatives.c

@@ -719,6 +719,7 @@ struct alternative {
 
 	struct commit_operation *commit_ops;
 
+	int ref_count;
 	bool modified;
 };
 
@@ -751,10 +752,23 @@ alternative_new(const char *name)
 	alt->choices = NULL;
 	alt->commit_ops = NULL;
 	alt->modified = false;
+	alt->ref_count = 1;
 
 	return alt;
 }
 
+static inline void
+alternative_ref(struct alternative *a)
+{
+	a->ref_count++;
+}
+
+static inline bool
+alternative_unref(struct alternative *a)
+{
+	return --a->ref_count == 0;
+}
+
 static void
 alternative_choices_free(struct alternative *a)
 {
@@ -802,6 +816,9 @@ alternative_reset(struct alternative *alt)
 static void
 alternative_free(struct alternative *alt)
 {
+	if (!alternative_unref(alt))
+		return;
+
 	alternative_reset(alt);
 	free(alt->master_name);
 	free(alt);