From patchwork Thu Jun 16 19:55:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 36277 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1128945pzb; Thu, 16 Jun 2022 13:06:56 -0700 (PDT) X-Google-Smtp-Source: AGRyM1usUvOAmgk1QAGF0a9m37hdBx1mqNFIcy/5tTd7MVSe42Ackn0+JyJjyp41Mz1gJ5K19U+V X-Received: by 2002:a17:907:a407:b0:704:34ac:835c with SMTP id sg7-20020a170907a40700b0070434ac835cmr6016645ejc.663.1655410016312; Thu, 16 Jun 2022 13:06:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655410016; cv=none; d=google.com; s=arc-20160816; b=uXrTXg6JNzo49Lg291msYyCZDw0YUQGVC7etGeM7dh+o4iX1nNGt+BpXbE0QdwY58o T85gMajGczTa2ILD+E1lI0TC9JpfCXLxRX/JbbZN+GmAX1xhbFbddsYERDVZkiJuO8PF 4Lqghk7t5KB3tGIqCg5ThyovSd4Panh5KjEJ1ja1zLdec7WngQjnFq2CkXJ56WwOW2+g hrhUzG3oHFB7kkIhJjuCARhI1JMbnrEumnj8mHovbrTnu+JViS3Ghz2S14kERZ9/M/Vw S1q3pU+vkIaZrrrsxVcmRbGVgDZDRQfV4b7cuTqxdolaxVTqsKOtS8ijstbSC6ZjT9Jo YeZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=DuDVfFGzjpslbhkidUnzaENRH/jlySZz4M1KQ0gdWus=; b=erXiVISafwKj0jHP+EZwYhTIjpnrJHAbtWnohACHNjohmiKB+UdKKA363cnn9onlsQ hpjOT8zjlJqnXB73Wexo0ZtDMw2guTXUL//eI+VfvRr1dy6kT0sE8JjC5aPudFJzG+z3 yCSfgTzTbkB5+cptCp3M3gOI6cYkcDvO46Q1bGxPouOhOKejYtKxOPUil7Z6b4l4EZNc hwiYvW7TQRrvC+/mJrk/zY1obQNv7wmbKo/XuXwkMpPuHrLo9UejhOv9nWDt5uw8JL1n C8xYWcwPlDzN1Z1DQ8hjHWwc5xz7onr6V01CQ4f0FeuAywu3hs7vnowb6CndFMMYHy+/ 0N4Q== ARC-Authentication-Results: i=1; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id v30-20020a17090651de00b00715790f7183si469472ejk.301.2022.06.16.13.06.51; Thu, 16 Jun 2022 13:06:56 -0700 (PDT) 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 7419A68B820; Thu, 16 Jun 2022 23:04:00 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EC47068B84C for ; Thu, 16 Jun 2022 23:03:45 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id AA061240175 for ; Thu, 16 Jun 2022 22:03:45 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 483E_z3lNVSe for ; Thu, 16 Jun 2022 22:03:44 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 7D49F240692 for ; Thu, 16 Jun 2022 22:03:34 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id D4A3A3A20EF; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:20 +0200 Message-Id: <20220616195534.5278-21-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220616195534.5278-1-anton@khirnov.net> References: <20220616195534.5278-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 21/35] fftools: add an object pool 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Pki1O2/Z71zB 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 + +#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