aboutsummaryrefslogtreecommitdiff
path: root/src/mm-port-probe-cache.c
blob: 86bfb6a2e7687b27e89a8cd862df5d5336c6efbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details:
 *
 * Copyright (C) 2011 Aleksander Morgado <aleksander@gnu.org>
 */

#include <glib.h>

#include "mm-port-probe.h"
#include "mm-port-probe-cache.h"

/* Cache of port probing objects */
static GHashTable *cache;

static gchar *
get_key (GUdevDevice *port)
{
    return g_strdup_printf ("%s%s",
                            g_udev_device_get_subsystem (port),
                            g_udev_device_get_name (port));
}

static void
probe_remove (MMPortProbe *probe)
{
    mm_port_probe_run_cancel (probe);
    g_object_unref (probe);
}

MMPortProbe *
mm_port_probe_cache_get (GUdevDevice *port,
                         const gchar *physdev_path,
                         const gchar *driver)
{
    MMPortProbe *probe = NULL;
    gchar *key;

    key = get_key (port);

    if (G_UNLIKELY (!cache)) {
        cache = g_hash_table_new_full (g_str_hash,
                                       g_str_equal,
                                       g_free,
                                       (GDestroyNotify)probe_remove);
    } else {
        probe = g_hash_table_lookup (cache, key);
    }

    if (!probe) {
        probe = mm_port_probe_new (port, physdev_path, driver);
        g_hash_table_insert (cache, key, probe);
    } else
        g_free (key);

    return g_object_ref (probe);
}

void
mm_port_probe_cache_remove (GUdevDevice *port)
{
    gchar *key;

    if (G_UNLIKELY (!cache))
        return;

    key = get_key (port);
    g_hash_table_remove (cache, key);
    g_free (key);
}

void
mm_port_probe_cache_clear (void)
{
    if (G_UNLIKELY (!cache))
        return;

    g_hash_table_remove_all (cache);
}