source: trunk/kLdr/kLdrHlp.h@ 2858

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

bugfixing - dinner break.

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