Tag
Hash :
c04a6435
Author :
Date :
2013-03-15T16:50:30
cache: handle pkg being NULL on pkg_cache_add()/pkg_cache_remove()
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
/*
* 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 "pkg.h"
static pkg_list_t pkg_cache = PKG_LIST_INITIALIZER;
/*
* pkg_cache_lookup(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.
*/
pkg_t *
pkg_cache_lookup(const char *id)
{
pkg_node_t *node;
PKG_FOREACH_LIST_ENTRY(pkg_cache.head, node)
{
pkg_t *pkg = node->data;
if (!strcmp(pkg->id, id))
return pkg_ref(pkg);
}
return NULL;
}
/*
* pkg_cache_add(pkg)
*
* adds an entry for the package to the package cache.
* the cache entry must be removed if the package is freed.
*/
void
pkg_cache_add(pkg_t *pkg)
{
if (pkg == NULL)
return;
pkg_ref(pkg);
pkg_node_insert(&pkg->cache_iter, pkg, &pkg_cache);
}
/*
* pkg_cache_remove(pkg)
*
* deletes a package from the cache entry.
*/
void
pkg_cache_remove(pkg_t *pkg)
{
if (pkg == NULL)
return;
pkg_node_delete(&pkg->cache_iter, &pkg_cache);
}
void
pkg_cache_free(void)
{
pkg_node_t *iter, *iter2;
PKG_FOREACH_LIST_ENTRY_SAFE(pkg_cache.head, iter2, iter)
{
pkg_t *pkg = iter->data;
pkg_free(pkg);
}
}