diff mbox series

[FFmpeg-devel,21/35] fftools: add an object pool

Message ID 20220616195534.5278-21-anton@khirnov.net
State Accepted
Commit 9ac78fb347e26270ebaf5ed41e55d34065853583
Headers show
Series [FFmpeg-devel,01/35] fftools/ffmpeg_mux: add private muxer context | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Anton Khirnov June 16, 2022, 7:55 p.m. UTC
Allows to avoid constantly allocating and freeing objects like AVFrame
or AVPacket.
---
 fftools/objpool.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++
 fftools/objpool.h |  37 +++++++++++++
 2 files changed, 168 insertions(+)
 create mode 100644 fftools/objpool.c
 create mode 100644 fftools/objpool.h

Comments

Andreas Rheinhardt June 16, 2022, 9:41 p.m. UTC | #1
Anton Khirnov:
> Allows to avoid constantly allocating and freeing objects like AVFrame
> or AVPacket.
> ---
>  fftools/objpool.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++
>  fftools/objpool.h |  37 +++++++++++++
>  2 files changed, 168 insertions(+)
>  create mode 100644 fftools/objpool.c
>  create mode 100644 fftools/objpool.h
> 
> diff --git a/fftools/objpool.c b/fftools/objpool.c
> new file mode 100644
> index 0000000000..b1561ecd69
> --- /dev/null
> +++ b/fftools/objpool.c
> @@ -0,0 +1,131 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include <stdint.h>
> +
> +#include "libavcodec/packet.h"
> +
> +#include "libavutil/common.h"
> +#include "libavutil/error.h"
> +#include "libavutil/frame.h"
> +#include "libavutil/mem.h"
> +
> +#include "objpool.h"
> +
> +struct ObjPool {
> +    void        *pool[32];
> +    unsigned int pool_count;
> +
> +    ObjPoolCBAlloc alloc;
> +    ObjPoolCBReset reset;
> +    ObjPoolCBFree  free;
> +};
> +
> +ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset,
> +                       ObjPoolCBFree cb_free)
> +{
> +    ObjPool *op = av_mallocz(sizeof(*op));
> +
> +    if (!op)
> +        return NULL;
> +
> +    op->alloc = cb_alloc;
> +    op->reset = cb_reset;
> +    op->free  = cb_free;
> +
> +    return op;
> +}
> +
> +void objpool_free(ObjPool **pop)
> +{
> +    ObjPool *op = *pop;
> +
> +    if (!op)
> +        return;
> +
> +    for (unsigned int i = 0; i < op->pool_count; i++)
> +        op->free(&op->pool[i]);
> +
> +    av_freep(pop);
> +}
> +
> +int  objpool_get(ObjPool *op, void **obj)
> +{
> +    if (op->pool_count) {
> +        *obj = op->pool[--op->pool_count];
> +        op->pool[op->pool_count] = NULL;
> +    } else
> +        *obj = op->alloc();
> +
> +    return *obj ? 0 : AVERROR(ENOMEM);
> +}
> +
> +void objpool_release(ObjPool *op, void **obj)
> +{
> +    if (!*obj)
> +        return;
> +
> +    op->reset(*obj);
> +
> +    if (op->pool_count < FF_ARRAY_ELEMS(op->pool))
> +        op->pool[op->pool_count++] = *obj;
> +    else
> +        op->free(obj);
> +
> +    *obj = NULL;
> +}
> +
> +static void *alloc_packet(void)
> +{
> +    return av_packet_alloc();
> +}
> +static void *alloc_frame(void)
> +{
> +    return av_frame_alloc();
> +}
> +
> +static void reset_packet(void *obj)
> +{
> +    return av_packet_unref(obj);
> +}
> +static void reset_frame(void *obj)
> +{
> +    return av_frame_unref(obj);
> +}
> +
> +static void free_packet(void **obj)
> +{
> +    AVPacket *pkt = *obj;
> +    av_packet_free(&pkt);
> +    *obj = NULL;
> +}
> +static void free_frame(void **obj)
> +{
> +    AVFrame *frame = *obj;
> +    av_frame_free(&frame);
> +    *obj = NULL;
> +}
> +
> +ObjPool *objpool_alloc_packets(void)
> +{
> +    return objpool_alloc(alloc_packet, reset_packet, free_packet);
> +}
> +ObjPool *objpool_alloc_frames(void)
> +{
> +    return objpool_alloc(alloc_frame, reset_frame, free_frame);
> +}
> diff --git a/fftools/objpool.h b/fftools/objpool.h
> new file mode 100644
> index 0000000000..1b2aea6aca
> --- /dev/null
> +++ b/fftools/objpool.h
> @@ -0,0 +1,37 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#ifndef FFTOOLS_OBJPOOL_H
> +#define FFTOOLS_OBJPOOL_H
> +
> +typedef struct ObjPool ObjPool;
> +
> +typedef void* (*ObjPoolCBAlloc)(void);
> +typedef void  (*ObjPoolCBReset)(void *);
> +typedef void  (*ObjPoolCBFree)(void **);
> +
> +void     objpool_free(ObjPool **op);
> +ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset,
> +                       ObjPoolCBFree cb_free);
> +ObjPool *objpool_alloc_packets(void);
> +ObjPool *objpool_alloc_frames(void);
> +
> +int  objpool_get(ObjPool *op, void **obj);
> +void objpool_release(ObjPool *op, void **obj);
> +
> +#endif // FFTOOLS_OBJPOOL_H

AVFifos are often used with non-POD elements that need custom init,
reset (unref) and free callbacks (in addition to the move callbacks
already supported). So why not add it to AVFifo? The only drawback to
this that I see is that the pool could not be shared among multiple
AVFifos, but apart from that it should support the usecases that you
propose and do so in a way that avoids having to drain the fifos
manually when freeing it.

- Andreas
Anton Khirnov July 22, 2022, 3:39 p.m. UTC | #2
Hi,
Quoting Andreas Rheinhardt (2022-06-16 23:41:36)
> AVFifos are often used with non-POD elements that need custom init,
> reset (unref) and free callbacks (in addition to the move callbacks
> already supported). So why not add it to AVFifo? The only drawback to
> this that I see is that the pool could not be shared among multiple
> AVFifos, but apart from that it should support the usecases that you
> propose and do so in a way that avoids having to drain the fifos
> manually when freeing it.

Since neither of us seems to have much time to deal with this right now
--- would you object to me pushing this set as is (Michael confirmed on
IRC he is done testing it) and leaving the objpool question for later?
Objpool is fully private, so it can always be changed or removed at
will.
Andreas Rheinhardt July 22, 2022, 5:58 p.m. UTC | #3
Anton Khirnov:
> Hi,
> Quoting Andreas Rheinhardt (2022-06-16 23:41:36)
>> AVFifos are often used with non-POD elements that need custom init,
>> reset (unref) and free callbacks (in addition to the move callbacks
>> already supported). So why not add it to AVFifo? The only drawback to
>> this that I see is that the pool could not be shared among multiple
>> AVFifos, but apart from that it should support the usecases that you
>> propose and do so in a way that avoids having to drain the fifos
>> manually when freeing it.
> 
> Since neither of us seems to have much time to deal with this right now
> --- would you object to me pushing this set as is (Michael confirmed on
> IRC he is done testing it) and leaving the objpool question for later?
> Objpool is fully private, so it can always be changed or removed at
> will.
> 

I don't object to this approach. Sorry for not having time to do any
testing/reviewing.

- Andreas
diff mbox series

Patch

diff --git a/fftools/objpool.c b/fftools/objpool.c
new file mode 100644
index 0000000000..b1561ecd69
--- /dev/null
+++ b/fftools/objpool.c
@@ -0,0 +1,131 @@ 
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdint.h>
+
+#include "libavcodec/packet.h"
+
+#include "libavutil/common.h"
+#include "libavutil/error.h"
+#include "libavutil/frame.h"
+#include "libavutil/mem.h"
+
+#include "objpool.h"
+
+struct ObjPool {
+    void        *pool[32];
+    unsigned int pool_count;
+
+    ObjPoolCBAlloc alloc;
+    ObjPoolCBReset reset;
+    ObjPoolCBFree  free;
+};
+
+ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset,
+                       ObjPoolCBFree cb_free)
+{
+    ObjPool *op = av_mallocz(sizeof(*op));
+
+    if (!op)
+        return NULL;
+
+    op->alloc = cb_alloc;
+    op->reset = cb_reset;
+    op->free  = cb_free;
+
+    return op;
+}
+
+void objpool_free(ObjPool **pop)
+{
+    ObjPool *op = *pop;
+
+    if (!op)
+        return;
+
+    for (unsigned int i = 0; i < op->pool_count; i++)
+        op->free(&op->pool[i]);
+
+    av_freep(pop);
+}
+
+int  objpool_get(ObjPool *op, void **obj)
+{
+    if (op->pool_count) {
+        *obj = op->pool[--op->pool_count];
+        op->pool[op->pool_count] = NULL;
+    } else
+        *obj = op->alloc();
+
+    return *obj ? 0 : AVERROR(ENOMEM);
+}
+
+void objpool_release(ObjPool *op, void **obj)
+{
+    if (!*obj)
+        return;
+
+    op->reset(*obj);
+
+    if (op->pool_count < FF_ARRAY_ELEMS(op->pool))
+        op->pool[op->pool_count++] = *obj;
+    else
+        op->free(obj);
+
+    *obj = NULL;
+}
+
+static void *alloc_packet(void)
+{
+    return av_packet_alloc();
+}
+static void *alloc_frame(void)
+{
+    return av_frame_alloc();
+}
+
+static void reset_packet(void *obj)
+{
+    return av_packet_unref(obj);
+}
+static void reset_frame(void *obj)
+{
+    return av_frame_unref(obj);
+}
+
+static void free_packet(void **obj)
+{
+    AVPacket *pkt = *obj;
+    av_packet_free(&pkt);
+    *obj = NULL;
+}
+static void free_frame(void **obj)
+{
+    AVFrame *frame = *obj;
+    av_frame_free(&frame);
+    *obj = NULL;
+}
+
+ObjPool *objpool_alloc_packets(void)
+{
+    return objpool_alloc(alloc_packet, reset_packet, free_packet);
+}
+ObjPool *objpool_alloc_frames(void)
+{
+    return objpool_alloc(alloc_frame, reset_frame, free_frame);
+}
diff --git a/fftools/objpool.h b/fftools/objpool.h
new file mode 100644
index 0000000000..1b2aea6aca
--- /dev/null
+++ b/fftools/objpool.h
@@ -0,0 +1,37 @@ 
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef FFTOOLS_OBJPOOL_H
+#define FFTOOLS_OBJPOOL_H
+
+typedef struct ObjPool ObjPool;
+
+typedef void* (*ObjPoolCBAlloc)(void);
+typedef void  (*ObjPoolCBReset)(void *);
+typedef void  (*ObjPoolCBFree)(void **);
+
+void     objpool_free(ObjPool **op);
+ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset,
+                       ObjPoolCBFree cb_free);
+ObjPool *objpool_alloc_packets(void);
+ObjPool *objpool_alloc_frames(void);
+
+int  objpool_get(ObjPool *op, void **obj);
+void objpool_release(ObjPool *op, void **obj);
+
+#endif // FFTOOLS_OBJPOOL_H