get:
Show a patch.

patch:
Update a patch.

put:
Update a patch.

GET /api/patches/117929/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 117929,
    "url": "http://patches.dpdk.org/api/patches/117929/?format=api",
    "web_url": "http://patches.dpdk.org/project/dpdk/patch/20221011121720.2657-4-fengchengwen@huawei.com/",
    "project": {
        "id": 1,
        "url": "http://patches.dpdk.org/api/projects/1/?format=api",
        "name": "DPDK",
        "link_name": "dpdk",
        "list_id": "dev.dpdk.org",
        "list_email": "dev@dpdk.org",
        "web_url": "http://core.dpdk.org",
        "scm_url": "git://dpdk.org/dpdk",
        "webscm_url": "http://git.dpdk.org/dpdk",
        "list_archive_url": "https://inbox.dpdk.org/dev",
        "list_archive_url_format": "https://inbox.dpdk.org/dev/{}",
        "commit_url_format": ""
    },
    "msgid": "<20221011121720.2657-4-fengchengwen@huawei.com>",
    "list_archive_url": "https://inbox.dpdk.org/dev/20221011121720.2657-4-fengchengwen@huawei.com",
    "date": "2022-10-11T12:17:14",
    "name": "[v8,3/9] memarea: support alloc/free/update-refcnt API",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": true,
    "hash": "011a45b9c329d4fd3d8dc33dfc133e03fb5540b1",
    "submitter": {
        "id": 2146,
        "url": "http://patches.dpdk.org/api/people/2146/?format=api",
        "name": "fengchengwen",
        "email": "fengchengwen@huawei.com"
    },
    "delegate": {
        "id": 1,
        "url": "http://patches.dpdk.org/api/users/1/?format=api",
        "username": "tmonjalo",
        "first_name": "Thomas",
        "last_name": "Monjalon",
        "email": "thomas@monjalon.net"
    },
    "mbox": "http://patches.dpdk.org/project/dpdk/patch/20221011121720.2657-4-fengchengwen@huawei.com/mbox/",
    "series": [
        {
            "id": 25146,
            "url": "http://patches.dpdk.org/api/series/25146/?format=api",
            "web_url": "http://patches.dpdk.org/project/dpdk/list/?series=25146",
            "date": "2022-10-11T12:17:17",
            "name": "introduce memarea library",
            "version": 8,
            "mbox": "http://patches.dpdk.org/series/25146/mbox/"
        }
    ],
    "comments": "http://patches.dpdk.org/api/patches/117929/comments/",
    "check": "success",
    "checks": "http://patches.dpdk.org/api/patches/117929/checks/",
    "tags": {},
    "related": [],
    "headers": {
        "Return-Path": "<dev-bounces@dpdk.org>",
        "X-Original-To": "patchwork@inbox.dpdk.org",
        "Delivered-To": "patchwork@inbox.dpdk.org",
        "Received": [
            "from mails.dpdk.org (mails.dpdk.org [217.70.189.124])\n\tby inbox.dpdk.org (Postfix) with ESMTP id 112A6A0545;\n\tTue, 11 Oct 2022 14:23:47 +0200 (CEST)",
            "from [217.70.189.124] (localhost [127.0.0.1])\n\tby mails.dpdk.org (Postfix) with ESMTP id 4B6D642E08;\n\tTue, 11 Oct 2022 14:23:13 +0200 (CEST)",
            "from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188])\n by mails.dpdk.org (Postfix) with ESMTP id 44AF242DE2\n for <dev@dpdk.org>; Tue, 11 Oct 2022 14:23:05 +0200 (CEST)",
            "from dggpeml500024.china.huawei.com (unknown [172.30.72.53])\n by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4Mmvvd6PFBzVhps;\n Tue, 11 Oct 2022 20:18:37 +0800 (CST)",
            "from localhost.localdomain (10.67.165.24) by\n dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server\n (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id\n 15.1.2375.31; Tue, 11 Oct 2022 20:23:03 +0800"
        ],
        "From": "Chengwen Feng <fengchengwen@huawei.com>",
        "To": "<david.marchand@redhat.com>, <mb@smartsharesystems.com>,\n <anatoly.burakov@intel.com>, <dmitry.kozliuk@gmail.com>,\n <jerinjacobk@gmail.com>, <hofors@lysator.liu.se>",
        "CC": "<thomas@monjalon.net>, <dev@dpdk.org>",
        "Subject": "[PATCH v8 3/9] memarea: support alloc/free/update-refcnt API",
        "Date": "Tue, 11 Oct 2022 12:17:14 +0000",
        "Message-ID": "<20221011121720.2657-4-fengchengwen@huawei.com>",
        "X-Mailer": "git-send-email 2.17.1",
        "In-Reply-To": "<20221011121720.2657-1-fengchengwen@huawei.com>",
        "References": "<20220721044648.6817-1-fengchengwen@huawei.com>\n <20221011121720.2657-1-fengchengwen@huawei.com>",
        "MIME-Version": "1.0",
        "Content-Type": "text/plain",
        "X-Originating-IP": "[10.67.165.24]",
        "X-ClientProxiedBy": "dggems701-chm.china.huawei.com (10.3.19.178) To\n dggpeml500024.china.huawei.com (7.185.36.10)",
        "X-CFilter-Loop": "Reflected",
        "X-BeenThere": "dev@dpdk.org",
        "X-Mailman-Version": "2.1.29",
        "Precedence": "list",
        "List-Id": "DPDK patches and discussions <dev.dpdk.org>",
        "List-Unsubscribe": "<https://mails.dpdk.org/options/dev>,\n <mailto:dev-request@dpdk.org?subject=unsubscribe>",
        "List-Archive": "<http://mails.dpdk.org/archives/dev/>",
        "List-Post": "<mailto:dev@dpdk.org>",
        "List-Help": "<mailto:dev-request@dpdk.org?subject=help>",
        "List-Subscribe": "<https://mails.dpdk.org/listinfo/dev>,\n <mailto:dev-request@dpdk.org?subject=subscribe>",
        "Errors-To": "dev-bounces@dpdk.org"
    },
    "content": "This patch supports rte_memarea_alloc()/rte_memarea_free()/\nrte_memarea_update_refcnt() API.\n\nSigned-off-by: Chengwen Feng <fengchengwen@huawei.com>\n---\n doc/guides/prog_guide/memarea_lib.rst |  10 ++\n lib/memarea/memarea_private.h         |   3 +\n lib/memarea/rte_memarea.c             | 155 ++++++++++++++++++++++++++\n lib/memarea/rte_memarea.h             |  56 ++++++++++\n lib/memarea/version.map               |   3 +\n 5 files changed, 227 insertions(+)",
    "diff": "diff --git a/doc/guides/prog_guide/memarea_lib.rst b/doc/guides/prog_guide/memarea_lib.rst\nindex 85ad57145f..a9c58dc44d 100644\n--- a/doc/guides/prog_guide/memarea_lib.rst\n+++ b/doc/guides/prog_guide/memarea_lib.rst\n@@ -33,6 +33,16 @@ returns the pointer to the created memarea or ``NULL`` if the creation failed.\n \n The ``rte_memarea_destroy()`` function is used to destroy a memarea.\n \n+The ``rte_memarea_alloc()`` function is used to alloc one memory object from\n+the memarea.\n+\n+The ``rte_memarea_free()`` function is used to free one memory object which\n+allocated by ``rte_memarea_alloc()``.\n+\n+The ``rte_memarea_update_refcnt()`` function is used to update the memory\n+object's reference count, if the count reaches zero, the memory object will\n+be freed to memarea.\n+\n Reference\n ---------\n \ndiff --git a/lib/memarea/memarea_private.h b/lib/memarea/memarea_private.h\nindex 59be9c1d00..f5accf2987 100644\n--- a/lib/memarea/memarea_private.h\n+++ b/lib/memarea/memarea_private.h\n@@ -28,6 +28,9 @@ struct rte_memarea {\n \tvoid                    *area_addr;\n \tstruct memarea_elem_list elem_list;\n \tstruct memarea_elem_list free_list;\n+\n+\tuint64_t alloc_fails;\n+\tuint64_t refcnt_check_fails;\n } __rte_cache_aligned;\n \n #endif /* MEMAREA_PRIVATE_H */\ndiff --git a/lib/memarea/rte_memarea.c b/lib/memarea/rte_memarea.c\nindex 85975029e2..8ad1c0acb5 100644\n--- a/lib/memarea/rte_memarea.c\n+++ b/lib/memarea/rte_memarea.c\n@@ -4,6 +4,7 @@\n \n #include <stdio.h>\n #include <stdlib.h>\n+#include <sys/queue.h>\n \n #include <rte_common.h>\n #include <rte_log.h>\n@@ -94,6 +95,8 @@ memarea_alloc_area(const struct rte_memarea_param *init)\n \t\tptr = memarea_alloc_from_libc(init->total_sz);\n \telse if (init->source == RTE_MEMAREA_SOURCE_USER)\n \t\tptr = init->user_addr;\n+\telse if (init->source == RTE_MEMAREA_SOURCE_MEMAREA)\n+\t\tptr = rte_memarea_alloc(init->src_memarea, init->total_sz, 0);\n \n \tif (ptr == NULL)\n \t\tRTE_LOG(ERR, MEMAREA, \"memarea: %s alloc memory area fail!\\n\", init->name);\n@@ -146,6 +149,8 @@ memarea_free_area(struct rte_memarea *ma)\n \t\trte_free(ma->area_addr);\n \telse if (ma->init.source == RTE_MEMAREA_SOURCE_LIBC)\n \t\tfree(ma->area_addr);\n+\telse if (ma->init.source == RTE_MEMAREA_SOURCE_MEMAREA)\n+\t\trte_memarea_free(ma->init.src_memarea, ma->area_addr);\n }\n \n void\n@@ -156,3 +161,153 @@ rte_memarea_destroy(struct rte_memarea *ma)\n \tmemarea_free_area(ma);\n \trte_free(ma);\n }\n+\n+static inline void\n+memarea_lock(struct rte_memarea *ma)\n+{\n+\tif (ma->init.mt_safe)\n+\t\trte_spinlock_lock(&ma->lock);\n+}\n+\n+static inline void\n+memarea_unlock(struct rte_memarea *ma)\n+{\n+\tif (ma->init.mt_safe)\n+\t\trte_spinlock_unlock(&ma->lock);\n+}\n+\n+static inline bool\n+memarea_whether_add_node(size_t free_size, size_t need_size)\n+{\n+\tsize_t align_size = RTE_ALIGN_CEIL(need_size, RTE_CACHE_LINE_SIZE);\n+\treturn free_size > align_size && (free_size - align_size) > sizeof(struct memarea_elem);\n+}\n+\n+static inline void\n+memarea_add_node(struct rte_memarea *ma, struct memarea_elem *elem, size_t need_size)\n+{\n+\tsize_t align_size = RTE_ALIGN_CEIL(need_size, RTE_CACHE_LINE_SIZE);\n+\tstruct memarea_elem *new_elem;\n+\tnew_elem = (struct memarea_elem *)RTE_PTR_ADD(elem, sizeof(struct memarea_elem) +\n+\t\t\t\t\t\t\t    align_size);\n+\tnew_elem->size = elem->size - align_size - sizeof(struct memarea_elem);\n+\tnew_elem->magic = MEMAREA_AVAILABLE_ELEM_MAGIC;\n+\tnew_elem->cookie = MEMAREA_AVAILABLE_ELEM_COOKIE;\n+\tnew_elem->refcnt = 0;\n+\tTAILQ_INSERT_AFTER(&ma->elem_list, elem, new_elem, elem_node);\n+\tTAILQ_INSERT_AFTER(&ma->free_list, elem, new_elem, free_node);\n+\telem->size = align_size;\n+}\n+\n+void *\n+rte_memarea_alloc(struct rte_memarea *ma, size_t size, uint32_t cookie)\n+{\n+\tstruct memarea_elem *elem;\n+\tvoid *ptr = NULL;\n+\n+\tif (unlikely(ma == NULL || size == 0))\n+\t\treturn NULL;\n+\n+\tmemarea_lock(ma);\n+\tTAILQ_FOREACH(elem, &ma->free_list, free_node) {\n+\t\tif (unlikely(elem->magic != MEMAREA_AVAILABLE_ELEM_MAGIC))\n+\t\t\tbreak;\n+\t\tif (elem->size < size)\n+\t\t\tcontinue;\n+\t\tif (memarea_whether_add_node(elem->size, size))\n+\t\t\tmemarea_add_node(ma, elem, size);\n+\t\telem->magic = MEMAREA_ALLOCATED_ELEM_MAGIC;\n+\t\telem->cookie = cookie;\n+\t\telem->refcnt = 1;\n+\t\tTAILQ_REMOVE(&ma->free_list, elem, free_node);\n+\t\tptr = RTE_PTR_ADD(elem, sizeof(struct memarea_elem));\n+\t\tbreak;\n+\t}\n+\tif (unlikely(ptr == NULL))\n+\t\tma->alloc_fails++;\n+\tmemarea_unlock(ma);\n+\n+\treturn ptr;\n+}\n+\n+void\n+rte_memarea_free(struct rte_memarea *ma, void *ptr)\n+{\n+\trte_memarea_update_refcnt(ma, ptr, -1);\n+}\n+\n+static inline void\n+memarea_merge_node(struct rte_memarea *ma, struct memarea_elem *curr,\n+\t\t   struct memarea_elem *next, bool del_next_from_free,\n+\t\t   bool add_curr_to_free)\n+{\n+\tcurr->size += next->size + sizeof(struct memarea_elem);\n+\tnext->size = 0;\n+\tnext->magic = 0;\n+\tnext->cookie = 0;\n+\tTAILQ_REMOVE(&ma->elem_list, next, elem_node);\n+\tif (del_next_from_free)\n+\t\tTAILQ_REMOVE(&ma->free_list, next, free_node);\n+\tif (add_curr_to_free) {\n+\t\tcurr->magic = MEMAREA_AVAILABLE_ELEM_MAGIC;\n+\t\tcurr->cookie = MEMAREA_AVAILABLE_ELEM_COOKIE;\n+\t\tTAILQ_INSERT_TAIL(&ma->free_list, curr, free_node);\n+\t}\n+}\n+\n+static inline void\n+memarea_free_elem(struct rte_memarea *ma, struct memarea_elem *elem)\n+{\n+\tstruct memarea_elem *prev, *next;\n+\tbool merged = false;\n+\tprev = TAILQ_PREV(elem, memarea_elem_list, elem_node);\n+\tnext = TAILQ_NEXT(elem, elem_node);\n+\tif (prev != NULL && prev->refcnt == 0) {\n+\t\tmemarea_merge_node(ma, prev, elem, false, false);\n+\t\telem = prev;\n+\t\tmerged = true;\n+\t}\n+\tif (next != NULL && next->refcnt == 0) {\n+\t\tmemarea_merge_node(ma, elem, next, true, !merged);\n+\t\tmerged = true;\n+\t}\n+\tif (!merged) {\n+\t\telem->magic = MEMAREA_AVAILABLE_ELEM_MAGIC;\n+\t\telem->cookie = MEMAREA_AVAILABLE_ELEM_COOKIE;\n+\t\tTAILQ_INSERT_TAIL(&ma->free_list, elem, free_node);\n+\t}\n+}\n+\n+void\n+rte_memarea_update_refcnt(struct rte_memarea *ma, void *ptr, int16_t value)\n+{\n+\tstruct memarea_elem *elem = (struct memarea_elem *)RTE_PTR_SUB(ptr,\n+\t\t\t\t\t\t\t    sizeof(struct memarea_elem));\n+\n+\tif (unlikely(ma == NULL || ptr == NULL))\n+\t\treturn;\n+\n+\tmemarea_lock(ma);\n+\tif (unlikely(elem->magic != MEMAREA_ALLOCATED_ELEM_MAGIC)) {\n+\t\tRTE_LOG(ERR, MEMAREA, \"memarea: %s magic: 0x%x check fail!\\n\",\n+\t\t\tma->init.name, elem->magic);\n+\t\tmemarea_unlock(ma);\n+\t\treturn;\n+\t}\n+\n+\tif (unlikely(elem->refcnt <= 0 || elem->refcnt + value < 0)) {\n+\t\tRTE_LOG(ERR, MEMAREA,\n+\t\t\t\"memarea: %s cookie: 0x%x curr refcnt: %d update refcnt: %d check fail!\\n\",\n+\t\t\tma->init.name, elem->cookie, elem->refcnt, value);\n+\t\tma->refcnt_check_fails++;\n+\t\tif (elem->refcnt > 0)\n+\t\t\telem->refcnt += value;\n+\t\tmemarea_unlock(ma);\n+\t\treturn;\n+\t}\n+\n+\telem->refcnt += value;\n+\tif (elem->refcnt == 0)\n+\t\tmemarea_free_elem(ma, elem);\n+\tmemarea_unlock(ma);\n+}\ndiff --git a/lib/memarea/rte_memarea.h b/lib/memarea/rte_memarea.h\nindex ac2d63a4d3..f866fa7b67 100644\n--- a/lib/memarea/rte_memarea.h\n+++ b/lib/memarea/rte_memarea.h\n@@ -129,6 +129,62 @@ struct rte_memarea *rte_memarea_create(const struct rte_memarea_param *init);\n __rte_experimental\n void rte_memarea_destroy(struct rte_memarea *ma);\n \n+/**\n+ * @warning\n+ * @b EXPERIMENTAL: this API may change without prior notice.\n+ *\n+ * Allocate memory from memarea.\n+ *\n+ * Allocate one memory object from the memarea.\n+ *\n+ * @param ma\n+ *   The pointer of memarea.\n+ * @param size\n+ *   The memory size to be allocated.\n+ * @param cookie\n+ *   User-provided footprint which could used to debug memory leak.\n+ *\n+ * @return\n+ *   Non-NULL on success. Otherwise NULL is returned.\n+ */\n+__rte_experimental\n+void *rte_memarea_alloc(struct rte_memarea *ma, size_t size, uint32_t cookie);\n+\n+/**\n+ * @warning\n+ * @b EXPERIMENTAL: this API may change without prior notice.\n+ *\n+ * Free memory to memarea.\n+ *\n+ * Free one memory object to the memarea.\n+ *\n+ * @param ma\n+ *   The pointer of memarea.\n+ * @param ptr\n+ *   The pointer of memory object which need be freed.\n+ */\n+__rte_experimental\n+void rte_memarea_free(struct rte_memarea *ma, void *ptr);\n+\n+/**\n+ * @warning\n+ * @b EXPERIMENTAL: this API may change without prior notice.\n+ *\n+ * Update memory's refcnt.\n+ *\n+ * Update one memory object's refcnt.\n+ * When refcnt is updated to be zero, the memory object is freed.\n+ *\n+ * @param ma\n+ *   The pointer of memarea.\n+ * @param ptr\n+ *   The pointer of memory object which need be updated refcnt.\n+ * @param value\n+ *   The value which need be updated.\n+ */\n+__rte_experimental\n+void rte_memarea_update_refcnt(struct rte_memarea *ma, void *ptr, int16_t value);\n+\n #ifdef __cplusplus\n }\n #endif\ndiff --git a/lib/memarea/version.map b/lib/memarea/version.map\nindex f36a04d7cf..a0026fc5f9 100644\n--- a/lib/memarea/version.map\n+++ b/lib/memarea/version.map\n@@ -1,8 +1,11 @@\n EXPERIMENTAL {\n \tglobal:\n \n+\trte_memarea_alloc;\n \trte_memarea_create;\n \trte_memarea_destroy;\n+\trte_memarea_free;\n+\trte_memarea_update_refcnt;\n \n \tlocal: *;\n };\n",
    "prefixes": [
        "v8",
        "3/9"
    ]
}