Debian Codegraph @

src/OVAL/probes/probe/ncache.c

raw symbols SCIP
1 /*
2 * Copyright 2011 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Daniel Kopecek <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stddef.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <pthread.h>
31 #include <sexp.h>
32
33 #include "common/bfind.h"
34
35 #include "ncache.h"
36
37 /**
38 * Lock cache for reading.
39 * @param c element name cache
40 * @param r return value on failure
41 */
42 #define PROBE_NCACHE_RLOCK(c,r) \
43 do { \
44 if (pthread_rwlock_rdlock (&(c)->lock) != 0) \
45 return (r); \
46 } while (0)
47
48 /**
49 * Unlock cache which was previously locked for reading.
50 * @param c element name cache
51 * @return this function calls abort(3) if it is unable to
52 * unlock the cache so as to prevent a deadlock.
53 */
54 #define PROBE_NCACHE_RUNLOCK(c) \
55 do { \
56 if (pthread_rwlock_unlock (&(c)->lock) != 0) \
57 abort (); \
58 } while (0)
59
60 /**
61 * Lock cache for writing.
62 * @param c element name cache
63 * @param r return value on failure
64 */
65 #define PROBE_NCACHE_WLOCK(c,r) \
66 do { \
67 if (pthread_rwlock_wrlock (&(c)->lock) != 0) \
68 return (r); \
69 } while (0)
70
71 /**
72 * Unlock cache which was previously locked for writing.
73 * @param c element name cache
74 * @return this function calls abort(3) if it is unable to
75 * unlock the cache so as to prevent a deadlock.
76 */
77 #define PROBE_NCACHE_WUNLOCK(c) PROBE_NCACHE_RUNLOCK(c)
78
79 probe_ncache_t *probe_ncache_new (void)
80 {
81 probe_ncache_t *cache = malloc(sizeof(probe_ncache_t));
82
83 if (pthread_rwlock_init (&cache->lock, NULL) != 0) {
84 free (cache);
85 return (NULL);
86 }
87
88 cache->name = calloc (PROBE_NCACHE_INIT_SIZE, sizeof (SEXP_t *));
89 cache->size = PROBE_NCACHE_INIT_SIZE;
90 cache->real = 0;
91
92 return (cache);
93 }
94
95 void probe_ncache_free (probe_ncache_t *cache)
96 {
97 size_t i;
98
99 if (cache == NULL) {
100 return;
101 }
102
103 for (i = 0; i < cache->real; ++i)
104 if (cache->name[i] != NULL)
105 SEXP_free (cache->name[i]);
106
107 free (cache->name);
108 pthread_rwlock_destroy (&cache->lock);
109 free (cache);
110
111 return;
112 }
113
114 void probe_ncache_clear (probe_ncache_t *cache)
115 {
116 size_t i;
117
118 if (cache == NULL)
119 return;
120
121 if (pthread_rwlock_wrlock(&(cache)->lock))
122 return;
123 for (i = 0; i < cache->real; ++i)
124 if (cache->name[i] != NULL)
125 SEXP_free(cache->name[i]);
126 cache->real = 0;
127 if (pthread_rwlock_unlock(&(cache)->lock))
128 abort();
129 }
130
131 static int probe_ncache_cmp1 (const char *name, const SEXP_t **sexp)
132 {
133 return ((-1) * SEXP_strcmp (*sexp, name));
134 }
135
136 static int probe_ncache_cmp2 (const SEXP_t **a, const SEXP_t **b)
137 {
138 return SEXP_string_cmp (*a, *b);
139 }
140
141 SEXP_t *probe_ncache_add (probe_ncache_t *cache, const char *name)
142 {
143 SEXP_t *ref;
144
145 if (cache == NULL || name == NULL) {
146 return NULL;
147 }
148
149 ref = SEXP_string_new (name, strlen (name));
150
151 if (ref == NULL)
152 return (NULL);
153
154 PROBE_NCACHE_WLOCK(cache, NULL);
155
156 if (cache->size <= cache->real) {
157 void *new_name = realloc (cache->name, sizeof (SEXP_t *) * (cache->size + PROBE_NCACHE_ADD_SIZE));
158 if (new_name == NULL) {
159 SEXP_free(ref);
160 PROBE_NCACHE_WUNLOCK(cache);
161 return NULL;
162 }
163 cache->name = new_name;
164 cache->size += PROBE_NCACHE_ADD_SIZE;
165 }
166
167 cache->name[cache->real] = ref;
168 ++cache->real;
169
170 ref = SEXP_ref (ref);
171 qsort (cache->name, cache->real, sizeof (SEXP_t *), (int (*)(const void *, const void *)) &probe_ncache_cmp2);
172
173 PROBE_NCACHE_WUNLOCK(cache);
174
175 return (ref);
176 }
177
178 SEXP_t *probe_ncache_get (probe_ncache_t *cache, const char *name)
179 {
180 SEXP_t **ref = NULL;
181
182 if (cache == NULL || name == NULL) {
183 return NULL;
184 }
185
186 PROBE_NCACHE_RLOCK(cache, NULL);
187
188 if (cache->real > 0) {
189 ref = (SEXP_t **)oscap_bfind (cache->name, cache->real, sizeof (SEXP_t *),
190 (void *)name, (int (*)(void *, void *))&probe_ncache_cmp1);
191
192 if (ref != NULL)
193 ref = (SEXP_t **)SEXP_ref (*ref);
194 }
195
196 PROBE_NCACHE_RUNLOCK(cache);
197
198 return ((SEXP_t *)(ref));
199 }
200
201 SEXP_t *probe_ncache_ref (probe_ncache_t *cache, const char *name)
202 {
203 SEXP_t *ref;
204
205 if (name == NULL) {
206 return NULL;
207 }
208
209 if (cache == NULL)
210 return SEXP_string_new (name, strlen (name));
211
212 ref = probe_ncache_get (cache, name);
213
214 if (ref == NULL)
215 ref = probe_ncache_add (cache, name);
216
217 return (ref);
218 }