/*-1* SPDX-License-Identifier: BSD-3-Clause2* Copyright (c) 1990 The Regents of the University of California.3*4* All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Chris Torek.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <sys/param.h>35#ifdef _KERNEL36#include <sys/systm.h>37#else38#include <string.h>39#endif4041#undef memcpy42#undef memmove4344/*45* sizeof(word) MUST BE A POWER OF TWO46* SO THAT wmask BELOW IS ALL ONES47*/48typedef long word; /* "word" used for optimal copy speed */4950#define wsize sizeof(word)51#define wmask (wsize - 1)5253/*54* Copy a block of memory, handling overlap.55* This is the routine that actually implements56* (the portable versions of) bcopy, memcpy, and memmove.57*/58void *59memcpy(void *dst0, const void *src0, size_t length)60{61char *dst;62const char *src;63size_t t;6465dst = dst0;66src = src0;6768if (length == 0 || dst == src) { /* nothing to do */69goto done;70}7172/*73* Macros: loop-t-times; and loop-t-times, t>074*/75#define TLOOP(s) if (t) TLOOP1(s)76#define TLOOP1(s) do { s; } while (--t)7778if ((unsigned long)dst < (unsigned long)src) {79/*80* Copy forward.81*/82t = (size_t)src; /* only need low bits */8384if ((t | (uintptr_t)dst) & wmask) {85/*86* Try to align operands. This cannot be done87* unless the low bits match.88*/89if ((t ^ (uintptr_t)dst) & wmask || length < wsize) {90t = length;91} else {92t = wsize - (t & wmask);93}9495length -= t;96TLOOP1(*dst++ = *src++);97}98/*99* Copy whole words, then mop up any trailing bytes.100*/101t = length / wsize;102TLOOP(*(word *)dst = *(const word *)src; src += wsize;103dst += wsize);104t = length & wmask;105TLOOP(*dst++ = *src++);106} else {107/*108* Copy backwards. Otherwise essentially the same.109* Alignment works as before, except that it takes110* (t&wmask) bytes to align, not wsize-(t&wmask).111*/112src += length;113dst += length;114t = (uintptr_t)src;115116if ((t | (uintptr_t)dst) & wmask) {117if ((t ^ (uintptr_t)dst) & wmask || length <= wsize) {118t = length;119} else {120t &= wmask;121}122123length -= t;124TLOOP1(*--dst = *--src);125}126t = length / wsize;127TLOOP(src -= wsize; dst -= wsize;128*(word *)dst = *(const word *)src);129t = length & wmask;130TLOOP(*--dst = *--src);131}132done:133return (dst0);134}135136__strong_reference(memcpy, memmove);137138139