source: trunk/kLdr/kLdrHlp.h@ 2836

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

more prototyping. (And avoid 64-bit div/rem)

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