source: trunk/kLdr/kLdrHlp.h@ 2893

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

interface adjustment (in progress).

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