From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> To: ffmpeg-devel@ffmpeg.org Subject: Re: [FFmpeg-devel] [PATCH 3/8] avutil/mem: Add av_fast_realloc_array() Date: Wed, 6 Jul 2022 16:46:42 +0200 Message-ID: <DB6PR0101MB2214D873CB5422DEE2051F418F809@DB6PR0101MB2214.eurprd01.prod.exchangelabs.com> (raw) In-Reply-To: <6e3e5682a3464edccd6395a783e61b3c4b6337e9.camel@acc.umu.se> Tomas Härdin: > tis 2022-07-05 klockan 22:26 +0200 skrev Andreas Rheinhardt: >> From: Andreas Rheinhardt <andres.rheinhardt@outlook.com> >> >> This is an array-equivalent of av_fast_realloc(). Its advantages >> compared to using av_fast_realloc() for allocating arrays are as >> follows: >> >> a) It performs its own overflow checks for the multiplication that is >> implicit in array allocations. (And it only needs to perform these >> checks (as well as the multiplication itself) in case the array needs >> to >> be reallocated.) >> b) It allows to limit the number of elements to an upper bound given >> by the caller. This allows to restrict the number of allocated >> elements >> to fit into an int and therefore makes this function usable with >> counters of this type. It can also be used to avoid overflow checks >> in >> the caller: E.g. setting it to UINT_MAX - 1 elements makes it safe to >> increase the desired number of elements in steps of one. And it >> avoids >> overallocations in situations where one already has an upper bound. >> c) av_fast_realloc_array() will always allocate in multiples of array >> elements; no memory is wasted with partial elements. >> d) By returning an int, av_fast_realloc_array() can distinguish >> between >> ordinary allocation failures (meriting AVERROR(ENOMEM)) and failures >> because of allocation limits (by returning AVERROR(ERANGE)). >> e) It is no longer possible for the user to accidentally lose the >> pointer by using ptr = av_fast_realloc(ptr, ...). > > If you add an option for clearing the newly allocated memory then this > could work for my av_fast_recalloc() use case in the jpeg2000 decoder. > Or we could have two functions. > I'd prefer it if the zeroing function were a wrapper around the non-zeroing function. > Small bikeshed: since the function takes a pointer to a pointer as > argument, av_fast_realloc_arrayp() might be a better name. I had in > mind to similarly rename av_fast_recalloc() to av_fast_recallocp(). > > >> + >> + nb = min_nb + (min_nb + 14) / 16; > > Not +15? Or +0? "av_fast_realloc_array() instead allocates nb + (nb + 14) / 16. Rounding up is done in order not to reallocate in steps of one if the current number is < 16; adding 14 instead of 15 has the effect of only allocating one element if one element is desired." > >> + >> + /* If min_nb is so big that the above calculation overflowed, >> + * just allocate as much as we are allowed to. */ >> + nb = nb < min_nb ? max_nb : FFMIN(nb, max_nb); > > Looks OK, but an explicit check for overflow is easier to verify > >> + >> + memcpy(&array, ptr, sizeof(array)); >> + >> + array = av_realloc(array, nb * elsize); >> + if (!array) >> + return AVERROR(ENOMEM); >> + >> + memcpy(ptr, &array, sizeof(array)); > > An optional memset() here would be useful for me > > Else it looks OK > _______________________________________________ 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".
next prev parent reply other threads:[~2022-07-06 14:46 UTC|newest] Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-07-05 20:09 [FFmpeg-devel] [PATCH 1/8] avutil/mem: Handle fast allocations near UINT_MAX properly Andreas Rheinhardt 2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 2/8] avformat/flvenc: Add deinit function Andreas Rheinhardt 2022-07-06 2:28 ` Steven Liu 2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 3/8] avutil/mem: Add av_fast_realloc_array() Andreas Rheinhardt 2022-07-06 14:40 ` Tomas Härdin 2022-07-06 14:46 ` Andreas Rheinhardt [this message] 2022-07-06 14:54 ` Tomas Härdin 2022-07-12 14:12 ` Andreas Rheinhardt 2022-07-14 8:14 ` Anton Khirnov 2022-07-14 12:51 ` Andreas Rheinhardt 2022-07-17 8:30 ` Anton Khirnov 2022-09-26 12:25 ` Andreas Rheinhardt 2022-09-26 14:21 ` Andreas Rheinhardt 2022-09-26 14:24 ` Tomas Härdin 2022-09-27 15:23 ` Tomas Härdin 2022-09-28 9:35 ` Tomas Härdin 2022-09-28 11:06 ` Andreas Rheinhardt 2022-09-28 11:41 ` Tomas Härdin 2022-07-21 21:23 ` Tomas Härdin 2022-08-17 15:29 ` Anton Khirnov 2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 4/8] avformat/flvenc: Use array instead of linked list for index Andreas Rheinhardt 2022-07-06 14:58 ` Tomas Härdin 2022-07-06 15:03 ` Andreas Rheinhardt 2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 5/8] avformat/matroskaenc: Use av_fast_realloc_array for index entries Andreas Rheinhardt 2022-07-06 15:03 ` Tomas Härdin 2022-07-06 15:10 ` Andreas Rheinhardt 2022-07-06 15:21 ` Tomas Härdin 2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 6/8] avcodec/movtextenc: Use av_fast_realloc_array Andreas Rheinhardt 2022-07-06 15:06 ` Tomas Härdin 2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 7/8] avutil/fifo: Simplify growing FIFO Andreas Rheinhardt 2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 8/8] avutil/fifo: Grow FIFO faster when growing automatically Andreas Rheinhardt 2022-07-06 13:02 ` [FFmpeg-devel] [PATCH 1/8] avutil/mem: Handle fast allocations near UINT_MAX properly Anton Khirnov 2022-07-06 13:08 ` Andreas Rheinhardt 2022-07-06 13:17 ` Anton Khirnov 2022-07-06 14:24 ` Tomas Härdin 2022-07-06 14:40 ` Andreas Rheinhardt 2022-08-17 14:31 ` Tomas Härdin 2022-09-26 11:50 ` Tomas Härdin
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=DB6PR0101MB2214D873CB5422DEE2051F418F809@DB6PR0101MB2214.eurprd01.prod.exchangelabs.com \ --to=andreas.rheinhardt@outlook.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git