summaryrefslogtreecommitdiff
path: root/lib/kobject.cc
blob: 8e8329e1f3cca5f445bcc3ac1b2d468bd5949a89 (plain)
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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */

/*
 * kobject — implementation.
 *
 * Lives in lib/ (mirroring Linux's lib/kobject.c) because kobject is a
 * generic primitive, not tied to the driver model.
 *
 * Reference: docs/specs/storage_stack.md §4.2
 */

#include <lume/kobject.h>
#include <lume/container_of.h>
#include <lume/klog.h>

void kobject_init(kobject* k, const kobj_type* type)
{
    if (k == nullptr) {
        return;
    }
    k->name = nullptr;
    k->parent = nullptr;
    k->ktype = type;
    k->state_in_sysfs = false;
    k->entry.init();
    k->children.init();
    k->lock.init("kobject");
    kref_init(&k->refcount);
}

int kobject_add(kobject* k, kobject* parent, const char* name)
{
    if (k == nullptr) {
        return -1;
    }
    k->name = name;
    if (parent != nullptr) {
        kobject_get(parent);
        k->parent = parent;
        LockGuard g(parent->lock);
        list_add_tail(&k->entry, &parent->children);
    }
    return 0;
}

void kobject_del(kobject* k)
{
    if (k == nullptr || k->parent == nullptr) {
        return;
    }
    kobject* parent = k->parent;
    {
        LockGuard g(parent->lock);
        list_del_init(&k->entry);
    }
    k->parent = nullptr;
    kobject_put(parent);
}

void kobject_get(kobject* k)
{
    if (k != nullptr) {
        kref_get(&k->refcount);
    }
}

static void kobject_release(kref* r)
{
    kobject* k = container_of(r, kobject, refcount);
    if (k->ktype != nullptr && k->ktype->release != nullptr) {
        k->ktype->release(k);
    }
}

void kobject_put(kobject* k)
{
    if (k == nullptr) {
        return;
    }
    kref_put(&k->refcount, kobject_release);
}