From patchwork Mon Nov 21 15:18:56 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 1511 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.90.1 with SMTP id o1csp1654534vsb; Mon, 21 Nov 2016 07:19:10 -0800 (PST) X-Received: by 10.28.154.86 with SMTP id c83mr14295921wme.23.1479741550705; Mon, 21 Nov 2016 07:19:10 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id pb5si20688102wjb.189.2016.11.21.07.19.10; Mon, 21 Nov 2016 07:19:10 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D79B3680D49; Mon, 21 Nov 2016 17:19:04 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vie01a-dmta-pe02-3.mx.upcmail.net (vie01a-dmta-pe02-3.mx.upcmail.net [62.179.121.159]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0DDBA68994F for ; Mon, 21 Nov 2016 17:18:58 +0200 (EET) Received: from [172.31.216.43] (helo=vie01a-pemc-psmtp-pe01) by vie01a-dmta-pe02.mx.upcmail.net with esmtp (Exim 4.87) (envelope-from ) id 1c8qN2-0004Eo-8P for ffmpeg-devel@ffmpeg.org; Mon, 21 Nov 2016 16:19:00 +0100 Received: from localhost ([213.47.41.20]) by vie01a-pemc-psmtp-pe01 with SMTP @ mailcloud.upcmail.net id AfJz1u00F0S5wYM01fK0Bc; Mon, 21 Nov 2016 16:19:00 +0100 X-SourceIP: 213.47.41.20 From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Mon, 21 Nov 2016 16:18:56 +0100 Message-Id: <20161121151858.703-1-michael@niedermayer.cc> X-Mailer: git-send-email 2.10.2 Subject: [FFmpeg-devel] [PATCH 1/3] avutil/mem: Fix *realloc() alignment with memalign hack X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Michael Niedermayer --- libavutil/mem.c | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/libavutil/mem.c b/libavutil/mem.c index 1a8fc21..6273d89 100644 --- a/libavutil/mem.c +++ b/libavutil/mem.c @@ -86,12 +86,15 @@ void *av_malloc(size_t size) return NULL; #if CONFIG_MEMALIGN_HACK - ptr = malloc(size + ALIGN); + ptr = malloc(size + ALIGN + 8); if (!ptr) return ptr; - diff = ((~(long)ptr)&(ALIGN - 1)) + 1; + + diff = ((((uintptr_t)ptr) + 9 + ALIGN - 1) & ~(ALIGN - 1)) - (uintptr_t)ptr; + av_assert0(diff>0 && diff<=ALIGN + 8); ptr = (char *)ptr + diff; - ((char *)ptr)[-1] = diff; + ((char *)ptr)[-9] = diff; + ((int64_t *)ptr)[-1] = size; #elif HAVE_POSIX_MEMALIGN if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation if (posix_memalign(&ptr, ALIGN, size)) @@ -146,6 +149,7 @@ void *av_realloc(void *ptr, size_t size) { #if CONFIG_MEMALIGN_HACK int diff; + void *ptr2; #endif /* let's disallow possibly ambiguous cases */ @@ -153,15 +157,31 @@ void *av_realloc(void *ptr, size_t size) return NULL; #if CONFIG_MEMALIGN_HACK - //FIXME this isn't aligned correctly, though it probably isn't needed if (!ptr) return av_malloc(size); - diff = ((char *)ptr)[-1]; - av_assert0(diff>0 && diff<=ALIGN); - ptr = realloc((char *)ptr - diff, size + diff); - if (ptr) - ptr = (char *)ptr + diff; - return ptr; + diff = ((char *)ptr)[-9]; + av_assert0(diff>0 && diff<=ALIGN + 8); + + // Quick path for apparently and likly aligned realloc() + if (diff == ALIGN) { + ptr = realloc((char *)ptr - diff, size + diff); + if (ptr) + ptr = (char *)ptr + diff; + if (!(((uintptr_t)ptr) & (ALIGN - 1))) { + if (ptr) + ((int64_t *)ptr)[-1] = size; + return ptr; + } + } + + ptr2 = av_malloc(size); + if (!ptr2) + return NULL; + + memcpy(ptr2, ptr, FFMIN(((int64_t *)ptr)[-1], size)); + ((int64_t *)ptr2)[-1] = size; + av_free(ptr); + return ptr2; #elif HAVE_ALIGNED_MALLOC return _aligned_realloc(ptr, size + !size, ALIGN); #else @@ -229,8 +249,8 @@ void av_free(void *ptr) { #if CONFIG_MEMALIGN_HACK if (ptr) { - int v= ((char *)ptr)[-1]; - av_assert0(v>0 && v<=ALIGN); + int v= ((char *)ptr)[-9]; + av_assert0(v>0 && v<=ALIGN + 8); free((char *)ptr - v); } #elif HAVE_ALIGNED_MALLOC