From: Tonghao Zhang Date: Fri, 8 Jun 2018 09:18:04 +0000 (-0700) Subject: vhost: refine new device function X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=76b1e4cec7dc15b733966f9f494d7d49e4f016f2;p=dpdk.git vhost: refine new device function Make sure find avalid device id before allocating virtio_net, if not, return directly. It may avoid allocating and freeing virtio_net when there is not valid device id. Signed-off-by: Tonghao Zhang Reviewed-by: Maxime Coquelin --- diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c index afded4952f..ed7b7ac7b7 100644 --- a/lib/librte_vhost/vhost.c +++ b/lib/librte_vhost/vhost.c @@ -268,21 +268,21 @@ vhost_new_device(void) struct virtio_net *dev; int i; - dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0); - if (dev == NULL) { - RTE_LOG(ERR, VHOST_CONFIG, - "Failed to allocate memory for new dev.\n"); - return -1; - } - for (i = 0; i < MAX_VHOST_DEVICE; i++) { if (vhost_devices[i] == NULL) break; } + if (i == MAX_VHOST_DEVICE) { RTE_LOG(ERR, VHOST_CONFIG, "Failed to find a free slot for new device.\n"); - rte_free(dev); + return -1; + } + + dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0); + if (dev == NULL) { + RTE_LOG(ERR, VHOST_CONFIG, + "Failed to allocate memory for new dev.\n"); return -1; }