/src/barebox/lib/bobject.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | |
3 | | #include <bobject.h> |
4 | | #include <stdio.h> |
5 | | |
6 | | /** |
7 | | * bobject_set_name - set a barebox object's name |
8 | | * @bobj: barebox object or device |
9 | | * @fmt: format string for the object's name |
10 | | * |
11 | | * NOTE: This function expects bobj->name to be free()-able, so extra |
12 | | * precautions needs to be taken when mixing its usage with manual |
13 | | * assignement of bobject.name. |
14 | | */ |
15 | | int bobject_set_name(bobject_t bobj, const char *fmt, ...) |
16 | 10.4k | { |
17 | 10.4k | va_list vargs; |
18 | 10.4k | int err; |
19 | | /* |
20 | | * Save old pointer in case we are overriding already set name |
21 | | */ |
22 | 10.4k | char *oldname = bobj.bobj->name; |
23 | | |
24 | 10.4k | va_start(vargs, fmt); |
25 | 10.4k | err = vasprintf(&bobj.bobj->name, fmt, vargs); |
26 | 10.4k | va_end(vargs); |
27 | | |
28 | | /* |
29 | | * Free old pointer, we do this after vasprintf call in case |
30 | | * old device name was in one of vargs |
31 | | */ |
32 | 10.4k | free_const(oldname); |
33 | | |
34 | 10.4k | return WARN_ON(err < 0) ? err : 0; |
35 | 10.4k | } |
36 | | EXPORT_SYMBOL_GPL(bobject_set_name); |
37 | | |
38 | | struct bobject *bobject_alloc(const char *name) |
39 | 0 | { |
40 | 0 | struct bobject *bobj = xzalloc(sizeof(*bobj)); |
41 | |
|
42 | 0 | bobject_init(bobj); |
43 | 0 | bobject_set_name(bobj, "%s", name); |
44 | |
|
45 | 0 | return bobj; |
46 | 0 | } |
47 | | EXPORT_SYMBOL_GPL(bobject_alloc); |
48 | | |
49 | | void bobject_free(struct bobject *bobj) |
50 | 0 | { |
51 | 0 | if (!bobj) |
52 | 0 | return; |
53 | | |
54 | 0 | bobject_del(bobj); |
55 | 0 | free(bobj); |
56 | 0 | } |
57 | | EXPORT_SYMBOL_GPL(bobject_free); |
58 | | |
59 | | /** |
60 | | * bobject_del - remove all parameters from a bobject and free their |
61 | | * memory |
62 | | * @param bobject The barebox object |
63 | | */ |
64 | | void bobject_del(struct bobject *bobj) |
65 | 9.45k | { |
66 | 9.45k | struct param_d *p, *n; |
67 | | |
68 | 9.45k | list_for_each_entry_safe(p, n, &bobj->parameters, list) |
69 | 437 | param_remove(p); |
70 | | |
71 | 9.45k | free_const(bobj->name); |
72 | 9.45k | } |
73 | | EXPORT_SYMBOL(bobject_del); |