Changeset 3545 for trunk/kStuff/include


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

Helpers and macros.

Location:
trunk/kStuff/include/k
Files:
2 added
2 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
Note: See TracChangeset for help on using the changeset viewer.