Changeset 3573 for trunk/kStuff/kLdr/kLdrHlp.h
- Timestamp:
- Aug 31, 2007, 6:09:23 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kStuff/kLdr/kLdrHlp.h
r3571 r3573 1 /* $Id$ */2 /** @file3 *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 modify12 * it under the terms of the GNU General Public License as published by13 * the Free Software Foundation; either version 2 of the License, or14 * (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 of18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the19 * GNU General Public License for more details.20 *21 * You should have received a copy of the GNU General Public License22 * along with kLdr; if not, write to the Free Software23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA24 *25 */26 27 28 #ifndef __kLdrHlp_h__29 #define __kLdrHlp_h__30 31 /** @defgroup grp_kLdrHlp kLdrHlp - Helper Functions32 * @internal33 * @{ */34 35 /*36 * Compiler specific helpers / CRT.37 * (I.e. operations that tend to have compiler intrinsic implementations).38 */39 #ifndef KLDR_USE_CRT40 41 # ifdef __GNUC__42 /** memchr */43 # define kLdrHlpMemChr(a,b,c) __builtin_memchr(a,b,c)44 /** memcmp */45 # define kLdrHlpMemComp(a,b,c) __builtin_memcmp(a,b,c)46 /** memcpy */47 # define kLdrHlpMemCopy(a,b,c) __builtin_memcpy(a,b,c)48 /** memmove */49 /*# define kLdrHlpMemMove(a,b,c) __builtin_memmove(a,b,c)*/50 # define kLdrHlpMemMove_needed51 /** memset */52 # define kLdrHlpMemSet(a,b,c) __builtin_memset(a,b,c)53 /** strchr */54 # define kLdrHlpStrChr(a, b) __builtin_strchr(a, b)55 /** strcmp */56 # define kLdrHlpStrComp(a, b) __builtin_strcmp(a, b)57 /** strncmp */58 # define kLdrHlpStrNComp(a,b,c) __builtin_strncmp(a, b, c)59 /** strlen */60 # define kLdrHlpStrLen(a) __builtin_strlen(a)61 /** alloca */62 # define kLdrHlpAllocA(a) __builtin_alloca(a)63 /** int3 */64 # define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)65 /** NULL */66 # ifndef NULL67 # define NULL 068 # endif69 # endif70 71 # ifdef _MSC_VER72 # include <string.h>73 # include <malloc.h>74 # pragma intrinsic(memcmp, memcpy, memset, strcmp, strlen, __debugbreak)75 /** memchr */76 # define kLdrHlpMemChr_needed77 /** memcmp */78 # define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)79 /** memcpy */80 # define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)81 /** memmove */82 # define kLdrHlpMemMove_needed83 /** memset */84 # define kLdrHlpMemSet(a,b,c) memset(a,b,c)85 /** strcmp */86 # define kLdrHlpStrComp(a, b) strcmp(a, b)87 /** strncmp */88 # define kLdrHlpStrNComp_needed89 /** strlen */90 # define kLdrHlpStrLen(a) strlen(a)91 /** strchr */92 # define kLdrHlpStrChr_needed93 /** alloca */94 # define kLdrHlpAllocA(a) alloca(a)95 /** int3 */96 # define kldrHlpBreakpoint() __debugbreak()97 /** NULL */98 # ifndef NULL99 # define NULL 0100 # endif101 # endif102 103 # ifdef kLdrHlpStrChr_needed104 char *kLdrHlpStrChr(const char *psz, int ch);105 # endif106 # ifdef kLdrHlpStrChr_needed107 int kLdrHlpStrNComp(const char *psz1, const char *psz2, KSIZE cch);108 # endif109 # ifdef kLdrHlpMemChr_needed110 void *kLdrHlpMemChr(const void *pv, int ch, KSIZE cb);111 # endif112 # ifdef kLdrHlpMemMove_needed113 void *kLdrHlpMemMove(void *pv1, const void *pv2, KSIZE cb);114 # endif115 116 117 #else /* KLDR_USE_CRT */118 119 # include <string.h>120 # include <stdlib.h>121 # ifdef _MSC_VER122 # include <malloc.h>123 # endif124 125 # define kLdrHlpMemChr(a,b,c) memchr(a,b,c)126 /** memcmp */127 # define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)128 /** memcpy */129 # define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)130 /** memmove */131 # define kLdrHlpMemMove(a,b,c) memmove(a,b,c)132 /** memset */133 # define kLdrHlpMemSet(a,b,c) memset(a,b,c)134 /** strchr */135 # define kLdrHlpStrChr(a, b) strchr(a, b)136 /** strcmp */137 # define kLdrHlpStrComp(a, b) strcmp(a, b)138 /** strncmp */139 # define kLdrHlpStrNComp(a,b,c) strncmp(a, b, c)140 /** strlen */141 # define kLdrHlpStrLen(a) strlen(a)142 /** alloca */143 # define kLdrHlpAllocA(a) alloca(a)144 /** int3 */145 # ifdef __GNUC__146 # define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)147 # endif148 # ifdef _MSC_VER149 # define kldrHlpBreakpoint() __debugbreak()150 # endif151 152 #endif153 154 #if (!defined(kLdrHlpMemChr) && !defined(kLdrHlpStrChr_needed))\155 || !defined(kLdrHlpMemComp) \156 || !defined(kLdrHlpMemCopy) \157 || !defined(kLdrHlpMemSet) \158 || (!defined(kLdrHlpStrChr) && !defined(kLdrHlpStrChr_needed)) \159 || !defined(kLdrHlpStrComp) \160 || (!defined(kLdrHlpStrNComp) && !defined(kLdrHlpStrNComp_needed)) \161 || !defined(kLdrHlpStrLen) \162 || !defined(kLdrHlpAllocA) \163 || !defined(kldrHlpBreakpoint)164 # error "Needs porting to your compiler."165 #endif166 167 #ifdef __cplusplus168 extern "C" {169 #endif170 171 KSIZE kLdrHlpStrNLen(const char *psz, KSIZE cchMax);172 int kLdrHlpMemIComp(const void *pv1, const void *pv2, KSIZE cb);173 int kLdrHlpStrIComp(const char *pv1, const char *pv2);174 175 int kldrHlpSemInit(void);176 void kldrHlpSemTerm(void);177 int kldrHlpSemRequest(void);178 void kldrHlpSemRelease(void);179 180 int kldrHlpPageAlloc(void **ppv, KSIZE cb, KPROT enmProt, unsigned fFixed);181 int kldrHlpPageProtect(void *pv, KSIZE cb, KPROT enmProt);182 int kldrHlpPageFree(void *pv, KSIZE cb);183 184 int kldrHlpHeapInit(void);185 void kldrHlpHeapTerm(void);186 void kldrHlpHeapDonate(void *pv, KSIZE cb);187 void * kldrHlpAlloc(KSIZE cb);188 void * kldrHlpAllocZ(KSIZE cb);189 void kldrHlpFree(void *pv);190 1 191 2 int kldrHlpGetEnv(const char *pszVar, char *pszVal, KSIZE cchVal); 192 3 int kldrHlpGetEnvUZ(const char *pszVar, KSIZE *pcb); 193 char *kldrHlpGetFilename(const char *pszFilename); 194 char *kldrHlpGetSuff(const char *pszFilename); 195 char *kldrHlpGetExt(const char *pszFilename); 196 int kldrHlpIsFilenameOnly(const char *pszFilename); 4 197 5 void kldrHlpExit(int rc); 198 6 void kldrHlpSleep(unsigned cMillies); 7 199 8 char *kldrHlpInt2Ascii(char *psz, KSIZE cch, long lVal, unsigned iBase); 200 void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);201 9 202 #ifdef __cplusplus203 }204 #endif205 206 207 /** Assertion macro.208 * Users should wrap it since this is ALWAYS compiled in. */209 #define kldrHlpAssert(expr) \210 do { \211 if (!(expr)) \212 { \213 kldrHlpAssertMsg(#expr, __FILE__, __LINE__, __FUNCTION__); \214 kldrHlpBreakpoint(); \215 } \216 } while (0)217 218 219 /** @name Parameter validation macros220 * @{ */221 222 /** Crash validation of a string argument. */223 #define KLDRHLP_VALIDATE_STRING(str) \224 do { kLdrHlpStrLen(str); } while (0)225 226 /** Crash validation of an optional string argument. */227 #define KLDRHLP_VALIDATE_OPTIONAL_STRING(str) \228 do { if (str) { KLDRHLP_VALIDATE_STRING(str); } } while (0)229 230 /** Return/Crash validation of an output buffer. */231 #define KLDRHLP_VALIDATE_BUFFER(buf, cb) \232 do { \233 if ((cb)) \234 { \235 KU8 __b; \236 KU8 volatile * __pb = (KU8 volatile *)(buf); \237 KSIZE __cbPage1 = 0x1000 - ((KUPTR)(__pb) & 0xfff); /* ASSUMES page size! */ \238 __b = *__pb; *__pb = 0xff; *__pb = __b; \239 if ((cb) > __cbPage1) \240 { \241 KSIZE __cb = (cb) - __cbPage1; \242 __pb -= __cbPage1; \243 for (;;) \244 { \245 __b = *__pb; *__pb = 0xff; *__pb = __b; \246 if (__cb < 0x1000) \247 break; \248 __pb += 0x1000; \249 __cb -= 0x1000; \250 } \251 } \252 } \253 else \254 return KLDR_ERR_INVALID_PARAMETER; \255 } while (0)256 257 /** Crash validation of an optional output buffer. */258 #define KLDRHLP_VALIDATE_OPTIONAL_BUFFER(buf, cb) \259 do { \260 if ((buf) != NULL && (cb) != 0) \261 { \262 KLDRHLP_VALIDATE_BUFFER(buf, cb); \263 } \264 } while (0)265 266 /** Return validation of an enum argument. */267 #define KLDRHLP_VALIDATE_ENUM(arg, enumname) \268 do { \269 if ((arg) <= enumname##_INVALID || (arg) >= enumname##_END) \270 { \271 return KLDR_ERR_INVALID_PARAMETER; \272 } \273 } while (0)274 275 /** Return validation of a flags argument. */276 #define KLDRHLP_VALIDATE_FLAGS(arg, AllowedMask) \277 do { \278 if ((arg) & ~(AllowedMask)) \279 { \280 return KLDR_ERR_INVALID_PARAMETER; \281 } \282 } while (0)283 284 /** @} */285 286 287 /** @} */288 289 #endif /* __kLdrHlp_h__ */290
Note:
See TracChangeset
for help on using the changeset viewer.