/* $Id: kLdrHlp.h 2828 2006-10-22 18:21:04Z bird $ */ /** @file * * kLdr - The Dynamic Loader, Helper Functions. * * Copyright (c) 2006 knut st. osmundsen * * * This file is part of kLdr. * * kLdr is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * kLdr is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with kLdr; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #ifndef __kLdrHlp_h__ #define __kLdrHlp_h__ /** @defgroup grp_kLdrHlp kLdrHlp - Helper Functions * @internal * @{ */ /** Get the minimum of two values. */ #define KLDR_MIN(a, b) ((a) <= (b) ? (a) : (b)) /** Calculate the offset of a structure member. */ #define KLDR_OFFSETOF(strct, memb) ( (size_t)( ((strct *)0)->memb ) ) /** Align a size_t value. */ #define KLDR_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(size_t)((align) - 1) ) /** Align a void * value. */ #define KLDR_ALIGN_P(pv, align) ( (void *)( ((uintptr_t)(pv) + ((align) - 1)) & ~(uintptr_t)((align) - 1) ) ) /** @def KLDRHLP_LE2H_U16 * Unsigned 16-bit little-endian to host translation. */ /** @def KLDRHLP_LE2H_U32 * Unsigned 32-bit little-endian to host translation. */ #if 1 # define KLDRHLP_LE2H_U16(u16) ((uint16_t)(u16)) # define KLDRHLP_LE2H_U32(u32) ((uint32_t)(u32)) #else # define KLDRHLP_LE2H_U16(u16) ( (uint16_t) (((u16) >> 8) | ((u16) << 8)) ) # define KLDRHLP_LE2H_U32(u32) ( ( ((u32) & UINT32_C(0xff000000)) >> 24 ) \ | ( ((u32) & UINT32_C(0x00ff0000)) >> 8 ) \ | ( ((u32) & UINT32_C(0x0000ff00)) << 8 ) \ | ( ((u32) & UINT32_C(0x000000ff)) << 24 ) \ ) #endif /* * Compiler specific helpers. * (I.e. operations that tend to have compiler intrinsic implementations). */ #ifdef __GNUC__ /** memcmp */ # define kLdrHlpMemComp(a,b,c) __builtin_memcmp(a,b,c) /** memcpy */ # define kLdrHlpMemCopy(a,b,c) __builtin_memcpy(a,b,c) /** memset */ # define kLdrHlpMemSet(a,b,c) __builtin_memset(a,b,c) /** strlen */ # define kLdrHlpStrLen(a) __builtin_strlen(a) /** alloca */ # define kLdrHlpAllocA(a) __builtin_alloca(a) /** int3 */ # define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0) /** NULL */ # ifndef NULL # define NULL 0 # endif #endif #ifdef _MSC_VER # include # include # pragma intrinsic(memcmp, memcpy, memset, strlen, __debugbreak) /** memcmp */ # define kLdrHlpMemComp(a,b,c) memcmp(a,b,c) /** memcpy */ # define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c) /** memset */ # define kLdrHlpMemSet(a,b,c) memset(a,b,c) /** strlen */ # define kLdrHlpStrLen(a) strlen(a) /** alloca */ # define kLdrHlpAllocA(a) alloca(a) /** int3 */ # define kldrHlpBreakpoint() __debugbreak() /** NULL */ # ifndef NULL # define NULL 0 # endif #endif #if !defined(kLdrHlpMemComp) \ || !defined(kLdrHlpMemCopy) \ || !defined(kLdrHlpMemSet) \ || !defined(kLdrHlpStrLen) \ || !defined(kLdrHlpAllocA) \ || !defined(kldrHlpBreakpoint) # error "Needs porting to your compiler." #endif int kldrSemInit(void); int kldrSemTerm(void); int kldrSemRequest(void); int kldrSemRelease(void); int kldrSemGlobalRequest(void); int kldrSemGlobalRelease(void); int kldrPrivateAlloc(void *pv, size_t cb, unsigned fFlags, void **ppv); int kldrPrivateFree(void *pv, size_t cb); int kldrSharedAlloc(void *pv, size_t cb, unsigned fFlags, const char *pszFilename, void *File, void **ppv); int kldrSharedFree(void *pv, size_t cb); int kldrHlpHeapInit(void); void kldrHlpHeapTerm(void); void kldrHlpHeapDonate(void *pv, size_t cb); void * kldrHlpAlloc(size_t cb); void kldrHlpFree(void *pv); int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t *pcchVal); void kldrHlpExit(int rc); void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction); /** Assertion macro. * Users should wrap it since this is ALWAYS compiled in. */ #define kldrHlpAssert(expr) \ do { \ if (!(expr)) \ { \ kldrHlpAssertMsg(#expr, __FILE__, __LINE__, __FUNCTION__); \ kldrHlpBreakpoint(); \ } \ } while (0) /** @} */ #endif /* __kLdrHlp_h__ */