forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj_core.c
287 lines (228 loc) · 6.11 KB
/
obj_core.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Copyright (c) 2023, Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/kernel/obj_core.h>
static struct k_spinlock lock;
sys_slist_t z_obj_type_list = SYS_SLIST_STATIC_INIT(&z_obj_type_list);
struct k_obj_type *z_obj_type_init(struct k_obj_type *type,
uint32_t id, size_t off)
{
sys_slist_init(&type->list);
sys_slist_append(&z_obj_type_list, &type->node);
type->id = id;
type->obj_core_offset = off;
return type;
}
void k_obj_core_init(struct k_obj_core *obj_core, struct k_obj_type *type)
{
obj_core->node.next = NULL;
obj_core->type = type;
#ifdef CONFIG_OBJ_CORE_STATS
obj_core->stats = NULL;
#endif /* CONFIG_OBJ_CORE_STATS */
}
void k_obj_core_link(struct k_obj_core *obj_core)
{
k_spinlock_key_t key = k_spin_lock(&lock);
sys_slist_append(&obj_core->type->list, &obj_core->node);
k_spin_unlock(&lock, key);
}
void k_obj_core_init_and_link(struct k_obj_core *obj_core,
struct k_obj_type *type)
{
k_obj_core_init(obj_core, type);
k_obj_core_link(obj_core);
}
void k_obj_core_unlink(struct k_obj_core *obj_core)
{
k_spinlock_key_t key = k_spin_lock(&lock);
sys_slist_find_and_remove(&obj_core->type->list, &obj_core->node);
k_spin_unlock(&lock, key);
}
struct k_obj_type *k_obj_type_find(uint32_t type_id)
{
struct k_obj_type *type;
struct k_obj_type *rv = NULL;
sys_snode_t *node;
k_spinlock_key_t key = k_spin_lock(&lock);
SYS_SLIST_FOR_EACH_NODE(&z_obj_type_list, node) {
type = CONTAINER_OF(node, struct k_obj_type, node);
if (type->id == type_id) {
rv = type;
break;
}
}
k_spin_unlock(&lock, key);
return rv;
}
int k_obj_type_walk_locked(struct k_obj_type *type,
int (*func)(struct k_obj_core *, void *),
void *data)
{
k_spinlock_key_t key;
struct k_obj_core *obj_core;
sys_snode_t *node;
int status = 0;
key = k_spin_lock(&lock);
SYS_SLIST_FOR_EACH_NODE(&type->list, node) {
obj_core = CONTAINER_OF(node, struct k_obj_core, node);
status = func(obj_core, data);
if (status != 0) {
break;
}
}
k_spin_unlock(&lock, key);
return status;
}
int k_obj_type_walk_unlocked(struct k_obj_type *type,
int (*func)(struct k_obj_core *, void *),
void *data)
{
struct k_obj_core *obj_core;
sys_snode_t *node;
sys_snode_t *next;
int status = 0;
SYS_SLIST_FOR_EACH_NODE_SAFE(&type->list, node, next) {
obj_core = CONTAINER_OF(node, struct k_obj_core, node);
status = func(obj_core, data);
if (status != 0) {
break;
}
}
return status;
}
#ifdef CONFIG_OBJ_CORE_STATS
int k_obj_core_stats_register(struct k_obj_core *obj_core, void *stats,
size_t stats_len)
{
int rv;
k_spinlock_key_t key = k_spin_lock(&lock);
if (obj_core->type->stats_desc == NULL) {
/* Object type not configured for statistics. */
rv = -ENOTSUP;
} else if (obj_core->type->stats_desc->raw_size != stats_len) {
/* Buffer size mismatch */
rv = -EINVAL;
} else {
obj_core->stats = stats;
rv = 0;
}
k_spin_unlock(&lock, key);
return rv;
}
int k_obj_core_stats_deregister(struct k_obj_core *obj_core)
{
int rv;
k_spinlock_key_t key = k_spin_lock(&lock);
if (obj_core->type->stats_desc == NULL) {
/* Object type not configured for statistics. */
rv = -ENOTSUP;
} else {
obj_core->stats = NULL;
rv = 0;
}
k_spin_unlock(&lock, key);
return rv;
}
int k_obj_core_stats_raw(struct k_obj_core *obj_core, void *stats,
size_t stats_len)
{
int rv;
struct k_obj_core_stats_desc *desc;
k_spinlock_key_t key = k_spin_lock(&lock);
desc = obj_core->type->stats_desc;
if ((desc == NULL) || (desc->raw == NULL)) {
/* The object type is not configured for this operation */
rv = -ENOTSUP;
} else if ((desc->raw_size != stats_len) || (obj_core->stats == NULL)) {
/*
* Either the size of the stats buffer is wrong or
* the kernel object was not registered for statistics.
*/
rv = -EINVAL;
} else {
rv = desc->raw(obj_core, stats);
}
k_spin_unlock(&lock, key);
return rv;
}
int k_obj_core_stats_query(struct k_obj_core *obj_core, void *stats,
size_t stats_len)
{
int rv;
struct k_obj_core_stats_desc *desc;
k_spinlock_key_t key = k_spin_lock(&lock);
desc = obj_core->type->stats_desc;
if ((desc == NULL) || (desc->query == NULL)) {
/* The object type is not configured for this operation */
rv = -ENOTSUP;
} else if ((desc->query_size != stats_len) || (obj_core->stats == NULL)) {
/*
* Either the size of the stats buffer is wrong or
* the kernel object was not registered for statistics.
*/
rv = -EINVAL;
} else {
rv = desc->query(obj_core, stats);
}
k_spin_unlock(&lock, key);
return rv;
}
int k_obj_core_stats_reset(struct k_obj_core *obj_core)
{
int rv;
struct k_obj_core_stats_desc *desc;
k_spinlock_key_t key = k_spin_lock(&lock);
desc = obj_core->type->stats_desc;
if ((desc == NULL) || (desc->reset == NULL)) {
/* The object type is not configured for this operation */
rv = -ENOTSUP;
} else if (obj_core->stats == NULL) {
/* This kernel object is not configured for statistics */
rv = -EINVAL;
} else {
rv = desc->reset(obj_core);
}
k_spin_unlock(&lock, key);
return rv;
}
int k_obj_core_stats_disable(struct k_obj_core *obj_core)
{
int rv;
struct k_obj_core_stats_desc *desc;
k_spinlock_key_t key = k_spin_lock(&lock);
desc = obj_core->type->stats_desc;
if ((desc == NULL) || (desc->disable == NULL)) {
/* The object type is not configured for this operation */
rv = -ENOTSUP;
} else if (obj_core->stats == NULL) {
/* This kernel object is not configured for statistics */
rv = -EINVAL;
} else {
rv = desc->disable(obj_core);
}
k_spin_unlock(&lock, key);
return rv;
}
int k_obj_core_stats_enable(struct k_obj_core *obj_core)
{
int rv;
struct k_obj_core_stats_desc *desc;
k_spinlock_key_t key = k_spin_lock(&lock);
desc = obj_core->type->stats_desc;
if ((desc == NULL) || (desc->enable == NULL)) {
/* The object type is not configured for this operation */
rv = -ENOTSUP;
} else if (obj_core->stats == NULL) {
/* This kernel object is not configured for statistics */
rv = -EINVAL;
} else {
rv = desc->enable(obj_core);
}
k_spin_unlock(&lock, key);
return rv;
}
#endif /* CONFIG_OBJ_CORE_STATS */