1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the
15 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301 USA.
18 * Copyright 2008 Red Hat, Inc.
21 #include "nm-default.h"
25 #include "nm-object-cache.h"
26 #include "nm-object.h"
28 static GHashTable *cache = NULL;
33 if (G_UNLIKELY (cache == NULL))
34 cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
38 _nm_object_cache_remove_by_path (char *path)
41 g_hash_table_remove (cache, path);
46 _nm_object_cache_add (NMObject *object)
51 path = g_strdup (nm_object_get_path (object));
52 g_hash_table_insert (cache, path, object);
53 g_object_set_data_full (G_OBJECT (object), "nm-object-cache-tag",
54 g_strdup (path), (GDestroyNotify) _nm_object_cache_remove_by_path);
58 _nm_object_cache_get (const char *path)
63 object = g_hash_table_lookup (cache, path);
64 return object ? g_object_ref (object) : NULL;
68 _nm_object_cache_clear (void)
78 g_hash_table_iter_init (&iter, cache);
79 while (g_hash_table_iter_next (&iter, (gpointer) &path, (gpointer) &obj)) {
80 /* Remove the callback so that if the object isn't yet released
81 * by a client, when it does finally get unrefed, it won't trigger
82 * the cache removal for a new object with the same path as the
85 foo = g_object_steal_data (obj, "nm-object-cache-tag");
88 g_hash_table_iter_remove (&iter);