| 1 | /* $Id: kHlpBare-gcc.c 29 2009-07-01 20:30:29Z 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-spamix@anduin.net> | 
|---|
| 8 | * | 
|---|
| 9 | * Permission is hereby granted, free of charge, to any person | 
|---|
| 10 | * obtaining a copy of this software and associated documentation | 
|---|
| 11 | * files (the "Software"), to deal in the Software without | 
|---|
| 12 | * restriction, including without limitation the rights to use, | 
|---|
| 13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell | 
|---|
| 14 | * copies of the Software, and to permit persons to whom the | 
|---|
| 15 | * Software is furnished to do so, subject to the following | 
|---|
| 16 | * conditions: | 
|---|
| 17 | * | 
|---|
| 18 | * The above copyright notice and this permission notice shall be | 
|---|
| 19 | * included in all copies or substantial portions of the Software. | 
|---|
| 20 | * | 
|---|
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | 
|---|
| 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | 
|---|
| 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | 
|---|
| 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | 
|---|
| 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | 
|---|
| 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | 
|---|
| 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | 
|---|
| 28 | * OTHER DEALINGS IN THE SOFTWARE. | 
|---|
| 29 | */ | 
|---|
| 30 |  | 
|---|
| 31 | #include <k/kLdr.h> | 
|---|
| 32 | #include "kHlp.h" | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | /******************************************************************************* | 
|---|
| 36 | *   Global Variables                                                           * | 
|---|
| 37 | *******************************************************************************/ | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 | void *memchr(const void *pv, int ch, KSIZE cb) | 
|---|
| 41 | { | 
|---|
| 42 | const char *pb = pv; | 
|---|
| 43 | while (cb-- > 0) | 
|---|
| 44 | { | 
|---|
| 45 | if (*pb == ch) | 
|---|
| 46 | return (void *)pb; | 
|---|
| 47 | pb++; | 
|---|
| 48 | } | 
|---|
| 49 | return 0; | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 |  | 
|---|
| 53 | int memcmp(const void *pv1, const void *pv2, KSIZE cb) | 
|---|
| 54 | { | 
|---|
| 55 | /* | 
|---|
| 56 | * Pointer size pointer size. | 
|---|
| 57 | */ | 
|---|
| 58 | if (    cb > 16 | 
|---|
| 59 | &&  !((KUPTR)pv1 & (sizeof(void *) - 1)) | 
|---|
| 60 | &&  !((KUPTR)pv2 & (sizeof(void *) - 1)) ) | 
|---|
| 61 | { | 
|---|
| 62 | const KUPTR *pu1 = pv1; | 
|---|
| 63 | const KUPTR *pu2 = pv2; | 
|---|
| 64 | while (cb >= sizeof(KUPTR)) | 
|---|
| 65 | { | 
|---|
| 66 | const KUPTR u1 = *pu1++; | 
|---|
| 67 | const KUPTR u2 = *pu2++; | 
|---|
| 68 | if (u1 != u2) | 
|---|
| 69 | return u1 > u2 ? 1 : -1; | 
|---|
| 70 | cb -= sizeof(KUPTR); | 
|---|
| 71 | } | 
|---|
| 72 | if (!cb) | 
|---|
| 73 | return 0; | 
|---|
| 74 | pv1 = (const void *)pu1; | 
|---|
| 75 | pv2 = (const void *)pu2; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | /* | 
|---|
| 79 | * Byte by byte. | 
|---|
| 80 | */ | 
|---|
| 81 | if (cb) | 
|---|
| 82 | { | 
|---|
| 83 | const unsigned char *pb1 = pv1; | 
|---|
| 84 | const unsigned char *pb2 = pv2; | 
|---|
| 85 | while (cb-- > 0) | 
|---|
| 86 | { | 
|---|
| 87 | const unsigned char b1 = *pb1++; | 
|---|
| 88 | const unsigned char b2 = *pb2++; | 
|---|
| 89 | if (b1 != b2) | 
|---|
| 90 | return b1 > b2 ? 1 : -1; | 
|---|
| 91 | } | 
|---|
| 92 | } | 
|---|
| 93 | return 0; | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|
| 97 | void *memcpy(void *pv1, const void *pv2, KSIZE cb) | 
|---|
| 98 | { | 
|---|
| 99 | void *pv1Start = pv1; | 
|---|
| 100 |  | 
|---|
| 101 | /* | 
|---|
| 102 | * Pointer size pointer size. | 
|---|
| 103 | */ | 
|---|
| 104 | if (    cb > 16 | 
|---|
| 105 | &&  !((KUPTR)pv1 & (sizeof(void *) - 1)) | 
|---|
| 106 | &&  !((KUPTR)pv2 & (sizeof(void *) - 1)) ) | 
|---|
| 107 | { | 
|---|
| 108 | KUPTR       *pu1 = pv1; | 
|---|
| 109 | const KUPTR *pu2 = pv2; | 
|---|
| 110 | while (cb >= sizeof(KUPTR)) | 
|---|
| 111 | { | 
|---|
| 112 | cb -= sizeof(KUPTR); | 
|---|
| 113 | *pu1++ = *pu2++; | 
|---|
| 114 | } | 
|---|
| 115 | if (!cb) | 
|---|
| 116 | return 0; | 
|---|
| 117 | pv1 = (void *)pu1; | 
|---|
| 118 | pv2 = (const void *)pu2; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | /* | 
|---|
| 122 | * byte by byte | 
|---|
| 123 | */ | 
|---|
| 124 | if (cb) | 
|---|
| 125 | { | 
|---|
| 126 | unsigned char       *pb1 = pv1; | 
|---|
| 127 | const unsigned char *pb2 = pv2; | 
|---|
| 128 | while (cb-- > 0) | 
|---|
| 129 | *pb1++ = *pb2++; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | return pv1Start; | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | void *memset(void *pv, int ch, KSIZE cb) | 
|---|
| 136 | { | 
|---|
| 137 | void *pvStart = pv; | 
|---|
| 138 |  | 
|---|
| 139 | /* | 
|---|
| 140 | * Pointer size pointer size. | 
|---|
| 141 | */ | 
|---|
| 142 | if (    cb > 16 | 
|---|
| 143 | &&  !((KUPTR)pv & (sizeof(void *) - 1))) | 
|---|
| 144 | { | 
|---|
| 145 | KUPTR  *pu = pv; | 
|---|
| 146 | KUPTR   u = ch | (ch << 8); | 
|---|
| 147 | u |= u << 16; | 
|---|
| 148 | #if K_ARCH_BITS >= 64 | 
|---|
| 149 | u |= u << 32; | 
|---|
| 150 | #endif | 
|---|
| 151 | #if K_ARCH_BITS >= 128 | 
|---|
| 152 | u |= u << 64; | 
|---|
| 153 | #endif | 
|---|
| 154 |  | 
|---|
| 155 | while (cb >= sizeof(KUPTR)) | 
|---|
| 156 | { | 
|---|
| 157 | cb -= sizeof(KUPTR); | 
|---|
| 158 | *pu++ = u; | 
|---|
| 159 | } | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | /* | 
|---|
| 163 | * Byte by byte | 
|---|
| 164 | */ | 
|---|
| 165 | if (cb) | 
|---|
| 166 | { | 
|---|
| 167 | unsigned char *pb = pv; | 
|---|
| 168 | while (cb-- > 0) | 
|---|
| 169 | *pb++ = ch; | 
|---|
| 170 | } | 
|---|
| 171 | return pvStart; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 |  | 
|---|
| 175 | int strcmp(const char *psz1, const char *psz2) | 
|---|
| 176 | { | 
|---|
| 177 | for (;;) | 
|---|
| 178 | { | 
|---|
| 179 | const char ch1 = *psz1++; | 
|---|
| 180 | const char ch2 = *psz2++; | 
|---|
| 181 | if (ch1 != ch2) | 
|---|
| 182 | return (int)ch1 - (int)ch2; | 
|---|
| 183 | if (!ch1) | 
|---|
| 184 | return 0; | 
|---|
| 185 | } | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 |  | 
|---|
| 189 | int strncmp(const char *psz1, const char *psz2, KSIZE cch) | 
|---|
| 190 | { | 
|---|
| 191 | while (cch-- > 0) | 
|---|
| 192 | { | 
|---|
| 193 | const char ch1 = *psz1++; | 
|---|
| 194 | const char ch2 = *psz2++; | 
|---|
| 195 | if (ch1 != ch2) | 
|---|
| 196 | return (int)ch1 - (int)ch2; | 
|---|
| 197 | if (!ch1) | 
|---|
| 198 | break; | 
|---|
| 199 | } | 
|---|
| 200 | return 0; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | char *strchr(const char *psz, int ch) | 
|---|
| 204 | { | 
|---|
| 205 | for (;;) | 
|---|
| 206 | { | 
|---|
| 207 | const char chCur = *psz; | 
|---|
| 208 | if (chCur == ch) | 
|---|
| 209 | return (char *)psz; | 
|---|
| 210 | if (!chCur) | 
|---|
| 211 | return 0; | 
|---|
| 212 | psz++; | 
|---|
| 213 | } | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | KSIZE strlen(const char *psz) | 
|---|
| 217 | { | 
|---|
| 218 | const char *pszStart = psz; | 
|---|
| 219 | while (*psz) | 
|---|
| 220 | psz++; | 
|---|
| 221 | return psz - pszStart; | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|