source: trunk/kLdr/kLdrHlp.h@ 2852

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

kLdrMod done.

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