| 1 | /* $Id: kHlpBare-gcc.c 3594 2007-10-02 21:35:22Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * kLdr - The Dynamic Loader, Helper Functions for GCC.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2006 knut st. osmundsen <bird@anduin.net>
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kLdr.
|
|---|
| 10 | *
|
|---|
| 11 | * kLdr is free software; you can redistribute it and/or modify
|
|---|
| 12 | * it under the terms of the GNU General Public License as published by
|
|---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 14 | * (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * kLdr 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
|
|---|
| 19 | * GNU General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU General Public License
|
|---|
| 22 | * along with kLdr; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | #include <k/kLdr.h>
|
|---|
| 28 | #include "kHlp.h"
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | /*******************************************************************************
|
|---|
| 32 | * Global Variables *
|
|---|
| 33 | *******************************************************************************/
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 | void *memchr(const void *pv, int ch, KSIZE cb)
|
|---|
| 37 | {
|
|---|
| 38 | const char *pb = pv;
|
|---|
| 39 | while (cb-- > 0)
|
|---|
| 40 | {
|
|---|
| 41 | if (*pb == ch)
|
|---|
| 42 | return (void *)pb;
|
|---|
| 43 | pb++;
|
|---|
| 44 | }
|
|---|
| 45 | return 0;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | int memcmp(const void *pv1, const void *pv2, KSIZE cb)
|
|---|
| 50 | {
|
|---|
| 51 | /*
|
|---|
| 52 | * Pointer size pointer size.
|
|---|
| 53 | */
|
|---|
| 54 | if ( cb > 16
|
|---|
| 55 | && !((KUPTR)pv1 & (sizeof(void *) - 1))
|
|---|
| 56 | && !((KUPTR)pv2 & (sizeof(void *) - 1)) )
|
|---|
| 57 | {
|
|---|
| 58 | const KUPTR *pu1 = pv1;
|
|---|
| 59 | const KUPTR *pu2 = pv2;
|
|---|
| 60 | while (cb >= sizeof(KUPTR))
|
|---|
| 61 | {
|
|---|
| 62 | const KUPTR u1 = *pu1++;
|
|---|
| 63 | const KUPTR u2 = *pu2++;
|
|---|
| 64 | if (u1 != u2)
|
|---|
| 65 | return u1 > u2 ? 1 : -1;
|
|---|
| 66 | cb -= sizeof(KUPTR);
|
|---|
| 67 | }
|
|---|
| 68 | if (!cb)
|
|---|
| 69 | return 0;
|
|---|
| 70 | pv1 = (const void *)pu1;
|
|---|
| 71 | pv2 = (const void *)pu2;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | /*
|
|---|
| 75 | * Byte by byte.
|
|---|
| 76 | */
|
|---|
| 77 | if (cb)
|
|---|
| 78 | {
|
|---|
| 79 | const unsigned char *pb1 = pv1;
|
|---|
| 80 | const unsigned char *pb2 = pv2;
|
|---|
| 81 | while (cb-- > 0)
|
|---|
| 82 | {
|
|---|
| 83 | const unsigned char b1 = *pb1++;
|
|---|
| 84 | const unsigned char b2 = *pb2++;
|
|---|
| 85 | if (b1 != b2)
|
|---|
| 86 | return b1 > b2 ? 1 : -1;
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | return 0;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | void *memcpy(void *pv1, const void *pv2, KSIZE cb)
|
|---|
| 94 | {
|
|---|
| 95 | void *pv1Start = pv1;
|
|---|
| 96 |
|
|---|
| 97 | /*
|
|---|
| 98 | * Pointer size pointer size.
|
|---|
| 99 | */
|
|---|
| 100 | if ( cb > 16
|
|---|
| 101 | && !((KUPTR)pv1 & (sizeof(void *) - 1))
|
|---|
| 102 | && !((KUPTR)pv2 & (sizeof(void *) - 1)) )
|
|---|
| 103 | {
|
|---|
| 104 | KUPTR *pu1 = pv1;
|
|---|
| 105 | const KUPTR *pu2 = pv2;
|
|---|
| 106 | while (cb >= sizeof(KUPTR))
|
|---|
| 107 | {
|
|---|
| 108 | cb -= sizeof(KUPTR);
|
|---|
| 109 | *pu1++ = *pu2++;
|
|---|
| 110 | }
|
|---|
| 111 | if (!cb)
|
|---|
| 112 | return 0;
|
|---|
| 113 | pv1 = (void *)pu1;
|
|---|
| 114 | pv2 = (const void *)pu2;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /*
|
|---|
| 118 | * byte by byte
|
|---|
| 119 | */
|
|---|
| 120 | if (cb)
|
|---|
| 121 | {
|
|---|
| 122 | unsigned char *pb1 = pv1;
|
|---|
| 123 | const unsigned char *pb2 = pv2;
|
|---|
| 124 | while (cb-- > 0)
|
|---|
| 125 | *pb1++ = *pb2++;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | return pv1Start;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | void *memset(void *pv, int ch, KSIZE cb)
|
|---|
| 132 | {
|
|---|
| 133 | void *pvStart = pv;
|
|---|
| 134 |
|
|---|
| 135 | /*
|
|---|
| 136 | * Pointer size pointer size.
|
|---|
| 137 | */
|
|---|
| 138 | if ( cb > 16
|
|---|
| 139 | && !((KUPTR)pv & (sizeof(void *) - 1)))
|
|---|
| 140 | {
|
|---|
| 141 | KUPTR *pu = pv;
|
|---|
| 142 | KUPTR u = ch | (ch << 8);
|
|---|
| 143 | u |= u << 16;
|
|---|
| 144 | #if K_ARCH_BITS >= 64
|
|---|
| 145 | u |= u << 32;
|
|---|
| 146 | #endif
|
|---|
| 147 | #if K_ARCH_BITS >= 128
|
|---|
| 148 | u |= u << 64;
|
|---|
| 149 | #endif
|
|---|
| 150 |
|
|---|
| 151 | while (cb >= sizeof(KUPTR))
|
|---|
| 152 | {
|
|---|
| 153 | cb -= sizeof(KUPTR);
|
|---|
| 154 | *pu++ = u;
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /*
|
|---|
| 159 | * Byte by byte
|
|---|
| 160 | */
|
|---|
| 161 | if (cb)
|
|---|
| 162 | {
|
|---|
| 163 | unsigned char *pb = pv;
|
|---|
| 164 | while (cb-- > 0)
|
|---|
| 165 | *pb++ = ch;
|
|---|
| 166 | }
|
|---|
| 167 | return pvStart;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 | int strcmp(const char *psz1, const char *psz2)
|
|---|
| 172 | {
|
|---|
| 173 | for (;;)
|
|---|
| 174 | {
|
|---|
| 175 | const char ch1 = *psz1++;
|
|---|
| 176 | const char ch2 = *psz2++;
|
|---|
| 177 | if (ch1 != ch2)
|
|---|
| 178 | return (int)ch1 - (int)ch2;
|
|---|
| 179 | if (!ch1)
|
|---|
| 180 | return 0;
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 | int strncmp(const char *psz1, const char *psz2, KSIZE cch)
|
|---|
| 186 | {
|
|---|
| 187 | while (cch-- > 0)
|
|---|
| 188 | {
|
|---|
| 189 | const char ch1 = *psz1++;
|
|---|
| 190 | const char ch2 = *psz2++;
|
|---|
| 191 | if (ch1 != ch2)
|
|---|
| 192 | return (int)ch1 - (int)ch2;
|
|---|
| 193 | if (!ch1)
|
|---|
| 194 | break;
|
|---|
| 195 | }
|
|---|
| 196 | return 0;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | char *strchr(const char *psz, int ch)
|
|---|
| 200 | {
|
|---|
| 201 | for (;;)
|
|---|
| 202 | {
|
|---|
| 203 | const char chCur = *psz;
|
|---|
| 204 | if (chCur == ch)
|
|---|
| 205 | return (char *)psz;
|
|---|
| 206 | if (!chCur)
|
|---|
| 207 | return 0;
|
|---|
| 208 | psz++;
|
|---|
| 209 | }
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | KSIZE strlen(const char *psz)
|
|---|
| 213 | {
|
|---|
| 214 | const char *pszStart = psz;
|
|---|
| 215 | while (*psz)
|
|---|
| 216 | psz++;
|
|---|
| 217 | return psz - pszStart;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|