diff options
author | Mike Yuan <me@yhndnzj.com> | 2023-09-21 08:59:26 +0200 |
---|---|---|
committer | Mike Yuan <me@yhndnzj.com> | 2023-09-27 15:48:39 +0200 |
commit | 087a25d2ca432caea2736296c9b71ee45cd7a67f (patch) | |
tree | 6d700dd8b1344815c25780415c1c8a02f900f76e /src/shared | |
parent | sleep/battery-capacity: rearrange functions (diff) | |
download | systemd-087a25d2ca432caea2736296c9b71ee45cd7a67f.tar.xz systemd-087a25d2ca432caea2736296c9b71ee45cd7a67f.zip |
sleep-config: several cleanups
* Rename free_sleep_config to sleep_config_free
* Rearrange functions
* Make SleepConfig.modes and .states only contain
operations that needs configuration
* Add missing assert
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/sleep-config.c | 55 | ||||
-rw-r--r-- | src/shared/sleep-config.h | 20 |
2 files changed, 41 insertions, 34 deletions
diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c index 6badee88dd..764c29d2a9 100644 --- a/src/shared/sleep-config.c +++ b/src/shared/sleep-config.c @@ -22,10 +22,32 @@ #include "strv.h" #include "time-util.h" -int parse_sleep_config(SleepConfig **ret_sleep_config) { - _cleanup_(free_sleep_configp) SleepConfig *sc = NULL; - int allow_suspend = -1, allow_hibernate = -1, - allow_s2h = -1, allow_hybrid_sleep = -1; +static const char* const sleep_operation_table[_SLEEP_OPERATION_MAX] = { + [SLEEP_SUSPEND] = "suspend", + [SLEEP_HIBERNATE] = "hibernate", + [SLEEP_HYBRID_SLEEP] = "hybrid-sleep", + [SLEEP_SUSPEND_THEN_HIBERNATE] = "suspend-then-hibernate", +}; + +DEFINE_STRING_TABLE_LOOKUP(sleep_operation, SleepOperation); + +SleepConfig* sleep_config_free(SleepConfig *sc) { + if (!sc) + return NULL; + + for (SleepOperation i = 0; i < _SLEEP_OPERATION_CONFIG_MAX; i++) { + strv_free(sc->modes[i]); + strv_free(sc->states[i]); + } + + return mfree(sc); +} + +int parse_sleep_config(SleepConfig **ret) { + _cleanup_(sleep_config_freep) SleepConfig *sc = NULL; + int allow_suspend = -1, allow_hibernate = -1, allow_s2h = -1, allow_hybrid_sleep = -1; + + assert(ret); sc = new(SleepConfig, 1); if (!sc) @@ -83,7 +105,7 @@ int parse_sleep_config(SleepConfig **ret_sleep_config) { || !sc->states[SLEEP_HIBERNATE] || !sc->modes[SLEEP_HYBRID_SLEEP] || !sc->states[SLEEP_HYBRID_SLEEP]) return log_oom(); - *ret_sleep_config = TAKE_PTR(sc); + *ret = TAKE_PTR(sc); return 0; } @@ -227,7 +249,7 @@ static int can_sleep_internal( } int can_sleep(SleepOperation operation) { - _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL; + _cleanup_(sleep_config_freep) SleepConfig *sleep_config = NULL; int r; r = parse_sleep_config(&sleep_config); @@ -236,24 +258,3 @@ int can_sleep(SleepOperation operation) { return can_sleep_internal(sleep_config, operation, true); } - -SleepConfig* free_sleep_config(SleepConfig *sc) { - if (!sc) - return NULL; - - for (SleepOperation i = 0; i < _SLEEP_OPERATION_MAX; i++) { - strv_free(sc->modes[i]); - strv_free(sc->states[i]); - } - - return mfree(sc); -} - -static const char* const sleep_operation_table[_SLEEP_OPERATION_MAX] = { - [SLEEP_SUSPEND] = "suspend", - [SLEEP_HIBERNATE] = "hibernate", - [SLEEP_HYBRID_SLEEP] = "hybrid-sleep", - [SLEEP_SUSPEND_THEN_HIBERNATE] = "suspend-then-hibernate", -}; - -DEFINE_STRING_TABLE_LOOKUP(sleep_operation, SleepOperation); diff --git a/src/shared/sleep-config.h b/src/shared/sleep-config.h index 67a6826687..45917d8b4f 100644 --- a/src/shared/sleep-config.h +++ b/src/shared/sleep-config.h @@ -9,27 +9,33 @@ typedef enum SleepOperation { SLEEP_SUSPEND, SLEEP_HIBERNATE, SLEEP_HYBRID_SLEEP, + _SLEEP_OPERATION_CONFIG_MAX, + /* The operations above require configuration for mode and state. The ones below are "combined" + * operations that use config from those individual operations. */ + SLEEP_SUSPEND_THEN_HIBERNATE, + _SLEEP_OPERATION_MAX, _SLEEP_OPERATION_INVALID = -EINVAL, } SleepOperation; +const char* sleep_operation_to_string(SleepOperation s) _const_; +SleepOperation sleep_operation_from_string(const char *s) _pure_; + typedef struct SleepConfig { bool allow[_SLEEP_OPERATION_MAX]; - char **modes[_SLEEP_OPERATION_MAX]; - char **states[_SLEEP_OPERATION_MAX]; + char **modes[_SLEEP_OPERATION_CONFIG_MAX]; + char **states[_SLEEP_OPERATION_CONFIG_MAX]; + usec_t hibernate_delay_usec; usec_t suspend_estimation_usec; } SleepConfig; -SleepConfig* free_sleep_config(SleepConfig *sc); -DEFINE_TRIVIAL_CLEANUP_FUNC(SleepConfig*, free_sleep_config); +SleepConfig* sleep_config_free(SleepConfig *sc); +DEFINE_TRIVIAL_CLEANUP_FUNC(SleepConfig*, sleep_config_free); int parse_sleep_config(SleepConfig **sleep_config); int can_sleep(SleepOperation operation); int can_sleep_disk(char **types); int can_sleep_state(char **types); - -const char* sleep_operation_to_string(SleepOperation s) _const_; -SleepOperation sleep_operation_from_string(const char *s) _pure_; |