All accesses to the h->state field are redirected through these macros
(except when we create the handle).
This is simple refactoring.
---
generator/generator | 18 +++++++++---------
lib/connect.c | 10 +++++-----
lib/disconnect.c | 8 ++++----
lib/internal.h | 3 +++
lib/is-state.c | 12 ++++++------
lib/rw.c | 4 ++--
6 files changed, 29 insertions(+), 26 deletions(-)
diff --git a/generator/generator b/generator/generator
index 8ed0a06..a3fbea9 100755
--- a/generator/generator
+++ b/generator/generator
@@ -2415,11 +2415,11 @@ let generate_lib_states_c () =
pr " enum state next_state = %s;\n" state_enum;
pr "\n";
pr " r = _enter_%s (h, &next_state, blocked);\n" state_enum;
- pr " if (h->state != next_state) {\n";
+ pr " if (get_state (h) != next_state) {\n";
pr " debug (h, \"transition: %%s -> %%s\",\n";
pr " \"%s\",\n" display_name;
pr " nbd_internal_state_short_string (next_state));\n";
- pr " h->state = next_state;\n";
+ pr " set_state (h, next_state);\n";
pr " }\n";
pr " return r;\n";
pr "}\n";
@@ -2434,7 +2434,7 @@ let generate_lib_states_c () =
pr " bool blocked;\n";
pr "\n";
pr " /* Validate and handle the external event. */\n";
- pr " switch (h->state)\n";
+ pr " switch (get_state (h))\n";
pr " {\n";
List.iter (
fun ({ parsed = { display_name; state_enum; events } } as state) ->
@@ -2446,7 +2446,7 @@ let generate_lib_states_c () =
fun (e, next_state) ->
pr " case %s:\n" (c_string_of_external_event e);
if state != next_state then (
- pr " h->state = %s;\n" next_state.parsed.state_enum;
+ pr " set_state (h, %s);\n" next_state.parsed.state_enum;
pr " debug (h, \"event %%s: %%s -> %%s\",\n";
pr " \"%s\", \"%s\",
\"%s\");\n"
(string_of_external_event e)
@@ -2462,7 +2462,7 @@ let generate_lib_states_c () =
pr " }\n";
pr "\n";
pr " set_error (EINVAL, \"external event %%d is invalid in state
%%s\",\n";
- pr " ev, nbd_internal_state_short_string (h->state));\n";
+ pr " ev, nbd_internal_state_short_string (get_state (h)));\n";
pr " return -1;\n";
pr "\n";
pr " ok:\n";
@@ -2470,7 +2470,7 @@ let generate_lib_states_c () =
pr " blocked = true;\n";
pr "\n";
pr " /* Run a single step. */\n";
- pr " switch (h->state)\n";
+ pr " switch (get_state (h))\n";
pr " {\n";
List.iter (
fun { parsed = { state_enum } } ->
@@ -2496,7 +2496,7 @@ let generate_lib_states_c () =
pr "{\n";
pr " int r = 0;\n";
pr "\n";
- pr " switch (h->state)\n";
+ pr " switch (get_state (h))\n";
pr " {\n";
List.iter (
fun ({ parsed = { state_enum; events } }) ->
@@ -2542,7 +2542,7 @@ let generate_lib_states_c () =
pr "const char *\n";
pr "nbd_unlocked_connection_state (struct nbd_handle *h)\n";
pr "{\n";
- pr " switch (h->state)\n";
+ pr " switch (get_state (h))\n";
pr " {\n";
List.iter (
fun ({ comment; parsed = { display_name; state_enum } }) ->
@@ -2841,7 +2841,7 @@ let generate_lib_api_c () =
pr " /* We can check the state outside the handle lock because the\n";
pr " * the state is atomic.\n";
pr " */\n";
- pr " enum state state = h->state;\n";
+ pr " enum state state = get_state (h);\n";
let tests =
List.map (
function
diff --git a/lib/connect.c b/lib/connect.c
index 63d2234..b889f80 100644
--- a/lib/connect.c
+++ b/lib/connect.c
@@ -38,16 +38,16 @@
static int
error_unless_ready (struct nbd_handle *h)
{
- if (nbd_internal_is_state_ready (h->state))
+ if (nbd_internal_is_state_ready (get_state (h)))
return 0;
/* Why did it fail? */
- if (nbd_internal_is_state_closed (h->state)) {
+ if (nbd_internal_is_state_closed (get_state (h))) {
set_error (0, "connection is closed");
return -1;
}
- if (nbd_internal_is_state_dead (h->state))
+ if (nbd_internal_is_state_dead (get_state (h)))
/* Don't set the error here, keep the error set when
* the connection died.
*/
@@ -55,14 +55,14 @@ error_unless_ready (struct nbd_handle *h)
/* Should probably never happen. */
set_error (0, "connection in an unexpected state (%s)",
- nbd_internal_state_short_string (h->state));
+ nbd_internal_state_short_string (get_state (h)));
return -1;
}
static int
wait_until_connected (struct nbd_handle *h)
{
- while (nbd_internal_is_state_connecting (h->state)) {
+ while (nbd_internal_is_state_connecting (get_state (h))) {
if (nbd_unlocked_poll (h, -1) == -1)
return -1;
}
diff --git a/lib/disconnect.c b/lib/disconnect.c
index 355a1c9..423edaf 100644
--- a/lib/disconnect.c
+++ b/lib/disconnect.c
@@ -29,14 +29,14 @@
int
nbd_unlocked_shutdown (struct nbd_handle *h)
{
- if (nbd_internal_is_state_ready (h->state) ||
- nbd_internal_is_state_processing (h->state)) {
+ if (nbd_internal_is_state_ready (get_state (h)) ||
+ nbd_internal_is_state_processing (get_state (h))) {
if (nbd_unlocked_aio_disconnect (h, 0) == -1)
return -1;
}
- while (!nbd_internal_is_state_closed (h->state) &&
- !nbd_internal_is_state_dead (h->state)) {
+ while (!nbd_internal_is_state_closed (get_state (h)) &&
+ !nbd_internal_is_state_dead (get_state (h))) {
if (nbd_unlocked_poll (h, -1) == -1)
return -1;
}
diff --git a/lib/internal.h b/lib/internal.h
index 691a1eb..7290247 100644
--- a/lib/internal.h
+++ b/lib/internal.h
@@ -291,6 +291,9 @@ extern const char *nbd_internal_state_short_string (enum state
state);
extern enum state_group nbd_internal_state_group (enum state state);
extern enum state_group nbd_internal_state_group_parent (enum state_group group);
+#define set_state(h,next_state) ((h)->state) = (next_state)
+#define get_state(h) ((h)->state)
+
/* utils.c */
extern void nbd_internal_hexdump (const void *data, size_t len, FILE *fp);
extern size_t nbd_internal_string_list_length (char **argv);
diff --git a/lib/is-state.c b/lib/is-state.c
index 55d103b..51a2d47 100644
--- a/lib/is-state.c
+++ b/lib/is-state.c
@@ -104,40 +104,40 @@ nbd_internal_is_state_closed (enum state state)
int
nbd_unlocked_aio_is_created (struct nbd_handle *h)
{
- return nbd_internal_is_state_created (h->state);
+ return nbd_internal_is_state_created (get_state (h));
}
/* NB: is_locked = false, may_set_error = false. */
int
nbd_unlocked_aio_is_connecting (struct nbd_handle *h)
{
- return nbd_internal_is_state_connecting (h->state);
+ return nbd_internal_is_state_connecting (get_state (h));
}
/* NB: is_locked = false, may_set_error = false. */
int
nbd_unlocked_aio_is_ready (struct nbd_handle *h)
{
- return nbd_internal_is_state_ready (h->state);
+ return nbd_internal_is_state_ready (get_state (h));
}
/* NB: is_locked = false, may_set_error = false. */
int
nbd_unlocked_aio_is_processing (struct nbd_handle *h)
{
- return nbd_internal_is_state_processing (h->state);
+ return nbd_internal_is_state_processing (get_state (h));
}
/* NB: is_locked = false, may_set_error = false. */
int
nbd_unlocked_aio_is_dead (struct nbd_handle *h)
{
- return nbd_internal_is_state_dead (h->state);
+ return nbd_internal_is_state_dead (get_state (h));
}
/* NB: is_locked = false, may_set_error = false. */
int
nbd_unlocked_aio_is_closed (struct nbd_handle *h)
{
- return nbd_internal_is_state_closed (h->state);
+ return nbd_internal_is_state_closed (get_state (h));
}
diff --git a/lib/rw.c b/lib/rw.c
index 5fe3c64..b38d95b 100644
--- a/lib/rw.c
+++ b/lib/rw.c
@@ -201,7 +201,7 @@ nbd_internal_command_common (struct nbd_handle *h,
* be handled automatically on a future cycle around to READY.
*/
if (h->cmds_to_issue != NULL) {
- assert (nbd_internal_is_state_processing (h->state));
+ assert (nbd_internal_is_state_processing (get_state (h)));
prev_cmd = h->cmds_to_issue;
while (prev_cmd->next)
prev_cmd = prev_cmd->next;
@@ -209,7 +209,7 @@ nbd_internal_command_common (struct nbd_handle *h,
}
else {
h->cmds_to_issue = cmd;
- if (nbd_internal_is_state_ready (h->state) &&
+ if (nbd_internal_is_state_ready (get_state (h)) &&
nbd_internal_run (h, cmd_issue) == -1)
return -1;
}
--
2.21.0