224 points · by octopus · 15y ago · 50 comments
void*
memmove(void *vdst, void *vsrc, int n)
{
char *dst, *src;
dst = vdst;
src = vsrc;
while(n-- > 0)
*dst++ = *src++;
return vdst;
}
If the two blocks overlap, and the address of dst is greater than the address of src, then parts of the source will be overwritten before it can be copied. void*
memmove(void *vdst, void *vsrc, int n)
{
char *dst, *src;
dst = vdst;
src = vsrc;
if(*vdst > *vsrc) {
*vdst += n;
*vsrc += n;
while(n-- > 0)
*dst-- = *src--;
}
else if(*vdst < *vsrc)
while(n-- > 0)
*dst++ == *src++;
return vdst;
} void*
memmove(void *vdst, void *vsrc, int n)
{
char *dst, *src;
dst = vdst;
src = vsrc;
if(vdst > vsrc) {
dst = vdst + n;
src = vsrc + n;
while(n-- > 0)
*--dst = *--src;
}
else if(vdst < vsrc)
while(n-- > 0)
*dst++ == *src++;
return vdst;
}
Does this solve the bugs?Second bug, cheers.
> The memory areas may overlap: copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the temporary array to dest.
http://www.kernel.org/doc/man-pages/online/pages/man3/memmov...
Notice how it says 'as though'; I can't see why doing the copy backwards (from end to start) wouldn't work, myself.
src starts at 50, ends at 100
dst starts at 25, ends at 75.
First we copy 100 to 75, then 99 to 74, etc ... now we get to copy from 75 to 50, but we've overwritten it already! /* memmove.c -- copy memory.
Copy LENGTH bytes from SOURCE to DEST. Does not null-terminate.
In the public domain.
By David MacKenzie <djm@gnu.ai.mit.edu>. */
#if HAVE_CONFIG_H
# include <config.h>
#endif
void *
memmove (dest, source, length)
char *dest;
const char *source;
unsigned length;
{
char *d0 = dest;
if (source < dest)
/* Moving from low mem to hi mem; start at end. */
for (source += length, dest += length; length; --length)
*--dest = *--source;
else if (source != dest)
{
/* Moving from hi mem to low mem; start at beginning. */
for (; length; --length)
*dest++ = *source++;
}
return (void *) d0;
}If I may prognosticate, stripped-down monolithic OSes running on hypervisors are the wave of the future. Look at VM/CMS for an extreme example: CMS is about as complex as MS-DOS, being a single-user single-tasking OS with no memory protection or security model. VM is the hypervisor. Together, they date to about 1968, or a little before if you include research systems.
Or, they could be lazy, and hope that this HN thread turns out to be a representative sample of the Textbooks/Operating Systems in use.
The project page gives this link to the schedule, where the lecture notes are supposed to be, but the link 404's.
Is there a working URL for these lecture notes?
The lectures themselves are not xv6 specific, although the projects are xv6 based.
Those students were MIT students. I'd expect MIT students to not have had a problem with either of these, so I'm a bit puzzled.
P.S. I still maintain a very HLL is ideal for presenting multi-processing/multi-tasking to students.
Edit: all the user tests passed.