| 1 | /* $Id: kHlpMemPMove.c 3608 2007-10-29 00:49:05Z bird $ */ | 
|---|
| 2 | /** @file | 
|---|
| 3 | * kHlpString - kHlpMemPMove. | 
|---|
| 4 | */ | 
|---|
| 5 |  | 
|---|
| 6 | /* | 
|---|
| 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net> | 
|---|
| 8 | * | 
|---|
| 9 | * This file is part of kStuff. | 
|---|
| 10 | * | 
|---|
| 11 | * kStuff is free software; you can redistribute it and/or | 
|---|
| 12 | * modify it under the terms of the GNU Lesser General Public | 
|---|
| 13 | * License as published by the Free Software Foundation; either | 
|---|
| 14 | * version 2.1 of the License, or (at your option) any later version. | 
|---|
| 15 | * | 
|---|
| 16 | * In addition to the permissions in the GNU Lesser General Public | 
|---|
| 17 | * License, you are granted unlimited permission to link the compiled | 
|---|
| 18 | * version of this file into combinations with other programs, and to | 
|---|
| 19 | * distribute those combinations without any restriction coming from | 
|---|
| 20 | * the use of this file. | 
|---|
| 21 | * | 
|---|
| 22 | * kStuff is distributed in the hope that it will be useful, | 
|---|
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 25 | * Lesser General Public License for more details. | 
|---|
| 26 | * | 
|---|
| 27 | * You should have received a copy of the GNU Lesser General Public | 
|---|
| 28 | * License along with kStuff; if not, write to the Free Software | 
|---|
| 29 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | 
|---|
| 30 | * 02110-1301, USA | 
|---|
| 31 | */ | 
|---|
| 32 |  | 
|---|
| 33 | /******************************************************************************* | 
|---|
| 34 | *   Header Files                                                               * | 
|---|
| 35 | *******************************************************************************/ | 
|---|
| 36 | #include <k/kHlpString.h> | 
|---|
| 37 |  | 
|---|
| 38 |  | 
|---|
| 39 | KHLP_DECL(void *) kHlpMemPMove(void *pv1, const void *pv2, KSIZE cb) | 
|---|
| 40 | { | 
|---|
| 41 | union | 
|---|
| 42 | { | 
|---|
| 43 | void *pv; | 
|---|
| 44 | KU8 *pb; | 
|---|
| 45 | KUPTR *pu; | 
|---|
| 46 | } u1; | 
|---|
| 47 | union | 
|---|
| 48 | { | 
|---|
| 49 | const void *pv; | 
|---|
| 50 | volatile KU8 *pb; | 
|---|
| 51 | volatile KUPTR *pu; | 
|---|
| 52 | } u2; | 
|---|
| 53 |  | 
|---|
| 54 | u1.pv = pv1; | 
|---|
| 55 | u2.pv = pv2; | 
|---|
| 56 |  | 
|---|
| 57 | if ((KUPTR)u1.pb <= (KUPTR)u2.pb) | 
|---|
| 58 | { | 
|---|
| 59 | /* forwards copy */ | 
|---|
| 60 | if (cb >= 32) | 
|---|
| 61 | { | 
|---|
| 62 | while (cb > sizeof(KUPTR)) | 
|---|
| 63 | { | 
|---|
| 64 | KUPTR u = *u2.pu++; | 
|---|
| 65 | *u1.pu++ = u; | 
|---|
| 66 | cb -= sizeof(KUPTR); | 
|---|
| 67 | } | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | while (cb-- > 0) | 
|---|
| 71 | { | 
|---|
| 72 | KU8 b = *u2.pb++; | 
|---|
| 73 | *u1.pb++ = b; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | return u1.pb; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | /* backwards copy */ | 
|---|
| 80 | u1.pb += cb; | 
|---|
| 81 | u2.pb += cb; | 
|---|
| 82 |  | 
|---|
| 83 | if (cb >= 32) | 
|---|
| 84 | { | 
|---|
| 85 | while (cb > sizeof(KUPTR)) | 
|---|
| 86 | { | 
|---|
| 87 | KUPTR u = *--u2.pu; | 
|---|
| 88 | *--u1.pu = u; | 
|---|
| 89 | cb -= sizeof(KUPTR); | 
|---|
| 90 | } | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | while (cb-- > 0) | 
|---|
| 94 | { | 
|---|
| 95 | KU8 b = *--u2.pb; | 
|---|
| 96 | *--u1.pb = b; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | return (KU8 *)pv1 + cb; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|