Changeset 3545 for trunk


Ignore:
Timestamp:
Aug 25, 2007, 7:15:18 PM (18 years ago)
Author:
bird
Message:

Helpers and macros.

Location:
trunk/kStuff
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/kStuff/include/k/kDefs.h

    r3541 r3545  
    379379#define K_BIT64(bit)            ( KU64_C(1) << (bit))
    380380
    381 /** @} */
    382 
    383 #endif
    384 
     381
     382/** @name Parameter validation macros
     383 * @{ */
     384
     385/** Return/Crash validation of a string argument. */
     386#define K_VALIDATE_STRING(str) \
     387    do { \
     388        if (!K_VALID_PTR(str)) \
     389            return KERR_INVALID_POINTER; \
     390        kHlpStrLen(str); \
     391    } while (0)
     392
     393/** Return/Crash validation of an optional string argument. */
     394#define K_VALIDATE_OPTIONAL_STRING(str) \
     395    do { \
     396        if (str) \
     397            K_VALIDATE_STRING(str); \
     398    } while (0)
     399
     400/** Return/Crash validation of an output buffer. */
     401#define K_VALIDATE_BUFFER(buf, cb) \
     402    do { \
     403        if (!K_VALID_PTR(buf)) \
     404            return KERR_INVALID_POINTER; \
     405        if ((cb) != 0) \
     406        { \
     407            KU8             __b; \
     408            KU8 volatile   *__pb = (KU8 volatile *)(buf); \
     409            KSIZE           __cbPage1 = 0x1000 - ((KUPTR)(__pb) & 0xfff); /* ASSUMES page size! */ \
     410            __b = *__pb; *__pb = 0xff; *__pb = __b; \
     411            if ((cb) > __cbPage1) \
     412            { \
     413                KSIZE __cb = (cb) - __cbPage1; \
     414                __pb -= __cbPage1; \
     415                for (;;) \
     416                { \
     417                    __b = *__pb; *__pb = 0xff; *__pb = __b; \
     418                    if (__cb < 0x1000) \
     419                        break; \
     420                    __pb += 0x1000; \
     421                    __cb -= 0x1000; \
     422                } \
     423            } \
     424        } \
     425        else \
     426            return KERR_INVALID_PARAMETER; \
     427    } while (0)
     428
     429/** Return/Crash validation of an optional output buffer. */
     430#define K_VALIDATE_OPTIONAL_BUFFER(buf, cb) \
     431    do { \
     432        if ((buf) && (cb) != 0) \
     433            K_VALIDATE_BUFFER(buf, cb); \
     434    } while (0)
     435
     436/** Return validation of an enum argument. */
     437#define K_VALIDATE_ENUM(arg, enumname) \
     438    do { \
     439        if ((arg) <= enumname##_INVALID || (arg) >= enumname##_END) \
     440            return KERR_INVALID_PARAMETER; \
     441    } while (0)
     442
     443/** Return validation of a flags argument. */
     444#define K_VALIDATE_FLAGS(arg, AllowedMask) \
     445    do { \
     446        if ((arg) & ~(AllowedMask)) \
     447            return KERR_INVALID_PARAMETER; \
     448    } while (0)
     449
     450/** @} */
     451
     452/** @} */
     453
     454#endif
     455
  • trunk/kStuff/include/k/kTypes.h

    r3541 r3545  
    326326#define KFOFF_PRI               KI64_PRI
    327327
     328
     329/**
     330 * Memory Protection.
     331 */
     332typedef enum KPROT
     333{
     334    /** The usual invalid 0. */
     335    KPROT_INVALID = 0,
     336    /** No access (page not present). */
     337    KPROT_NOACCESS,
     338    /** Read only. */
     339    KPROT_READONLY,
     340    /** Read & write. */
     341    KPROT_READWRITE,
     342    /** Read & copy on write. */
     343    KPROT_WRITECOPY,
     344    /** Execute only. */
     345    KPROT_EXECUTE,
     346    /** Execute & read. */
     347    KPROT_EXECUTE_READ,
     348    /** Execute, read & write. */
     349    KPROT_EXECUTE_READWRITE,
     350    /** Execute, read & copy on write. */
     351    KPROT_EXECUTE_WRITECOPY,
     352    /** The usual end value. (exclusive) */
     353    KPROT_END,
     354    /** Blow the type up to 32-bits. */
     355    KPROT_32BIT_HACK = 0x7fffffff
     356} KPROT;
     357/** Pointer to a memory protection enum. */
     358typedef KPROT                  *PKPROT;
     359/** Pointer to a const memory protection enum. */
     360typedef KPROT const            *PCKPROT;
     361
     362/** Boolean.
     363 * This can be used as a tri-state type, but then you *must* do == checks. */
     364typedef KI8                     KBOOL;
     365/** Pointer to a boolean value. */
     366typedef KBOOL                  *PKBOOL;
     367/** Pointer to a const boolean value. */
     368typedef KBOOL const            *PCKBOOL;
     369/** Maxium value the KBOOL type can hold (officially). */
     370#define KBOOL_MIN               KI8_C(-1)
     371/** Maxium value the KBOOL type can hold (officially). */
     372#define KBOOL_MAX               KI8_C(1)
     373/** The KBOOL printf format. */
     374#define KBOOL_PRI               KU8_PRI
     375/** Boolean true constant. */
     376#define K_TRUE                  KI8_C(1)
     377/** Boolean false constant. */
     378#define K_FALSE                 KI8_C(0)
     379/** Boolean unknown constant (the third state). */
     380#define K_UNKNOWN               KI8_C(-1)
     381
     382
    328383/** @} */
    329384
  • trunk/kStuff/kRdr/kRdrFile.c

    r3544 r3545  
    804804    return 0x1000;
    805805
    806 #elif defined(__WIN__)
     806#elif K_OS == K_OS_WINDOWS
    807807    SYSTEM_INFO SysInfo;
    808808    GetSystemInfo(&SysInfo);
     
    851851        pRdrFile->off = ulNew;
    852852
    853 #elif defined(__WIN__)
     853#elif K_OS == K_OS_WINDOWS
    854854        LONG offHigh = 0;
    855855        LONG offLow;
     
    886886    /* check for underflow */
    887887    if (pRdrFile->cMappings <= 0)
    888 #if defined(__OS2__) || defined(__WIN__)
     888#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
    889889        return ERROR_INVALID_PARAMETER;
    890890#else
     
    918918        pRdrFile->pvMapping = kHlpAlloc(cb);
    919919        if (!pRdrFile->pvMapping)
    920 #if defined(__OS2__) || defined(__WIN__)
     920#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
    921921            return ERROR_NOT_ENOUGH_MEMORY;
    922922#else
     
    960960        }
    961961
    962 #elif defined(__WIN__)
     962#elif K_OS == K_OS_WINDOWS
    963963        LONG offHigh;
    964964        LONG offLow;
     
    10001000    }
    10011001
    1002 #elif defined(__WIN__)
     1002#elif K_OS == K_OS_WINDOWS
    10031003    {
    10041004    DWORD cbRead = 0;
     
    10351035    rc = DosClose(pRdrFile->File);
    10361036
    1037 #elif defined(__WIN__)
     1037#elif K_OS == K_OS_WINDOWS
    10381038    rc = 0;
    10391039    if (!CloseHandle(pRdrFile->File))
     
    11021102    cb = Info.cbFile;
    11031103
    1104 #elif defined(__WIN__)
     1104#elif K_OS == K_OS_WINDOWS
    11051105    SECURITY_ATTRIBUTES SecAttr;
    11061106    DWORD               High;
     
    11491149    pRdrFile = (PKRDRFILE)kHlpAlloc(sizeof(*pRdrFile) + cchFilename);
    11501150    if (!pRdrFile)
    1151 #if defined(__OS2__)
     1151#if K_OS == K_OS_OS2
    11521152    {
    11531153        DosClose(File);
    11541154        return ERROR_NOT_ENOUGH_MEMORY;
    11551155    }
    1156 #elif defined(__WIN__)
     1156#elif K_OS == K_OS_WINDOWS
    11571157    {
    11581158        CloseHandle(File);
Note: See TracChangeset for help on using the changeset viewer.