Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2001, Travis Geiselbrecht. All rights reserved. |
| 3 | ** Distributed under the terms of the NewOS License. |
| 4 | */ |
| 5 | /* |
| 6 | * Copyright (c) 2008 Travis Geiselbrecht |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining |
| 9 | * a copy of this software and associated documentation files |
| 10 | * (the "Software"), to deal in the Software without restriction, |
| 11 | * including without limitation the rights to use, copy, modify, merge, |
| 12 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 13 | * and to permit persons to whom the Software is furnished to do so, |
| 14 | * subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be |
| 17 | * included in all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 23 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 24 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | */ |
| 27 | #include <string.h> |
| 28 | #include <sys/types.h> |
| 29 | |
| 30 | #if !_ASM_MEMMOVE |
| 31 | |
| 32 | typedef long word; |
| 33 | |
| 34 | #define lsize sizeof(word) |
| 35 | #define lmask (lsize - 1) |
| 36 | |
| 37 | void * |
| 38 | memmove(void *dest, void const *src, size_t count) |
| 39 | { |
| 40 | char *d = (char *)dest; |
| 41 | const char *s = (const char *)src; |
| 42 | int len; |
| 43 | |
| 44 | if(count == 0 || dest == src) |
| 45 | return dest; |
| 46 | |
| 47 | if((long)d < (long)s) { |
| 48 | if(((long)d | (long)s) & lmask) { |
| 49 | // src and/or dest do not align on word boundary |
| 50 | if((((long)d ^ (long)s) & lmask) || (count < lsize)) |
| 51 | len = count; // copy the rest of the buffer with the byte mover |
| 52 | else |
| 53 | len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary |
| 54 | |
| 55 | count -= len; |
| 56 | for(; len > 0; len--) |
| 57 | *d++ = *s++; |
| 58 | } |
| 59 | for(len = count / lsize; len > 0; len--) { |
| 60 | *(word *)d = *(word *)s; |
| 61 | d += lsize; |
| 62 | s += lsize; |
| 63 | } |
| 64 | for(len = count & lmask; len > 0; len--) |
| 65 | *d++ = *s++; |
| 66 | } else { |
| 67 | d += count; |
| 68 | s += count; |
| 69 | if(((long)d | (long)s) & lmask) { |
| 70 | // src and/or dest do not align on word boundary |
| 71 | if((((long)d ^ (long)s) & lmask) || (count <= lsize)) |
| 72 | len = count; |
| 73 | else |
| 74 | len = ((long)d & lmask); |
| 75 | |
| 76 | count -= len; |
| 77 | for(; len > 0; len--) |
| 78 | *--d = *--s; |
| 79 | } |
| 80 | for(len = count / lsize; len > 0; len--) { |
| 81 | d -= lsize; |
| 82 | s -= lsize; |
| 83 | *(word *)d = *(word *)s; |
| 84 | } |
| 85 | for(len = count & lmask; len > 0; len--) |
| 86 | *--d = *--s; |
| 87 | } |
| 88 | |
| 89 | return dest; |
| 90 | } |
| 91 | |
| 92 | #endif |
| 93 | |