Module Name: src Committed By: christos Date: Tue Oct 8 23:11:21 UTC 2024
Modified Files: src/share/man/man3: Makefile Added Files: src/share/man/man3: container_of.3 Log Message: Add man page for container_of To generate a diff of this commit: cvs rdiff -u -r1.93 -r1.94 src/share/man/man3/Makefile cvs rdiff -u -r0 -r1.1 src/share/man/man3/container_of.3 Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/share/man/man3/Makefile diff -u src/share/man/man3/Makefile:1.93 src/share/man/man3/Makefile:1.94 --- src/share/man/man3/Makefile:1.93 Thu Aug 15 10:16:34 2024 +++ src/share/man/man3/Makefile Tue Oct 8 19:11:21 2024 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.93 2024/08/15 14:16:34 riastradh Exp $ +# $NetBSD: Makefile,v 1.94 2024/10/08 23:11:21 christos Exp $ # @(#)Makefile 8.2 (Berkeley) 12/13/93 MAN= _DIAGASSERT.3 __CONCAT.3 __FPTRCAST.3 __UNCONST.3 __USE.3 CMSG_DATA.3 \ @@ -7,8 +7,8 @@ MAN= _DIAGASSERT.3 __CONCAT.3 __FPTRCAST __builtin_return_address.3 \ __builtin_types_compatible_p.3 __insn_barrier.3 \ assert.3 attribute.3 bitmap.3 bitops.3 bits.3 bitstring.3 \ - cdefs.3 dirent.3 dlfcn.3 dlinfo.3 dl_iterate_phdr.3 end.3 \ - fast_divide32.3 ffs32.3 gcq.3 \ + cdefs.3 container_of.3 dirent.3 dlfcn.3 dlinfo.3 dl_iterate_phdr.3 \ + end.3 fast_divide32.3 ffs32.3 gcq.3 \ ilog2.3 intro.3 inttypes.3 iso646.3 limits.3 \ makedev.3 offsetof.3 param.3 paths.3 queue.3 rbtree.3 sigevent.3 \ stdarg.3 stdbool.3 stddef.3 stdint.3 stdlib.3 sysexits.3 \ Added files: Index: src/share/man/man3/container_of.3 diff -u /dev/null src/share/man/man3/container_of.3:1.1 --- /dev/null Tue Oct 8 19:11:21 2024 +++ src/share/man/man3/container_of.3 Tue Oct 8 19:11:21 2024 @@ -0,0 +1,77 @@ +.\" $NetBSD: container_of.3,v 1.1 2024/10/08 23:11:21 christos Exp $ +.\" +.\" Copyright (c) 2024 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +.\" POSSIBILITY OF SUCH DAMAGE. +.\" +.Dd October 8, 2011 +.Dt CONTAINER_OF 3 +.Os +.Sh NAME +.Nm container_of +.Nd cast a pointer to member of a structure to a pointer of its +container structure. +.Sh SYNOPSIS +.In sys/container_of.h +.Ft type +.Fn container_of "pointer" "type" "member" +.Sh DESCRIPTION +Given a +.Fa pointer +that points to a +.Fa member +of the container +.Fa type +the +.Fn container_of +macro returns a pointer that points to the enclosing container structure. +.Pp +A compiler error will result if +.Ar member +is not aligned to a byte boundary (i.e. it is a bit-field). +.Sh EXAMPLES +.Bd -literal -offset indent +#include <assert.h> +#include <sys/container_of.h> +struct container { + double other_member; + int member; +}; + +struct container one; + +void test(void) { + int *ptr = &one.member; + struct container *onep = container_of(ptr, struct container, member); + assert(onep == &one); +} +.Ed +.Sh SEE ALSO +.Xr __alignof__ 3 , +.Xr offsetof 3 , +.Xr stddef 3 , +.Xr typeof 3 +.Sh HISTORY +The +.Fn container_of +macro appeared first in the Linux kernel.