qemu-e2k/fsdev
Christian Schoenebeck 30e702abf6 9pfs: introduce P9Array
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 = malloc(n * sizeof(Foo));
      for (...) {
          foos[i].s = 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) {
      P9ARRAY_REF(Foo) foos = NULL;
      P9ARRAY_NEW(Foo, foos, n);
      for (...) {
          foos[i].s = malloc(...);
          if (...) {
              return; /* array auto freed here */
          }
      }
      /* array auto freed here */
  }

Unlike GArray, P9Array does not require special macros, function
calls or struct member dereferencing to access the individual array
elements:

  C-array = P9Array:   vs.  GArray:

  for (...) {           |   for (...) {
      ... = arr[i].m;   |       ... = g_array_index(arr, Foo, i).m;
      arr[i].m = ... ;  |       g_array_index(arr, Foo, i).m = ... ;
  }                     |   }

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 P9Array 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 P9Array##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 P9Array 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 <qemu_oss@crudebyte.com>
Message-Id: <a954ef47b5ac26085a16c5c2aec8695374e0424d.1633097129.git.qemu_oss@crudebyte.com>
2021-10-27 14:45:22 +02:00
..
9p-iov-marshal.c fsdev: don't allow unknown format in marshal/unmarshal 2017-05-25 10:30:13 +02:00
9p-iov-marshal.h Clean up ill-advised or unusual header guards 2016-07-12 16:20:46 +02:00
9p-marshal.c 9pfs: drop useless v9fs_string_null() function 2016-09-16 08:56:15 +02:00
9p-marshal.h fsdev: open brace '{' following struct go on the same line 2020-12-13 17:46:36 +01:00
file-op-9p.h fsdev: open brace '{' following struct go on the same line 2020-12-13 17:46:36 +01:00
meson.build meson: Declare have_virtfs_proxy_helper in main meson.build 2021-01-23 15:55:04 -05:00
p9array.h 9pfs: introduce P9Array 2021-10-27 14:45:22 +02:00
qemu-fsdev-dummy.c Include qemu/module.h where needed, drop it from qemu-common.h 2019-06-12 13:18:33 +02:00
qemu-fsdev-opts.c 9p: Added virtfs option 'multidevs=remap|forbid|warn' 2019-10-10 11:36:05 +02:00
qemu-fsdev-throttle.c fsdev: Add return value to fsdev_throttle_parse_opts() 2019-10-10 11:36:05 +02:00
qemu-fsdev-throttle.h fsdev: Add return value to fsdev_throttle_parse_opts() 2019-10-10 11:36:05 +02:00
qemu-fsdev.c 9p: null terminate fs driver options list 2020-07-10 12:48:06 +02:00
qemu-fsdev.h fsdev: Move some types definition to qemu-fsdev.c 2019-05-17 17:34:48 +02:00
virtfs-proxy-helper.c qemu/: fix some comment spelling errors 2020-09-17 20:35:43 +02:00