diff mbox

[FFmpeg-devel,RFC] lavu/mem: Allow allocations close to max_alloc_size with av_fast_realloc()

Message ID CAB0OVGp-RsossxaJkeBhf6fXbC8HuV0pMtw1-k-Rfb=0XeawTQ@mail.gmail.com
State Accepted
Headers show

Commit Message

Carl Eugen Hoyos Jan. 2, 2018, 1 a.m. UTC
2018-01-02 1:14 GMT+01:00 Michael Niedermayer <michael@niedermayer.cc>:
> On Mon, Jan 01, 2018 at 11:10:57PM +0100, Carl Eugen Hoyos wrote:
>> Hi!
>>
>> Similar reason as last mem.c patch: av_fast_realloc() can currently
>> fail in situations where the allocation is possible and allowed.
>> The patch does not change behaviour for the failure case, if this is
>> wanted, it should be done separately.
>>
>> Please comment, Carl Eugen
>
>>  mem.c |    5 ++++-
>>  1 file changed, 4 insertions(+), 1 deletion(-)
>> 7529e1d584c62ece463f4461279ea6e3973162c9  0001-lavu-mem-Allow-allocations-close-to-max_alloc_size-w.patch
>> From ac69f4e8402f7c7ee6df09c0450354e2bb900e5a Mon Sep 17 00:00:00 2001
>> From: Carl Eugen Hoyos <ceffmpeg@gmail.com>
>> Date: Mon, 1 Jan 2018 23:04:58 +0100
>> Subject: [PATCH] lavu/mem: Allow allocations close to max_alloc_size with
>>  av_fast_realloc().
>>
>> ---
>>  libavutil/mem.c |    5 ++++-
>>  1 file changed, 4 insertions(+), 1 deletion(-)
>>
>> diff --git a/libavutil/mem.c b/libavutil/mem.c
>> index 0729e1d..934987f 100644
>> --- a/libavutil/mem.c
>> +++ b/libavutil/mem.c
>> @@ -466,7 +466,10 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
>>      if (min_size <= *size)
>>          return ptr;
>>
>> -    min_size = FFMAX(min_size + min_size / 16 + 32, min_size);
>> +    if (min_size > (max_alloc_size - 32))
>> +        return NULL;
>
> This failure mode differs from the existing in what it does with *size

New patch attached.

Thank you, Carl Eugen

Comments

Bang He Jan. 2, 2018, 2:59 p.m. UTC | #1
what is the mean:   min_size + min_size / 16 + 32

On Tue, Jan 2, 2018 at 9:00 AM, Carl Eugen Hoyos <ceffmpeg@gmail.com> wrote:

> 2018-01-02 1:14 GMT+01:00 Michael Niedermayer <michael@niedermayer.cc>:
> > On Mon, Jan 01, 2018 at 11:10:57PM +0100, Carl Eugen Hoyos wrote:
> >> Hi!
> >>
> >> Similar reason as last mem.c patch: av_fast_realloc() can currently
> >> fail in situations where the allocation is possible and allowed.
> >> The patch does not change behaviour for the failure case, if this is
> >> wanted, it should be done separately.
> >>
> >> Please comment, Carl Eugen
> >
> >>  mem.c |    5 ++++-
> >>  1 file changed, 4 insertions(+), 1 deletion(-)
> >> 7529e1d584c62ece463f4461279ea6e3973162c9  0001-lavu-mem-Allow-
> allocations-close-to-max_alloc_size-w.patch
> >> From ac69f4e8402f7c7ee6df09c0450354e2bb900e5a Mon Sep 17 00:00:00 2001
> >> From: Carl Eugen Hoyos <ceffmpeg@gmail.com>
> >> Date: Mon, 1 Jan 2018 23:04:58 +0100
> >> Subject: [PATCH] lavu/mem: Allow allocations close to max_alloc_size
> with
> >>  av_fast_realloc().
> >>
> >> ---
> >>  libavutil/mem.c |    5 ++++-
> >>  1 file changed, 4 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/libavutil/mem.c b/libavutil/mem.c
> >> index 0729e1d..934987f 100644
> >> --- a/libavutil/mem.c
> >> +++ b/libavutil/mem.c
> >> @@ -466,7 +466,10 @@ void *av_fast_realloc(void *ptr, unsigned int
> *size, size_t min_size)
> >>      if (min_size <= *size)
> >>          return ptr;
> >>
> >> -    min_size = FFMAX(min_size + min_size / 16 + 32, min_size);
> >> +    if (min_size > (max_alloc_size - 32))
> >> +        return NULL;
> >
> > This failure mode differs from the existing in what it does with *size
>
> New patch attached.
>
> Thank you, Carl Eugen
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
>
Michael Niedermayer Jan. 4, 2018, 3:10 a.m. UTC | #2
On Tue, Jan 02, 2018 at 02:00:50AM +0100, Carl Eugen Hoyos wrote:
> 2018-01-02 1:14 GMT+01:00 Michael Niedermayer <michael@niedermayer.cc>:
> > On Mon, Jan 01, 2018 at 11:10:57PM +0100, Carl Eugen Hoyos wrote:
> >> Hi!
> >>
> >> Similar reason as last mem.c patch: av_fast_realloc() can currently
> >> fail in situations where the allocation is possible and allowed.
> >> The patch does not change behaviour for the failure case, if this is
> >> wanted, it should be done separately.
> >>
> >> Please comment, Carl Eugen
> >
> >>  mem.c |    5 ++++-
> >>  1 file changed, 4 insertions(+), 1 deletion(-)
> >> 7529e1d584c62ece463f4461279ea6e3973162c9  0001-lavu-mem-Allow-allocations-close-to-max_alloc_size-w.patch
> >> From ac69f4e8402f7c7ee6df09c0450354e2bb900e5a Mon Sep 17 00:00:00 2001
> >> From: Carl Eugen Hoyos <ceffmpeg@gmail.com>
> >> Date: Mon, 1 Jan 2018 23:04:58 +0100
> >> Subject: [PATCH] lavu/mem: Allow allocations close to max_alloc_size with
> >>  av_fast_realloc().
> >>
> >> ---
> >>  libavutil/mem.c |    5 ++++-
> >>  1 file changed, 4 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/libavutil/mem.c b/libavutil/mem.c
> >> index 0729e1d..934987f 100644
> >> --- a/libavutil/mem.c
> >> +++ b/libavutil/mem.c
> >> @@ -466,7 +466,10 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
> >>      if (min_size <= *size)
> >>          return ptr;
> >>
> >> -    min_size = FFMAX(min_size + min_size / 16 + 32, min_size);
> >> +    if (min_size > (max_alloc_size - 32))
> >> +        return NULL;
> >
> > This failure mode differs from the existing in what it does with *size
> 
> New patch attached.
> 
> Thank you, Carl Eugen

>  mem.c |    7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 0dff8a3ec385fe8ddeeab7c95cbfc7daa47b1631  0001-lavu-mem-Allow-allocations-close-to-max_alloc_size-w.patch
> From 9586fb78e4b304923569a09fb275ece0531726e0 Mon Sep 17 00:00:00 2001
> From: Carl Eugen Hoyos <ceffmpeg@gmail.com>
> Date: Tue, 2 Jan 2018 01:58:35 +0100
> Subject: [PATCH] lavu/mem: Allow allocations close to max_alloc_size with
>  av_fast_realloc().
> 
> ---
>  libavutil/mem.c |    7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)

should be ok

thx


[...]
Carl Eugen Hoyos Jan. 4, 2018, 4:40 a.m. UTC | #3
2018-01-04 4:10 GMT+01:00 Michael Niedermayer <michael@niedermayer.cc>:
> On Tue, Jan 02, 2018 at 02:00:50AM +0100, Carl Eugen Hoyos wrote:
>> 2018-01-02 1:14 GMT+01:00 Michael Niedermayer <michael@niedermayer.cc>:
>> > On Mon, Jan 01, 2018 at 11:10:57PM +0100, Carl Eugen Hoyos wrote:
>> >> Hi!
>> >>
>> >> Similar reason as last mem.c patch: av_fast_realloc() can currently
>> >> fail in situations where the allocation is possible and allowed.
>> >> The patch does not change behaviour for the failure case, if this is
>> >> wanted, it should be done separately.
>> >>
>> >> Please comment, Carl Eugen
>> >
>> >>  mem.c |    5 ++++-
>> >>  1 file changed, 4 insertions(+), 1 deletion(-)
>> >> 7529e1d584c62ece463f4461279ea6e3973162c9  0001-lavu-mem-Allow-allocations-close-to-max_alloc_size-w.patch
>> >> From ac69f4e8402f7c7ee6df09c0450354e2bb900e5a Mon Sep 17 00:00:00 2001
>> >> From: Carl Eugen Hoyos <ceffmpeg@gmail.com>
>> >> Date: Mon, 1 Jan 2018 23:04:58 +0100
>> >> Subject: [PATCH] lavu/mem: Allow allocations close to max_alloc_size with
>> >>  av_fast_realloc().
>> >>
>> >> ---
>> >>  libavutil/mem.c |    5 ++++-
>> >>  1 file changed, 4 insertions(+), 1 deletion(-)
>> >>
>> >> diff --git a/libavutil/mem.c b/libavutil/mem.c
>> >> index 0729e1d..934987f 100644
>> >> --- a/libavutil/mem.c
>> >> +++ b/libavutil/mem.c
>> >> @@ -466,7 +466,10 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
>> >>      if (min_size <= *size)
>> >>          return ptr;
>> >>
>> >> -    min_size = FFMAX(min_size + min_size / 16 + 32, min_size);
>> >> +    if (min_size > (max_alloc_size - 32))
>> >> +        return NULL;
>> >
>> > This failure mode differs from the existing in what it does with *size
>>
>> New patch attached.
>>
>> Thank you, Carl Eugen
>
>>  mem.c |    7 ++++++-
>>  1 file changed, 6 insertions(+), 1 deletion(-)
>> 0dff8a3ec385fe8ddeeab7c95cbfc7daa47b1631  0001-lavu-mem-Allow-allocations-close-to-max_alloc_size-w.patch
>> From 9586fb78e4b304923569a09fb275ece0531726e0 Mon Sep 17 00:00:00 2001
>> From: Carl Eugen Hoyos <ceffmpeg@gmail.com>
>> Date: Tue, 2 Jan 2018 01:58:35 +0100
>> Subject: [PATCH] lavu/mem: Allow allocations close to max_alloc_size with
>>  av_fast_realloc().
>>
>> ---
>>  libavutil/mem.c |    7 ++++++-
>>  1 file changed, 6 insertions(+), 1 deletion(-)
>
> should be ok

Patch applied.

Thank you, Carl Eugen
diff mbox

Patch

From 9586fb78e4b304923569a09fb275ece0531726e0 Mon Sep 17 00:00:00 2001
From: Carl Eugen Hoyos <ceffmpeg@gmail.com>
Date: Tue, 2 Jan 2018 01:58:35 +0100
Subject: [PATCH] lavu/mem: Allow allocations close to max_alloc_size with
 av_fast_realloc().

---
 libavutil/mem.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/libavutil/mem.c b/libavutil/mem.c
index 0729e1d..6149755 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -466,7 +466,12 @@  void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
     if (min_size <= *size)
         return ptr;
 
-    min_size = FFMAX(min_size + min_size / 16 + 32, min_size);
+    if (min_size > max_alloc_size - 32) {
+        *size = 0;
+        return NULL;
+    }
+
+    min_size = FFMIN(max_alloc_size - 32, FFMAX(min_size + min_size / 16 + 32, min_size));
 
     ptr = av_realloc(ptr, min_size);
     /* we could set this to the unmodified min_size but this is safer
-- 
1.7.10.4