From patchwork Mon Mar 7 16:46:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 548869 Delivered-To: patch@linaro.org Received: by 2002:adf:80eb:0:0:0:0:0 with SMTP id 98csp1091371wrl; Mon, 7 Mar 2022 08:48:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJwVGgMd2BjnQyxPvV8YAn3cgiBBjLcOXXn3QvSMysjWUKHtzCP7ioZct1U7GdBZh/ccrzcW X-Received: by 2002:a0d:fb45:0:b0:2d0:d09a:576c with SMTP id l66-20020a0dfb45000000b002d0d09a576cmr9449838ywf.447.1646671710200; Mon, 07 Mar 2022 08:48:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646671710; cv=none; d=google.com; s=arc-20160816; b=1EG0owD2APO6XWmXJkIKwfmuFYmTYRgR//pA84XTwssi1Uq2nrAfGgqg3U3He4SAqI 7vE/etdRLnCFWKfVIwr3Xv93vB3eWLqLxNEE58dd/0l6ufH3E1OSivNLX98PL9W8eEua qYGhl9JiEvEo13LP7285X+kNywDuCR9McuLSOoaIR9JqfrWApY9Be/DC7TVld/RnOM3z uUJaGdW/3bkzCs6Dl3LWCuZYNgs7DUlfo0+/QyO6N09g4YKuw/zGQ1mRjXiYlp91gIck F5yXuSrvMiC6Xbc7lyNq8OyIuXp3srxvtMqdCmHAJg5ypVh6PAR6XmG8QtQC+9NPrT9Z klpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=88/vli9sz3m24lTiI0cGWu1ovNoRhU1oqr+a87iSyJw=; b=sI6VC1sdWSJp2BTw0CByp2BKJjNgffEO0PneoP9CoLn0yDMxq8/darDnH47SUS6xaC pvYig8XNoFlOPDHDk+4V4bYYjeQjfSOjsHS1SSrhv+8KBZjxdQw1rlQCpTVe1TJXxRkb 3FLG41q72JBGLBTKZKVjHdBlu9yGAc2QO/kMU2oHeCo40RS2Gu3Pj/JGaFhYJ11wI5kd W6N+lTVdEosyeXNbG0xIeG/8bHfyzhsoDy6Clj0AbfqBTpcRWXaEh6DPH7AUsqDYAT8m iJq0Grb8nMOkDQlozxpTdLxIUUlgyHU3FApJCy4imdssPulS4fNf671FNaEzYEntEorI x05A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iDa9H092; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id 141-20020a250293000000b0062865f7a121si9020169ybc.34.2022.03.07.08.48.30 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Mar 2022 08:48:30 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iDa9H092; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:60864 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nRGX7-0001Zy-Js for patch@linaro.org; Mon, 07 Mar 2022 11:48:29 -0500 Received: from eggs.gnu.org ([209.51.188.92]:33970) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nRGVz-0001T5-80 for qemu-devel@nongnu.org; Mon, 07 Mar 2022 11:47:19 -0500 Received: from [2a00:1450:4864:20::335] (port=56171 helo=mail-wm1-x335.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nRGVx-0007jP-GK for qemu-devel@nongnu.org; Mon, 07 Mar 2022 11:47:18 -0500 Received: by mail-wm1-x335.google.com with SMTP id i66so9554896wma.5 for ; Mon, 07 Mar 2022 08:47:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=88/vli9sz3m24lTiI0cGWu1ovNoRhU1oqr+a87iSyJw=; b=iDa9H092MybqwrOSEyObQw2tgUbZDEZNrbeQ6QZO49MO6OOpuzHuPBHqiWRmVLocmK 9Xd5C4c3iCOC2KUFqf8FLj/IQYhT1/I3YwQ0PFlFiLixY9UdKFWzVOZv1HwK9LLLWErE We8u2K9aP14MCgRlud1MbCum2iapB6JbJ2rQIBM16w7knAdwwgrDZYdx9osetAnIibZV 8FBJj80R3aTGXGOfz++GQpRkgqh7jz301QesimhTqIkoZtIOANphksc51NzXcOem1ODE 7D3q3U7v12dEAjUmrIKmB2A0gah4uSFBWwchMtVh2wuCWyjxWgqBUC3C35oomyJZE8k6 cqrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=88/vli9sz3m24lTiI0cGWu1ovNoRhU1oqr+a87iSyJw=; b=OdqW8xCnlgqX78tIZ+UDb1Qzsaxkk+fhaHsxq2GC4ChfcGVghpXzNPAvgLbkBCQ5c5 flNgAc6FtxMpkbBAnJo0F257n3Z34eLW/4Tu1qyZB/Ge9bIoWQfDkG591btk9Rn1/q8e 3OeeIMmcUYHRS2tOOI93GwYkPC92fsNGao2G4xaCkmoyXZ5zeynwZxgZBS/etxM0yvmU ZRtegUt5pnR6ADuZmDd8tce2QFp7zf8DAmvIUjAwwjJlc3k5ieuQXQuTP0JdaxqEqIHz pnuob3R3MLIQSni8og0180MWxEM+lylmeHnukNI2PyCX+G6IGZCj/Z6f7MrmOOwm1Jk4 Mf/w== X-Gm-Message-State: AOAM530pkx5YWKudAoAS4iMVWKOip+VeP8GfzWYELUXb8PDDiyyRYy+X Y3Qw/s3qa0pNvQbYWHcc0ByGGD6oyamZVw== X-Received: by 2002:a05:600c:154c:b0:382:6377:14bd with SMTP id f12-20020a05600c154c00b00382637714bdmr9949277wmg.125.1646671636166; Mon, 07 Mar 2022 08:47:16 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id v188-20020a1cacc5000000b00384b71a50d5sm13806652wme.24.2022.03.07.08.47.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Mar 2022 08:47:15 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PULL 05/18] util: Share qemu_try_memalign() implementation between POSIX and Windows Date: Mon, 7 Mar 2022 16:46:56 +0000 Message-Id: <20220307164709.2503250-6-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220307164709.2503250-1-peter.maydell@linaro.org> References: <20220307164709.2503250-1-peter.maydell@linaro.org> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::335 (failed) Received-SPF: pass client-ip=2a00:1450:4864:20::335; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x335.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" The qemu_try_memalign() functions for POSIX and Windows used to be significantly different, but these days they are identical except for the actual allocation function called, and the POSIX version already has to have ifdeffery for different allocation functions. Move to a single implementation in memalign.c, which uses the Windows _aligned_malloc if we detect that function in meson. Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Message-id: 20220226180723.1706285-7-peter.maydell@linaro.org Reviewed-by: Philippe Mathieu-Daudé --- meson.build | 1 + util/memalign.c | 39 +++++++++++++++++++++++++++++++++++++++ util/oslib-posix.c | 29 ----------------------------- util/oslib-win32.c | 17 ----------------- 4 files changed, 40 insertions(+), 46 deletions(-) diff --git a/meson.build b/meson.build index b541506c1c5..bc0ac931e1a 100644 --- a/meson.build +++ b/meson.build @@ -1622,6 +1622,7 @@ config_host_data.set('CONFIG_POSIX_FALLOCATE', cc.has_function('posix_fallocate' # Note that we need to specify prefix: here to avoid incorrectly # thinking that Windows has posix_memalign() config_host_data.set('CONFIG_POSIX_MEMALIGN', cc.has_function('posix_memalign', prefix: '#include ')) +config_host_data.set('CONFIG_ALIGNED_MALLOC', cc.has_function('_aligned_malloc')) config_host_data.set('CONFIG_PPOLL', cc.has_function('ppoll')) config_host_data.set('CONFIG_PREADV', cc.has_function('preadv', prefix: '#include ')) config_host_data.set('CONFIG_SEM_TIMEDWAIT', cc.has_function('sem_timedwait', dependencies: threads)) diff --git a/util/memalign.c b/util/memalign.c index 6dfc20abbb1..22b405700e3 100644 --- a/util/memalign.c +++ b/util/memalign.c @@ -25,6 +25,45 @@ */ #include "qemu/osdep.h" +#include "qemu/host-utils.h" +#include "trace.h" + +void *qemu_try_memalign(size_t alignment, size_t size) +{ + void *ptr; + + if (alignment < sizeof(void*)) { + alignment = sizeof(void*); + } else { + g_assert(is_power_of_2(alignment)); + } + + /* + * Handling of 0 allocations varies among the different + * platform APIs (for instance _aligned_malloc() will + * fail) -- ensure that we always return a valid non-NULL + * pointer that can be freed by qemu_vfree(). + */ + if (size == 0) { + size++; + } +#if defined(CONFIG_POSIX_MEMALIGN) + int ret; + ret = posix_memalign(&ptr, alignment, size); + if (ret != 0) { + errno = ret; + ptr = NULL; + } +#elif defined(CONFIG_ALIGNED_MALLOC) + ptr = _aligned_malloc(size, alignment); +#elif defined(CONFIG_BSD) + ptr = valloc(size); +#else + ptr = memalign(alignment, size); +#endif + trace_qemu_memalign(alignment, size, ptr); + return ptr; +} void *qemu_memalign(size_t alignment, size_t size) { diff --git a/util/oslib-posix.c b/util/oslib-posix.c index f7e22f4ff9b..91798f7e504 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -199,35 +199,6 @@ fail_close: return false; } -void *qemu_try_memalign(size_t alignment, size_t size) -{ - void *ptr; - - if (alignment < sizeof(void*)) { - alignment = sizeof(void*); - } else { - g_assert(is_power_of_2(alignment)); - } - - if (size == 0) { - size++; - } -#if defined(CONFIG_POSIX_MEMALIGN) - int ret; - ret = posix_memalign(&ptr, alignment, size); - if (ret != 0) { - errno = ret; - ptr = NULL; - } -#elif defined(CONFIG_BSD) - ptr = valloc(size); -#else - ptr = memalign(alignment, size); -#endif - trace_qemu_memalign(alignment, size, ptr); - return ptr; -} - /* alloc shared memory pages */ void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared, bool noreserve) diff --git a/util/oslib-win32.c b/util/oslib-win32.c index 8c28d70904d..d9768532bec 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -44,23 +44,6 @@ /* this must come after including "trace.h" */ #include -void *qemu_try_memalign(size_t alignment, size_t size) -{ - void *ptr; - - if (alignment < sizeof(void *)) { - alignment = sizeof(void *); - } else { - g_assert(is_power_of_2(alignment)); - } - if (size == 0) { - size++; - } - ptr = _aligned_malloc(size, alignment); - trace_qemu_memalign(alignment, size, ptr); - return ptr; -} - static int get_allocation_granularity(void) { SYSTEM_INFO system_info;