/* $Id: kLdrHlp-gcc.c 2944 2007-01-13 15:55:40Z bird $ */ /** @file * * kLdr - The Dynamic Loader, Helper Functions for GCC. * * Copyright (c) 2006 knut st. osmundsen * * * This file is part of kLdr. * * kLdr is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * kLdr is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with kLdr; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include #include "kLdrHlp.h" /******************************************************************************* * Global Variables * *******************************************************************************/ void *memchr(const void *pv, int ch, size_t cb) { const char *pb = pv; while (cb-- > 0) { if (*pb == ch) return (void *)pb; pb++; } return 0; } int memcmp(const void *pv1, const void *pv2, size_t cb) { /* * Pointer size pointer size. */ if ( cb > 16 && !((uintptr_t)pv1 & (sizeof(void *) - 1)) && !((uintptr_t)pv2 & (sizeof(void *) - 1)) ) { const uintptr_t *pu1 = pv1; const uintptr_t *pu2 = pv2; while (cb >= sizeof(uintptr_t)) { const uintptr_t u1 = *pu1++; const uintptr_t u2 = *pu2++; if (u1 != u2) return u1 > u2 ? 1 : -1; cb -= sizeof(uintptr_t); } if (!cb) return 0; pv1 = (const void *)pu1; pv2 = (const void *)pu2; } /* * Byte by byte. */ if (cb) { const unsigned char *pb1 = pv1; const unsigned char *pb2 = pv2; while (cb-- > 0) { const unsigned char b1 = *pb1++; const unsigned char b2 = *pb2++; if (b1 != b2) return b1 > b2 ? 1 : -1; } } return 0; } void *memcpy(void *pv1, const void *pv2, size_t cb) { void *pv1Start = pv1; /* * Pointer size pointer size. */ if ( cb > 16 && !((uintptr_t)pv1 & (sizeof(void *) - 1)) && !((uintptr_t)pv2 & (sizeof(void *) - 1)) ) { uintptr_t *pu1 = pv1; const uintptr_t *pu2 = pv2; while (cb >= sizeof(uintptr_t)) { cb -= sizeof(uintptr_t); *pu1++ = *pu2++; } if (!cb) return 0; pv1 = (void *)pu1; pv2 = (const void *)pu2; } /* * byte by byte */ if (cb) { unsigned char *pb1 = pv1; const unsigned char *pb2 = pv2; while (cb-- > 0) *pb1++ = *pb2++; } return pv1Start; } void *memset(void *pv, int ch, size_t cb) { void *pvStart = pv; /* * Pointer size pointer size. */ if ( cb > 16 && !((uintptr_t)pv & (sizeof(void *) - 1))) { uintptr_t *pu = pv; uintptr_t u = ch | (ch << 8); u |= u << 16; #if defined(__AMD64__) u |= u << 32; #endif while (cb >= sizeof(uintptr_t)) { cb -= sizeof(uintptr_t); *pu++ = u; } } /* * Byte by byte */ if (cb) { unsigned char *pb = pv; while (cb-- > 0) *pb++ = ch; } return pvStart; } int strcmp(const char *psz1, const char *psz2) { for (;;) { const char ch1 = *psz1++; const char ch2 = *psz2++; if (ch1 != ch2) return (int)ch1 - (int)ch2; if (!ch1) return 0; } } int strncmp(const char *psz1, const char *psz2, size_t cch) { while (cch-- > 0) { const char ch1 = *psz1++; const char ch2 = *psz2++; if (ch1 != ch2) return (int)ch1 - (int)ch2; if (!ch1) break; } return 0; } char *strchr(const char *psz, int ch) { for (;;) { const char chCur = *psz; if (chCur == ch) return (char *)psz; if (!chCur) return 0; psz++; } } size_t strlen(const char *psz) { const char *pszStart = psz; while (*psz) psz++; return psz - pszStart; }