From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 8A71048810 for ; Sun, 18 Feb 2024 16:17:16 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6062F68D352; Sun, 18 Feb 2024 18:17:14 +0200 (EET) 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-Google-Smtp-Source: AGHT+IHPmqZEl95JBKrql2sZiopRiIvyiSfOTizM3+orFrfxChvUUXpXAepIBn2oxlR6OETiIrOhEQ== 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: 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; -- 2.43.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".