From patchwork Sat Jan 20 04:29:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muhammad Faiz X-Patchwork-Id: 7358 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.156.27 with SMTP id q27csp330570jak; Fri, 19 Jan 2018 20:36:24 -0800 (PST) X-Google-Smtp-Source: AH8x226GGpkOrJV3bBCy+06gafETvtHhqofHgPLLADLbimtFha9x9MMFMkiL51tfa7mxyfoZ/uwD X-Received: by 10.28.25.67 with SMTP id 64mr276750wmz.86.1516422984866; Fri, 19 Jan 2018 20:36:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516422984; cv=none; d=google.com; s=arc-20160816; b=tZ3EqvtqlAgaHDV9/UAP1HdsR2LXUWDsQTJYYTpUGKGhG1G3XvIa0iKI8bpjd4oIvP eX79j7SL/VdgDVHnrZuOpqr4b0devbkJw1WKY3y0itYEf3rq5wlblfGJClzdaOuC4bl0 n06FceQ+uyc6jo8vBPmCC8s/fX4fu4YQas0bBift3I1zFQkvUc3BQxNwkBQCkIxJsCXy LnlMay99o3UskBrDDEK69sMDTjGCOjOu8iv8+moBFIVgmOCmx0O5ZMLnKagDNObfRDi8 mWp0vnEfDO1PpuK3iYonFKZ+kPDY8ffkt0uEzffyhIDFCyEGlF40uYiJeSkG2SwDfwlv dlmw== 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:message-id:date:to:from :dkim-signature:delivered-to:arc-authentication-results; bh=nRgK5CGk33J5kktZgF0BaQYEr4KHAlnnEGM/h+Pn5rc=; b=qb8PoQ78rW7GlDQFTCmSzSYbZ35US1pKwi2D+8vnmr6yKS80hOVaKgEFkqF/XbVZHX ONhmKlS/EleYNBaaZZuJDd0z5cGnNfNl6S4m6Er2pPfZc2vcZvlYQf+lH6olctZV88CX zrvfb1ApHo3HY0dYO+WRtXxQCQGplU9EtFbzq1fn/JJOfIu5yKwlNhzRVXOOU/6eCY2B zQWLoCM2qU7xvBFOeZn7ul8uQXP3UtqMksw3B8W7/qk0fDtu/bHhFhseeRhSMa09yn2S Ry8wgFh+wBfqbVMXVjTasm1/zIJo6F7rWsvWipPaOtFn24D9fS3zY7v/6Q3EZObhutKs w17g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=GoqIBLLh; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id m184si1883154wmg.181.2018.01.19.20.36.20; Fri, 19 Jan 2018 20:36:24 -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; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=GoqIBLLh; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 90927689DD9; Sat, 20 Jan 2018 06:36:18 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f196.google.com (mail-pf0-f196.google.com [209.85.192.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 31860680953 for ; Sat, 20 Jan 2018 06:36:12 +0200 (EET) Received: by mail-pf0-f196.google.com with SMTP id e11so2888292pff.6 for ; Fri, 19 Jan 2018 20:36:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=VostfwL4nxl8yHvNBvty4Sb3vaujXDOpGvWpl9xGZ2g=; b=GoqIBLLh7lGvGSTC2fwU2A6BeRjszBCdlFXHefXN66vhmmWnXyj1OuMt78YFSkx+BN qJ06i2HC7gU3wsyS8WmTFnrA+KkazoNNioislbc0Eg9PstaQDCPRQi35PJ76m/mTkKr6 cjQhKqsyKzXot55CRa541TVggA5acgSt5oN2agUtGfbr9kWBwxDYgyPs7rAX4bDcswEA pdZBuCXbd7FSSqvDiaiDdOd8x2v6QFG0V9lAbabEnKSZopT9oEnbvmPGq79YGCKV8Y7I jBaKSl3Xj0njDV9Uhxsgjme8xLeY0IVRcO1MV+8h/MXcO8MldOnbk1shQdJltyvPnJ1z JPUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=VostfwL4nxl8yHvNBvty4Sb3vaujXDOpGvWpl9xGZ2g=; b=FYPClxLl8zPRdO7Ehsk+LhdqUMTmKVpDBOxrmPHoCallPNDzjS1EGa5Ex6J4kwl0aY MzlEtZWCnnhbTbaIafUs/iFR0atZkLt3neey0eChdJN8QMHGUwWzdzd0l/UAjgPxLFD7 ArSOT2eW8xIL7dwXLPxMbIjL5y4Neo9/XvUEwqJSP12fnwWj26tJHpBnEdpnj1iAH9m4 ZRBd3dXmlWVneMImDpE5Efs+440tH+zewlFPJMToI+EQIFulanOL336iEK6NhbN/0Eep pRUoiDRSSjcRRtI0g56SPgzFl4ADJzpoV/po3+/x7yUhC0poV+qG/wtnF4YMzYrxCr1h zGWQ== X-Gm-Message-State: AKwxytdWA3NG7oqL0zESzP8r2kMV2bnRY1yFLAkgct84aSFc4ENq3PBy qEWF1bj1jJXwYU/dIMTarRoEsznh X-Received: by 10.101.73.203 with SMTP id t11mr831386pgs.48.1516422573921; Fri, 19 Jan 2018 20:29:33 -0800 (PST) Received: from localhost.localdomain ([114.124.138.76]) by smtp.gmail.com with ESMTPSA id s67sm22663416pfe.133.2018.01.19.20.29.32 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Jan 2018 20:29:33 -0800 (PST) From: Muhammad Faiz To: ffmpeg-devel@ffmpeg.org Date: Sat, 20 Jan 2018 11:29:13 +0700 Message-Id: <20180120042915.9161-1-mfcc64@gmail.com> X-Mailer: git-send-email 2.13.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/3] avutil: add staticpool 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Help avoiding malloc-free cycles when allocating-freeing common structures. Signed-off-by: Muhammad Faiz --- libavutil/staticpool.h | 117 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 libavutil/staticpool.h diff --git a/libavutil/staticpool.h b/libavutil/staticpool.h new file mode 100644 index 0000000000..9c9b2784bc --- /dev/null +++ b/libavutil/staticpool.h @@ -0,0 +1,117 @@ +/* + * 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 AVUTIL_STATICPOOL_H +#define AVUTIL_STATICPOOL_H + +#include +#include "avassert.h" +#include "mem.h" + +/** + * FF_STATICPOOL allocate memory without av_malloc if possible + * @param size must be 2^n between 64 and 4096 + */ +#define FF_STATICPOOL_DECLARE(type, size) \ +typedef struct type##_StaticPoolWrapper { \ + type buf; \ + unsigned index; \ + atomic_uint next; \ +} type##_StaticPoolWrapper; \ + \ +static atomic_uint type##_staticpool_next; \ +static atomic_uint type##_staticpool_last; \ +static type##_StaticPoolWrapper type##_staticpool_table[size]; \ + \ +static type *type##_staticpool_malloc(void) \ +{ \ + unsigned val, index, serial, new_val; \ + \ + av_assert0((size) >= 64 && (size) <= 4096 && !((size) & ((size) - 1))); \ + \ + /* use serial, avoid spinlock */ \ + /* acquire, so we don't get stalled table[index].next */ \ + val = atomic_load_explicit(&type##_staticpool_next, memory_order_acquire); \ + do { \ + index = val & ((size) - 1); \ + serial = val & ~((size) - 1); \ + new_val = atomic_load_explicit(&type##_staticpool_table[index].next, memory_order_relaxed) | (serial + (size)); \ + } while (!atomic_compare_exchange_strong_explicit(&type##_staticpool_next, &val, new_val, \ + memory_order_acquire, memory_order_acquire)); \ + \ + index = val & ((size) - 1); \ + if (index) \ + return &type##_staticpool_table[index].buf; \ + \ + index = atomic_fetch_add_explicit(&type##_staticpool_last, 1, memory_order_relaxed) + 1; \ + if (index < (size)) { \ + type##_staticpool_table[index].index = index; \ + return &type##_staticpool_table[index].buf; \ + } \ + \ + atomic_fetch_add_explicit(&type##_staticpool_last, -1, memory_order_relaxed); \ + return av_malloc(sizeof(type)); \ +} \ + \ +static inline type *type##_staticpool_mallocz(void) \ +{ \ + type *ptr = type##_staticpool_malloc(); \ + if (ptr) \ + memset(ptr, 0, sizeof(*ptr)); \ + return ptr; \ +} \ + \ +static void type##_staticpool_free(type *ptr) \ +{ \ + type##_StaticPoolWrapper *entry = (type##_StaticPoolWrapper *) ptr; \ + unsigned val, serial, index, new_val; \ + \ + if ((uintptr_t)ptr <= (uintptr_t)(type##_staticpool_table) || \ + (uintptr_t)ptr >= (uintptr_t)(type##_staticpool_table + size)) { \ + av_free(ptr); \ + return; \ + } \ + \ + if (CONFIG_MEMORY_POISONING) \ + memset(&entry->buf, FF_MEMORY_POISON, sizeof(entry->buf)); \ + \ + val = atomic_load_explicit(&type##_staticpool_next, memory_order_relaxed); \ + do { \ + index = val & ((size) - 1); \ + serial = val & ~((size) - 1); \ + atomic_store_explicit(&entry->next, index, memory_order_relaxed); \ + new_val = entry->index | (serial + (size)); \ + } while (!atomic_compare_exchange_strong_explicit(&type##_staticpool_next, &val, new_val, \ + memory_order_release, memory_order_relaxed)); \ +} \ + \ +static inline void type##_staticpool_freep(type **ptr) \ +{ \ + if (ptr) { \ + type##_staticpool_free(*ptr); \ + *ptr = NULL; \ + } \ +} \ +/* FF_STATICPOOL_DECLARE */ + +#define FF_STATICPOOL_MALLOC(type) type##_staticpool_malloc() +#define FF_STATICPOOL_MALLOCZ(type) type##_staticpool_mallocz() +#define FF_STATICPOOL_FREE(type, v) type##_staticpool_free(v) +#define FF_STATICPOOL_FREEP(type, v) type##_staticpool_freep(v) + +#endif