Subject: cleaning up the rest of dtom()
To: None <tech-kern@NetBSD.ORG>
From: Jason Thorpe <thorpej@nas.nasa.gov>
List: tech-kern
Date: 06/23/1997 22:39:35
Hi folks...

Today I committed changes from Koji Imada that eliminate the use of
dtom() from the networking code.  I decided to hunt down and eliminate
the remaining uses of dtom().  The only place left was in the AF_UNIX
code...  Attached below are diffs to eliminate it.  I would just commit
them, but I'd like a sanity check on them first...

Thanks!

Jason R. Thorpe                                       thorpej@nas.nasa.gov
NASA Ames Research Center                               Home: 408.866.1912
NAS: M/S 258-6                                          Work: 415.604.0935
Moffett Field, CA 94035                                Pager: 415.428.6939

Index: sys/mbuf.h
===================================================================
RCS file: /mastersrc/netbsd/src/sys/sys/mbuf.h,v
retrieving revision 1.26
diff -c -r1.26 mbuf.h
*** mbuf.h	1997/06/10 18:33:52	1.26
--- mbuf.h	1997/06/24 05:16:16
***************
*** 62,73 ****
  /*
   * Macros for type conversion
   * mtod(m,t) -	convert mbuf pointer to data pointer of correct type
-  * dtom(x) -	convert data pointer within mbuf to mbuf pointer (XXX)
   * mtocl(x) -	convert pointer within cluster to cluster index #
   * cltom(x) -	convert cluster # to ptr to beginning of cluster
   */
  #define	mtod(m,t)	((t)((m)->m_data))
- #define	dtom(x)		((struct mbuf *)((long)(x) & ~(MSIZE-1)))
  #define	mtocl(x)	(((u_long)(x) - (u_long)mbutl) >> MCLSHIFT)
  #define	cltom(x)	((caddr_t)((u_long)mbutl + ((u_long)(x) << MCLSHIFT)))
  
--- 62,71 ----
Index: sys/unpcb.h
===================================================================
RCS file: /mastersrc/netbsd/src/sys/sys/unpcb.h,v
retrieving revision 1.1.1.4
diff -c -r1.1.1.4 unpcb.h
*** unpcb.h	1997/05/18 07:21:32	1.1.1.4
--- unpcb.h	1997/06/24 05:15:25
***************
*** 77,82 ****
--- 77,83 ----
  	struct	unpcb *unp_refs;	/* referencing socket linked list */
  	struct 	unpcb *unp_nextref;	/* link in unp_refs list */
  	struct	sockaddr_un *unp_addr;	/* bound address of socket */
+ 	size_t	unp_addrlen;		/* size of socket address */
  	int	unp_cc;			/* copy of rcv.sb_cc */
  	int	unp_mbcnt;		/* copy of rcv.sb_mbcnt */
  	struct	timespec unp_ctime;	/* holds creation time */
Index: kern/uipc_usrreq.c
===================================================================
RCS file: /mastersrc/netbsd/src/sys/kern/uipc_usrreq.c,v
retrieving revision 1.1.1.6
diff -c -r1.1.1.6 uipc_usrreq.c
*** uipc_usrreq.c	1997/05/18 07:18:39	1.1.1.6
--- uipc_usrreq.c	1997/06/24 02:57:07
***************
*** 391,397 ****
  	soisdisconnected(unp->unp_socket);
  	unp->unp_socket->so_pcb = 0;
  	if (unp->unp_addr)
! 		m_freem(dtom(unp->unp_addr));
  	if (unp_rights) {
  		/*
  		 * Normally the receive buffer is flushed later,
--- 391,397 ----
  	soisdisconnected(unp->unp_socket);
  	unp->unp_socket->so_pcb = 0;
  	if (unp->unp_addr)
! 		free(unp->unp_addr, M_SONAME);
  	if (unp_rights) {
  		/*
  		 * Normally the receive buffer is flushed later,
***************
*** 419,424 ****
--- 419,426 ----
  	int error;
  	struct nameidata nd;
  
+ 	if (nam->m_len > sizeof(struct sockaddr_un))
+ 		return (EINVAL);
  	if (unp->unp_vnode != 0)
  		return (EINVAL);
  	NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_SYSSPACE,
***************
*** 451,458 ****
  	vp = nd.ni_vp;
  	vp->v_socket = unp->unp_socket;
  	unp->unp_vnode = vp;
! 	unp->unp_addr =
! 	    mtod(m_copy(nam, 0, (int)M_COPYALL), struct sockaddr_un *);
  	VOP_UNLOCK(vp);
  	return (0);
  }
--- 453,461 ----
  	vp = nd.ni_vp;
  	vp->v_socket = unp->unp_socket;
  	unp->unp_vnode = vp;
! 	unp->unp_addrlen = nam->m_len;
! 	unp->unp_addr = malloc(unp->unp_addrlen, M_SONAME, M_WAITOK);
! 	m_copydata(nam, 0, unp->unp_addrlen, (caddr_t)unp->unp_addr);
  	VOP_UNLOCK(vp);
  	return (0);
  }
***************
*** 502,510 ****
  		}
  		unp2 = sotounpcb(so2);
  		unp3 = sotounpcb(so3);
! 		if (unp2->unp_addr)
! 			unp3->unp_addr = mtod(m_copy(dtom(unp2->unp_addr), 0,
! 			    (int)M_COPYALL), struct sockaddr_un *);
  		so2 = so3;
  	}
  	error = unp_connect2(so, so2);
--- 505,517 ----
  		}
  		unp2 = sotounpcb(so2);
  		unp3 = sotounpcb(so3);
! 		if (unp2->unp_addr) {
! 			unp3->unp_addr = malloc(unp2->unp_addrlen,
! 			    M_SONAME, M_WAITOK);
! 			bcopy(unp2->unp_addr, unp3->unp_addr,
! 			    unp2->unp_addrlen);
! 			unp3->unp_addrlen = unp2->unp_addrlen;
! 		}
  		so2 = so3;
  	}
  	error = unp_connect2(so, so2);
***************
*** 615,621 ****
  		so->so_pcb = 0;
  		sofree(so);
  		if (unp->unp_addr)
! 			m_freem(dtom(unp->unp_addr));
  		free(unp, M_PCB);
  	}
  }
--- 622,628 ----
  		so->so_pcb = 0;
  		sofree(so);
  		if (unp->unp_addr)
! 			free(unp->unp_addr, M_SONAME);
  		free(unp, M_PCB);
  	}
  }
Index: miscfs/portal/portal_vnops.c
===================================================================
RCS file: /mastersrc/netbsd/src/sys/miscfs/portal/portal_vnops.c,v
retrieving revision 1.1.1.2
diff -c -r1.1.1.2 portal_vnops.c
*** portal_vnops.c	1997/05/11 22:50:09	1.1.1.2
--- portal_vnops.c	1997/06/24 03:16:20
***************
*** 271,279 ****
  
  	unp2 = sotounpcb(so2);
  	unp3 = sotounpcb(so3);
! 	if (unp2->unp_addr)
! 		unp3->unp_addr = mtod(m_copy(dtom(unp2->unp_addr), 0,
! 		    (int)M_COPYALL), struct sockaddr_un *);
  
  	so2 = so3;
  
--- 271,283 ----
  
  	unp2 = sotounpcb(so2);
  	unp3 = sotounpcb(so3);
! 	if (unp2->unp_addr) {
! 		unp3->unp_addr = malloc(unp2->unp_addrlen,
! 		    M_SONAME, M_WAITOK);
! 		bcopy(unp2->unp_addr, unp3->unp_addr,
! 		    unp2->unp_addrlen);
! 		unp3->unp_addrlen = unp2->unp_addrlen;
! 	}
  
  	so2 = so3;