00001 /*- 00002 * Copyright (c) 1990, 1993 00003 * The Regents of the University of California. All rights reserved. 00004 * 00005 * This code is derived from software contributed to Berkeley by 00006 * Chris Torek. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 1. Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in the 00015 * documentation and/or other materials provided with the distribution. 00016 * 3. All advertising materials mentioning features or use of this software 00017 * must display the following acknowledgement: 00018 * This product includes software developed by the University of 00019 * California, Berkeley and its contributors. 00020 * 4. Neither the name of the University nor the names of its contributors 00021 * may be used to endorse or promote products derived from this software 00022 * without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00025 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00027 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00030 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00031 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00032 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00033 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00034 * SUCH DAMAGE. 00035 * 00036 * $ID: $ 00037 */ 00038 00039 #include <ubixos/types.h> 00040 00041 /* 00042 * sizeof(word) MUST BE A POWER OF TWO 00043 * SO THAT wmask BELOW IS ALL ONES 00044 */ 00045 typedef int word; /* "word" used for optimal copy speed */ 00046 typedef uint32_t uintptr_t; 00047 00048 #define wsize sizeof(word) 00049 #define wmask (wsize - 1) 00050 00051 /* 00052 * Copy a block of memory, handling overlap. 00053 * This is the routine that actually implements 00054 * (the portable versions of) bcopy, memcpy, and memmove. 00055 */ 00056 #if defined(MEMCOPY) || defined(MEMMOVE) 00057 #include <string.h> 00058 00059 void * 00060 #ifdef MEMCOPY 00061 memcpy 00062 #else 00063 memmove 00064 #endif 00065 (void *dst0, const void *src0, size_t length) 00066 #else 00067 #include <strings.h> 00068 00069 void 00070 bcopy(const void *src0, void *dst0, size_t length) 00071 #endif 00072 { 00073 char *dst = dst0; 00074 const char *src = src0; 00075 size_t t; 00076 00077 if (length == 0 || dst == src) /* nothing to do */ 00078 goto done; 00079 00080 /* 00081 * Macros: loop-t-times; and loop-t-times, t>0 00082 */ 00083 #define TLOOP(s) if (t) TLOOP1(s) 00084 #define TLOOP1(s) do { s; } while (--t) 00085 00086 if ((unsigned long)dst < (unsigned long)src) { 00087 /* 00088 * Copy forward. 00089 */ 00090 t = (uintptr_t)src; /* only need low bits */ 00091 if ((t | (uintptr_t)dst) & wmask) { 00092 /* 00093 * Try to align operands. This cannot be done 00094 * unless the low bits match. 00095 */ 00096 if ((t ^ (uintptr_t)dst) & wmask || length < wsize) 00097 t = length; 00098 else 00099 t = wsize - (t & wmask); 00100 length -= t; 00101 TLOOP1(*dst++ = *src++); 00102 } 00103 /* 00104 * Copy whole words, then mop up any trailing bytes. 00105 */ 00106 t = length / wsize; 00107 TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize); 00108 t = length & wmask; 00109 TLOOP(*dst++ = *src++); 00110 } else { 00111 /* 00112 * Copy backwards. Otherwise essentially the same. 00113 * Alignment works as before, except that it takes 00114 * (t&wmask) bytes to align, not wsize-(t&wmask). 00115 */ 00116 src += length; 00117 dst += length; 00118 t = (uintptr_t)src; 00119 if ((t | (uintptr_t)dst) & wmask) { 00120 if ((t ^ (uintptr_t)dst) & wmask || length <= wsize) 00121 t = length; 00122 else 00123 t &= wmask; 00124 length -= t; 00125 TLOOP1(*--dst = *--src); 00126 } 00127 t = length / wsize; 00128 TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src); 00129 t = length & wmask; 00130 TLOOP(*--dst = *--src); 00131 } 00132 done: 00133 #if defined(MEMCOPY) || defined(MEMMOVE) 00134 return (dst0); 00135 #else 00136 return; 00137 #endif 00138 }