| 1 | /* $Id: kHlpMemMove.c 3574 2007-09-02 19:30:58Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kHlpString - kHlpMemMove.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2007 knut st. osmundsen <bird-src-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 | * kStuff is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 19 | * Lesser General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 22 | * License along with kStuff; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | /*******************************************************************************
|
|---|
| 28 | * Header Files *
|
|---|
| 29 | *******************************************************************************/
|
|---|
| 30 | #include <k/kHlpString.h>
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | KHLP_DECL(void *) kHlpMemMove(void *pv1, const void *pv2, KSIZE cb)
|
|---|
| 34 | {
|
|---|
| 35 | union
|
|---|
| 36 | {
|
|---|
| 37 | void *pv;
|
|---|
| 38 | KU8 *pb;
|
|---|
| 39 | KUPTR *pu;
|
|---|
| 40 | } u1;
|
|---|
| 41 | union
|
|---|
| 42 | {
|
|---|
| 43 | const void *pv;
|
|---|
| 44 | volatile KU8 *pb;
|
|---|
| 45 | volatile KUPTR *pu;
|
|---|
| 46 | } u2;
|
|---|
| 47 |
|
|---|
| 48 | u1.pv = pv1;
|
|---|
| 49 | u2.pv = pv2;
|
|---|
| 50 |
|
|---|
| 51 | if ((KUPTR)u1.pb <= (KUPTR)u2.pb)
|
|---|
| 52 | {
|
|---|
| 53 | /* forward copy */
|
|---|
| 54 | if (cb >= 32)
|
|---|
| 55 | {
|
|---|
| 56 | while (cb > sizeof(KUPTR))
|
|---|
| 57 | {
|
|---|
| 58 | KUPTR u = *u2.pu++;
|
|---|
| 59 | *u1.pu++ = u;
|
|---|
| 60 | cb -= sizeof(KUPTR);
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | while (cb-- > 0)
|
|---|
| 65 | {
|
|---|
| 66 | KU8 b = *u2.pb++;
|
|---|
| 67 | *u1.pb++ = b;
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 | else
|
|---|
| 71 | {
|
|---|
| 72 | /* backwards copy */
|
|---|
| 73 | u1.pb += cb;
|
|---|
| 74 | u2.pb += cb;
|
|---|
| 75 |
|
|---|
| 76 | if (cb >= 32)
|
|---|
| 77 | {
|
|---|
| 78 | while (cb > sizeof(KUPTR))
|
|---|
| 79 | {
|
|---|
| 80 | KUPTR u = *--u2.pu;
|
|---|
| 81 | *--u1.pu = u;
|
|---|
| 82 | cb -= sizeof(KUPTR);
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | while (cb-- > 0)
|
|---|
| 87 | {
|
|---|
| 88 | KU8 b = *--u2.pb;
|
|---|
| 89 | *--u1.pb = b;
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | return pv1;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|