5
* Copyright (c) 2013 pkgconf authors (see AUTHORS).
7
* Permission to use, copy, modify, and/or distribute this software for any
8
* purpose with or without fee is hereby granted, provided that the above
9
* copyright notice and this permission notice appear in all copies.
11
* This software is provided 'as is' and without any warranty, express or
12
* implied. In no event shall the authors be liable for any damages arising
13
* from the use of this software.
16
#include <libpkgconf/stdinc.h>
17
#include <libpkgconf/libpkgconf.h>
22
* libpkgconf `cache` module
23
* =========================
25
* The libpkgconf `cache` module manages a package/module object cache, allowing it to
26
* avoid loading duplicate copies of a package/module.
28
* A cache is tied to a specific pkgconf client object, so package objects should not
29
* be shared across threads.
35
* .. c:function:: pkgconf_pkg_t *pkgconf_cache_lookup(const pkgconf_client_t *client, const char *id)
37
* Looks up a package in the cache given an `id` atom,
38
* such as ``gtk+-3.0`` and returns the already loaded version
41
* :param pkgconf_client_t* client: The client object to access.
42
* :param char* id: The package atom to look up in the client object's cache.
43
* :return: A package object if present, else ``NULL``.
44
* :rtype: pkgconf_pkg_t *
47
pkgconf_cache_lookup(pkgconf_client_t *client, const char *id)
51
PKGCONF_FOREACH_LIST_ENTRY(client->pkg_cache.head, node)
53
pkgconf_pkg_t *pkg = node->data;
55
if (!strcmp(pkg->id, id))
57
PKGCONF_TRACE(client, "found: %s @%p", id, pkg);
58
return pkgconf_pkg_ref(client, pkg);
62
PKGCONF_TRACE(client, "miss: %s", id);
69
* .. c:function:: void pkgconf_cache_add(pkgconf_client_t *client, pkgconf_pkg_t *pkg)
71
* Adds an entry for the package to the package cache.
72
* The cache entry must be removed if the package is freed.
74
* :param pkgconf_client_t* client: The client object to modify.
75
* :param pkgconf_pkg_t* pkg: The package object to add to the client object's cache.
79
pkgconf_cache_add(pkgconf_client_t *client, pkgconf_pkg_t *pkg)
84
pkgconf_pkg_ref(client, pkg);
85
pkgconf_node_insert(&pkg->cache_iter, pkg, &client->pkg_cache);
87
PKGCONF_TRACE(client, "added @%p to cache", pkg);
89
/* mark package as cached */
90
pkg->flags |= PKGCONF_PKG_PROPF_CACHED;
96
* .. c:function:: void pkgconf_cache_remove(pkgconf_client_t *client, pkgconf_pkg_t *pkg)
98
* Deletes a package from the client object's package cache.
100
* :param pkgconf_client_t* client: The client object to modify.
101
* :param pkgconf_pkg_t* pkg: The package object to remove from the client object's cache.
105
pkgconf_cache_remove(pkgconf_client_t *client, pkgconf_pkg_t *pkg)
110
if (!(pkg->flags & PKGCONF_PKG_PROPF_CACHED))
113
PKGCONF_TRACE(client, "removed @%p from cache", pkg);
115
pkgconf_node_delete(&pkg->cache_iter, &client->pkg_cache);
121
* .. c:function:: void pkgconf_cache_free(pkgconf_client_t *client)
123
* Releases all resources related to a client object's package cache.
124
* This function should only be called to clear a client object's package cache,
125
* as it may release any package in the cache.
127
* :param pkgconf_client_t* client: The client object to modify.
130
pkgconf_cache_free(pkgconf_client_t *client)
132
pkgconf_node_t *iter, *iter2;
134
PKGCONF_FOREACH_LIST_ENTRY_SAFE(client->pkg_cache.head, iter2, iter)
136
pkgconf_pkg_t *pkg = iter->data;
137
pkgconf_pkg_unref(client, pkg);
140
memset(&client->pkg_cache, 0, sizeof client->pkg_cache);
142
PKGCONF_TRACE(client, "cleared package cache");