| 1 | #include <mem/alloc.h> |
| 2 | #include <mem/alloc_or_die.h> |
| 3 | #include <stdarg.h> |
| 4 | #include <string.h> |
| 5 | #include <time/clock_evdev.h> |
| 6 | |
| 7 | #include "internal.h" |
| 8 | |
| 9 | struct clock_evdev_group *clock_evdev_group_create(const char *name, ...) { |
| 10 | struct clock_evdev_group *cedg = |
| 11 | kmalloc(sizeof(struct clock_evdev_group), ALLOC_FLAGS_ZERO); |
| 12 | |
| 13 | if (!cedg) |
| 14 | return NULL; |
| 15 | |
| 16 | INIT_LIST_HEAD(list: &cedg->clock_evdevs); |
| 17 | va_list args; |
| 18 | va_start(args, name); |
| 19 | int ret = ERR_GUARD(vasprintf(&cedg->name, name, args), ERR_NO_MEM); |
| 20 | va_end(args); |
| 21 | |
| 22 | if (ret == ERR_NO_MEM) { |
| 23 | kfree(cedg); |
| 24 | return NULL; |
| 25 | } |
| 26 | |
| 27 | return cedg; |
| 28 | } |
| 29 | |
| 30 | struct clock_evdev *clock_evdev_create(const char *name, ...) { |
| 31 | struct clock_evdev *ced = |
| 32 | kmalloc(sizeof(struct clock_evdev), ALLOC_FLAGS_ZERO); |
| 33 | if (!ced) |
| 34 | return NULL; |
| 35 | |
| 36 | if (!cpu_mask_init(m: &ced->cpu_mask, nbits: global.core_count)) { |
| 37 | kfree(ced); |
| 38 | return NULL; |
| 39 | } |
| 40 | |
| 41 | va_list args; |
| 42 | va_start(args, name); |
| 43 | int ret = ERR_GUARD(vasprintf(&ced->name, name, args), ERR_NO_MEM); |
| 44 | va_end(args); |
| 45 | |
| 46 | if (ret == ERR_NO_MEM) { |
| 47 | cpu_mask_deinit(m: &ced->cpu_mask); |
| 48 | kfree(ced); |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | return ced; |
| 53 | } |
| 54 | |
| 55 | void clock_evdev_register(struct clock_evdev *ced) { |
| 56 | kassert(!ced->in_global_list); |
| 57 | ced->in_global_list = true; |
| 58 | locked_list_add(ll: &clock_global.clock_evdevs, lh: &ced->list_internal); |
| 59 | } |
| 60 | |
| 61 | void clock_evdev_group_register(struct clock_evdev_group *cedg) { |
| 62 | locked_list_add(ll: &clock_global.clock_evdev_groups, lh: &cedg->list_internal); |
| 63 | } |
| 64 | |
| 65 | void clock_evdev_group_unregister(struct clock_evdev_group *cedg) { |
| 66 | locked_list_del(ll: &clock_global.clock_evdev_groups, lh: &cedg->list_internal); |
| 67 | } |
| 68 | |
| 69 | struct clock_evdev_group *clock_evdev_group_search_for(const char *name) { |
| 70 | enum irql irql = locked_list_lock(ll: &clock_global.clock_evdev_groups); |
| 71 | |
| 72 | struct clock_evdev_group *group = NULL; |
| 73 | list_for_each_entry(group, &clock_global.clock_evdev_groups.list, |
| 74 | list_internal) { |
| 75 | if (strcmp(str1: group->name, str2: name) == 0) |
| 76 | goto out; |
| 77 | } |
| 78 | |
| 79 | out: |
| 80 | locked_list_unlock(ll: &clock_global.clock_evdev_groups, irql); |
| 81 | return group; |
| 82 | } |
| 83 | |