From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl0-f68.google.com (mail-pl0-f68.google.com [209.85.160.68]) by dpdk.org (Postfix) with ESMTP id 65BA77CE1 for ; Fri, 8 Jun 2018 11:18:14 +0200 (CEST) Received: by mail-pl0-f68.google.com with SMTP id t12-v6so7939086plo.7 for ; Fri, 08 Jun 2018 02:18:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=N4eh1rQjBatUsQLD0d2RxDTPc9nSzm7RJCoPcdTGRgA=; b=aotxYO5itdkRVOqdL+5WtQRGN8K8Cm4h/iD1sk7szFgqQqNHB7HEGnwveFbnKauM22 J72s8Lay2ZB7u/gtkvJ7/goxzFlySaw7p8uZu4itzWx2jOJxaNzElza0uGALW72uqU4X VVxvfjbM2o+ZD12tLEYOcnNBXSnmvsOaGVllgvK9hrPZ1P2d+zZ+vEZToKzyBndkhCR7 gzb3WsiERo7j4S6RIZVaQR+hrw6dWb/bnDSV9BwvrZLwM+elQGZ8swkWv5tcvQwOvYjI DJ6bE7ErPTAUrurdwcgULvWnsONSNDkpAbpfJjzx054kGAJXGGLNLkO+19lbS8YOVfXY 94Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=N4eh1rQjBatUsQLD0d2RxDTPc9nSzm7RJCoPcdTGRgA=; b=mHruPdI3hqDvHxqPVNtPwtOTVOvxIfgCAGrCGXwF43kQcUQxRWOe+xrn7tSMag5Ci+ ThAOV8G98/yqWP3nVkNdZEq7ognVdGff43K3rYSdta6etptgWjHqwMKgzPPaN8ZO5jyr 4PM5UcYheUzU50vZbVSwgsUSCV9B2JUMsqEXGmgGZ2Yc4gPMi2RPhavyNncwpUrGXt2p 2RiPK/piH7hCyb47w12QJjXqEO8meV1kFU9eb2R/Lf+GGVffQs/wVFalQ8mCYkQFr0Q1 pHrMvH5lMDqjjOJ2SY1bhkTmO1zh6fp7FKAnUilKvK82M+yEaVEm82wJIzDmgCnxIs0y SNsw== X-Gm-Message-State: APt69E2E84TQNTnWxe61KqrVJ7zUXmwTgPz0M2VUKkAZny3h8W2n/Cu4 WMRGHWiXgkNrVF2zkBzzaKwSEceG X-Google-Smtp-Source: ADUXVKKkHJ33QLBt3tlpD3WqgIxxA+xtVG7rN5DPGceExpoz2nRr1qhzDOyX8ScCzBIT46r6gKM+Mw== X-Received: by 2002:a17:902:6b45:: with SMTP id g5-v6mr5827388plt.67.1528449493337; Fri, 08 Jun 2018 02:18:13 -0700 (PDT) Received: from local.opencloud.tech.localdomain ([13.94.31.177]) by smtp.gmail.com with ESMTPSA id q68-v6sm99787890pfb.182.2018.06.08.02.18.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Jun 2018 02:18:12 -0700 (PDT) From: xiangxia.m.yue@gmail.com To: dev@dpdk.org Cc: Tonghao Zhang Date: Fri, 8 Jun 2018 02:18:04 -0700 Message-Id: <1528449484-8664-1-git-send-email-xiangxia.m.yue@gmail.com> X-Mailer: git-send-email 1.8.3.1 Subject: [dpdk-dev] [PATCH] vhost: refine the vhost_new_device X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 Jun 2018 09:18:14 -0000 From: Tonghao Zhang 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 --- lib/librte_vhost/vhost.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c index afded49..ed7b7ac 100644 --- a/lib/librte_vhost/vhost.c +++ b/lib/librte_vhost/vhost.c @@ -268,21 +268,21 @@ 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; } -- 1.8.3.1