]> git.openfabrics.org - ~shefty/libibverbs.git/commitdiff
Fix some issues in the examples
authorDotan Barak <dotanb@dev.mellanox.co.il>
Wed, 10 Oct 2007 09:26:18 +0000 (11:26 +0200)
committerRoland Dreier <rolandd@cisco.com>
Mon, 18 Feb 2008 19:52:34 +0000 (11:52 -0800)
Fix the following issues reported by valgrind in the examples:
 * memory leaks
 * uninitialized members of attribute structures

Signed-off-by: Dotan Barak <dotanb@dev.mellanox.co.il>
Signed-off-by: Roland Dreier <rolandd@cisco.com>
examples/device_list.c
examples/devinfo.c
examples/rc_pingpong.c
examples/srq_pingpong.c
examples/uc_pingpong.c
examples/ud_pingpong.c

index b53d4b1c79699f9d0887acce4f8d082c26c2654a..3ce8cbdb58ec14489d814d381a590fc1a584f0eb 100644 (file)
@@ -45,8 +45,9 @@
 int main(int argc, char *argv[])
 {
        struct ibv_device **dev_list;
+       int num_devices, i;
 
-       dev_list = ibv_get_device_list(NULL);
+       dev_list = ibv_get_device_list(&num_devices);
        if (!dev_list) {
                fprintf(stderr, "No IB devices found\n");
                return 1;
@@ -55,12 +56,13 @@ int main(int argc, char *argv[])
        printf("    %-16s\t   node GUID\n", "device");
        printf("    %-16s\t----------------\n", "------");
 
-       while (*dev_list) {
+       for (i = 0; i < num_devices; ++i) {
                printf("    %-16s\t%016llx\n",
-                      ibv_get_device_name(*dev_list),
-                      (unsigned long long) ntohll(ibv_get_device_guid(*dev_list)));
-               ++dev_list;
+                      ibv_get_device_name(dev_list[i]),
+                      (unsigned long long) ntohll(ibv_get_device_guid(dev_list[i])));
        }
 
+       ibv_free_device_list(dev_list);
+
        return 0;
 }
index d054999d750f38cfd7cc7115059416012f02a50d..4e4316a416c2339c12f572ba5ec3bb92f5424960 100644 (file)
@@ -323,7 +323,7 @@ int main(int argc, char *argv[])
 {
        char *ib_devname = NULL;
        int ret = 0;
-       struct ibv_device **dev_list;
+       struct ibv_device **dev_list, **orig_dev_list;
        int num_of_hcas;
        int ib_port = 0;
 
@@ -360,7 +360,7 @@ int main(int argc, char *argv[])
                        break;
 
                case 'l':
-                       dev_list = ibv_get_device_list(&num_of_hcas);
+                       dev_list = orig_dev_list = ibv_get_device_list(&num_of_hcas);
                        if (!dev_list) {
                                fprintf(stderr, "Failed to get IB devices list");
                                return -1;
@@ -375,6 +375,9 @@ int main(int argc, char *argv[])
                        }
 
                        printf("\n");
+
+                       ibv_free_device_list(orig_dev_list);
+
                        return 0;
 
                default:
@@ -383,7 +386,7 @@ int main(int argc, char *argv[])
                }
        }
 
-       dev_list = ibv_get_device_list(NULL);
+       dev_list = orig_dev_list = ibv_get_device_list(NULL);
        if (!dev_list) {
                fprintf(stderr, "Failed to get IB device list\n");
                return -1;
@@ -417,5 +420,7 @@ int main(int argc, char *argv[])
        if (ib_devname)
                free(ib_devname);
 
+       ibv_free_device_list(orig_dev_list);
+
        return ret;
 }
index 258eb8f7cac804815b738fc942aff660a280b692..81fd4a6a6e6cd3d99f30a71a4e93dfd4586ccaef 100644 (file)
@@ -146,6 +146,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
 
        if (n < 0) {
                fprintf(stderr, "%s for %s:%d\n", gai_strerror(n), servername, port);
+               free(service);
                return NULL;
        }
 
@@ -160,6 +161,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
        }
 
        freeaddrinfo(res);
+       free(service);
 
        if (sockfd < 0) {
                fprintf(stderr, "Couldn't connect to %s:%d\n", servername, port);
@@ -214,6 +216,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
 
        if (n < 0) {
                fprintf(stderr, "%s for port %d\n", gai_strerror(n), port);
+               free(service);
                return NULL;
        }
 
@@ -232,6 +235,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
        }
 
        freeaddrinfo(res);
+       free(service);
 
        if (sockfd < 0) {
                fprintf(stderr, "Couldn't listen to port %d\n", port);
@@ -358,12 +362,12 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size,
        }
 
        {
-               struct ibv_qp_attr attr;
-
-               attr.qp_state        = IBV_QPS_INIT;
-               attr.pkey_index      = 0;
-               attr.port_num        = port;
-               attr.qp_access_flags = 0;
+               struct ibv_qp_attr attr = {
+                       .qp_state        = IBV_QPS_INIT,
+                       .pkey_index      = 0,
+                       .port_num        = port,
+                       .qp_access_flags = 0
+               };
 
                if (ibv_modify_qp(ctx->qp, &attr,
                                  IBV_QP_STATE              |
index 490ad0a702d409375622a36c6bd5b7662d8d7745..91fd566da654fb317df372bf51b63972a487f03a 100644 (file)
@@ -157,6 +157,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
 
        if (n < 0) {
                fprintf(stderr, "%s for %s:%d\n", gai_strerror(n), servername, port);
+               free(service);
                return NULL;
        }
 
@@ -171,6 +172,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
        }
 
        freeaddrinfo(res);
+       free(service);
 
        if (sockfd < 0) {
                fprintf(stderr, "Couldn't connect to %s:%d\n", servername, port);
@@ -238,6 +240,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
 
        if (n < 0) {
                fprintf(stderr, "%s for port %d\n", gai_strerror(n), port);
+               free(service);
                return NULL;
        }
 
@@ -256,6 +259,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
        }
 
        freeaddrinfo(res);
+       free(service);
 
        if (sockfd < 0) {
                fprintf(stderr, "Couldn't listen to port %d\n", port);
@@ -408,12 +412,12 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size,
        }
 
        for (i = 0; i < num_qp; ++i) {
-               struct ibv_qp_attr attr;
-
-               attr.qp_state        = IBV_QPS_INIT;
-               attr.pkey_index      = 0;
-               attr.port_num        = port;
-               attr.qp_access_flags = 0;
+               struct ibv_qp_attr attr = {
+                       .qp_state        = IBV_QPS_INIT,
+                       .pkey_index      = 0,
+                       .port_num        = port,
+                       .qp_access_flags = 0
+               };
 
                if (ibv_modify_qp(ctx->qp[i], &attr,
                                  IBV_QP_STATE              |
index b6051c8d8faef0ab9993b94ae82f23130188df9c..32652f51e669aea3c852da80657158e92d5f0f61 100644 (file)
@@ -134,6 +134,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
 
        if (n < 0) {
                fprintf(stderr, "%s for %s:%d\n", gai_strerror(n), servername, port);
+               free(service);
                return NULL;
        }
 
@@ -148,6 +149,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
        }
 
        freeaddrinfo(res);
+       free(service);
 
        if (sockfd < 0) {
                fprintf(stderr, "Couldn't connect to %s:%d\n", servername, port);
@@ -202,6 +204,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
 
        if (n < 0) {
                fprintf(stderr, "%s for port %d\n", gai_strerror(n), port);
+               free(service);
                return NULL;
        }
 
@@ -220,6 +223,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
        }
 
        freeaddrinfo(res);
+       free(service);
 
        if (sockfd < 0) {
                fprintf(stderr, "Couldn't listen to port %d\n", port);
@@ -346,12 +350,12 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size,
        }
 
        {
-               struct ibv_qp_attr attr;
-
-               attr.qp_state        = IBV_QPS_INIT;
-               attr.pkey_index      = 0;
-               attr.port_num        = port;
-               attr.qp_access_flags = 0;
+               struct ibv_qp_attr attr = {
+                       .qp_state        = IBV_QPS_INIT,
+                       .pkey_index      = 0,
+                       .port_num        = port,
+                       .qp_access_flags = 0
+               };
 
                if (ibv_modify_qp(ctx->qp, &attr,
                                  IBV_QP_STATE              |
index c631e250ea74dccc6a000381b8416b35ec3dc058..baf69b788e3167a229c6f9d6514f8b819f992283 100644 (file)
@@ -79,7 +79,6 @@ struct pingpong_dest {
 static int pp_connect_ctx(struct pingpong_context *ctx, int port, int my_psn,
                          struct pingpong_dest *dest)
 {
-       struct ibv_qp_attr attr;
        struct ibv_ah_attr ah_attr = {
                .is_global     = 0,
                .dlid          = dest->lid,
@@ -87,8 +86,9 @@ static int pp_connect_ctx(struct pingpong_context *ctx, int port, int my_psn,
                .src_path_bits = 0,
                .port_num      = port
        };
-
-       attr.qp_state           = IBV_QPS_RTR;
+       struct ibv_qp_attr attr = {
+               .qp_state               = IBV_QPS_RTR
+       };
 
        if (ibv_modify_qp(ctx->qp, &attr, IBV_QP_STATE)) {
                fprintf(stderr, "Failed to modify QP to RTR\n");
@@ -135,6 +135,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
 
        if (n < 0) {
                fprintf(stderr, "%s for %s:%d\n", gai_strerror(n), servername, port);
+               free(service);
                return NULL;
        }
 
@@ -149,6 +150,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
        }
 
        freeaddrinfo(res);
+       free(service);
 
        if (sockfd < 0) {
                fprintf(stderr, "Couldn't connect to %s:%d\n", servername, port);
@@ -203,6 +205,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
 
        if (n < 0) {
                fprintf(stderr, "%s for port %d\n", gai_strerror(n), port);
+               free(service);
                return NULL;
        }
 
@@ -221,6 +224,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
        }
 
        freeaddrinfo(res);
+       free(service);
 
        if (sockfd < 0) {
                fprintf(stderr, "Couldn't listen to port %d\n", port);
@@ -347,12 +351,12 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size,
        }
 
        {
-               struct ibv_qp_attr attr;
-
-               attr.qp_state        = IBV_QPS_INIT;
-               attr.pkey_index      = 0;
-               attr.port_num        = port;
-               attr.qkey            = 0x11111111;
+               struct ibv_qp_attr attr = {
+                       .qp_state        = IBV_QPS_INIT,
+                       .pkey_index      = 0,
+                       .port_num        = port,
+                       .qkey            = 0x11111111
+               };
 
                if (ibv_modify_qp(ctx->qp, &attr,
                                  IBV_QP_STATE              |