Module Name:    src
Committed By:   thorpej
Date:           Sat Jan 22 11:58:15 UTC 2022

Modified Files:
        src/sys/kern: subr_device.c
        src/sys/sys: device.h

Log Message:
Add a function to compare 2 devhandles.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/sys/kern/subr_device.c
cvs rdiff -u -r1.177 -r1.178 src/sys/sys/device.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/subr_device.c
diff -u src/sys/kern/subr_device.c:1.10 src/sys/kern/subr_device.c:1.11
--- src/sys/kern/subr_device.c:1.10	Fri Jan 21 15:55:36 2022
+++ src/sys/kern/subr_device.c	Sat Jan 22 11:58:15 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_device.c,v 1.10 2022/01/21 15:55:36 thorpej Exp $	*/
+/*	$NetBSD: subr_device.c,v 1.11 2022/01/22 11:58:15 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2006, 2021 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_device.c,v 1.10 2022/01/21 15:55:36 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_device.c,v 1.11 2022/01/22 11:58:15 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/device.h>
@@ -77,6 +77,49 @@ devhandle_type(devhandle_t handle)
 	return handle.impl->type;
 }
 
+int
+devhandle_compare(devhandle_t handle1, devhandle_t handle2)
+{
+	devhandle_type_t type1 = devhandle_type(handle1);
+	devhandle_type_t type2 = devhandle_type(handle2);
+
+	if (type1 == DEVHANDLE_TYPE_INVALID) {
+		return -1;
+	}
+	if (type2 == DEVHANDLE_TYPE_INVALID) {
+		return 1;
+	}
+
+	if (type1 < type2) {
+		return -1;
+	}
+	if (type1 > type2) {
+		return 1;
+	}
+
+	/* For private handles, we also compare the impl pointers. */
+	if (type1 == DEVHANDLE_TYPE_PRIVATE) {
+		intptr_t impl1 = (intptr_t)handle1.impl;
+		intptr_t impl2 = (intptr_t)handle2.impl;
+
+		if (impl1 < impl2) {
+			return -1;
+		}
+		if (impl1 > impl2) {
+			return 1;
+		}
+	}
+
+	if (handle1.integer < handle2.integer) {
+		return -1;
+	}
+	if (handle1.integer > handle2.integer) {
+		return 1;
+	}
+
+	return 0;
+}
+
 device_call_t
 devhandle_lookup_device_call(devhandle_t handle, const char *name,
     devhandle_t *call_handlep)

Index: src/sys/sys/device.h
diff -u src/sys/sys/device.h:1.177 src/sys/sys/device.h:1.178
--- src/sys/sys/device.h:1.177	Sat Jan 22 11:16:00 2022
+++ src/sys/sys/device.h	Sat Jan 22 11:58:15 2022
@@ -1,4 +1,4 @@
-/* $NetBSD: device.h,v 1.177 2022/01/22 11:16:00 thorpej Exp $ */
+/* $NetBSD: device.h,v 1.178 2022/01/22 11:58:15 thorpej Exp $ */
 
 /*
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -674,6 +674,7 @@ devhandle_t	device_handle(device_t);
 bool		devhandle_is_valid(devhandle_t);
 devhandle_t	devhandle_invalid(void);
 devhandle_type_t devhandle_type(devhandle_t);
+int		devhandle_compare(devhandle_t, devhandle_t);
 
 device_call_t	devhandle_lookup_device_call(devhandle_t, const char *,
 		    devhandle_t *);

Reply via email to