[comp.lang.c] copying routines, order, and overlap

chris@mimsy.UUCP (Chris Torek) (03/23/88)

In article <1304@ut-emx.UUCP> wca@ut-emx.UUCP (William C. Anderson) writes:
>... the ndbm(3) routines in 4.3BSD depend upon bcopy() doing the correct
>ordering in cases of overlap.  Luckily, it is simple to do the code correctly.

(This has moved into a new topic)

The ANSI X3J11 draft standard specifies two kinds of block copy
operations.  One (memcpy, I think) is allowed to do the copy in any
order, and must not be given overlapping regions.  The other (memmove)
is required to determine whether the regions overlap and if so, to do
the copy in the nondestructive order.  (I may have the names reversed.
There is not much to distinguish them.)

In 4.2BSD, bcopy was allowed to do the copy in any order.  A chain of
events (which I think I started) resulted in the 4.3BSD bcopy being
constrained to check for overlap.  Note that this is the more general
operation; if you are going to provide only one operation, use the
4.3BSD semantics.*

This does mean that if you move code from 4.3-based systems to 4.2-
based systems, you must be careful about bcopy.

-----
*The test is relatively cheap.  For instance, here is the top of
the 4.3BSD bcopy:

	movl	4(ap),r1	# from
	movl	8(ap),r3	# to
	movl	12(ap),r6	# count
	cmpl	r1,r3
	bgtr	2f		# normal forward case
	blss	3f		# overlapping, must do backwards
	ret			# equal, nothing to do

Eliminating the constraint removes one compare and one branch (and
also the possibility of the final optimisation above).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

thomson@hub.toronto.edu (Brian Thomson) (03/24/88)

In article <10762@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
>
>In 4.2BSD, bcopy was allowed to do the copy in any order.  A chain of
>events (which I think I started) resulted in the 4.3BSD bcopy being
>constrained to check for overlap.

I did a bit of 4.2 porting several (4) years ago, and I distinctly
remember bcopy() checking for overlap and getting it right.
I began with VAX "bcopy.s 4.4 9/25/83".  Is this not original 4.2 code?
-- 
		    Brian Thomson,	    CSRI Univ. of Toronto
		    utcsri!uthub!thomson, thomson@hub.toronto.edu

chris@mimsy.UUCP (Chris Torek) (03/26/88)

>In article <10762@mimsy.UUCP> I said that
>>In 4.2BSD, bcopy was allowed to do the copy in any order.

In article <8803240119.AA21810@hub.toronto.edu> thomson@hub.toronto.edu
(Brian Thomson) writes:
>I did a bit of 4.2 porting several (4) years ago, and I distinctly
>remember bcopy() checking for overlap and getting it right.
>I began with VAX "bcopy.s 4.4 9/25/83".  Is this not original 4.2 code?

Possibly not.  There were several `4.2BSD' tapes (just as there
were several `4.1BSD' tapes and several `4.3BSD' tapes, with fixes
quietly inserted).  The SCCS revision files show that rev. 4.2 was
the first one to handle transfers > 65535 bytes; that was done on
83/01/14.  The next change was on 83/09/12: `correct comment on usage';
on 83/09/25 it was changed `to insure that overlapping strings are
properly handled'.  (No substantive changes have been made since then.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris