File: cache.c

package info (click to toggle)
pkgconf 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,820 kB
  • sloc: ansic: 8,111; sh: 6,689; makefile: 247; python: 157
file content (231 lines) | stat: -rw-r--r-- 5,693 bytes parent folder | download
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * cache.c
 * package object cache
 *
 * Copyright (c) 2013 pkgconf authors (see AUTHORS).
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * This software is provided 'as is' and without any warranty, express or
 * implied.  In no event shall the authors be liable for any damages arising
 * from the use of this software.
 */

#include <libpkgconf/stdinc.h>
#include <libpkgconf/libpkgconf.h>

#include <assert.h>

/*
 * !doc
 *
 * libpkgconf `cache` module
 * =========================
 *
 * The libpkgconf `cache` module manages a package/module object cache, allowing it to
 * avoid loading duplicate copies of a package/module.
 *
 * A cache is tied to a specific pkgconf client object, so package objects should not
 * be shared across threads.
 */

static int
cache_member_cmp(const void *a, const void *b)
{
	const char *key = a;
	const pkgconf_pkg_t *pkg = *(void **) b;

	return strcmp(key, pkg->id);
}

static int
cache_member_sort_cmp(const void *a, const void *b)
{
	const pkgconf_pkg_t *pkgA = *(void **) a;
	const pkgconf_pkg_t *pkgB = *(void **) b;

	if (pkgA == NULL)
		return 1;

	if (pkgB == NULL)
		return -1;

	return strcmp(pkgA->id, pkgB->id);
}

static void
cache_dump(const pkgconf_client_t *client)
{
	size_t i;

	PKGCONF_TRACE(client, "dumping package cache contents");

	for (i = 0; i < client->cache_count; i++)
	{
		const pkgconf_pkg_t *pkg = client->cache_table[i];

		PKGCONF_TRACE(client, SIZE_FMT_SPECIFIER": %p(%s)",
			i, pkg, pkg == NULL ? "NULL" : pkg->id);
	}
}

/*
 * !doc
 *
 * .. c:function:: pkgconf_pkg_t *pkgconf_cache_lookup(const pkgconf_client_t *client, const char *id)
 *
 *    Looks up a package in the cache given an `id` atom,
 *    such as ``gtk+-3.0`` and returns the already loaded version
 *    if present.
 *
 *    :param pkgconf_client_t* client: The client object to access.
 *    :param char* id: The package atom to look up in the client object's cache.
 *    :return: A package object if present, else ``NULL``.
 *    :rtype: pkgconf_pkg_t *
 */
pkgconf_pkg_t *
pkgconf_cache_lookup(pkgconf_client_t *client, const char *id)
{
	if (client->cache_table == NULL)
		return NULL;

	pkgconf_pkg_t **pkg;

	pkg = bsearch(id, client->cache_table,
		client->cache_count, sizeof (void *),
		cache_member_cmp);

	if (pkg != NULL)
	{
		PKGCONF_TRACE(client, "found: %s @%p", id, *pkg);
		return pkgconf_pkg_ref(client, *pkg);
	}

	PKGCONF_TRACE(client, "miss: %s", id);
	return NULL;
}

/*
 * !doc
 *
 * .. c:function:: void pkgconf_cache_add(pkgconf_client_t *client, pkgconf_pkg_t *pkg)
 *
 *    Adds an entry for the package to the package cache.
 *    The cache entry must be removed if the package is freed.
 *
 *    :param pkgconf_client_t* client: The client object to modify.
 *    :param pkgconf_pkg_t* pkg: The package object to add to the client object's cache.
 *    :return: nothing
 */
void
pkgconf_cache_add(pkgconf_client_t *client, pkgconf_pkg_t *pkg)
{
	if (pkg == NULL)
		return;

	pkgconf_pkg_ref(client, pkg);

	PKGCONF_TRACE(client, "added @%p to cache", pkg);

	/* mark package as cached */
	pkg->flags |= PKGCONF_PKG_PROPF_CACHED;

	++client->cache_count;
	client->cache_table = pkgconf_reallocarray(client->cache_table,
		client->cache_count, sizeof (void *));
	client->cache_table[client->cache_count - 1] = pkg;

	qsort(client->cache_table, client->cache_count,
		sizeof(void *), cache_member_sort_cmp);
}

/*
 * !doc
 *
 * .. c:function:: void pkgconf_cache_remove(pkgconf_client_t *client, pkgconf_pkg_t *pkg)
 *
 *    Deletes a package from the client object's package cache.
 *
 *    :param pkgconf_client_t* client: The client object to modify.
 *    :param pkgconf_pkg_t* pkg: The package object to remove from the client object's cache.
 *    :return: nothing
 */
void
pkgconf_cache_remove(pkgconf_client_t *client, pkgconf_pkg_t *pkg)
{
	if (client->cache_table == NULL)
		return;

	if (pkg == NULL)
		return;

	if (!(pkg->flags & PKGCONF_PKG_PROPF_CACHED))
		return;

	PKGCONF_TRACE(client, "removed @%p from cache", pkg);

	pkgconf_pkg_t **slot;

	slot = bsearch(pkg->id, client->cache_table,
		client->cache_count, sizeof (void *),
		cache_member_cmp);

	if (slot == NULL)
		return;

	(*slot)->flags &= ~PKGCONF_PKG_PROPF_CACHED;
	pkgconf_pkg_unref(client, *slot);
	*slot = NULL;

	qsort(client->cache_table, client->cache_count,
		sizeof(void *), cache_member_sort_cmp);

	if (client->cache_table[client->cache_count - 1] != NULL)
	{
		PKGCONF_TRACE(client, "end of cache table refers to %p, not NULL",
			client->cache_table[client->cache_count - 1]);
		cache_dump(client);
		abort();
	}

	client->cache_count--;
	if (client->cache_count > 0)
	{
		client->cache_table = pkgconf_reallocarray(client->cache_table,
			client->cache_count, sizeof(void *));
	}
	else
	{
		free(client->cache_table);
		client->cache_table = NULL;
	}
}

/*
 * !doc
 *
 * .. c:function:: void pkgconf_cache_free(pkgconf_client_t *client)
 *
 *    Releases all resources related to a client object's package cache.
 *    This function should only be called to clear a client object's package cache,
 *    as it may release any package in the cache.
 *
 *    :param pkgconf_client_t* client: The client object to modify.
 */
void
pkgconf_cache_free(pkgconf_client_t *client)
{
	if (client->cache_table == NULL)
		return;

	while (client->cache_count > 0)
		pkgconf_cache_remove(client, client->cache_table[0]);

	free(client->cache_table);
	client->cache_table = NULL;
	client->cache_count = 0;

	PKGCONF_TRACE(client, "cleared package cache");
}