From patchwork Sat Feb 26 18:07:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 546185 Delivered-To: patch@linaro.org Received: by 2002:ac0:9f50:0:0:0:0:0 with SMTP id l16csp1377903imf; Sat, 26 Feb 2022 10:14:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJzPfm+L6KAGEs4KsMUtKVO9hNg+cNDuypL7bYWu3UcMfYCfhytU/kJY2t0kI226SQMrx4O3 X-Received: by 2002:a0d:db83:0:b0:2ca:287c:6d36 with SMTP id d125-20020a0ddb83000000b002ca287c6d36mr13367017ywe.475.1645899289698; Sat, 26 Feb 2022 10:14:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645899289; cv=none; d=google.com; s=arc-20160816; b=uFd5ns23DOJHmwWkNJtgzGXNPNALHuX9o2+TB2FUQ6JrdIuqQmpZSoXNPW+C2D4mrj zAIU9oRg7xKLvzz39egHmGvFay4YGFBmUQIUJ1aRLNpZFOGosEsOByrJCjf07QZuLcjY pX7td2pSmdmGRgDGdXJ9eLVyJ+Mb/9q/nYYwfNBMY5MHPBgRSTAOg24FaPXo6YDvjAEX /ZeFI8N30SSNTSS/Zh4+WmVRMc7YSHrsc51eCPFXfmXsXgpLEOuXGB+KaCBPvk6d2Tk+ DXJsgEyvEXl7P4pte0E/ITrRF9rl9hY2s9zCzI2ea8MG+DXf4keyJq88cXJKJTZ6UaMR JCUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc: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=wtF+98JRuxd5OjAw52Lho5GMRtdIqK6nH1UDlgPa8sQ=; b=viXvXSxWOCsca/LugDkIdtmx+3FZ+oV8hxNKl8vLjIjC5xTujJJnxDFkfIxbj4Pluz zpExyn1N+gVBcf+5KIUOxMBavfVNfvnkJ/QypcQcS/AEJd89KKt9R8MzaF5UHS5gwkTk ArEXzBUaWyrI0dPxF2VValSgfpo9PlVT6zyCOcEstb5bFnYifsEgPiQ89wYSRsHPxnTL bgdqh9N81yXKeTgBhX5RmmSR+lKQ4SThw7fJtlmsAQmW16Ch4C5J5oC4SsjjHTQZhKBg KxJD4AeWlwBXpaHfuGdIJRLBXoQM82jwnFlA2QAiCppfPGR4k3hvD//scmpdTObXAufA NMFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=oc1gnn9J; 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=fail (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 c129-20020a813587000000b002d8063f12basi5120555ywa.42.2022.02.26.10.14.49 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 26 Feb 2022 10:14:49 -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=fail header.i=@linaro.org header.s=google header.b=oc1gnn9J; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:46076 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nO1aj-0000lu-78 for patch@linaro.org; Sat, 26 Feb 2022 13:14:49 -0500 Received: from eggs.gnu.org ([209.51.188.92]:43412) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nO1Tl-0005eH-Qs for qemu-devel@nongnu.org; Sat, 26 Feb 2022 13:07:37 -0500 Received: from [2a00:1450:4864:20::432] (port=45010 helo=mail-wr1-x432.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nO1Ti-0004OK-4n for qemu-devel@nongnu.org; Sat, 26 Feb 2022 13:07:37 -0500 Received: by mail-wr1-x432.google.com with SMTP id u1so9143570wrg.11 for ; Sat, 26 Feb 2022 10:07:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wtF+98JRuxd5OjAw52Lho5GMRtdIqK6nH1UDlgPa8sQ=; b=oc1gnn9JfFBpofKSeZ4sPS81/OJJ2C6fwbIB5s7T9rtwMdy58DK44GVZYsb4Qucm9w 7Ck7P3kgXi7CUCoF6vt5LUYMJpFjqkvcDbLvXtGZP8PuzHieUoVNMjjul2MECKmq+jvG EK5ZSWuu4Ba6+VTRd3zP6hidUOdl9q3Kt61vaAweTSfQ2q8KulU2U8BxKuFchFJKRQAG CiJ2j80oA0GN1qE1ymnSkcxgvT0MDBl9RVZ9G/TvScmrcWimIFpoa4J5jwcVU4PlUdwj ayPpDBAqEmJRGNgAxEex/keLewXJvWtxADSm9s+wqxn1CsY+CTcwRmv3UnVbnEj4RLGD 0HCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wtF+98JRuxd5OjAw52Lho5GMRtdIqK6nH1UDlgPa8sQ=; b=oQ9bhJdqyeprawQXw5VC0wOVPRp1VP8rgJl1LKbC3e/rvuUZPWliZfm4TznQJYNYOB eTv/hAHcy47459YA1WvxMJL1p3ulZFunycFKAjrp97NXcfGFS0WNBCByv9hGtMwqP5vc 5pQRiMPUIKWLg7JhJzDH4Y4c+3IFgNUJRicvAWbmjoOu2evC6iTqCwRJx5mDYph4WPCy 9dzxW1QGftsRwuf1tglaejUIAynmzhs5aOTBOtWc03bifGyYmD+lCwov8N/q/EEdgo8r +stUll4UQFBlb0WbJwpyOR2F5hz4Q7SmZ5nK3eW+AZFX7jZnF7XhFkNHFf/DJICjTqSv 8S4Q== X-Gm-Message-State: AOAM53272dYtFd/e29Qh0OeZBlEvY+MwMTn5ujWYUKVcPfzxAiRuQrGR TWopm9SgA8Ws6MDj0kdMfum+le1roCwy5w== X-Received: by 2002:a5d:470a:0:b0:1ed:e403:579a with SMTP id y10-20020a5d470a000000b001ede403579amr10139761wrq.224.1645898851607; Sat, 26 Feb 2022 10:07:31 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id b15-20020adfe64f000000b001e60965a5d4sm5622409wrn.44.2022.02.26.10.07.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Feb 2022 10:07:31 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PATCH 6/9] util: Share qemu_try_memalign() implementation between POSIX and Windows Date: Sat, 26 Feb 2022 18:07:20 +0000 Message-Id: <20220226180723.1706285-7-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220226180723.1706285-1-peter.maydell@linaro.org> References: <20220226180723.1706285-1-peter.maydell@linaro.org> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::432 (failed) Received-SPF: pass client-ip=2a00:1450:4864:20::432; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x432.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: , Cc: Paolo Bonzini , qemu-block@nongnu.org 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 --- meson.build | 1 + util/memalign.c | 33 +++++++++++++++++++++++++++++++++ util/oslib-posix.c | 26 -------------------------- util/oslib-win32.c | 18 ------------------ 4 files changed, 34 insertions(+), 44 deletions(-) diff --git a/meson.build b/meson.build index e08de177c87..21511d4fb61 100644 --- a/meson.build +++ b/meson.build @@ -1609,6 +1609,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..ee3393cc601 100644 --- a/util/memalign.c +++ b/util/memalign.c @@ -26,6 +26,39 @@ #include "qemu/osdep.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)); + } + +#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) + /* _aligned_malloc() fails on 0 size */ + if (size) { + ptr = _aligned_malloc(size, alignment); + } else { + ptr = NULL; + } +#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) { void *p = qemu_try_memalign(alignment, size); diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 0278902ee79..91798f7e504 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -199,32 +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 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 8c1c64719d7..d9768532bec 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -44,24 +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) { - ptr = _aligned_malloc(size, alignment); - } else { - ptr = NULL; - } - trace_qemu_memalign(alignment, size, ptr); - return ptr; -} - static int get_allocation_granularity(void) { SYSTEM_INFO system_info;