source: trunk/kLdr/kLdrHlp.h@ 2944

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

split up kLdrHlp.c and kLdr.c to make it more flexible (like using the module interpreters without the dynamic loader bit and similar).

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