source: trunk/kLdr/kLdrHlp.h@ 2826

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

keyword expansion: Id

  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1/* $Id: kLdrHlp.h 2826 2006-10-22 15:58:55Z 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/** Get the minimum of two values. */
35#define KLDR_MIN(a, b) ((a) <= (b) ? (a) : (b))
36
37/** Align a size_t value. */
38#define KLDR_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(size_t)((align) - 1) )
39/** Align a void * value. */
40#define KLDR_ALIGN_P(pv, align) ( (void *)( ((uintptr_t)(pv) + ((align) - 1)) & ~(uintptr_t)((align) - 1) ) )
41
42/*
43 * Compiler specific helpers.
44 * (I.e. operations that tend to have compiler intrinsic implementations).
45 */
46#ifdef __GNUC__
47/** memcmp */
48# define kLdrMemComp(a,b,c) __builtin_memcmp(a,b,c)
49/** memcpy */
50# define kLdrMemCopy(a,b,c) __builtin_memcpy(a,b,c)
51/** memset */
52# define kLdrMemSet(a,b,c) __builtin_memset(a,b,c)
53/** strlen */
54# define kLdrStrLen(a) __builtin_strlen(a)
55/** alloca */
56# define kLdrAllocA(a) __builtin_alloca(a)
57/** int3 */
58# define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
59/** NULL */
60# ifndef NULL
61# define NULL 0
62# endif
63#endif
64
65#ifdef _MSC_VER
66# include <string.h>
67# include <malloc.h>
68# pragma intrinsic(memcmp, memcpy, memset, strlen, __debugbreak)
69/** memcmp */
70# define kLdrMemComp(a,b,c) memcmp(a,b,c)
71/** memcpy */
72# define kLdrMemCopy(a,b,c) memcpy(a,b,c)
73/** memset */
74# define kLdrMemSet(a,b,c) memset(a,b,c)
75/** strlen */
76# define kLdrStrLen(a) strlen(a)
77/** alloca */
78# define kLdrAllocA(a) alloca(a)
79/** int3 */
80# define kldrHlpBreakpoint() __debugbreak()
81/** NULL */
82# ifndef NULL
83# define NULL 0
84# endif
85#endif
86
87#if !defined(kLdrMemComp) \
88 || !defined(kLdrMemCopy) \
89 || !defined(kLdrMemSet) \
90 || !defined(kLdrStrLen) \
91 || !defined(kLdrAllocA) \
92 || !defined(kldrHlpBreakpoint)
93# error "Needs porting to your compiler."
94#endif
95
96
97int kldrSemInit(void);
98int kldrSemTerm(void);
99int kldrSemRequest(void);
100int kldrSemRelease(void);
101int kldrSemGlobalRequest(void);
102int kldrSemGlobalRelease(void);
103
104int kldrPrivateAlloc(void *pv, size_t cb, unsigned fFlags, void **ppv);
105int kldrPrivateFree(void *pv, size_t cb);
106int kldrSharedAlloc(void *pv, size_t cb, unsigned fFlags, const char *pszFilename, void *File, void **ppv);
107int kldrSharedFree(void *pv, size_t cb);
108
109int kldrHlpHeapInit(void);
110void kldrHlpHeapTerm(void);
111void kldrHlpHeapDonate(void *pv, size_t cb);
112void * kldrHlpAlloc(size_t cb);
113void kldrHlpFree(void *pv);
114
115int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t *pcchVal);
116void kldrHlpExit(int rc);
117void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
118
119/** Assertion macro.
120 * Users should wrap it since this is ALWAYS compiled in. */
121#define kldrHlpAssert(expr) \
122 do { \
123 if (!(expr)) \
124 { \
125 kldrHlpAssertMsg(#expr, __FILE__, __LINE__, __FUNCTION__); \
126 kldrHlpBreakpoint(); \
127 } \
128 } while (0)
129
130
131/** @} */
132
133
134#endif /* __kLdrHlp_h__ */
135
Note: See TracBrowser for help on using the repository browser.