copying routines, order, and overlap
Chris Torek
chris at mimsy.UUCP
Wed Mar 23 05:33:34 AEST 1988
In article <1304 at ut-emx.UUCP> wca at 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 at mimsy.umd.edu Path: uunet!mimsy!chris
More information about the Comp.lang.c
mailing list