Linux Device Mapper development
 help / color / mirror / Atom feed
From: Benjamin Marzinski <bmarzins@redhat.com>
To: Martin Wilck <martin.wilck@suse.com>
Cc: Christophe Varoqui <christophe.varoqui@opensvc.com>,
	Brian Bunker <brian@purestorage.com>,
	dm-devel@lists.linux.dev,
	Xose Vazquez Perez <xose.vazquez@gmail.com>,
	Martin Wilck <mwilck@suse.com>
Subject: Re: [PATCH v2 04/19] libmpathutil: add implementation of generic shared pointer
Date: Wed, 27 May 2026 19:16:45 -0400	[thread overview]
Message-ID: <ahd7Xcfe3alqjROA@redhat.com> (raw)
In-Reply-To: <20260522164415.846589-5-mwilck@suse.com>

On Fri, May 22, 2026 at 06:44:00PM +0200, Martin Wilck wrote:
> Add a set of simple functions to handle refcounted pointers.
> 
> Signed-off-by: Martin Wilck <mwilck@suse.com>
> Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com>
> ---
>  libmpathutil/libmpathutil.version |  7 +++++
>  libmpathutil/util.c               | 45 ++++++++++++++++++++++++++++---
>  libmpathutil/util.h               |  5 ++++
>  3 files changed, 53 insertions(+), 4 deletions(-)
> 
> diff --git a/libmpathutil/util.c b/libmpathutil/util.c
> index 23a9797..3c623ec 100644
> --- a/libmpathutil/util.c
> +++ b/libmpathutil/util.c
> @@ -384,3 +382,42 @@ void cleanup_udev_device(struct udev_device **udd)
>  	if (*udd)
>  		udev_device_unref(*udd);
>  }
> +
> +struct shared_ptr {
> +	long refcnt;
> +	void (*destructor)(void *);
> +	char __attribute__((aligned(sizeof(void *)))) ptr[];
> +};
> +
> +void *alloc_shared_ptr(size_t size, void (*destructor)(void *))
> +{
> +	struct shared_ptr *sp = malloc(sizeof(*sp) + size);
> +
> +	if (!sp)
> +		return NULL;
> +	uatomic_set(&sp->refcnt, 1);
> +	sp->destructor = destructor;
> +	return sp->ptr;
> +}
> +
> +void get_shared_ptr(void *ptr)
> +{

We should probably return here if ptr is NULL, like we do in
put_shared_ptr(). Alternatively, this might be a reasonable place for an
assert(), since nobody should be calling these with NULL pointers.

As far as the general question Hannes raise about this implentantion,
I'm happy with it as-is. 

-Ben

> +	struct shared_ptr *sp = container_of(ptr, struct shared_ptr, ptr);
> +
> +	if (uatomic_add_return(&sp->refcnt, 1) < 0)
> +		condlog(0, "%s: refcount overflow", __func__);
> +}
> +
> +void put_shared_ptr(void *ptr)
> +{
> +	struct shared_ptr *sp;
> +
> +	if (!ptr)
> +		return;
> +	sp = container_of(ptr, struct shared_ptr, ptr);
> +	if (uatomic_sub_return(&sp->refcnt, 1) == 0) {
> +		if (sp->destructor)
> +			sp->destructor(ptr);
> +		free(sp);
> +	}
> +}


  parent reply	other threads:[~2026-05-27 23:16 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260522164415.846589-1-mwilck@suse.com>
     [not found] ` <20260522164415.846589-5-mwilck@suse.com>
2026-05-27  9:28   ` [PATCH v2 04/19] libmpathutil: add implementation of generic shared pointer Hannes Reinecke
2026-05-27 10:43     ` Martin Wilck
2026-05-27 23:16   ` Benjamin Marzinski [this message]
2026-05-28  7:50     ` Martin Wilck
     [not found] ` <20260522164415.846589-10-mwilck@suse.com>
2026-05-27  9:40   ` [PATCH v2 09/19] libmpathutil: runner: use shared_ptr Hannes Reinecke
2026-05-27 10:11     ` Martin Wilck
2026-05-27 13:13       ` Hannes Reinecke
2026-05-27 14:11         ` Martin Wilck
2026-05-27 17:30           ` Hannes Reinecke
2026-06-02  5:53 ` [PATCH v2 00/19] multipath-tools: asynchronous checker framework Benjamin Marzinski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ahd7Xcfe3alqjROA@redhat.com \
    --to=bmarzins@redhat.com \
    --cc=brian@purestorage.com \
    --cc=christophe.varoqui@opensvc.com \
    --cc=dm-devel@lists.linux.dev \
    --cc=martin.wilck@suse.com \
    --cc=mwilck@suse.com \
    --cc=xose.vazquez@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox