device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm / nm-object-cache.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
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.
7  *
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.
12  *
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.
17  *
18  * Copyright 2008 Red Hat, Inc.
19  */
20
21 #include "nm-default.h"
22
23 #include <string.h>
24
25 #include "nm-object-cache.h"
26 #include "nm-object.h"
27
28 static GHashTable *cache = NULL;
29
30 static void
31 _init_cache (void)
32 {
33         if (G_UNLIKELY (cache == NULL))
34                 cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
35 }
36
37 static void
38 _nm_object_cache_remove_by_path (char *path)
39 {
40         _init_cache ();
41         g_hash_table_remove (cache, path);
42         g_free (path);
43 }
44
45 void
46 _nm_object_cache_add (NMObject *object)
47 {
48         char *path;
49
50         _init_cache ();
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);
55 }
56
57 NMObject *
58 _nm_object_cache_get (const char *path)
59 {
60         NMObject *object;
61
62         _init_cache ();
63         object = g_hash_table_lookup (cache, path);
64         return object ? g_object_ref (object) : NULL;
65 }
66
67 void
68 _nm_object_cache_clear (void)
69 {
70         GHashTableIter iter;
71         GObject *obj;
72         const char *path;
73         char *foo;
74
75         if (!cache)
76                 return;
77
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
83                  * one being released.
84                  */
85                 foo = g_object_steal_data (obj, "nm-object-cache-tag");
86                 g_free (foo);
87
88                 g_hash_table_iter_remove (&iter);
89         }
90 }