From patchwork Sun Feb 18 16:16:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 46352 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:dda5:b0:19e:cdac:8cce with SMTP id kw37csp829135pzb; Sun, 18 Feb 2024 08:17:18 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCWjlwno/U1eraF3sOKQ4be9wy+PjZuc4iVm/hlakquk74cdWtCbmZT849eoHCwN5DLDu0Z4CTvqtIi6oVySab6u4CPV7X0chPSD0g== X-Google-Smtp-Source: AGHT+IEg3GmGjsRm4YHPTFjS5M+ZeYz9sFmiNMdJR3TQvW6Dq6wC7M+FnMqKVF1mvkZFfu2V1/0O X-Received: by 2002:a50:d694:0:b0:562:1a04:e0cb with SMTP id r20-20020a50d694000000b005621a04e0cbmr4651349edi.1.1708273038531; Sun, 18 Feb 2024 08:17:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708273038; cv=none; d=google.com; s=arc-20160816; b=xyyiYddN/nZGZ9MuNxcU1UHDauSlnAX/bcipSD19vOWCenr2SSYssbqIzCrKTXCykm wAO+C4J2ZTHd9kttW7CTc7jfi2p7tOpycc8yNIAnpxb5bnZqpx6c3JmNA1z7jtlrQvSg /MwIYSCBaL/3eiSCHlIl4f0O5AYoEOzLvuDw+S2XIsOEOB5BD85WOSgQ60yx0IRUVtMd H+v1G4FDB1qo2pfSkpvMeqC4OG7en/oalyLULFgDl6IBXn25RiQPw2LtM2VN0IiMRY+m k+KzBPGTkIGRJQfdIiqH9wOZxwk5wJxm8cstHmML68TAGgBRzTJDBsVDNV4sDa90P7SE guZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=8Ok4ZxXAHuncUZ1elGMz+axhoeybCsFKlB7D/nociLs=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=eQNFVdJtif52DFXhWS2RCi1LRbtsISLm+dUdO/KearTi37YpdH6qRf50Mf8/ZggfLe CBxQEga7rAV5EHF3mT1TCmWlEy5KURfxtUrPtwZCIebccTDAlAeEmD+91KhtVCB7dxlA +0LwzMS2OsW6quyVdKHrQaJ+dNYD/6RVZuEOQhxcX4n+g2oIvUHh0Co1Ccycmz7iN9l+ 6ZiIzeZRJmbWMFsV3f0ah0y7E+uQafn6eKrkSD+jkijViSvr9HqcbMOJF+rQV5ldBbwj ZNebHohzAQX+tUCr3pWntEG2OPyJbzhHqtBEYcBDNFc729VbcICBCfOIEfbvwGPp6kVx XZSg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=a2W59WYP; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id a3-20020a509b43000000b00560f9a26f9csi1677635edj.580.2024.02.18.08.17.18; Sun, 18 Feb 2024 08:17:18 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=a2W59WYP; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9175368D35C; Sun, 18 Feb 2024 18:17:14 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8692B68D314 for ; Sun, 18 Feb 2024 18:17:07 +0200 (EET) Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-5cedfc32250so2924525a12.0 for ; Sun, 18 Feb 2024 08:17:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708273024; x=1708877824; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=KRQwA3MMNW4NiPP8dxwJh/nkbDqzhhrE2OzE1VfJT3Y=; b=a2W59WYP71T/qw0tTCB8D2rpkoRFW86lZh7VgBCmXmEvherxreIFDbjoCMt5oAWZhl 9iJWwB77q5/tX9evmYuwp8tW4aqjWmYER2uiGvBVWFCfn68r+ZexYUHM7JDEatmKVDMe qFTxbjL5ak3f99nw4kIzpcgQLPRYeYcD0bwLkk+ah+9MZ+/aWQS5mxQq8o1ukRT9MHVc ZjEdLL4AOoatusXFtD7XI+LOmI7MXJQXzPUM//qWtCRpCRWUuE+eJMY+osVbf7PmkIhL 3yFiw2gQ8Qw9vOTMLlqasW8t2j50WVsEUtO3mj1ch3coNZV0wIF0s58K94x3uPVazLW1 z9wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708273024; x=1708877824; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KRQwA3MMNW4NiPP8dxwJh/nkbDqzhhrE2OzE1VfJT3Y=; b=P4LSqZN3+WN7lRYnQChz9nt4zkC945e0jP/RfmaRkXSabrskwbtBtLT6GZk8F7zKCE jWsermbj10sjK+xEzrJ6bFlOZS+0NYOWIqELyw8KHNSZiTbRDgLrQfy+woC/6aJHlIdy 2Ahx0ccGjaXPw3UzIHiFnnrmX8ueufgqvtBQC8gT1xATjdTfwWGFTZUy9jHyaUdkTUTy Kq8UPOFZ7VWOyN5PsrbnE5PBf3UlzNuGEWh/oca83TGaIilPGpeFANEF8UTfVPqevq9Q ubkAdYcwEF0a9q45W+ZqepTNVq6i+HZgOHk1knuAlQFXzTM7MHYpjXNu0MhnBgBWC23z 99mg== X-Gm-Message-State: AOJu0YzaU5DAiIWYOlYTVjm8Q3Ut8X2Luq+FIFYK00pRn5pOPpvMZeDQ 687dFjZLv3Pu9Pb9s/oNh4mbQPnk0MeUUTA6tEEcMxfZwlsE2a6L3vtgLu29 X-Received: by 2002:a05:6a20:959a:b0:19e:99be:c53e with SMTP id iu26-20020a056a20959a00b0019e99bec53emr10704155pzb.48.1708273023911; Sun, 18 Feb 2024 08:17:03 -0800 (PST) Received: from localhost.localdomain (host197.190-225-105.telecom.net.ar. [190.225.105.197]) by smtp.gmail.com with ESMTPSA id bj19-20020a17090b089300b0029988480e2fsm1103216pjb.30.2024.02.18.08.17.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 08:17:03 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sun, 18 Feb 2024 13:16:36 -0300 Message-ID: <20240218161636.15649-1-jamrial@gmail.com> X-Mailer: git-send-email 2.43.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avutil/mem: use C11 aligned_malloc() X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: xVZao0bT5b2c Save for the Microsoft C Runtime library, where free() can't handle aligned buffers, aligned_malloc() should be available and working on all supported targets. Also, malloc() alone may be sufficient if alignment requirement is low, so add a check for it. Signed-off-by: James Almer --- configure | 2 -- libavutil/mem.c | 42 ++++++------------------------------------ 2 files changed, 6 insertions(+), 38 deletions(-) diff --git a/configure b/configure index 7c45ac25c8..8fd2895ac2 100755 --- a/configure +++ b/configure @@ -6450,8 +6450,6 @@ if test -n "$custom_allocator"; then fi check_func_headers malloc.h _aligned_malloc && enable aligned_malloc -check_func ${malloc_prefix}memalign && enable memalign -check_func ${malloc_prefix}posix_memalign && enable posix_memalign check_func access check_func_headers stdlib.h arc4random_buf diff --git a/libavutil/mem.c b/libavutil/mem.c index 36b8940a0c..a72981d1ab 100644 --- a/libavutil/mem.c +++ b/libavutil/mem.c @@ -100,44 +100,14 @@ void *av_malloc(size_t size) if (size > atomic_load_explicit(&max_alloc_size, memory_order_relaxed)) return NULL; -#if HAVE_POSIX_MEMALIGN - if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation - if (posix_memalign(&ptr, ALIGN, size)) - ptr = NULL; -#elif HAVE_ALIGNED_MALLOC +#if HAVE_ALIGNED_MALLOC ptr = _aligned_malloc(size, ALIGN); -#elif HAVE_MEMALIGN -#ifndef __DJGPP__ - ptr = memalign(ALIGN, size); -#else - ptr = memalign(size, ALIGN); -#endif - /* Why 64? - * Indeed, we should align it: - * on 4 for 386 - * on 16 for 486 - * on 32 for 586, PPro - K6-III - * on 64 for K7 (maybe for P3 too). - * Because L1 and L2 caches are aligned on those values. - * But I don't want to code such logic here! - */ - /* Why 32? - * For AVX ASM. SSE / NEON needs only 16. - * Why not larger? Because I did not see a difference in benchmarks ... - */ - /* benchmarks with P3 - * memalign(64) + 1 3071, 3051, 3032 - * memalign(64) + 2 3051, 3032, 3041 - * memalign(64) + 4 2911, 2896, 2915 - * memalign(64) + 8 2545, 2554, 2550 - * memalign(64) + 16 2543, 2572, 2563 - * memalign(64) + 32 2546, 2545, 2571 - * memalign(64) + 64 2570, 2533, 2558 - * - * BTW, malloc seems to do 8-byte alignment by default here. - */ #else - ptr = malloc(size); + // malloc may already allocate sufficiently aligned buffers + if (ALIGN > _Alignof(max_align_t)) + ptr = aligned_malloc(size, ALIGN); + else + ptr = malloc(size); #endif if(!ptr && !size) { size = 1;