source: trunk/kLdr/kLdrHlp.h@ 2827

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

image format headers.

  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/* $Id: kLdrHlp.h 2827 2006-10-22 18:05:28Z 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/** memcmp */
65# define kLdrMemComp(a,b,c) __builtin_memcmp(a,b,c)
66/** memcpy */
67# define kLdrMemCopy(a,b,c) __builtin_memcpy(a,b,c)
68/** memset */
69# define kLdrMemSet(a,b,c) __builtin_memset(a,b,c)
70/** strlen */
71# define kLdrStrLen(a) __builtin_strlen(a)
72/** alloca */
73# define kLdrAllocA(a) __builtin_alloca(a)
74/** int3 */
75# define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
76/** NULL */
77# ifndef NULL
78# define NULL 0
79# endif
80#endif
81
82#ifdef _MSC_VER
83# include <string.h>
84# include <malloc.h>
85# pragma intrinsic(memcmp, memcpy, memset, strlen, __debugbreak)
86/** memcmp */
87# define kLdrMemComp(a,b,c) memcmp(a,b,c)
88/** memcpy */
89# define kLdrMemCopy(a,b,c) memcpy(a,b,c)
90/** memset */
91# define kLdrMemSet(a,b,c) memset(a,b,c)
92/** strlen */
93# define kLdrStrLen(a) strlen(a)
94/** alloca */
95# define kLdrAllocA(a) alloca(a)
96/** int3 */
97# define kldrHlpBreakpoint() __debugbreak()
98/** NULL */
99# ifndef NULL
100# define NULL 0
101# endif
102#endif
103
104#if !defined(kLdrMemComp) \
105 || !defined(kLdrMemCopy) \
106 || !defined(kLdrMemSet) \
107 || !defined(kLdrStrLen) \
108 || !defined(kLdrAllocA) \
109 || !defined(kldrHlpBreakpoint)
110# error "Needs porting to your compiler."
111#endif
112
113
114int kldrSemInit(void);
115int kldrSemTerm(void);
116int kldrSemRequest(void);
117int kldrSemRelease(void);
118int kldrSemGlobalRequest(void);
119int kldrSemGlobalRelease(void);
120
121int kldrPrivateAlloc(void *pv, size_t cb, unsigned fFlags, void **ppv);
122int kldrPrivateFree(void *pv, size_t cb);
123int kldrSharedAlloc(void *pv, size_t cb, unsigned fFlags, const char *pszFilename, void *File, void **ppv);
124int kldrSharedFree(void *pv, size_t cb);
125
126int kldrHlpHeapInit(void);
127void kldrHlpHeapTerm(void);
128void kldrHlpHeapDonate(void *pv, size_t cb);
129void * kldrHlpAlloc(size_t cb);
130void kldrHlpFree(void *pv);
131
132int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t *pcchVal);
133void kldrHlpExit(int rc);
134void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
135
136/** Assertion macro.
137 * Users should wrap it since this is ALWAYS compiled in. */
138#define kldrHlpAssert(expr) \
139 do { \
140 if (!(expr)) \
141 { \
142 kldrHlpAssertMsg(#expr, __FILE__, __LINE__, __FUNCTION__); \
143 kldrHlpBreakpoint(); \
144 } \
145 } while (0)
146
147
148/** @} */
149
150
151#endif /* __kLdrHlp_h__ */
152
Note: See TracBrowser for help on using the repository browser.