From patchwork Mon Jun 25 17:36:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 10487087 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6243C6038C for ; Mon, 25 Jun 2018 17:38:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4D05B20182 for ; Mon, 25 Jun 2018 17:38:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 41E3522362; Mon, 25 Jun 2018 17:38:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E609720182 for ; Mon, 25 Jun 2018 17:38:38 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 897856E369; Mon, 25 Jun 2018 17:38:35 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wr0-x242.google.com (mail-wr0-x242.google.com [IPv6:2a00:1450:400c:c0c::242]) by gabe.freedesktop.org (Postfix) with ESMTPS id 897356E36C for ; Mon, 25 Jun 2018 17:38:31 +0000 (UTC) Received: by mail-wr0-x242.google.com with SMTP id e18-v6so14526039wrs.5 for ; Mon, 25 Jun 2018 10:38:31 -0700 (PDT) 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:in-reply-to :references; bh=5TSRMT3DnntplCGmjTsYyjgjzLnp1FHWwFKOJjBkITc=; b=jJ62pKk67Q9sZC7CTgVQez1/Bj9Ave/vdUIgmwIMEkkwR9AmdjW4cGykTYht9xacMm eiWBmcv8Q92GKn2QcK91iWC6Y1J24yriDGPFycOCuP3Cp5pjJ92h5g5OTllSVvbiltQC xj/vv5fNzFFzkABHF2jdCLLTsJouP3/TowCrv746hqPMDa8ORWadRbLMCnQnGReTK8JB yQuZgLYDKggbdxLotlqTx7O1QdNqZ5tKmzpzIzRgzuWJ5dgUbrAMNFtbKiRxGunZLa/E s74Givk5WlE0O5mYb/bzjOVnBNkMu/D+GWx1dtHN+m6xpWESlctMK1Ce/FfUmsO1ztG6 xb1A== X-Gm-Message-State: APt69E2nfBSU9pxf99JZ0o2N7a/a8axChI9EsuTeBiqbNyT3ILFw6xkg 6zho1R3svH0DksM5yWsDBJ7wBA== X-Google-Smtp-Source: AAOMgpeH92+aWbcSYqE3+wiliv8kPELeTkDYQ2Ch3X+xhbTsmrRbbFd7596vOZ1HD8UOPlOX4JEMgg== X-Received: by 2002:adf:ef85:: with SMTP id d5-v6mr1736997wro.195.1529948309979; Mon, 25 Jun 2018 10:38:29 -0700 (PDT) Received: from arch-x1c3.cbg.collabora.co.uk ([2a00:5f00:102:0:9665:9cff:feee:aa4d]) by smtp.gmail.com with ESMTPSA id p8-v6sm6581019wrq.70.2018.06.25.10.38.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Jun 2018 10:38:28 -0700 (PDT) From: Emil Velikov To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm 04/10] xf86drm: Allocate drmDevicePtr's on stack Date: Mon, 25 Jun 2018 18:36:20 +0100 Message-Id: <20180625173626.8046-4-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180625173626.8046-1-emil.l.velikov@gmail.com> References: <20180625173626.8046-1-emil.l.velikov@gmail.com> X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: emil.l.velikov@gmail.com MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Emil Velikov Currently we dynamically allocate 16 pointers and reallocate more as needed. Instead, allocate the maximum number (256) on stack - the number is small enough and is unlikely to change in the foreseeable future. This allows us to simplify the error handling and even shed a few bytes off the final binary. Signed-off-by: Emil Velikov --- xf86drm.c | 64 ++++++------------------------------------------------- 1 file changed, 6 insertions(+), 58 deletions(-) diff --git a/xf86drm.c b/xf86drm.c index 114cf855..d4810740 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -3846,7 +3846,7 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device) return 0; #else - drmDevicePtr *local_devices; + drmDevicePtr local_devices[256]; drmDevicePtr d; DIR *sysdir; struct dirent *dent; @@ -3854,7 +3854,6 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device) int subsystem_type; int maj, min; int ret, i, node_count; - int max_count = 16; dev_t find_rdev; if (drm_device_validate_flags(flags)) @@ -3877,15 +3876,9 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device) if (subsystem_type < 0) return subsystem_type; - local_devices = calloc(max_count, sizeof(drmDevicePtr)); - if (local_devices == NULL) - return -ENOMEM; - sysdir = opendir(DRM_DIR_NAME); - if (!sysdir) { - ret = -errno; - goto free_locals; - } + if (!sysdir) + return -errno; i = 0; while ((dent = readdir(sysdir))) { @@ -3893,16 +3886,6 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device) if (ret) continue; - if (i >= max_count) { - drmDevicePtr *temp; - - max_count += 16; - temp = realloc(local_devices, max_count * sizeof(drmDevicePtr)); - if (!temp) - goto free_devices; - local_devices = temp; - } - local_devices[i] = d; i++; } @@ -3921,18 +3904,9 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device) } closedir(sysdir); - free(local_devices); if (*device == NULL) return -ENODEV; return 0; - -free_devices: - drmFreeDevices(local_devices, i); - closedir(sysdir); - -free_locals: - free(local_devices); - return ret; #endif } @@ -3968,25 +3942,18 @@ int drmGetDevice(int fd, drmDevicePtr *device) */ int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_devices) { - drmDevicePtr *local_devices; + drmDevicePtr local_devices[256]; drmDevicePtr device; DIR *sysdir; struct dirent *dent; int ret, i, node_count, device_count; - int max_count = 16; if (drm_device_validate_flags(flags)) return -EINVAL; - local_devices = calloc(max_count, sizeof(drmDevicePtr)); - if (local_devices == NULL) - return -ENOMEM; - sysdir = opendir(DRM_DIR_NAME); - if (!sysdir) { - ret = -errno; - goto free_locals; - } + if (!sysdir) + return -errno; i = 0; while ((dent = readdir(sysdir))) { @@ -3994,16 +3961,6 @@ int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_devices) if (ret) continue; - if (i >= max_count) { - drmDevicePtr *temp; - - max_count += 16; - temp = realloc(local_devices, max_count * sizeof(drmDevicePtr)); - if (!temp) - goto free_devices; - local_devices = temp; - } - local_devices[i] = device; i++; } @@ -4025,16 +3982,7 @@ int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_devices) } closedir(sysdir); - free(local_devices); return device_count; - -free_devices: - drmFreeDevices(local_devices, i); - closedir(sysdir); - -free_locals: - free(local_devices); - return ret; } /**