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
RevLine 
[2826]1/* $Id: kLdrHlp.h 2832 2006-10-24 22:26:01Z 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))
[2827]37/** Calculate the offset of a structure member. */
38#define KLDR_OFFSETOF(strct, memb) ( (size_t)( ((strct *)0)->memb ) )
[2825]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) ) )
[2827]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
[2821]58
[2825]59/*
60 * Compiler specific helpers.
61 * (I.e. operations that tend to have compiler intrinsic implementations).
62 */
63#ifdef __GNUC__
[2832]64/** memchr */
65# define kLdrHlpMemChr(a,b,c) __builtin_memchr(a,b,c)
[2825]66/** memcmp */
[2828]67# define kLdrHlpMemComp(a,b,c) __builtin_memcmp(a,b,c)
[2825]68/** memcpy */
[2828]69# define kLdrHlpMemCopy(a,b,c) __builtin_memcpy(a,b,c)
[2825]70/** memset */
[2828]71# define kLdrHlpMemSet(a,b,c) __builtin_memset(a,b,c)
[2832]72/** strchr */
73# define kLdrHlpStrChr(a, b) __builtin_strchr(a, b)
[2825]74/** strlen */
[2828]75# define kLdrHlpStrLen(a) __builtin_strlen(a)
[2825]76/** alloca */
[2828]77# define kLdrHlpAllocA(a) __builtin_alloca(a)
[2825]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)
[2832]90/** memchr */
91# define kLdrHlpMemChr_needed
[2825]92/** memcmp */
[2828]93# define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
[2825]94/** memcpy */
[2828]95# define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
[2825]96/** memset */
[2828]97# define kLdrHlpMemSet(a,b,c) memset(a,b,c)
[2825]98/** strlen */
[2828]99# define kLdrHlpStrLen(a) strlen(a)
[2832]100/** strchr */
101# define kLdrHlpStrChr_needed
[2825]102/** alloca */
[2828]103# define kLdrHlpAllocA(a) alloca(a)
[2825]104/** int3 */
105# define kldrHlpBreakpoint() __debugbreak()
106/** NULL */
107# ifndef NULL
108# define NULL 0
109# endif
110#endif
111
[2832]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) \
[2828]121 || !defined(kLdrHlpMemCopy) \
122 || !defined(kLdrHlpMemSet) \
[2832]123 || (!defined(kLdrHlpStrChr) && !defined(kLdrHlpStrChr_needed)) \
[2828]124 || !defined(kLdrHlpStrLen) \
125 || !defined(kLdrHlpAllocA) \
[2825]126 || !defined(kldrHlpBreakpoint)
127# error "Needs porting to your compiler."
128#endif
129
[2830]130int kldrHlpSemInit(void);
131void kldrHlpSemTerm(void);
132int kldrHlpSemRequest(void);
133void kldrHlpSemRelease(void);
[2821]134
[2830]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);
[2821]138
[2825]139int kldrHlpHeapInit(void);
140void kldrHlpHeapTerm(void);
141void kldrHlpHeapDonate(void *pv, size_t cb);
142void * kldrHlpAlloc(size_t cb);
143void kldrHlpFree(void *pv);
[2821]144
[2825]145int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t *pcchVal);
146void kldrHlpExit(int rc);
[2830]147void kldrHlpSleep(unsigned cMillies);
[2825]148void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
[2821]149
[2825]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)
[2821]160
161
[2825]162/** @} */
[2821]163
164
[2825]165#endif /* __kLdrHlp_h__ */
[2821]166
Note: See TracBrowser for help on using the repository browser.