#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: subr_specificdata.c,v 1.14 2017/06/01 02:45:13 chs Exp $");
#include <sys/param.h>
#include <sys/kmem.h>
#include <sys/specificdata.h>
#include <sys/queue.h>
#include <sys/mutex.h>
typedef struct {
specificdata_dtor_t ski_dtor;
} specificdata_key_impl;
struct specificdata_container {
size_t sc_nkey;
LIST_ENTRY(specificdata_container) sc_list;
void * sc_data[];
};
#define SPECIFICDATA_CONTAINER_BYTESIZE(n) \
(sizeof(struct specificdata_container) + ((n) * sizeof(void *)))
struct specificdata_domain {
kmutex_t sd_lock;
unsigned int sd_nkey;
LIST_HEAD(, specificdata_container) sd_list;
specificdata_key_impl *sd_keys;
};
static void
specificdata_container_link(specificdata_domain_t sd,
specificdata_container_t sc)
{
LIST_INSERT_HEAD(&sd->sd_list, sc, sc_list);
}
static void
specificdata_container_unlink(specificdata_domain_t sd,
specificdata_container_t sc)
{
LIST_REMOVE(sc, sc_list);
}
static void
specificdata_destroy_datum(specificdata_domain_t sd,
specificdata_container_t sc, specificdata_key_t key)
{
specificdata_dtor_t dtor;
void *data;
if (key >= sc->sc_nkey)
return;
KASSERT(key < sd->sd_nkey);
data = sc->sc_data[key];
dtor = sd->sd_keys[key].ski_dtor;
if (dtor != NULL) {
if (data != NULL) {
sc->sc_data[key] = NULL;
(*dtor)(data);
}
} else {
KASSERT(data == NULL);
}
}
static void
specificdata_noop_dtor(void *data)
{
}
specificdata_domain_t
specificdata_domain_create(void)
{
specificdata_domain_t sd;
sd = kmem_zalloc(sizeof(*sd), KM_SLEEP);
mutex_init(&sd->sd_lock, MUTEX_DEFAULT, IPL_NONE);
LIST_INIT(&sd->sd_list);
return (sd);
}
void
specificdata_domain_delete(specificdata_domain_t sd)
{
panic("specificdata_domain_delete: not implemented");
}
int
specificdata_key_create(specificdata_domain_t sd, specificdata_key_t *keyp,
specificdata_dtor_t dtor)
{
specificdata_key_impl *newkeys;
specificdata_key_t key = 0;
size_t nsz;
ASSERT_SLEEPABLE();
if (dtor == NULL)
dtor = specificdata_noop_dtor;
mutex_enter(&sd->sd_lock);
if (sd->sd_keys == NULL)
goto needalloc;
for (; key < sd->sd_nkey; key++) {
if (sd->sd_keys[key].ski_dtor == NULL)
goto gotit;
}
needalloc:
nsz = (sd->sd_nkey + 1) * sizeof(*newkeys);
newkeys = kmem_zalloc(nsz, KM_SLEEP);
if (sd->sd_keys != NULL) {
size_t osz = sd->sd_nkey * sizeof(*newkeys);
memcpy(newkeys, sd->sd_keys, osz);
kmem_free(sd->sd_keys, osz);
}
sd->sd_keys = newkeys;
sd->sd_nkey++;
gotit:
sd->sd_keys[key].ski_dtor = dtor;
mutex_exit(&sd->sd_lock);
*keyp = key;
return (0);
}
void
specificdata_key_delete(specificdata_domain_t sd, specificdata_key_t key)
{
specificdata_container_t sc;
mutex_enter(&sd->sd_lock);
if (key >= sd->sd_nkey)
goto out;
LIST_FOREACH(sc, &sd->sd_list, sc_list) {
specificdata_destroy_datum(sd, sc, key);
}
sd->sd_keys[key].ski_dtor = NULL;
out:
mutex_exit(&sd->sd_lock);
}
int
specificdata_init(specificdata_domain_t sd, specificdata_reference *ref)
{
ref->specdataref_container = NULL;
mutex_init(&ref->specdataref_lock, MUTEX_DEFAULT, IPL_NONE);
return (0);
}
void
specificdata_fini(specificdata_domain_t sd, specificdata_reference *ref)
{
specificdata_container_t sc;
specificdata_key_t key;
ASSERT_SLEEPABLE();
mutex_destroy(&ref->specdataref_lock);
sc = ref->specdataref_container;
if (sc == NULL)
return;
ref->specdataref_container = NULL;
mutex_enter(&sd->sd_lock);
specificdata_container_unlink(sd, sc);
for (key = 0; key < sc->sc_nkey; key++) {
specificdata_destroy_datum(sd, sc, key);
}
mutex_exit(&sd->sd_lock);
kmem_free(sc, SPECIFICDATA_CONTAINER_BYTESIZE(sc->sc_nkey));
}
void *
specificdata_getspecific(specificdata_domain_t sd, specificdata_reference *ref,
specificdata_key_t key)
{
specificdata_container_t sc;
void *data = NULL;
mutex_enter(&ref->specdataref_lock);
sc = ref->specdataref_container;
if (sc != NULL && key < sc->sc_nkey)
data = sc->sc_data[key];
mutex_exit(&ref->specdataref_lock);
return (data);
}
void *
specificdata_getspecific_unlocked(specificdata_domain_t sd,
specificdata_reference *ref,
specificdata_key_t key)
{
specificdata_container_t sc;
sc = ref->specdataref_container;
if (sc != NULL && key < sc->sc_nkey)
return (sc->sc_data[key]);
return (NULL);
}
void
specificdata_setspecific(specificdata_domain_t sd,
specificdata_reference *ref,
specificdata_key_t key, void *data)
{
specificdata_container_t sc, newsc;
size_t newnkey, sz;
ASSERT_SLEEPABLE();
mutex_enter(&ref->specdataref_lock);
sc = ref->specdataref_container;
if (__predict_true(sc != NULL && key < sc->sc_nkey)) {
sc->sc_data[key] = data;
mutex_exit(&ref->specdataref_lock);
return;
}
mutex_exit(&ref->specdataref_lock);
mutex_enter(&sd->sd_lock);
newnkey = sd->sd_nkey;
if (key >= newnkey) {
mutex_exit(&sd->sd_lock);
panic("specificdata_setspecific");
}
sz = SPECIFICDATA_CONTAINER_BYTESIZE(newnkey);
newsc = kmem_zalloc(sz, KM_SLEEP);
newsc->sc_nkey = newnkey;
mutex_enter(&ref->specdataref_lock);
sc = ref->specdataref_container;
if (sc != NULL) {
if (key < sc->sc_nkey) {
sc->sc_data[key] = data;
mutex_exit(&ref->specdataref_lock);
mutex_exit(&sd->sd_lock);
kmem_free(newsc, sz);
return;
}
specificdata_container_unlink(sd, sc);
memcpy(newsc->sc_data, sc->sc_data,
sc->sc_nkey * sizeof(void *));
}
newsc->sc_data[key] = data;
specificdata_container_link(sd, newsc);
ref->specdataref_container = newsc;
mutex_exit(&ref->specdataref_lock);
mutex_exit(&sd->sd_lock);
if (sc != NULL)
kmem_free(sc, SPECIFICDATA_CONTAINER_BYTESIZE(sc->sc_nkey));
}