tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: General device properties API



> Date: Sun, 22 Aug 2021 10:07:52 -0700
> From: Jason Thorpe <thorpej%me.com@localhost>
> 
> > On Aug 22, 2021, at 9:28 AM, Taylor R Campbell <campbell+netbsd-tech-kern%mumble.net@localhost> wrote:
> > 
> >> As for the way device_call() works, propose an alternative with the
> >> following attributes: [...]
> > 
> > Here's a sketch of a typed version of what I think is essentially the
> > same thing the untyped device_call does:
> > 
> > [...]
> 
> This proposal does not have all of the attributes I laid out.
> Please go back and read what I said again.

Here's another sketch that is much more flexible and general.
Adapting it to link sets should be easy; the main point is that the
only untyped parts going through void * are limited to the definition
site of a generic operation; everything else is fully typed.

That said, I haven't processed all of the abstractions you're
introducing or the class of related problem domains they intend to
solve.  So I'm not convinced yet that the additional flexibility is
necessary or useful.  But at least it is guaranteed to detect typos
and type errors.

If this doesn't meet the attributes you laid out earlier either, I
would appreciate it if you could identify which ones I missed --
there's too much going on here to fit in my head at once.
/* generic.h */

struct generic {
	const char *name;
};

struct vtable {
	struct vtable_entry {
		const struct generic	*generic;
		void			*method;
	}		*entries;
	size_t		nentries;
};

void *vtable_get(const struct vtable *, const struct generic *);

/* i2c_whatever.h */

struct i2c_attach_args;
typedef struct device device_t;

typedef int i2c_enumerate_devices_t(struct i2c_attach_args *,
    bool (*)(device_t, void *), void *);

extern struct generic i2c_enumerate_devices_generic;
int i2c_enumerate_devices_typecheck(i2c_enumerate_devices_t *);
i2c_enumerate_devices_t *i2c_enumerate_devices(const struct vtable *);

#define	I2C_ENUMERATE_DEVICES_METHOD(fn)				      \
	{								      \
		.generic = &i2c_enumerate_devices_generic,		      \
		.method = 0*sizeof(i2c_enumerate_devices_typecheck(&(fn))) +  \
		    &(fn),						      \
	}

/* i2c_whatever.c */

struct generic i2c_enumerate_devices_generic = {"i2c_enumerate_devices"};

i2c_enumerate_devices_t *
i2c_enumerate_devices(const struct vtable *V)
{
	return vtable_get(V, &i2c_enumerate_devices_generic);
}


Home | Main Index | Thread Index | Old Index