2021-02-28 02:18:39 +01:00
|
|
|
/* See the end of this file for copyright, license, and warranty information. */
|
2020-12-08 16:38:19 +01:00
|
|
|
|
|
|
|
#include <ardix/atom.h>
|
2021-02-28 01:28:07 +01:00
|
|
|
#include <ardix/malloc.h>
|
2020-12-08 16:38:19 +01:00
|
|
|
#include <ardix/kent.h>
|
|
|
|
#include <ardix/list.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2021-05-11 14:31:05 +02:00
|
|
|
struct kent _kent_root;
|
|
|
|
struct kent *kent_root = NULL;
|
|
|
|
|
2021-02-28 01:28:07 +01:00
|
|
|
int kent_root_init(void)
|
|
|
|
{
|
|
|
|
if (kent_root != NULL)
|
|
|
|
return -EEXIST;
|
2021-05-11 14:31:05 +02:00
|
|
|
kent_root = &_kent_root;
|
2021-02-28 01:28:07 +01:00
|
|
|
|
|
|
|
kent_root->parent = NULL;
|
2021-07-31 15:58:29 +02:00
|
|
|
kent_root->destroy = NULL;
|
2021-02-28 01:28:07 +01:00
|
|
|
atom_init(&kent_root->refcount);
|
|
|
|
kent_get(kent_root);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int kent_init(struct kent *kent)
|
|
|
|
{
|
2021-07-31 15:58:29 +02:00
|
|
|
if (kent->parent == NULL || kent->destroy == NULL)
|
2020-12-08 16:38:19 +01:00
|
|
|
return -EFAULT;
|
2021-02-28 01:28:07 +01:00
|
|
|
kent_get(kent->parent);
|
2020-12-08 16:38:19 +01:00
|
|
|
|
|
|
|
atom_init(&kent->refcount);
|
|
|
|
kent_get(kent);
|
|
|
|
|
2021-02-28 01:28:07 +01:00
|
|
|
return 0;
|
2020-12-08 16:38:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void kent_get(struct kent *kent)
|
|
|
|
{
|
|
|
|
atom_get(&kent->refcount);
|
|
|
|
}
|
|
|
|
|
|
|
|
void kent_put(struct kent *kent)
|
|
|
|
{
|
2021-02-28 01:28:07 +01:00
|
|
|
struct kent *parent = kent->parent;
|
2020-12-08 16:38:19 +01:00
|
|
|
|
2021-02-28 01:28:07 +01:00
|
|
|
if (atom_put(&kent->refcount) == 0) {
|
2021-07-31 15:58:29 +02:00
|
|
|
kent->destroy(kent);
|
2021-02-28 01:28:07 +01:00
|
|
|
|
|
|
|
if (parent != NULL)
|
|
|
|
kent_put(parent);
|
2020-12-08 16:38:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-02-28 02:18:39 +01:00
|
|
|
* This file is part of Ardix.
|
|
|
|
* Copyright (c) 2020, 2021 Felix Kopp <owo@fef.moe>.
|
2020-12-08 16:38:19 +01:00
|
|
|
*
|
2021-05-10 16:19:38 +02:00
|
|
|
* Ardix is non-violent software: you may only use, redistribute,
|
|
|
|
* and/or modify it under the terms of the CNPLv6+ as found in
|
|
|
|
* the LICENSE file in the source code root directory or at
|
|
|
|
* <https://git.pixie.town/thufie/CNPL>.
|
2020-12-08 16:38:19 +01:00
|
|
|
*
|
2021-05-10 16:19:38 +02:00
|
|
|
* Ardix comes with ABSOLUTELY NO WARRANTY, to the extent
|
|
|
|
* permitted by applicable law. See the CNPLv6+ for details.
|
2020-12-08 16:38:19 +01:00
|
|
|
*/
|