Codebase list cinnamon-menus / 0abbcde
Revert "Memory leak fixes" This reverts commit 7d90221e27fcc72de497f45db4f3ef3c2d623fd6. Jasper St. Pierre authored 10 years ago Clement Lefebvre committed 6 years ago
3 changed file(s) with 69 addition(s) and 126 deletion(s). Raw diff Collapse all Expand all
445445 g_return_val_if_fail (entry != NULL, NULL);
446446 g_return_val_if_fail (entry->refcount > 0, NULL);
447447
448 g_atomic_int_inc (&entry->refcount);
448 entry->refcount += 1;
449449
450450 return entry;
451451 }
739739 g_return_val_if_fail (set != NULL, NULL);
740740 g_return_val_if_fail (set->refcount > 0, NULL);
741741
742 g_atomic_int_inc (&set->refcount);
742 set->refcount += 1;
743743
744744 return set;
745745 }
747747 void
748748 desktop_entry_set_unref (DesktopEntrySet *set)
749749 {
750 gboolean is_zero;
751
752750 g_return_if_fail (set != NULL);
753751 g_return_if_fail (set->refcount > 0);
754752
755 is_zero = g_atomic_int_dec_and_test (&set->refcount);
756 if (is_zero)
753 set->refcount -= 1;
754 if (set->refcount == 0)
757755 {
758756 menu_verbose (" Deleting entry set %p\n", set);
759757
3939
4040 guint entry_type : 2;
4141 guint is_legacy : 1;
42 volatile gint refcount;
42 guint refcount : 24;
4343 };
4444
4545 struct EntryDirectoryList
4646 {
47 volatile int refcount;
47 int refcount;
4848 int length;
4949 GList *dirs;
5050 };
6464 guint have_read_entries : 1;
6565 guint deleted : 1;
6666
67 GFunc notify;
68 gpointer notify_data;
69
70 volatile gint references;
67 guint references : 28;
7168 };
7269
7370 struct CachedDirMonitor
8279 static void cached_dir_free (CachedDir *dir);
8380 static gboolean cached_dir_load_entries_recursive (CachedDir *dir,
8481 const char *dirname);
85 static void cached_dir_unref (CachedDir *dir);
86 static CachedDir * cached_dir_add_subdir (CachedDir *dir,
87 const char *basename,
88 const char *path);
89 static gboolean cached_dir_remove_subdir (CachedDir *dir,
90 const char *basename);
9182
9283 static void handle_cached_dir_changed (MenuMonitor *monitor,
9384 MenuMonitorEvent event,
10091
10192 static CachedDir *dir_cache = NULL;
10293
103 static void
104 clear_cache (CachedDir *dir,
105 gpointer *cache)
106 {
107 *cache = NULL;
108 }
109
11094 static CachedDir *
11195 cached_dir_new (const char *name)
11296 {
11397 CachedDir *dir;
11498
11599 dir = g_new0 (CachedDir, 1);
100
116101 dir->name = g_strdup (name);
117
118 return dir;
119 }
120
121 static CachedDir *
122 cached_dir_new_full (const char *name,
123 GFunc notify,
124 gpointer notify_data)
125 {
126 CachedDir *dir;
127
128 dir = cached_dir_new (name);
129
130 dir->notify = notify;
131 dir->notify_data = notify_data;
132102
133103 return dir;
134104 }
156126 dir->entries = NULL;
157127
158128 g_slist_foreach (dir->subdirs,
159 (GFunc) cached_dir_unref,
129 (GFunc) cached_dir_free,
160130 NULL);
161131 g_slist_free (dir->subdirs);
162132 dir->subdirs = NULL;
168138 g_free (dir);
169139 }
170140
171 static CachedDir *
172 cached_dir_ref (CachedDir *dir)
173 {
174 g_atomic_int_inc (&dir->references);
175
176 return dir;
177 }
178
179 static void
180 cached_dir_unref (CachedDir *dir)
181 {
182 gboolean is_zero;
183
184 is_zero = g_atomic_int_dec_and_test (&dir->references);
185 if (is_zero)
186 {
187 CachedDir *parent;
188
189 parent = dir->parent;
190
191 if (parent != NULL)
192 cached_dir_remove_subdir (parent, dir->name);
193
194 if (dir->notify)
195 dir->notify (dir, dir->notify_data);
196
197 cached_dir_free (dir);
198 }
199 }
200
201141 static inline CachedDir *
202142 find_subdir (CachedDir *dir,
203143 const char *subdir)
227167 tmp = dir->entries;
228168 while (tmp != NULL)
229169 {
230 const char *entry_basename;
231
232 entry_basename = desktop_entry_get_basename (tmp->data);
233 if (strcmp (entry_basename, basename) == 0)
234 {
235 return tmp->data;
236 }
170 if (strcmp (desktop_entry_get_basename (tmp->data), basename) == 0)
171 return tmp->data;
237172
238173 tmp = tmp->next;
239174 }
281216 int i;
282217
283218 if (dir_cache == NULL)
284 dir_cache = cached_dir_new_full ("/",
285 (GFunc) clear_cache,
286 &dir_cache);
219 dir_cache = cached_dir_new ("/");
287220 dir = dir_cache;
288221
289222 g_assert (canonical != NULL && canonical[0] == G_DIR_SEPARATOR);
297230 {
298231 CachedDir *subdir;
299232
300 subdir = cached_dir_add_subdir (dir, split[i], NULL);
233 if ((subdir = find_subdir (dir, split[i])) == NULL)
234 {
235 subdir = cached_dir_new (split[i]);
236 dir->subdirs = g_slist_prepend (dir->subdirs, subdir);
237 subdir->parent = dir;
238 }
301239
302240 dir = subdir;
303241
347285 tmp = dir->entries;
348286 while (tmp != NULL)
349287 {
350 const char *entry_basename;
351 entry_basename = desktop_entry_get_basename (tmp->data);
352 if (strcmp (entry_basename, basename) == 0)
288 if (strcmp (desktop_entry_get_basename (tmp->data), basename) == 0)
353289 {
354290 if (!desktop_entry_reload (tmp->data))
355291 {
374310 tmp = dir->entries;
375311 while (tmp != NULL)
376312 {
377 const char *entry_basename;
378 entry_basename = desktop_entry_get_basename (tmp->data);
379
380 if (strcmp (entry_basename, basename) == 0)
313 if (strcmp (desktop_entry_get_basename (tmp->data), basename) == 0)
381314 {
382315 desktop_entry_unref (tmp->data);
383316 dir->entries = g_slist_delete_link (dir->entries, tmp);
390323 return FALSE;
391324 }
392325
393 static CachedDir *
326 static gboolean
394327 cached_dir_add_subdir (CachedDir *dir,
395328 const char *basename,
396329 const char *path)
402335 if (subdir != NULL)
403336 {
404337 subdir->deleted = FALSE;
405 return subdir;
338 return TRUE;
406339 }
407340
408341 subdir = cached_dir_new (basename);
409342
410 if (path != NULL && !cached_dir_load_entries_recursive (subdir, path))
343 if (!cached_dir_load_entries_recursive (subdir, path))
411344 {
412345 cached_dir_free (subdir);
413 return NULL;
346 return FALSE;
414347 }
415348
416349 menu_verbose ("Caching dir \"%s\"\n", basename);
417350
418351 subdir->parent = dir;
419 dir->subdirs = g_slist_prepend (dir->subdirs, cached_dir_ref (subdir));
420
421 return subdir;
352 dir->subdirs = g_slist_prepend (dir->subdirs, subdir);
353
354 return TRUE;
422355 }
423356
424357 static gboolean
435368
436369 if (subdir->references == 0)
437370 {
438 cached_dir_unref (subdir);
371 cached_dir_free (subdir);
439372 dir->subdirs = g_slist_remove (dir->subdirs, subdir);
440373 }
441374
630563 switch (event)
631564 {
632565 case MENU_MONITOR_EVENT_CREATED:
633 handled = cached_dir_add_subdir (dir, basename, path) != NULL;
566 handled = cached_dir_add_subdir (dir, basename, path);
634567 break;
635568
636569 case MENU_MONITOR_EVENT_CHANGED:
802735 static void
803736 cached_dir_add_reference (CachedDir *dir)
804737 {
805 cached_dir_ref (dir);
738 dir->references++;
806739
807740 if (dir->parent != NULL)
808741 {
817750
818751 parent = dir->parent;
819752
820 cached_dir_unref (dir);
753 if (--dir->references == 0 && dir->deleted)
754 {
755 if (dir->parent != NULL)
756 {
757 GSList *tmp;
758
759 tmp = parent->subdirs;
760 while (tmp != NULL)
761 {
762 CachedDir *subdir = tmp->data;
763
764 if (!strcmp (subdir->name, dir->name))
765 {
766 parent->subdirs = g_slist_delete_link (parent->subdirs, tmp);
767 break;
768 }
769
770 tmp = tmp->next;
771 }
772 }
773
774 cached_dir_free (dir);
775 }
821776
822777 if (parent != NULL)
823778 {
889844 g_return_val_if_fail (ed != NULL, NULL);
890845 g_return_val_if_fail (ed->refcount > 0, NULL);
891846
892 g_atomic_int_inc (&ed->refcount);
847 ed->refcount++;
893848
894849 return ed;
895850 }
897852 void
898853 entry_directory_unref (EntryDirectory *ed)
899854 {
900 gboolean is_zero;
901
902855 g_return_if_fail (ed != NULL);
903856 g_return_if_fail (ed->refcount > 0);
904857
905 is_zero = g_atomic_int_dec_and_test (&ed->refcount);
906 if (is_zero)
858 if (--ed->refcount == 0)
907859 {
908860 cached_dir_remove_reference (ed->dir);
909861
1019971
1020972 if (desktop_entry_get_type (entry) == ed->entry_type)
1021973 {
1022 gboolean ret;
1023 char *file_id;
1024 const char *basename;
1025
1026 basename = desktop_entry_get_basename (entry);
1027 g_string_append (relative_path, basename);
974 gboolean ret;
975 char *file_id;
976
977 g_string_append (relative_path,
978 desktop_entry_get_basename (entry));
1028979
1029980 file_id = get_desktop_file_id_from_path (ed,
1030981 ed->entry_type,
11041055 DesktopEntry *entry = tmp->data;
11051056 const char *basename;
11061057
1107 basename = desktop_entry_get_path (entry);
1058 basename = desktop_entry_get_basename (entry);
11081059
11091060 if (desktop_entries &&
11101061 desktop_entry_get_type (entry) == DESKTOP_ENTRY_DESKTOP)
11771128 g_return_val_if_fail (list != NULL, NULL);
11781129 g_return_val_if_fail (list->refcount > 0, NULL);
11791130
1180 g_atomic_int_inc (&list->refcount);
1131 list->refcount += 1;
11811132
11821133 return list;
11831134 }
11851136 void
11861137 entry_directory_list_unref (EntryDirectoryList *list)
11871138 {
1188 gboolean is_zero;
1189
11901139 g_return_if_fail (list != NULL);
11911140 g_return_if_fail (list->refcount > 0);
11921141
1193 is_zero = g_atomic_int_dec_and_test (&list->refcount);
1194 if (is_zero)
1142 list->refcount -= 1;
1143 if (list->refcount == 0)
11951144 {
11961145 g_list_foreach (list->dirs, (GFunc) entry_directory_unref, NULL);
11971146 g_list_free (list->dirs);
162162 MenuLayoutNode *layout);
163163 static void gmenu_tree_force_recanonicalize (GMenuTree *tree);
164164 static void gmenu_tree_invoke_monitors (GMenuTree *tree);
165
165
166166 static void gmenu_tree_item_unref_and_unset_parent (gpointer itemp);
167167
168168 typedef enum
12541254 append_directory_path (directory, path);
12551255
12561256 if (entry != NULL)
1257 {
1258 const char *basename;
1259
1260 basename = desktop_entry_get_basename (entry->desktop_entry);
1261 g_string_append (path, basename);
1262 }
1257 g_string_append (path,
1258 desktop_entry_get_basename (entry->desktop_entry));
12631259
12641260 return g_string_free (path, FALSE);
12651261 }
12891285 const char *
12901286 gmenu_tree_entry_get_desktop_file_id (GMenuTreeEntry *entry)
12911287 {
1292 g_return_val_if_fail (entry != NULL, FALSE);
1288 g_return_val_if_fail (entry != NULL, NULL);
12931289
12941290 return entry->desktop_file_id;
12951291 }
15011497 NULL);
15021498 g_slist_free (directory->contents);
15031499 directory->contents = NULL;
1504
1500
15051501 g_slist_foreach (directory->default_layout_info,
15061502 (GFunc) menu_layout_node_unref,
15071503 NULL);