source: trunk/kLdr/kLdrHlp.h@ 2832

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

specified more of the api.

  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1/* $Id: kLdrHlp.h 2832 2006-10-24 22:26:01Z 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);
148void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
149
150/** Assertion macro.
151 * Users should wrap it since this is ALWAYS compiled in. */
152#define kldrHlpAssert(expr) \
153 do { \
154 if (!(expr)) \
155 { \
156 kldrHlpAssertMsg(#expr, __FILE__, __LINE__, __FUNCTION__); \
157 kldrHlpBreakpoint(); \
158 } \
159 } while (0)
160
161
162/** @} */
163
164
165#endif /* __kLdrHlp_h__ */
166
Note: See TracBrowser for help on using the repository browser.