source: trunk/kLdr/kLdrHlp-gcc.c@ 2899

Last change on this file since 2899 was 2899, checked in by bird, 19 years ago

Made it build (version + name length).

File size: 4.8 KB
Line 
1/* $Id: kLdrHlp-os2.c 2826 2006-10-22 15:58:55Z 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 <kLdr.h>
28#include "kLdrHlp.h"
29
30
31/*******************************************************************************
32* Global Variables *
33*******************************************************************************/
34
35
36void *memchr(const void *pv, int ch, size_t 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
49int memcmp(const void *pv1, const void *pv2, size_t cb)
50{
51 /*
52 * Pointer size pointer size.
53 */
54 if ( cb > 16
55 && !((uintptr_t)pv1 & (sizeof(void *) - 1))
56 && !((uintptr_t)pv2 & (sizeof(void *) - 1)) )
57 {
58 const uintptr_t *pu1 = pv1;
59 const uintptr_t *pu2 = pv2;
60 while (cb >= sizeof(uintptr_t))
61 {
62 const uintptr_t u1 = *pu1++;
63 const uintptr_t u2 = *pu2++;
64 if (u1 != u2)
65 return u1 > u2 ? 1 : -1;
66 cb -= sizeof(uintptr_t);
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
93void *memcpy(void *pv1, const void *pv2, size_t cb)
94{
95 void *pv1Start = pv1;
96
97 /*
98 * Pointer size pointer size.
99 */
100 if ( cb > 16
101 && !((uintptr_t)pv1 & (sizeof(void *) - 1))
102 && !((uintptr_t)pv2 & (sizeof(void *) - 1)) )
103 {
104 uintptr_t *pu1 = pv1;
105 const uintptr_t *pu2 = pv2;
106 while (cb >= sizeof(uintptr_t))
107 {
108 cb -= sizeof(uintptr_t);
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
131void *memset(void *pv, int ch, size_t cb)
132{
133 void *pvStart = pv;
134
135 /*
136 * Pointer size pointer size.
137 */
138 if ( cb > 16
139 && !((uintptr_t)pv & (sizeof(void *) - 1)))
140 {
141 uintptr_t *pu = pv;
142 uintptr_t u = ch | (ch << 8);
143 u |= u << 16;
144#if defined(__AMD64__)
145 u |= u << 32;
146#endif
147
148 while (cb >= sizeof(uintptr_t))
149 {
150 cb -= sizeof(uintptr_t);
151 *pu++ = u;
152 }
153 }
154
155 /*
156 * Byte by byte
157 */
158 if (cb)
159 {
160 unsigned char *pb = pv;
161 while (cb-- > 0)
162 *pb++ = ch;
163 }
164 return pvStart;
165}
166
167
168int strcmp(const char *psz1, const char *psz2)
169{
170 for (;;)
171 {
172 const char ch1 = *psz1++;
173 const char ch2 = *psz2++;
174 if (ch1 != ch2)
175 return (int)ch1 - (int)ch2;
176 if (!ch1)
177 return 0;
178 }
179}
180
181
182int strncmp(const char *psz1, const char *psz2, size_t cch)
183{
184 while (cch-- > 0)
185 {
186 const char ch1 = *psz1++;
187 const char ch2 = *psz2++;
188 if (ch1 != ch2)
189 return (int)ch1 - (int)ch2;
190 if (!ch1)
191 break;
192 }
193 return 0;
194}
195
196char *strchr(const char *psz, int ch)
197{
198 for (;;)
199 {
200 const char chCur = *psz;
201 if (chCur == ch)
202 return (char *)psz;
203 if (!chCur)
204 return 0;
205 psz++;
206 }
207}
208
209size_t strlen(const char *psz)
210{
211 const char *pszStart = psz;
212 while (*psz)
213 psz++;
214 return psz - pszStart;
215}
216
Note: See TracBrowser for help on using the repository browser.