From nobody Thu May 2 20:32:40 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=crudebyte.com ARC-Seal: i=1; a=rsa-sha256; t=1629983463; cv=none; d=zohomail.com; s=zohoarc; b=gHEhmIjVvmLQqoSNf1DXQIB+yFquZXZy8TiCLP3OzAR0YAzXTZqHaQ6+B0z5m78+mcLmhJ04+3yjuo4nuEpM4dtRiEReLzOcmkOtqw3OY/mnLJMTPIlRUmGUWL/arCVhtZh1ShcHc10tDgPGUSkps7P7jjXfmGZ3+DfER/2+6Zo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1629983463; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=DULwtQkszoQ8knBnrugTmJUgRsMP9wlcE+djoh6Jkn0=; b=BPv6OtJVjFLirXpxewy/z44x+DEySkhCA0fq77vPEF2bTfhbAEFnReOOC3vuVRvCE8lCHiDi5QJsqVk5MCyr/CPu8GAFjktIJa+1ED2IYi14iVodBKYmXrhunXb7c/6IZK8sMZIEkvgeZ9yDJbegLgpTnrdI7XPvMxoJsONvE+Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1629983463779974.0408040702616; Thu, 26 Aug 2021 06:11:03 -0700 (PDT) Received: from localhost ([::1]:40418 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mJF9q-0004wq-HK for importer@patchew.org; Thu, 26 Aug 2021 09:11:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48494) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <04b29e0dd19c9a71dfaa500131293f5116085a52@lizzy.crudebyte.com>) id 1mJF7a-0003OB-0a for qemu-devel@nongnu.org; Thu, 26 Aug 2021 09:08:42 -0400 Received: from lizzy.crudebyte.com ([91.194.90.13]:43223) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <04b29e0dd19c9a71dfaa500131293f5116085a52@lizzy.crudebyte.com>) id 1mJF7W-0006ZN-Oh for qemu-devel@nongnu.org; Thu, 26 Aug 2021 09:08:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=crudebyte.com; s=lizzy; h=Cc:To:Subject:Date:From:References:In-Reply-To: Message-Id:Content-Type:Content-Transfer-Encoding:MIME-Version:Content-ID: Content-Description; bh=DULwtQkszoQ8knBnrugTmJUgRsMP9wlcE+djoh6Jkn0=; b=PlIvo GxVbMMnhk33t+ylUrrPWxvTgZ5PVGzapYTE4Wgxdqe3+zti7kFZEq5ABKSVBl6hIg5pWqHjHkIg2Z ZUBIjFW2jLLZK5xKsDYsJzOQwIsZqRLOldauHco/n/bO/bY81ekvp7vY0dPhmpqqAbDk0zfSy23GY VuVsb9v3yFA3ciqyxy9m3K41A8jucoReksWB9hUMdRm5dHnoYV0wLrmBS8mBz4ZHSeKal3SeBJ7Bl bhXjLJQ417abOmgW69n4VwopU7bHHeXw4Y/WR7MYcwJ5kkPyYwt0ujOgLS9ip/W2gkK7n9yE/1YqK lD0kyUcKjhvOyXMYEpUPnFbfVdINw==; Message-Id: <04b29e0dd19c9a71dfaa500131293f5116085a52.1629982046.git.qemu_oss@crudebyte.com> In-Reply-To: References: From: Christian Schoenebeck Date: Thu, 26 Aug 2021 14:30:54 +0200 Subject: [PATCH v3 1/5] qemu/qarray.h: introduce QArray To: qemu-devel@nongnu.org Cc: Greg Kurz , Richard Henderson , Markus Armbruster Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=91.194.90.13; envelope-from=04b29e0dd19c9a71dfaa500131293f5116085a52@lizzy.crudebyte.com; helo=lizzy.crudebyte.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @crudebyte.com) X-ZM-MESSAGEID: 1629985029597100002 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Implements deep auto free of arrays while retaining common C-style squared bracket access. Main purpose of this API is to get rid of error prone individual array deallocation pathes in user code, i.e. turning something like this: void doSomething(size_t n) { Foo *foos =3D malloc(n * sizeof(Foo)); for (...) { foos[i].s =3D malloc(...); if (...) { goto out; } } out: if (...) { for (...) { /* deep deallocation */ free(foos[i].s); } /* array deallocation */ free(foos); } } into something more simple and safer like: void doSomething(size_t n) { QARRAY_REF(Foo) foos =3D NULL; QARRAY_CREATE(Foo, foos, n); for (...) { foos[i].s =3D malloc(...); if (...) { return; /* array auto freed here */ } } /* array auto freed here */ } Unlike GArray, QArray does not require special macros, function calls or struct member dereferencing to access the individual array elements: C-array =3D QArray: vs. GArray: for (...) { | for (...) { ... =3D arr[i].m; | ... =3D g_array_index(arr, Foo, i).m; arr[i].m =3D ... ; | g_array_index(arr, Foo, i).m =3D ... ; } | } So existing C-style array code can be retained with only very little changes; basically limited to replacing array allocation call and of course removing individual array deallocation pathes. In this initial version QArray only supports the concept of unique pointers, i.e. it does not support reference counting. The array (and all dynamically allocated memory of individual array elements) is auto freed once execution leaves the scope of the reference variable (unique pointer) associated with the array. Internally a flex array struct is used in combination with macros spanned over a continuous memory space for both the array's meta data (private) and the actual C-array user data (public): struct QArray##scalar_type { size_t len; /* private, hidden from user code */ scalar_type first[]; /* public, directly exposed to user code */ }; Which has the advantage that the compiler automatically takes care about correct padding, alignment and overall size for all scalar data types on all systems and that the user space exposed pointer can directly be translated back and forth between user space C-array pointer and internal QArray struct whenever needed, in a type-safe manner. This header file is released under MIT license, to allow this file being used in other C-projects as well. The common QEMU license GPL2+ might have construed a conflict for other projects. Signed-off-by: Christian Schoenebeck --- include/qemu/qarray.h | 154 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 include/qemu/qarray.h diff --git a/include/qemu/qarray.h b/include/qemu/qarray.h new file mode 100644 index 0000000000..25af97a8a6 --- /dev/null +++ b/include/qemu/qarray.h @@ -0,0 +1,154 @@ +/* + * QArray - deep auto free C-array + * + * Copyright (c) 2021 Crudebyte + * + * Authors: + * Christian Schoenebeck + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ +#ifndef QEMU_QARRAY_H +#define QEMU_QARRAY_H + +/** + * QArray provides a mechanism to access arrays in common C-style (e.g. by + * square bracket [] operator) in conjunction with reference variables that + * perform deep auto free of the array when leaving the scope of the auto + * reference variable. That means not only is the array itself automatical= ly + * freed, but also memory dynamically allocated by the individual array + * elements. + * + * Example: + * + * Consider the following user struct @c Foo which shall be used as scalar + * (element) type of an array: + * @code + * typedef struct Foo { + * int i; + * char *s; + * } Foo; + * @endcode + * and assume it has the following function to free memory allocated by @c= Foo + * instances: + * @code + * void free_foo(Foo *foo) { + * free(foo->s); + * } + * @endcode + * Add the following to a shared header file: + * @code + * DECLARE_QARRAY_TYPE(Foo); + * @endcode + * and the following to a C unit file: + * @code + * DEFINE_QARRAY_TYPE(Foo, free_foo); + * @endcode + * Finally the array may then be used like this: + * @code + * void doSomething(size_t n) { + * QARRAY_REF(Foo) foos =3D NULL; + * QARRAY_CREATE(Foo, foos, n); + * for (size_t i =3D 0; i < n; ++i) { + * foos[i].i =3D i; + * foos[i].s =3D calloc(4096, 1); + * snprintf(foos[i].s, 4096, "foo %d", i); + * if (...) { + * return; // array auto freed here + * } + * } + * // array auto freed here + * } + * @endcode + */ + +/** + * Declares an array type for the passed @a scalar_type. + * + * This is typically used from a shared header file. + * + * @param scalar_type - type of the individual array elements + */ +#define DECLARE_QARRAY_TYPE(scalar_type) \ + typedef struct QArray##scalar_type { \ + size_t len; \ + scalar_type first[]; \ + } QArray##scalar_type; \ + \ + void qarray_create_##scalar_type(scalar_type **auto_var, size_t len); \ + void qarray_auto_free_##scalar_type(scalar_type **auto_var); \ + +/** + * Defines an array type for the passed @a scalar_type and appropriate + * @a scalar_cleanup_func. + * + * This is typically used from a C unit file. + * + * @param scalar_type - type of the individual array elements + * @param scalar_cleanup_func - appropriate function to free memory dynami= cally + * allocated by individual array elements bef= ore + */ +#define DEFINE_QARRAY_TYPE(scalar_type, scalar_cleanup_func) \ + void qarray_create_##scalar_type(scalar_type **auto_var, size_t len) \ + { \ + qarray_auto_free_##scalar_type(auto_var); \ + QArray##scalar_type *arr =3D g_malloc0(sizeof(QArray##scalar_type)= + \ + len * sizeof(scalar_type)); \ + arr->len =3D len; \ + *auto_var =3D &arr->first[0]; \ + } \ + \ + void qarray_auto_free_##scalar_type(scalar_type **auto_var) \ + { \ + scalar_type *first =3D (*auto_var); \ + if (!first) { \ + return; \ + } \ + QArray##scalar_type *arr =3D (QArray##scalar_type *) ( \ + ((char *)first) - offsetof(QArray##scalar_type, first) \ + ); \ + for (size_t i =3D 0; i < arr->len; ++i) { \ + scalar_cleanup_func(&arr->first[i]); \ + } \ + g_free(arr); \ + } \ + +/** + * Used to declare a reference variable (unique pointer) for an array. Aft= er + * leaving the scope of the reference variable, the associated array is + * automatically freed. + * + * @param scalar_type - type of the individual array elements + */ +#define QARRAY_REF(scalar_type) \ + __attribute((__cleanup__(qarray_auto_free_##scalar_type))) scalar_type* + +/** + * Allocates a new array of passed @a scalar_type with @a len number of ar= ray + * elements and assigns the created array to the reference variable + * @a auto_var. + * + * @param scalar_type - type of the individual array elements + * @param auto_var - destination reference variable + * @param len - amount of array elements to be allocated immediately + */ +#define QARRAY_CREATE(scalar_type, auto_var, len) \ + qarray_create_##scalar_type((&auto_var), len) + +#endif /* QEMU_QARRAY_H */ --=20 2.20.1 From nobody Thu May 2 20:32:40 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=crudebyte.com ARC-Seal: i=1; a=rsa-sha256; t=1629983640; cv=none; d=zohomail.com; s=zohoarc; b=GG5LgmJhIqQpOnyaWgEzf5AU5s+jmT/xGQYYBk98cHFfAPaVETdV6njqqP58v5jV8FI+e9yRBq01zfEFfH5eBwQcPCxBHWFerDD65VT97k43UUGbrDwo9cJgUnmQNM3sz9uiT92z3cFZmbmgSG2kEw/QscVu0ExYk+UdX/4ZLK0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1629983640; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=QM7jlm+lK73XJkbRnxBjKjHRqXA4X8qtnPqwh2ND8bk=; b=J22uvS1yehnIVNBdQdYbIZnz+BCEP816muxlWqUbDkQfI1qOqlgCjWgLSYE/RcPCUiQI5CTdMd9Z3VkqZMelhXvWhQ2+SgcxTOsX157XgokWEXB6KZci1WZCH3R0LFWd1qtwkUsXdLtbZH5U+X2ZxeJEj02bhnLw2QCsM0Yg5rk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 162998364052365.38900150091774; Thu, 26 Aug 2021 06:14:00 -0700 (PDT) Received: from localhost ([::1]:45380 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mJFCh-0000EB-HH for importer@patchew.org; Thu, 26 Aug 2021 09:13:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48534) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mJF7h-0003Tl-Va for qemu-devel@nongnu.org; Thu, 26 Aug 2021 09:08:53 -0400 Received: from lizzy.crudebyte.com ([91.194.90.13]:59459) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mJF7e-0006dh-3a for qemu-devel@nongnu.org; Thu, 26 Aug 2021 09:08:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=crudebyte.com; s=lizzy; h=Cc:To:Subject:Date:From:References:In-Reply-To: Message-Id:Content-Type:Content-Transfer-Encoding:MIME-Version:Content-ID: Content-Description; bh=QM7jlm+lK73XJkbRnxBjKjHRqXA4X8qtnPqwh2ND8bk=; b=bgKYp aqmhA1jmUOk5TLXbWhEkxsA7eDFxkfTWqgkhpHEmvsssJOlxsRWvAoCW95hWm1hHXHkKDmmNU68ML DwkNIUepfgABPGDrGzeg8ynlWRhUk8RLE3uN5uRL5fZcVh1oSS52P+WdLjTLFHbW3qb59+YxJt1tC +iqx7Dur5ubuGjzOGLGTNSQqOgWegKhr3HQXyBFrtnSNYKJbckZjKmLQBay9aGvh1qyt85GRjUy97 6wDfo+yhFJak6PatVA4EMj/CpnR7p2SFgQzNH/jM2DopM4tDt/kUhOVb4TDYOir2wizllgye5ZpPv Yi0M71sH2bkVN1SnDRtacAUVAI7og==; Message-Id: In-Reply-To: References: From: Christian Schoenebeck Date: Thu, 26 Aug 2021 14:31:20 +0200 Subject: [PATCH v3 2/5] qemu/qarray.h: check scalar type in QARRAY_CREATE() To: qemu-devel@nongnu.org Cc: Greg Kurz , Richard Henderson , Markus Armbruster Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=91.194.90.13; envelope-from=bc5af750d9edfe8698db5d881013d9ac93bb2caf@lizzy.crudebyte.com; helo=lizzy.crudebyte.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @crudebyte.com) X-ZM-MESSAGEID: 1629983760239100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Make sure at compile time that the scalar type of the array requested to be created via QARRAY_CREATE() matches the scalar type of the passed auto reference variable (unique pointer). Suggested-by: Richard Henderson Signed-off-by: Christian Schoenebeck --- include/qemu/qarray.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/qemu/qarray.h b/include/qemu/qarray.h index 25af97a8a6..7adf28c03c 100644 --- a/include/qemu/qarray.h +++ b/include/qemu/qarray.h @@ -27,6 +27,8 @@ #ifndef QEMU_QARRAY_H #define QEMU_QARRAY_H =20 +#include "qemu/compiler.h" + /** * QArray provides a mechanism to access arrays in common C-style (e.g. by * square bracket [] operator) in conjunction with reference variables that @@ -149,6 +151,10 @@ * @param len - amount of array elements to be allocated immediately */ #define QARRAY_CREATE(scalar_type, auto_var, len) \ + QEMU_BUILD_BUG_MSG( \ + !__builtin_types_compatible_p(scalar_type, typeof(*auto_var)), \ + "QArray scalar type mismatch" \ + ); \ qarray_create_##scalar_type((&auto_var), len) =20 #endif /* QEMU_QARRAY_H */ --=20 2.20.1 From nobody Thu May 2 20:32:40 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=crudebyte.com ARC-Seal: i=1; a=rsa-sha256; t=1629983794; cv=none; d=zohomail.com; s=zohoarc; b=ZjEF3zUlqpWGHpEIkaTz6b2U774F17C7Pc39oOl9z8ujCt6M+OC0JrFuDMlyLZ9bhqG+DxVp1v7cISrpJD/Wu4qTf8F+Ab7xu9TUiqQLIeKEiH+a40dfmpOr/3qXRGIy2omVZ0GPZJMl1eztqo4CDqA/+IpxxwJNaIMxe9nCpKg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1629983794; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=7V7AtSA/GGpLfVhFDdKh0FTfh0YNxsRShVikFoZsWHQ=; b=Gb8K+K74x1bahh9VWjQIEpiEZ/czqziPFFauGPFbbHsSNrPh6uCVCcW9sUIv0JjWBTSvEwg6HHBaQjoCRXNCscj5qD6/KTyRPMyYnEA/fV8mzOgGROUb53cUmRDICA86IZnQndZ2bjKcK1IZNPxbyrAfmRuzCQ7+IgbPohislsc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1629983794266201.83396617439053; Thu, 26 Aug 2021 06:16:34 -0700 (PDT) Received: from localhost ([::1]:49794 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mJFFB-0003UT-2p for importer@patchew.org; Thu, 26 Aug 2021 09:16:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48556) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <7b71cae8424b62c78461047ae722d74122e3ecb8@lizzy.crudebyte.com>) id 1mJF7l-0003UY-Ud for qemu-devel@nongnu.org; Thu, 26 Aug 2021 09:08:55 -0400 Received: from lizzy.crudebyte.com ([91.194.90.13]:49007) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <7b71cae8424b62c78461047ae722d74122e3ecb8@lizzy.crudebyte.com>) id 1mJF7k-0006jd-K3 for qemu-devel@nongnu.org; Thu, 26 Aug 2021 09:08:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=crudebyte.com; s=lizzy; h=Cc:To:Subject:Date:From:References:In-Reply-To: Message-Id:Content-Type:Content-Transfer-Encoding:MIME-Version:Content-ID: Content-Description; bh=7V7AtSA/GGpLfVhFDdKh0FTfh0YNxsRShVikFoZsWHQ=; b=Y8tpZ h37+okSrtFLFHpRXfryLhi6vAGAGGpQ5hDpnCBj7Vooct1j3llbcXWo9LVy9vO1AT5+DVaYplWvMr ArDZQty9zdEr70RaPIIfAtftA1n1NblsJW/527Czm9X83/o2NSChVPcKXbOhwjcylSW/2sA5JZq/N ppWDsV4InAc5YjrwUsV6UTL/hAMhMe1NUQ7twelSqFk7bKjy3KNoGCN+51yq1MkK1+b7qDskoLD69 Z20S/DbnAqqMqP+J3TPn7S1SbbOrPYYAebakdLkL+Bb218DXIo1Kc32SvVMgr6Kqrf1MiXM3vYNpG HJ6oNk2zCq3rKcyBcdG1jaT7fltkg==; Message-Id: <7b71cae8424b62c78461047ae722d74122e3ecb8.1629982046.git.qemu_oss@crudebyte.com> In-Reply-To: References: From: Christian Schoenebeck Date: Thu, 26 Aug 2021 14:31:43 +0200 Subject: [PATCH v3 3/5] 9pfs: make V9fsString usable via QArray API To: qemu-devel@nongnu.org Cc: Greg Kurz , Richard Henderson , Markus Armbruster Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=91.194.90.13; envelope-from=7b71cae8424b62c78461047ae722d74122e3ecb8@lizzy.crudebyte.com; helo=lizzy.crudebyte.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @crudebyte.com) X-ZM-MESSAGEID: 1629985201922100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Christian Schoenebeck --- fsdev/9p-marshal.c | 2 ++ fsdev/9p-marshal.h | 3 +++ 2 files changed, 5 insertions(+) diff --git a/fsdev/9p-marshal.c b/fsdev/9p-marshal.c index a01bba6908..fbfc2a62cd 100644 --- a/fsdev/9p-marshal.c +++ b/fsdev/9p-marshal.c @@ -18,6 +18,8 @@ =20 #include "9p-marshal.h" =20 +DEFINE_QARRAY_TYPE(V9fsString, v9fs_string_free); + void v9fs_string_free(V9fsString *str) { g_free(str->data); diff --git a/fsdev/9p-marshal.h b/fsdev/9p-marshal.h index ceaf2f521e..7229e4e617 100644 --- a/fsdev/9p-marshal.h +++ b/fsdev/9p-marshal.h @@ -1,10 +1,13 @@ #ifndef QEMU_9P_MARSHAL_H #define QEMU_9P_MARSHAL_H =20 +#include "qemu/qarray.h" + typedef struct V9fsString { uint16_t size; char *data; } V9fsString; +DECLARE_QARRAY_TYPE(V9fsString); =20 typedef struct V9fsQID { uint8_t type; --=20 2.20.1 From nobody Thu May 2 20:32:40 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=crudebyte.com ARC-Seal: i=1; a=rsa-sha256; t=1629983642; cv=none; d=zohomail.com; s=zohoarc; b=G/1O517tmROljXPjxF+WABZnRCJzxyOVmIX/35uHS+R3JEbVlOPD1ECf9uOtUtzMaslmYUnTiwsl/nz2i44le0ccpdMxYiZ+vw75RCdlfPLOGk3CxNI1Gi/21Fb+ZP7NmWmnPEKAzLMppzNdvN4OlVJv7FuuQNqlgIM27hsLEMc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1629983642; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=vQaRlv4ehwFofDzfKsbV8n12CLuEweQkA+Zqbl7DPSM=; b=WLYkJLgKgW7xyzGwnNnzPVnfOwqbROkSoc+wOaG7+EIEi6Ju+W+lR/UXQ/U7HvU01YHgHxOH8hhBB52djGfRzaYSXuN7K1E7qJpy3se66WkOuYJxKBoUumKxt+At24ksNTslpyghN7iQNBaTXxhsWIpibdtvGRi0U2t7Eb49fkE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1629983642430209.2479349431385; Thu, 26 Aug 2021 06:14:02 -0700 (PDT) Received: from localhost ([::1]:45504 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mJFCj-0000JN-Bk for importer@patchew.org; Thu, 26 Aug 2021 09:14:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48594) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <462b178cb715f0ed889cda49c10e834df5ba84d6@lizzy.crudebyte.com>) id 1mJF7w-0003X1-RG for qemu-devel@nongnu.org; Thu, 26 Aug 2021 09:09:05 -0400 Received: from lizzy.crudebyte.com ([91.194.90.13]:57419) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <462b178cb715f0ed889cda49c10e834df5ba84d6@lizzy.crudebyte.com>) id 1mJF7t-0006oR-9L for qemu-devel@nongnu.org; Thu, 26 Aug 2021 09:09:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=crudebyte.com; s=lizzy; h=Cc:To:Subject:Date:From:References:In-Reply-To: Message-Id:Content-Type:Content-Transfer-Encoding:MIME-Version:Content-ID: Content-Description; bh=vQaRlv4ehwFofDzfKsbV8n12CLuEweQkA+Zqbl7DPSM=; b=nAlzh wsesPDdSDDa9eA/4wBUF6SE9iL+t/zvC2CAubtdLrkt4za8108ATyXlWyrexaqLJQYUEjJyw9rtP9 +B1deePEPUEnqoLr3hd0xQag2aJb7IG4+lWUvtMhPkmIOijt4XXHtg6hHbWWvBVkdI2Z7skRlJ2bl Gl9iXglCopU0VbpgfNDxxki+34b71/e/NvzJb1XLxb4Bqcr4IyDlB1m/LA/MDCMe36D4f22ys7DaP SFNo5E3t585kHAJy764N9G65dyGvB80OWNp+ecieOefwAFGzEyUkrfykQW9UFUs2wwQLq6Rwk+E7m WYvhF599tjXsGruHk/W3isrEDvUXw==; Message-Id: <462b178cb715f0ed889cda49c10e834df5ba84d6.1629982046.git.qemu_oss@crudebyte.com> In-Reply-To: References: From: Christian Schoenebeck Date: Thu, 26 Aug 2021 14:31:55 +0200 Subject: [PATCH v3 4/5] 9pfs: make V9fsPath usable via QArray API To: qemu-devel@nongnu.org Cc: Greg Kurz , Richard Henderson , Markus Armbruster Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=91.194.90.13; envelope-from=462b178cb715f0ed889cda49c10e834df5ba84d6@lizzy.crudebyte.com; helo=lizzy.crudebyte.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @crudebyte.com) X-ZM-MESSAGEID: 1629983762958100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Christian Schoenebeck --- fsdev/file-op-9p.h | 2 ++ hw/9pfs/9p.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/fsdev/file-op-9p.h b/fsdev/file-op-9p.h index 42f677cf38..7630f0e538 100644 --- a/fsdev/file-op-9p.h +++ b/fsdev/file-op-9p.h @@ -18,6 +18,7 @@ #include #include #include "qemu-fsdev-throttle.h" +#include "qemu/qarray.h" =20 #define SM_LOCAL_MODE_BITS 0600 #define SM_LOCAL_DIR_MODE_BITS 0700 @@ -105,6 +106,7 @@ struct V9fsPath { uint16_t size; char *data; }; +DECLARE_QARRAY_TYPE(V9fsPath); =20 typedef union V9fsFidOpenState V9fsFidOpenState; =20 diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c index c857b31321..b59572fa79 100644 --- a/hw/9pfs/9p.c +++ b/hw/9pfs/9p.c @@ -50,6 +50,8 @@ enum { Oappend =3D 0x80, }; =20 +DEFINE_QARRAY_TYPE(V9fsPath, v9fs_path_free); + static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, .= ..) { ssize_t ret; --=20 2.20.1 From nobody Thu May 2 20:32:40 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=crudebyte.com ARC-Seal: i=1; a=rsa-sha256; t=1629984018; cv=none; d=zohomail.com; s=zohoarc; b=newim+ACD9AFWfpS0D7ac+8/aec2ivKq+yBEcg/yyp8E5upNCctKkE9DJKL6bx2x25ltiBJ+fxAJXQfs3mNEQMHxkALYzxQsjK+MtD3Ql/Gjg/Bn7HzTgXwLP0iq2UEsOq7BR7qJuJFaShE/XntLj0hT+uRb8VDZ3lltCy8hlhw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1629984018; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=qnPVCqg4uoyWvT+bOoAAfsKYYVyyOh1vHtIPpMdIT2M=; b=IU4WIhh8CbyS+iGX39+CKrn5ZxtJ+9/6J0oGU3WmFRMeCJbfbQ6NvH1OYtCA8x8SQXpRajaQBQsypeFeMRfngy7Z+QjS2m9wj7OHq791dvbQaPfkbM0R/4sfoowDbloHM9GeDflkfqbB7+tTDhWNaK4xHRkBH4CrTYM1QkVe4I4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1629984018290999.9248774079513; Thu, 26 Aug 2021 06:20:18 -0700 (PDT) Received: from localhost ([::1]:56336 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mJFIn-00089P-3Z for importer@patchew.org; Thu, 26 Aug 2021 09:20:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48624) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <62f08c3c6e1e4f669b034af831dd898aed9a902c@lizzy.crudebyte.com>) id 1mJF80-0003cI-6x for qemu-devel@nongnu.org; Thu, 26 Aug 2021 09:09:08 -0400 Received: from lizzy.crudebyte.com ([91.194.90.13]:53759) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <62f08c3c6e1e4f669b034af831dd898aed9a902c@lizzy.crudebyte.com>) id 1mJF7y-0006tk-So for qemu-devel@nongnu.org; Thu, 26 Aug 2021 09:09:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=crudebyte.com; s=lizzy; h=Cc:To:Subject:Date:From:References:In-Reply-To: Message-Id:Content-Type:Content-Transfer-Encoding:MIME-Version:Content-ID: Content-Description; bh=qnPVCqg4uoyWvT+bOoAAfsKYYVyyOh1vHtIPpMdIT2M=; b=NFPn/ CXtQIVBdn8x/AbM8g2SF9NDXWqHwlD11a+2dxFqeIDqEyVUCOCSHE1EoGsbbdl0JqKpLiyBhvsQbh TVQdkLUIMgfmvpp2DpGAM1H4kqovoGYIAT4irf49VGYI4kWFz6QA5X2Bl/grKNgGW/nF2y8s1wMSY jfUnyCmD19hs6vtkOssEQpykL07uKHIHYSvgVeQoEPgaJL/KWyyW9n9C3fdefuvkE5mzTtHMeX8G/ iXJmVnp+IdD1lRWe/PjWW+RJpD6AWMoWQ79Bax1k1cjTVj5x765SWf0Bm+nkqE/qo07Xce363ZbPo boCBXgwEjj39sCbEMQuUAgW+y7eWQ==; Message-Id: <62f08c3c6e1e4f669b034af831dd898aed9a902c.1629982046.git.qemu_oss@crudebyte.com> In-Reply-To: References: From: Christian Schoenebeck Date: Thu, 26 Aug 2021 14:32:09 +0200 Subject: [PATCH v3 5/5] 9pfs: use QArray in v9fs_walk() To: qemu-devel@nongnu.org Cc: Greg Kurz , Richard Henderson , Markus Armbruster Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=91.194.90.13; envelope-from=62f08c3c6e1e4f669b034af831dd898aed9a902c@lizzy.crudebyte.com; helo=lizzy.crudebyte.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @crudebyte.com) X-ZM-MESSAGEID: 1629985413062100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Christian Schoenebeck --- hw/9pfs/9p.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c index b59572fa79..9275c23df0 100644 --- a/hw/9pfs/9p.c +++ b/hw/9pfs/9p.c @@ -1707,13 +1707,14 @@ static void coroutine_fn v9fs_walk(void *opaque) int name_idx; g_autofree V9fsQID *qids =3D NULL; int i, err =3D 0; - V9fsPath dpath, path, *pathes =3D NULL; + V9fsPath dpath, path; + QARRAY_REF(V9fsPath) pathes =3D NULL; uint16_t nwnames; struct stat stbuf, fidst; g_autofree struct stat *stbufs =3D NULL; size_t offset =3D 7; int32_t fid, newfid; - V9fsString *wnames =3D NULL; + QARRAY_REF(V9fsString) wnames =3D NULL; V9fsFidState *fidp; V9fsFidState *newfidp =3D NULL; V9fsPDU *pdu =3D opaque; @@ -1734,10 +1735,10 @@ static void coroutine_fn v9fs_walk(void *opaque) goto out_nofid; } if (nwnames) { - wnames =3D g_new0(V9fsString, nwnames); + QARRAY_CREATE(V9fsString, wnames, nwnames); qids =3D g_new0(V9fsQID, nwnames); stbufs =3D g_new0(struct stat, nwnames); - pathes =3D g_new0(V9fsPath, nwnames); + QARRAY_CREATE(V9fsPath, pathes, nwnames); for (i =3D 0; i < nwnames; i++) { err =3D pdu_unmarshal(pdu, offset, "s", &wnames[i]); if (err < 0) { @@ -1869,14 +1870,6 @@ out: v9fs_path_free(&path); out_nofid: pdu_complete(pdu, err); - if (nwnames && nwnames <=3D P9_MAXWELEM) { - for (name_idx =3D 0; name_idx < nwnames; name_idx++) { - v9fs_string_free(&wnames[name_idx]); - v9fs_path_free(&pathes[name_idx]); - } - g_free(wnames); - g_free(pathes); - } } =20 static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path) --=20 2.20.1