]> git.ozlabs.org Git - petitboot/commitdiff
pb-protocol: Don't allocate in deserialise functions
authorJeremy Kerr <jk@ozlabs.org>
Fri, 8 Mar 2013 09:04:21 +0000 (17:04 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Tue, 16 Apr 2013 03:41:43 +0000 (11:41 +0800)
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 <jk@ozlabs.org>
discover/discover-server.c
lib/pb-protocol/pb-protocol.c
lib/pb-protocol/pb-protocol.h
ui/common/discover-client.c

index 6c803728511cb5ea523ee52f33668a7f6982c166..9ec33826254f0515a79fc320aad89ca1def3f297 100644 (file)
@@ -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;
        struct pb_protocol_message *message;
        struct boot_command *boot_command;
        struct client *client = arg;
+       int rc;
 
        message = pb_protocol_read_message(client, client->fd);
 
 
        message = pb_protocol_read_message(client, client->fd);
 
@@ -143,8 +144,10 @@ static int discover_server_process_message(void *arg)
                return 0;
        }
 
                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;
        }
                pb_log("%s: no boot command?", __func__);
                return 0;
        }
index 31637c611c19993a390bd6e1fe86ff917a96ba58..c6d8f635ad6a495d5f9bd15b7c6b94e851d375b1 100644 (file)
@@ -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)
 {
                const struct pb_protocol_message *message)
 {
-       struct device *dev;
        const char *pos;
        int i, n_options;
        unsigned int len;
        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;
 
        len = message->payload_len;
        pos = message->payload;
 
-       dev = talloc(ctx, struct device);
-
        if (read_string(dev, &pos, &len, &dev->id))
                goto out_err;
 
        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);
        }
 
                list_add(&dev->boot_options, &opt->list);
        }
 
-       return dev;
+       return 0;
 
 out_err:
 
 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)
 {
                const struct pb_protocol_message *message)
 {
-       struct boot_command *cmd;
        const char *pos;
        unsigned int len;
 
        len = message->payload_len;
        pos = message->payload;
 
        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;
 
        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;
 
        if (read_string(cmd, &pos, &len, &cmd->boot_args))
                goto out_err;
 
-       return cmd;
+       return 0;
 
 out_err:
 
 out_err:
-       talloc_free(cmd);
-       return NULL;
+       return -1;
 }
 }
index 6068f05ca7b5ae495d0dc1a50d9ef4e9c6f533a9..de2ae7c9e82f607893151e9d234fac36fc8a76af 100644 (file)
@@ -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 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);
 
                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 */
                const struct pb_protocol_message *message);
 
 #endif /* _PB_PROTOCOL_H */
index f75cfb71822949d9f5533a2bd303519592a8b301..c0cfea0fe19a8ddae06d5b0546a2f6f504d6c369 100644 (file)
@@ -83,6 +83,7 @@ static int discover_client_process(void *arg)
        struct pb_protocol_message *message;
        struct device *dev;
        char *dev_id;
        struct pb_protocol_message *message;
        struct device *dev;
        char *dev_id;
+       int rc;
 
        message = pb_protocol_read_message(client, client->fd);
 
 
        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:
 
        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;
                }
                        pb_log("%s: no device?\n", __func__);
                        return 0;
                }