| 1 | /* $Id: kHlpBare-gcc.c 2 2007-11-16 16:07:14Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kHlpBare - The Dynamic Loader, Helper Functions for GCC.
|
|---|
| 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 | #include <k/kLdr.h>
|
|---|
| 34 | #include "kHlp.h"
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | /*******************************************************************************
|
|---|
| 38 | * Global Variables *
|
|---|
| 39 | *******************************************************************************/
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | void *memchr(const void *pv, int ch, KSIZE cb)
|
|---|
| 43 | {
|
|---|
| 44 | const char *pb = pv;
|
|---|
| 45 | while (cb-- > 0)
|
|---|
| 46 | {
|
|---|
| 47 | if (*pb == ch)
|
|---|
| 48 | return (void *)pb;
|
|---|
| 49 | pb++;
|
|---|
| 50 | }
|
|---|
| 51 | return 0;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | int memcmp(const void *pv1, const void *pv2, KSIZE cb)
|
|---|
| 56 | {
|
|---|
| 57 | /*
|
|---|
| 58 | * Pointer size pointer size.
|
|---|
| 59 | */
|
|---|
| 60 | if ( cb > 16
|
|---|
| 61 | && !((KUPTR)pv1 & (sizeof(void *) - 1))
|
|---|
| 62 | && !((KUPTR)pv2 & (sizeof(void *) - 1)) )
|
|---|
| 63 | {
|
|---|
| 64 | const KUPTR *pu1 = pv1;
|
|---|
| 65 | const KUPTR *pu2 = pv2;
|
|---|
| 66 | while (cb >= sizeof(KUPTR))
|
|---|
| 67 | {
|
|---|
| 68 | const KUPTR u1 = *pu1++;
|
|---|
| 69 | const KUPTR u2 = *pu2++;
|
|---|
| 70 | if (u1 != u2)
|
|---|
| 71 | return u1 > u2 ? 1 : -1;
|
|---|
| 72 | cb -= sizeof(KUPTR);
|
|---|
| 73 | }
|
|---|
| 74 | if (!cb)
|
|---|
| 75 | return 0;
|
|---|
| 76 | pv1 = (const void *)pu1;
|
|---|
| 77 | pv2 = (const void *)pu2;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /*
|
|---|
| 81 | * Byte by byte.
|
|---|
| 82 | */
|
|---|
| 83 | if (cb)
|
|---|
| 84 | {
|
|---|
| 85 | const unsigned char *pb1 = pv1;
|
|---|
| 86 | const unsigned char *pb2 = pv2;
|
|---|
| 87 | while (cb-- > 0)
|
|---|
| 88 | {
|
|---|
| 89 | const unsigned char b1 = *pb1++;
|
|---|
| 90 | const unsigned char b2 = *pb2++;
|
|---|
| 91 | if (b1 != b2)
|
|---|
| 92 | return b1 > b2 ? 1 : -1;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | return 0;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | void *memcpy(void *pv1, const void *pv2, KSIZE cb)
|
|---|
| 100 | {
|
|---|
| 101 | void *pv1Start = pv1;
|
|---|
| 102 |
|
|---|
| 103 | /*
|
|---|
| 104 | * Pointer size pointer size.
|
|---|
| 105 | */
|
|---|
| 106 | if ( cb > 16
|
|---|
| 107 | && !((KUPTR)pv1 & (sizeof(void *) - 1))
|
|---|
| 108 | && !((KUPTR)pv2 & (sizeof(void *) - 1)) )
|
|---|
| 109 | {
|
|---|
| 110 | KUPTR *pu1 = pv1;
|
|---|
| 111 | const KUPTR *pu2 = pv2;
|
|---|
| 112 | while (cb >= sizeof(KUPTR))
|
|---|
| 113 | {
|
|---|
| 114 | cb -= sizeof(KUPTR);
|
|---|
| 115 | *pu1++ = *pu2++;
|
|---|
| 116 | }
|
|---|
| 117 | if (!cb)
|
|---|
| 118 | return 0;
|
|---|
| 119 | pv1 = (void *)pu1;
|
|---|
| 120 | pv2 = (const void *)pu2;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /*
|
|---|
| 124 | * byte by byte
|
|---|
| 125 | */
|
|---|
| 126 | if (cb)
|
|---|
| 127 | {
|
|---|
| 128 | unsigned char *pb1 = pv1;
|
|---|
| 129 | const unsigned char *pb2 = pv2;
|
|---|
| 130 | while (cb-- > 0)
|
|---|
| 131 | *pb1++ = *pb2++;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | return pv1Start;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | void *memset(void *pv, int ch, KSIZE cb)
|
|---|
| 138 | {
|
|---|
| 139 | void *pvStart = pv;
|
|---|
| 140 |
|
|---|
| 141 | /*
|
|---|
| 142 | * Pointer size pointer size.
|
|---|
| 143 | */
|
|---|
| 144 | if ( cb > 16
|
|---|
| 145 | && !((KUPTR)pv & (sizeof(void *) - 1)))
|
|---|
| 146 | {
|
|---|
| 147 | KUPTR *pu = pv;
|
|---|
| 148 | KUPTR u = ch | (ch << 8);
|
|---|
| 149 | u |= u << 16;
|
|---|
| 150 | #if K_ARCH_BITS >= 64
|
|---|
| 151 | u |= u << 32;
|
|---|
| 152 | #endif
|
|---|
| 153 | #if K_ARCH_BITS >= 128
|
|---|
| 154 | u |= u << 64;
|
|---|
| 155 | #endif
|
|---|
| 156 |
|
|---|
| 157 | while (cb >= sizeof(KUPTR))
|
|---|
| 158 | {
|
|---|
| 159 | cb -= sizeof(KUPTR);
|
|---|
| 160 | *pu++ = u;
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | /*
|
|---|
| 165 | * Byte by byte
|
|---|
| 166 | */
|
|---|
| 167 | if (cb)
|
|---|
| 168 | {
|
|---|
| 169 | unsigned char *pb = pv;
|
|---|
| 170 | while (cb-- > 0)
|
|---|
| 171 | *pb++ = ch;
|
|---|
| 172 | }
|
|---|
| 173 | return pvStart;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 | int strcmp(const char *psz1, const char *psz2)
|
|---|
| 178 | {
|
|---|
| 179 | for (;;)
|
|---|
| 180 | {
|
|---|
| 181 | const char ch1 = *psz1++;
|
|---|
| 182 | const char ch2 = *psz2++;
|
|---|
| 183 | if (ch1 != ch2)
|
|---|
| 184 | return (int)ch1 - (int)ch2;
|
|---|
| 185 | if (!ch1)
|
|---|
| 186 | return 0;
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 | int strncmp(const char *psz1, const char *psz2, KSIZE cch)
|
|---|
| 192 | {
|
|---|
| 193 | while (cch-- > 0)
|
|---|
| 194 | {
|
|---|
| 195 | const char ch1 = *psz1++;
|
|---|
| 196 | const char ch2 = *psz2++;
|
|---|
| 197 | if (ch1 != ch2)
|
|---|
| 198 | return (int)ch1 - (int)ch2;
|
|---|
| 199 | if (!ch1)
|
|---|
| 200 | break;
|
|---|
| 201 | }
|
|---|
| 202 | return 0;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | char *strchr(const char *psz, int ch)
|
|---|
| 206 | {
|
|---|
| 207 | for (;;)
|
|---|
| 208 | {
|
|---|
| 209 | const char chCur = *psz;
|
|---|
| 210 | if (chCur == ch)
|
|---|
| 211 | return (char *)psz;
|
|---|
| 212 | if (!chCur)
|
|---|
| 213 | return 0;
|
|---|
| 214 | psz++;
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | KSIZE strlen(const char *psz)
|
|---|
| 219 | {
|
|---|
| 220 | const char *pszStart = psz;
|
|---|
| 221 | while (*psz)
|
|---|
| 222 | psz++;
|
|---|
| 223 | return psz - pszStart;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|