File: | build-analysis/../src/lib/s4/src/lib/sourcepref.c |
Warning: | line 65, column 14 Call to 'malloc' has an allocation size of 0 bytes |
1 | /* S4 - An XMMS2 medialib backend | |||
2 | * Copyright (C) 2009, 2010 Sivert Berg | |||
3 | * | |||
4 | * This library is free software; you can redistribute it and/or | |||
5 | * modify it under the terms of the GNU Lesser General Public | |||
6 | * License as published by the Free Software Foundation; either | |||
7 | * version 2.1 of the License, or (at your option) any later version. | |||
8 | * | |||
9 | * This library is distributed in the hope that it will be useful, | |||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
12 | * Lesser General Public License for more details. | |||
13 | */ | |||
14 | ||||
15 | #include "s4_priv.h" | |||
16 | #include <stdlib.h> | |||
17 | #include <string.h> | |||
18 | ||||
19 | struct s4_sourcepref_St { | |||
20 | GHashTable *table; | |||
21 | GMutex lock; | |||
22 | GPatternSpec **specs; | |||
23 | int spec_count; | |||
24 | int ref_count; | |||
25 | }; | |||
26 | ||||
27 | /** | |||
28 | * @defgroup Sourcepref Source Preferences | |||
29 | * @ingroup S4 | |||
30 | * @brief Handles source preferences | |||
31 | * | |||
32 | * @{ | |||
33 | */ | |||
34 | ||||
35 | /* Helper function to s4_sourcepref_get_priority */ | |||
36 | static int _get_priority (s4_sourcepref_t *sp, const char *src) | |||
37 | { | |||
38 | int i; | |||
39 | for (i = 0; i < sp->spec_count; i++) { | |||
40 | if (g_pattern_match_string (sp->specs[i], src)) { | |||
41 | return i; | |||
42 | } | |||
43 | } | |||
44 | ||||
45 | return INT_MAX2147483647; | |||
46 | } | |||
47 | ||||
48 | /** | |||
49 | * Creates a new source preferences that can be used when querying | |||
50 | * | |||
51 | * @param srcprefs An NULL terminated array of sources, where the | |||
52 | * first one has the highest priority. The sources may use glob- | |||
53 | * like patterns, for example "plugin*". | |||
54 | * @return A new sourcepref | |||
55 | */ | |||
56 | s4_sourcepref_t *s4_sourcepref_create (const char **srcprefs) | |||
57 | { | |||
58 | int i; | |||
59 | s4_sourcepref_t *sp = malloc (sizeof (s4_sourcepref_t)); | |||
60 | sp->table = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL((void*)0), free); | |||
61 | g_mutex_init (&sp->lock); | |||
62 | ||||
63 | for (i = 0; srcprefs[i] != NULL((void*)0); i++); | |||
| ||||
64 | ||||
65 | sp->specs = malloc (sizeof (GPatternSpec*) * i); | |||
| ||||
66 | sp->spec_count = i; | |||
67 | sp->ref_count = 1; | |||
68 | ||||
69 | for (i = 0; i < sp->spec_count; i++) | |||
70 | sp->specs[i] = g_pattern_spec_new (srcprefs[i]); | |||
71 | ||||
72 | return sp; | |||
73 | } | |||
74 | ||||
75 | /** | |||
76 | * Decreases the reference of a sourcepref. | |||
77 | * If the reference count is less than or equal to zero | |||
78 | * it will be freed. | |||
79 | * @param sp The sourcepref to free | |||
80 | */ | |||
81 | void s4_sourcepref_unref (s4_sourcepref_t *sp) | |||
82 | { | |||
83 | int i; | |||
84 | ||||
85 | if (g_atomic_int_dec_and_test (&sp->ref_count)(__extension__ ({ typedef char _GStaticAssertCompileTimeAssertion_2 [(sizeof *(&sp->ref_count) == sizeof (gint)) ? 1 : -1] __attribute__((__unused__)); (void) (0 ? *(&sp->ref_count ) ^ *(&sp->ref_count) : 0); __sync_fetch_and_sub ((& sp->ref_count), 1) == 1; }))) { | |||
86 | g_hash_table_destroy (sp->table); | |||
87 | g_mutex_clear (&sp->lock); | |||
88 | ||||
89 | for (i = 0; i < sp->spec_count; i++) | |||
90 | g_pattern_spec_free (sp->specs[i]); | |||
91 | ||||
92 | free (sp->specs); | |||
93 | free (sp); | |||
94 | } | |||
95 | } | |||
96 | ||||
97 | /** | |||
98 | * Increases the refcount of a sourcepref | |||
99 | * @param sp The sourcepref to reference | |||
100 | */ | |||
101 | s4_sourcepref_t *s4_sourcepref_ref (s4_sourcepref_t *sp) | |||
102 | { | |||
103 | g_atomic_int_inc (&sp->ref_count)(__extension__ ({ typedef char _GStaticAssertCompileTimeAssertion_3 [(sizeof *(&sp->ref_count) == sizeof (gint)) ? 1 : -1] __attribute__((__unused__)); (void) (0 ? *(&sp->ref_count ) ^ *(&sp->ref_count) : 0); (void) __sync_fetch_and_add ((&sp->ref_count), 1); })); | |||
104 | return sp; | |||
105 | } | |||
106 | ||||
107 | /** | |||
108 | * Gets the priority of a source | |||
109 | * | |||
110 | * @param sp The sourcepref to check against | |||
111 | * @param src The source to check | |||
112 | * @return The priority of the source | |||
113 | */ | |||
114 | int s4_sourcepref_get_priority (s4_sourcepref_t *sp, const char *src) | |||
115 | { | |||
116 | if (sp == NULL((void*)0)) | |||
117 | return 0; | |||
118 | ||||
119 | g_mutex_lock (&sp->lock); | |||
120 | ||||
121 | int *i = g_hash_table_lookup (sp->table, src); | |||
122 | ||||
123 | if (i == NULL((void*)0)) { | |||
124 | int pri = _get_priority (sp, src); | |||
125 | ||||
126 | i = malloc (sizeof (int)); | |||
127 | *i = pri; | |||
128 | g_hash_table_insert (sp->table, (void*)src, i); | |||
129 | g_mutex_unlock (&sp->lock); | |||
130 | ||||
131 | return pri; | |||
132 | } | |||
133 | g_mutex_unlock (&sp->lock); | |||
134 | ||||
135 | return *i; | |||
136 | } | |||
137 | ||||
138 | /** | |||
139 | * @} | |||
140 | */ |