source: trunk/kLdr/kLdrHlp.h@ 2875

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

top half of the filesearching is done.

  • Property svn:keywords set to Id
File size: 8.1 KB
Line 
1/* $Id: kLdrHlp.h 2867 2006-11-11 09:33:17Z bird $ */
2/** @file
3 *
4 * kLdr - The Dynamic Loader, Helper Functions.
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
28#ifndef __kLdrHlp_h__
29#define __kLdrHlp_h__
30
31/** @defgroup grp_kLdrHlp kLdrHlp - Helper Functions
32 * @internal
33 * @{ */
34
35/** Get the minimum of two values. */
36#define KLDR_MIN(a, b) ((a) <= (b) ? (a) : (b))
37/** Get the maximum of two values. */
38#define KLDR_MAX(a, b) ((a) >= (b) ? (a) : (b))
39/** Calculate the offset of a structure member. */
40#define KLDR_OFFSETOF(strct, memb) ( (size_t)( &((strct *)0)->memb ) )
41/** Align a size_t value. */
42#define KLDR_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(size_t)((align) - 1) )
43/** Align a void * value. */
44#define KLDR_ALIGN_P(pv, align) ( (void *)( ((uintptr_t)(pv) + ((align) - 1)) & ~(uintptr_t)((align) - 1) ) )
45/** Number of elements in an array. */
46#define KLDR_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) )
47
48/** @def KLDRHLP_LE2H_U16
49 * Unsigned 16-bit little-endian to host translation. */
50/** @def KLDRHLP_LE2H_U32
51 * Unsigned 32-bit little-endian to host translation. */
52#if 1
53# define KLDRHLP_LE2H_U16(u16) ((uint16_t)(u16))
54# define KLDRHLP_LE2H_U32(u32) ((uint32_t)(u32))
55#else
56# define KLDRHLP_LE2H_U16(u16) ( (uint16_t) (((u16) >> 8) | ((u16) << 8)) )
57# define KLDRHLP_LE2H_U32(u32) ( ( ((u32) & UINT32_C(0xff000000)) >> 24 ) \
58 | ( ((u32) & UINT32_C(0x00ff0000)) >> 8 ) \
59 | ( ((u32) & UINT32_C(0x0000ff00)) << 8 ) \
60 | ( ((u32) & UINT32_C(0x000000ff)) << 24 ) \
61 )
62#endif
63
64/*
65 * Compiler specific helpers.
66 * (I.e. operations that tend to have compiler intrinsic implementations).
67 */
68#ifdef __GNUC__
69/** memchr */
70# define kLdrHlpMemChr(a,b,c) __builtin_memchr(a,b,c)
71/** memcmp */
72# define kLdrHlpMemComp(a,b,c) __builtin_memcmp(a,b,c)
73/** memcpy */
74# define kLdrHlpMemCopy(a,b,c) __builtin_memcpy(a,b,c)
75/** memset */
76# define kLdrHlpMemSet(a,b,c) __builtin_memset(a,b,c)
77/** strchr */
78# define kLdrHlpStrChr(a, b) __builtin_strchr(a, b)
79/** strcmp */
80# define kLdrHlpStrComp(a, b) __builtin_strcmp(a, b)
81/** strlen */
82# define kLdrHlpStrLen(a) __builtin_strlen(a)
83/** alloca */
84# define kLdrHlpAllocA(a) __builtin_alloca(a)
85/** int3 */
86# define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
87/** NULL */
88# ifndef NULL
89# define NULL 0
90# endif
91#endif
92
93#ifdef _MSC_VER
94# include <string.h>
95# include <malloc.h>
96# pragma intrinsic(memcmp, memcpy, memset, strcmp, strlen, __debugbreak)
97/** memchr */
98# define kLdrHlpMemChr_needed
99/** memcmp */
100# define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
101/** memcpy */
102# define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
103/** memset */
104# define kLdrHlpMemSet(a,b,c) memset(a,b,c)
105/** strcmp */
106# define kLdrHlpStrComp(a, b) strcmp(a, b)
107/** strlen */
108# define kLdrHlpStrLen(a) strlen(a)
109/** strchr */
110# define kLdrHlpStrChr_needed
111/** alloca */
112# define kLdrHlpAllocA(a) alloca(a)
113/** int3 */
114# define kldrHlpBreakpoint() __debugbreak()
115/** NULL */
116# ifndef NULL
117# define NULL 0
118# endif
119#endif
120
121#ifdef kLdrHlpStrChr_needed
122char *kLdrHlpStrChr(const char *psz, int ch);
123#endif
124#ifdef kLdrHlpStrChr_needed
125void *kLdrHlpMemChr(const void *pv, int ch, size_t cb);
126#endif
127int kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb);
128int kLdrHlpStrIComp(const char *pv1, const char *pv2);
129
130
131#if (!defined(kLdrHlpMemChr) && !defined(kLdrHlpStrChr_needed))\
132 || !defined(kLdrHlpMemComp) \
133 || !defined(kLdrHlpMemCopy) \
134 || !defined(kLdrHlpMemSet) \
135 || (!defined(kLdrHlpStrChr) && !defined(kLdrHlpStrChr_needed)) \
136 || !defined(kLdrHlpStrLen) \
137 || !defined(kLdrHlpAllocA) \
138 || !defined(kldrHlpBreakpoint)
139# error "Needs porting to your compiler."
140#endif
141
142int kldrHlpSemInit(void);
143void kldrHlpSemTerm(void);
144int kldrHlpSemRequest(void);
145void kldrHlpSemRelease(void);
146
147int kldrHlpPageAlloc(void **ppv, size_t cb, KLDRPROT enmProt, unsigned fFixed);
148int kldrHlpPageProtect(void *pv, size_t cb, KLDRPROT enmProt);
149int kldrHlpPageFree(void *pv, size_t cb);
150
151int kldrHlpHeapInit(void);
152void kldrHlpHeapTerm(void);
153void kldrHlpHeapDonate(void *pv, size_t cb);
154void * kldrHlpAlloc(size_t cb);
155void * kldrHlpAllocZ(size_t cb);
156void kldrHlpFree(void *pv);
157
158int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t cchVal);
159int kldrHlpGetEnvUZ(const char *pszVar, size_t *pcb);
160char *kldrHlpGetFilename(const char *pszFilename);
161char *kldrHlpGetSuff(const char *pszFilename);
162char *kldrHlpGetExt(const char *pszFilename);
163int kldrHlpIsFilenameOnly(const char *pszFilename);
164void kldrHlpExit(int rc);
165void kldrHlpSleep(unsigned cMillies);
166char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase);
167void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
168
169/** Assertion macro.
170 * Users should wrap it since this is ALWAYS compiled in. */
171#define kldrHlpAssert(expr) \
172 do { \
173 if (!(expr)) \
174 { \
175 kldrHlpAssertMsg(#expr, __FILE__, __LINE__, __FUNCTION__); \
176 kldrHlpBreakpoint(); \
177 } \
178 } while (0)
179
180
181/** @name Parameter validation macros
182 * @{ */
183
184/** Crash validation of a string argument. */
185#define KLDRHLP_VALIDATE_STRING(str) \
186 do { strlen(str); } while (0)
187
188/** Crash validation of an optional string argument. */
189#define KLDRHLP_VALIDATE_OPTIONAL_STRING(str) \
190 do { if (str) { KLDRHLP_VALIDATE_STRING(str); } } while (0)
191
192/** Return/Crash validation of an output buffer. */
193#define KLDRHLP_VALIDATE_BUFFER(buf, cb) \
194 do { \
195 if ((cb)) \
196 { \
197 uint8_t __b; \
198 uint8_t volatile * __pb = (uint8_t volatile *)(buf); \
199 size_t __cbPage1 = 0x1000 - ((uintptr_t)(__pb) & 0xfff); /* ASSUMES page size! */ \
200 __b = *__pb; *__pb = 0xff; *__pb = __b; \
201 if ((cb) > __cbPage1) \
202 { \
203 size_t __cb = (cb) - __cbPage1; \
204 __pb -= __cbPage1; \
205 for (;;) \
206 { \
207 __b = *__pb; *__pb = 0xff; *__pb = __b; \
208 if (__cb < 0x1000) \
209 break; \
210 __pb += 0x1000; \
211 __cb -= 0x1000; \
212 } \
213 } \
214 } \
215 else \
216 return KLDR_ERR_INVALID_PARAMETER; \
217 } while (0)
218
219/** Crash validation of an optional output buffer. */
220#define KLDRHLP_VALIDATE_OPTIONAL_BUFFER(buf, cb) \
221 do { \
222 if ((buf) != NULL && (cb) != 0) \
223 { \
224 KLDRHLP_VALIDATE_BUFFER(buf, cb); \
225 } \
226 } while (0)
227
228/** Return validation of an enum argument. */
229#define KLDRHLP_VALIDATE_ENUM(arg, enumname) \
230 do { \
231 if ((arg) <= enumname##_INVALID || (arg) >= enumname##_END) \
232 { \
233 return KLDR_ERR_INVALID_PARAMETER; \
234 } \
235 } while (0)
236
237/** Return validation of a flags argument. */
238#define KLDRHLP_VALIDATE_FLAGS(arg, AllowedMask) \
239 do { \
240 if ((arg) & ~(AllowedMask)) \
241 { \
242 return KLDR_ERR_INVALID_PARAMETER; \
243 } \
244 } while (0)
245
246/** @} */
247
248
249/** @} */
250
251#endif /* __kLdrHlp_h__ */
252
Note: See TracBrowser for help on using the repository browser.