]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sleep-config: several cleanups
authorMike Yuan <me@yhndnzj.com>
Thu, 21 Sep 2023 06:59:26 +0000 (14:59 +0800)
committerMike Yuan <me@yhndnzj.com>
Wed, 27 Sep 2023 13:48:39 +0000 (21:48 +0800)
* Rename free_sleep_config to sleep_config_free
* Rearrange functions
* Make SleepConfig.modes and .states only contain
  operations that needs configuration
* Add missing assert

src/shared/sleep-config.c
src/shared/sleep-config.h
src/sleep/sleep.c
src/test/test-sleep-config.c

index 6badee88dd7559f55b06c2419c5a8f4b55332e0a..764c29d2a94ec7e9b17175c0dc7419035456b3a4 100644 (file)
 #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);
index 67a6826687bc65df10729b6e1be59253d25024aa..45917d8b4f0c4162830ea87cd75b8c0835c965d6 100644 (file)
@@ -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_;
index 3527d3632f7166c6ef9a100a7d1d9785a961be1f..a299568ccf77606488021e6ec6f20536584c15dd 100644 (file)
@@ -612,7 +612,7 @@ static int parse_argv(int argc, char *argv[]) {
 }
 
 static int run(int argc, char *argv[]) {
-        _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
+        _cleanup_(sleep_config_freep) SleepConfig *sleep_config = NULL;
         int r;
 
         log_setup();
index c96b7bb57697b9adff619f37c16f9c0e6b9bf490..58d910d1d9d45da9cf19b0bb4b623b0b6df82d2c 100644 (file)
@@ -10,7 +10,7 @@
 #include "tests.h"
 
 TEST(parse_sleep_config) {
-        _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
+        _cleanup_(sleep_config_freep) SleepConfig *sleep_config = NULL;
 
         assert_se(parse_sleep_config(&sleep_config) == 0);