From b838cf777ed3d21b166f8daddd4b11fc75e07307 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Fri, 8 Mar 2013 17:04:21 +0800 Subject: [PATCH] pb-protocol: Don't allocate in deserialise functions Curently, the protocol deserialise functions are allocating device and boot_command structures. This (implicitly) makes them responsible for initialisation of these structures too. Rather that making the protocol responsible for initialising the devices and boot commands, this change gives the deserialise functions an argument to an already-instanciated structure. This means that the creation is no longer implied by the deserialise. Signed-off-by: Jeremy Kerr --- discover/discover-server.c | 7 +++++-- lib/pb-protocol/pb-protocol.c | 20 ++++++-------------- lib/pb-protocol/pb-protocol.h | 4 ++-- ui/common/discover-client.c | 7 +++++-- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/discover/discover-server.c b/discover/discover-server.c index 6c80372..9ec3382 100644 --- a/discover/discover-server.c +++ b/discover/discover-server.c @@ -132,6 +132,7 @@ static int discover_server_process_message(void *arg) struct pb_protocol_message *message; struct boot_command *boot_command; struct client *client = arg; + int rc; message = pb_protocol_read_message(client, client->fd); @@ -143,8 +144,10 @@ static int discover_server_process_message(void *arg) return 0; } - boot_command = pb_protocol_deserialise_boot_command(client, message); - if (!boot_command) { + boot_command = talloc(client, struct boot_command); + + rc = pb_protocol_deserialise_boot_command(boot_command, message); + if (rc) { pb_log("%s: no boot command?", __func__); return 0; } diff --git a/lib/pb-protocol/pb-protocol.c b/lib/pb-protocol/pb-protocol.c index 31637c6..c6d8f63 100644 --- a/lib/pb-protocol/pb-protocol.c +++ b/lib/pb-protocol/pb-protocol.c @@ -338,10 +338,9 @@ struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd) } -struct device *pb_protocol_deserialise_device(void *ctx, +int pb_protocol_deserialise_device(struct device *dev, const struct pb_protocol_message *message) { - struct device *dev; const char *pos; int i, n_options; unsigned int len; @@ -349,8 +348,6 @@ struct device *pb_protocol_deserialise_device(void *ctx, len = message->payload_len; pos = message->payload; - dev = talloc(ctx, struct device); - if (read_string(dev, &pos, &len, &dev->id)) goto out_err; @@ -398,25 +395,21 @@ struct device *pb_protocol_deserialise_device(void *ctx, list_add(&dev->boot_options, &opt->list); } - return dev; + return 0; out_err: - talloc_free(dev); - return NULL; + return -1; } -struct boot_command *pb_protocol_deserialise_boot_command(void *ctx, +int pb_protocol_deserialise_boot_command(struct boot_command *cmd, const struct pb_protocol_message *message) { - struct boot_command *cmd; const char *pos; unsigned int len; len = message->payload_len; pos = message->payload; - cmd = talloc(ctx, struct boot_command); - if (read_string(cmd, &pos, &len, &cmd->option_id)) goto out_err; @@ -429,9 +422,8 @@ struct boot_command *pb_protocol_deserialise_boot_command(void *ctx, if (read_string(cmd, &pos, &len, &cmd->boot_args)) goto out_err; - return cmd; + return 0; out_err: - talloc_free(cmd); - return NULL; + return -1; } diff --git a/lib/pb-protocol/pb-protocol.h b/lib/pb-protocol/pb-protocol.h index 6068f05..de2ae7c 100644 --- a/lib/pb-protocol/pb-protocol.h +++ b/lib/pb-protocol/pb-protocol.h @@ -47,10 +47,10 @@ struct pb_protocol_message *pb_protocol_create_message(void *ctx, struct pb_protocol_message *pb_protocol_read_message(void *ctx, int fd); -struct device *pb_protocol_deserialise_device(void *ctx, +int pb_protocol_deserialise_device(struct device *dev, const struct pb_protocol_message *message); -struct boot_command *pb_protocol_deserialise_boot_command(void *ctx, +int pb_protocol_deserialise_boot_command(struct boot_command *cmd, const struct pb_protocol_message *message); #endif /* _PB_PROTOCOL_H */ diff --git a/ui/common/discover-client.c b/ui/common/discover-client.c index f75cfb7..c0cfea0 100644 --- a/ui/common/discover-client.c +++ b/ui/common/discover-client.c @@ -83,6 +83,7 @@ static int discover_client_process(void *arg) struct pb_protocol_message *message; struct device *dev; char *dev_id; + int rc; message = pb_protocol_read_message(client, client->fd); @@ -91,8 +92,10 @@ static int discover_client_process(void *arg) switch (message->action) { case PB_PROTOCOL_ACTION_ADD: - dev = pb_protocol_deserialise_device(client, message); - if (!dev) { + dev = talloc(client, struct device); + + rc = pb_protocol_deserialise_device(dev, message); + if (rc) { pb_log("%s: no device?\n", __func__); return 0; } -- 2.39.2