home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre3.z / postgre3 / src / lib / H / tmp / c.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-06  |  22.7 KB  |  970 lines

  1. /* ----------------------------------------------------------------
  2.  *   FILE
  3.  *    c.h
  4.  *
  5.  *   DESCRIPTION
  6.  *    Fundamental C definitions.  This is included by nearly
  7.  *    every .c file in postgres.
  8.  *
  9.  *   TABLE OF CONTENTS
  10.  *
  11.  *    When adding stuff to this file, please try and put stuff
  12.  *    into the relevent section, or add new sections as appropriate.
  13.  *
  14.  *    section    description
  15.  *    -------    ------------------------------------------------
  16.  *    1)    palloc debugging defines
  17.  *    2)    bool, true, false, TRUE, FALSE
  18.  *    3)    __STDC__, non-ansi C definitions:
  19.  *        Pointer typedef, NULL
  20.  *        cpp magic macros
  21.  *        ARGS() prototype macro
  22.  *        type prefixes: const, signed, volatile, inline
  23.  *
  24.  *    4)    standard system types
  25.  *    5)    malloc, free, malloc_debug stuff, new, delete, ALLOCATE
  26.  *    6)    IsValid macros for system types
  27.  *    7)    offsetof, lengthof, endof 
  28.  *    8)    exception handling definitions, Assert, Trap, etc macros
  29.  *    9)    Min, Max, Abs macros
  30.  *    10)    LintCast, RevisionId, RcsId, SccsId macros
  31.  *    11)    SymbolDecl, ExternDecl macros
  32.  *    12)    externs
  33.  *
  34.  *   NOTES
  35.  *    This file reorganized as a result of eliminating wastful
  36.  *    nested IsValid calls -cim 4/27/91
  37.  *
  38.  *    This file is MACHINE AND COMPILER dependent!!!  (For now.)
  39.  *
  40.  *   IDENTIFICATION
  41.  *    $Header: /private/postgres/src/lib/H/tmp/RCS/c.h,v 1.39 1992/07/13 07:40:12 hong Exp $
  42.  * ----------------------------------------------------------------
  43.  */
  44.  
  45. #ifndef    CIncluded        /* Include this file only once */
  46. #define CIncluded    1
  47.  
  48. #define C_H    "$Header: /private/postgres/src/lib/H/tmp/RCS/c.h,v 1.39 1992/07/13 07:40:12 hong Exp $"
  49.  
  50. /* ----------------------------------------------------------------
  51.  *        Section 1:  palloc debugging defines
  52.  * ----------------------------------------------------------------
  53.  */
  54.  
  55. /* ----------------
  56.  *    file / line macros
  57.  *
  58.  *    _FLD_         File/Line Data
  59.  *    _FLV_         File/Line Variables
  60.  *    _FLV_DCL_     File/Line Variable Declarations
  61.  *
  62.  *    the FLD0 and FLV0 macros are for functions taking no arguments
  63.  * ----------------
  64.  */
  65. #define _FLD_         __FILE__, __LINE__, 
  66. #define _FLD0_         __FILE__, __LINE__
  67. #define _FLV_         _FL_file, int _FL_line,
  68. #define _FLV0_         _FL_file, int _FL_line
  69. #define _FLV_DCL_     char *_FL_file; int _FL_line;
  70. #define _FL_PRINT_    printf("f: %s l: %d ", _FL_file, _FL_line)
  71.  
  72. /* ----------------
  73.  *    storage managers
  74.  *
  75.  *    These are experimental and are not supported in the code that
  76.  *    we distribute to other sites.
  77.  * ----------------
  78.  */
  79. #undef SONY_JUKEBOX
  80. #undef MAIN_MEMORY
  81.  
  82. /* ----------------
  83.  *    allocation debugging stuff
  84.  * ----------------
  85.  */
  86. #undef PALLOC_DEBUG 
  87.  
  88. #ifdef PALLOC_DEBUG
  89. #define palloc(size)       palloc_debug(_FLD_ size)
  90. #define pfree(ptr)         pfree_debug(_FLD_ ptr)
  91. #define MemoryContextAlloc(context, size) \
  92.     MemoryContextAlloc_Debug(_FLD_ context, size)
  93. #define MemoryContextFree(context, ptr)  \
  94.     MemoryContextFree_Debug(_FLD_ context, ptr)
  95. #define AllocSetReset(set) AllocSetReset_debug(_FLD_ set)
  96. #define malloc(size)        malloc_debug(_FLD_ size)
  97. #define free(ptr)        free_debug(_FLD_ ptr)
  98. #endif /* PALLOC_DEBUG */
  99.  
  100. /* -------------------
  101.  *    buffer pool pin count debugging stuff
  102.  * -------------------
  103.  */
  104. #undef BUFMGR_DEBUG
  105.  
  106. #ifdef BUFMGR_DEBUG
  107. #define IncrBufferRefCount(buffer)    IncrBufferRefCount_Debug(_FLD_ buffer)
  108. #define ReleaseBuffer(buffer)        ReleaseBuffer_Debug(_FLD_ buffer)
  109. #define ReleaseAndReadBuffer(buffer, relation, blockNum) \
  110.         ReleaseAndReadBuffer_Debug(_FLD_ buffer, relation, blockNum)
  111. #define ReadBuffer(reln, blockNum)    ReadBuffer_Debug(_FLD_ reln, blockNum)
  112. #define WriteBuffer(buffer)        WriteBuffer_Debug(_FLD_ buffer)
  113. /*
  114. #define PinBuffer(buf)            PinBuffer_Debug(_FLD_ buf)
  115. #define UnpinBuffer(buf)        UnpinBuffer_Debug(_FLD_ buf)
  116. */
  117. #endif /* BUFMGR_DEBUG */
  118.  
  119. /*
  120.  * Begin COMPILER DEPENDENT section
  121.  */
  122.  
  123. /* ----------------------------------------------------------------
  124.  *        Section 2:  bool, true, false, TRUE, FALSE
  125.  * ----------------------------------------------------------------
  126.  */
  127. /*
  128.  * bool --
  129.  *    Boolean value, either true or false.
  130.  *
  131.  * used to be:
  132.  *
  133.  * typedef enum bool {
  134.  *    false,        must be first, to be 0
  135.  *    true
  136.  * } bool;
  137.  *
  138.  * this may soon be moved to postgres.h -cim
  139.  */
  140. #define bool    char
  141. #define false    ((char) 0)
  142. #define true    ((char) 1)
  143. typedef bool    *BoolPtr;
  144.  
  145. #define TRUE    1
  146. #define FALSE    0
  147.  
  148. /* ----------------------------------------------------------------
  149.  *        Section 3: __STDC__, non-ansi C definitions:
  150.  *
  151.  *        cpp magic macros
  152.  *        ARGS() prototype macro
  153.  *        Pointer typedef, NULL
  154.  *        type prefixes: const, signed, volatile, inline
  155.  * ----------------------------------------------------------------
  156.  */
  157.  
  158. #ifdef    __STDC__ /* ANSI C */
  159.  
  160. #define PROTOTYPES
  161. /*
  162.  * Pointer --
  163.  *    Variable holding address of any memory resident object.
  164.  */
  165. typedef void    *Pointer;
  166.  
  167. #ifndef    NULL
  168. /*
  169.  * NULL --
  170.  *    Null pointer.
  171.  */
  172. #define NULL    ((void *) 0)
  173. #endif    /* !defined(NULL) */
  174.  
  175. /*
  176.  * CppIdentity --
  177.  *    C preprocessor identity macro, returns the argument.
  178.  */
  179. #define CppIdentity(x)x
  180.  
  181. /*
  182.  * CppAsString --
  183.  *    Convert the argument to a string, using the C preprocessor.
  184.  */
  185. #define CppAsString(identifier)    #identifier
  186.  
  187. /*
  188.  * CppConcat --
  189.  *    Concatenate two arguments together, using the C preprocessor.
  190.  */
  191. #define CppConcat(x, y)        x##y
  192. #define CppConcat0(x, y)    x##y
  193. #define CppConcat1(x, y)    x##y
  194. #define CppConcat2(x, y)    x##y
  195. #define CppConcat3(x, y)    x##y
  196. #define CppConcat4(x, y)    x##y
  197.  
  198. #else    /* !defined(__STDC__) */ /* NOT ANSI C */
  199.  
  200. /*
  201.  * Pointer --
  202.  *    Variable containing address of any memory resident object.
  203.  */
  204. typedef char    *Pointer;
  205.  
  206. #ifndef    NULL
  207. /*
  208.  * NULL --
  209.  *    Null pointer.
  210.  */
  211. #define NULL    0
  212. #endif    /* !defined(NULL) */
  213.  
  214. /*
  215.  * CppIdentity --
  216.  *    C preprocessor identity macro, returns the argument.
  217.  */
  218. #define CppIdentity(x)x
  219.  
  220. /*
  221.  * CppAsString --
  222.  *    Convert the argument to a string, using the C preprocessor.
  223.  */
  224. #define CppAsString(identifier)    "identifier"
  225.  
  226. /*
  227.  * CppConcat --
  228.  *    Concatenate two arguments together, using the C preprocessor.
  229.  */
  230. #ifdef sprite
  231. #define CppConcat(x, y)         x/**/y
  232. #define CppConcat0(x, y)        x/**/y
  233. #define CppConcat1(x, y)        x/**/y
  234. #define CppConcat2(x, y)        x/**/y
  235. #define CppConcat3(x, y)        x/**/y
  236. #define CppConcat4(x, y)        x/**/y
  237. #else
  238. #define CppConcat(x, y)        CppIdentity(x)y
  239. #define CppConcat0(x, y)    CppIdentity(x)y
  240. #define CppConcat1(x, y)    CppIdentity(x)y
  241. #define CppConcat2(x, y)    CppIdentity(x)y
  242. #define CppConcat3(x, y)    CppIdentity(x)y
  243. #define CppConcat4(x, y)    CppIdentity(x)y
  244. #endif /* sprite */
  245.  
  246. /*
  247.  * const --
  248.  *    Type modifier.  Identifies read only variables.
  249.  *
  250.  * Example:
  251.  *    extern const Version    RomVersion;
  252.  */
  253. #define const        /* const */
  254.  
  255. /*
  256.  * signed --
  257.  *    Type modifier.  Identifies signed integral types.
  258.  */
  259. #define signed        /* signed */
  260.  
  261. /*
  262.  * volatile --
  263.  *    Type modifier.  Identifies variables which may change in ways not
  264.  *    noticeable by the compiler, e.g. via asynchronous interrupts.
  265.  *
  266.  * Example:
  267.  *    extern volatile unsigned int    NumberOfInterrupts;
  268.  */
  269. #define volatile    /* volatile */
  270.  
  271.  
  272. #endif    /* !defined(__STDC__) */ /* NOT ANSI C */
  273.  
  274.  
  275. #ifndef __GNUC__    /* GNU cc */
  276. # define inline
  277. #endif
  278.  
  279. #ifdef    PROTOTYPES
  280. #define ARGS(args)    args
  281. #else
  282. #define ARGS(args)    (/*args*/)
  283. #endif
  284.  
  285.  
  286. /* ----------------------------------------------------------------
  287.  *        Section 4:  standard system types
  288.  * ----------------------------------------------------------------
  289.  */
  290.  
  291. /*
  292.  * End COMPILER DEPENDENT section
  293.  */
  294.  
  295.  
  296. /*
  297.  * Begin COMPILER AND HARDWARE DEPENDENT section
  298.  */
  299.  
  300. /*
  301.  * intN --
  302.  *    Signed integer, AT LEAST N BITS IN SIZE,
  303.  *    used for numerical computations.
  304.  */
  305. typedef signed char    int8;        /* >= 8 bits */
  306. typedef signed short    int16;        /* >= 16 bits */
  307. typedef signed long    int32;        /* >= 32 bits */
  308.  
  309. /*
  310.  * AsInt8 --
  311.  *    Coerce the argument to int8.
  312.  *
  313.  * Note:
  314.  *    This macro must be used to reference all int8 variables, because
  315.  *
  316.  *        o Not all compilers support the signed char type.
  317.  *        o Depending on the compiler and/or machine, char
  318.  *          types can be either signed or unsigned.
  319.  */
  320. #define AsInt8(n)    ((int) (((n) & 0x80) ? (~0x7f | (n)) : (n)))
  321.  
  322. /*
  323.  * uintN --
  324.  *    Unsigned integer, AT LEAST N BITS IN SIZE,
  325.  *    used for numerical computations.
  326.  */
  327. typedef unsigned char    uint8;        /* >= 8 bits */
  328. typedef unsigned short    uint16;        /* >= 16 bits */
  329. typedef unsigned long    uint32;        /* >= 32 bits */
  330.  
  331. /*
  332.  * floatN --
  333.  *    Floating point number, AT LEAST N BITS IN SIZE,
  334.  *    used for numerical computations.
  335.  *
  336.  *    Since sizeof(floatN) may be > sizeof(char *), always pass
  337.  *    floatN by reference.
  338.  */
  339. typedef float        float32data;
  340. typedef double        float64data;
  341. typedef float        *float32;
  342. typedef double        *float64;
  343.  
  344. /*
  345.  * AsUint8 --
  346.  *    Coerce the argument to uint8.
  347.  *
  348.  * Note:
  349.  *    This macro must be used to reference all int8 variables, because
  350.  *
  351.  *        o Not all compilers support the unsigned char type.
  352.  *        o Depending on the compiler and/or machine, char
  353.  *          types can be either signed or unsigned.
  354.  */
  355. #define AsUint8(n)    ((unsigned) ((n) & 0xff))
  356.  
  357. /*
  358.  * boolN --
  359.  *    Boolean value, AT LEAST N BITS IN SIZE.
  360.  */
  361. typedef uint8        bool8;        /* >= 8 bits */
  362. typedef uint16        bool16;        /* >= 16 bits */
  363. typedef uint32        bool32;        /* >= 32 bits */
  364.  
  365. /*
  366.  * bitsN --
  367.  *    Unit of bitwise operation, AT LEAST N BITS IN SIZE.
  368.  */
  369. typedef uint8        bits8;        /* >= 8 bits */
  370. typedef uint16        bits16;        /* >= 16 bits */
  371. typedef uint32        bits32;        /* >= 32 bits */
  372.  
  373. /*
  374.  * wordN --
  375.  *    Unit of storage, AT LEAST N BITS IN SIZE,
  376.  *    used to fetch/store data.
  377.  */
  378. typedef uint8        word8;        /* >= 8 bits */
  379. typedef uint16        word16;        /* >= 16 bits */
  380. typedef uint32        word32;        /* >= 32 bits */
  381.  
  382. /*
  383.  * IoChar --
  384.  *    Either a character or EOF, as returned by getchar.
  385.  */
  386. typedef int        IoChar;
  387.  
  388. /*
  389.  * Address --
  390.  *    Address of any memory resident object.
  391.  *
  392.  * Note:
  393.  *    This differs from Pointer type in that an object of type
  394.  *    Pointer contains an address, whereas an object of type
  395.  *    Address is an address (e.g. a constant).
  396.  *
  397.  * Example:
  398.  *    extern Address    EndOfMemory;
  399.  *    extern Pointer    TopOfStack;
  400.  */
  401. typedef char        Address[];
  402.  
  403. /*
  404.  * Size --
  405.  *    Size of any memory resident object, as returned by sizeof.
  406.  */
  407. typedef unsigned int    Size;
  408.  
  409. /*
  410.  * Index --
  411.  *    Index into any memory resident array.
  412.  *
  413.  * Note:
  414.  *    Indices are non negative.
  415.  */
  416. typedef unsigned int    Index;
  417.  
  418. /*
  419.  * Count --
  420.  *    Generic counter type.
  421.  */
  422. typedef unsigned int    Count;
  423.  
  424. /*
  425.  * Offset --
  426.  *    Offset into any memory resident array.
  427.  *
  428.  * Note:
  429.  *    This differs from an Index in that an Index is always
  430.  *    non negative, whereas Offset may be negative.
  431.  */
  432. typedef signed int    Offset;
  433.  
  434. /*
  435.  * String --
  436.  *    String of characters.
  437.  */
  438. typedef char    *String;
  439.  
  440. typedef int    *intPtr;
  441. /*
  442.  * ReturnStatus --
  443.  *    Return status from POSTGRES library functions.
  444.  *
  445.  * Note:
  446.  *    Most POSTGRES functions will not return a status.
  447.  *    In the future, there should be a global variable
  448.  *    which indicates the reason for the failure--the
  449.  *    identifier of an error message in the ERROR relation.
  450.  */
  451. typedef int    ReturnStatus;
  452.  
  453. /* ----------------------------------------------------------------
  454.  *        Section 5:  malloc, free, malloc_debug stuff
  455.  *                new, delete macros
  456.  * ----------------------------------------------------------------
  457.  */
  458. #ifdef PALLOC_DEBUG
  459. extern
  460. char *    /* as defined in /usr/lib/lint/llib-lc */
  461. malloc_debug ARGS((
  462.     String    file,
  463.     int     line,
  464.     Size    nBytes
  465. ));
  466. extern
  467. /* void */      /* as defined in /usr/lib/lint/llib-lc */
  468. free_debug ARGS((
  469.     String    file,
  470.     int    line,
  471.         char    *p
  472. ));
  473. #else /* PALLOC_DEBUG */
  474. /* the free and malloc definition is wrong. This conflicts with flex-generated
  475.    parsers, who define free() and malloc() using void types (kai) */
  476. #ifdef __STDC__
  477. extern void free ARGS((void * __ptr));    
  478. extern void * malloc ARGS((unsigned int __size));
  479. #else
  480. extern
  481. char *    /* as defined in /usr/lib/lint/llib-lc */
  482. malloc ARGS((
  483.     Size    nBytes
  484. ));
  485. extern
  486. /* void */      /* as defined in /usr/lib/lint/llib-lc */
  487. free ARGS((
  488.         char    *p
  489. ));
  490. #endif /* __STDC__ */
  491. #endif /* PALLOC_DEBUG */
  492.  
  493. /*
  494.  * new --
  495.  *    Allocate a new instance of the given type.
  496.  *
  497.  * Note:
  498.  *    Does NOT work with arrays.  Use newv instead.
  499.  */
  500. #define new(type)    LintCast(type *, malloc(sizeof (type)))
  501.  
  502. /*
  503.  * newv --
  504.  *    Allocate a new array.
  505.  */
  506. #define newv(type, n)    LintCast(type *, malloc(sizeof (type) * (n)))
  507.  
  508. /*
  509.  * delete --
  510.  *    Free allocated storage.
  511.  *
  512.  * Note:
  513.  *    The variable is set to null to help catch errors.
  514.  */
  515. #define delete(pointer)    (free((char *) (pointer)), (pointer) = NULL)
  516.  
  517. /* 
  518.  * ALLOCATE() macro
  519.  */
  520. #define ALLOCATE(foo) (foo) palloc(sizeof(struct CppConcat(_,foo)))
  521.  
  522. /* ----------------------------------------------------------------
  523.  *        Section 6:  IsValid macros for system types
  524.  * ----------------------------------------------------------------
  525.  */
  526. /*
  527.  * BoolIsValid --
  528.  *    True iff bool is valid.
  529.  */
  530. #define    BoolIsValid(boolean)    ((boolean) == false || (boolean) == true)
  531.  
  532. #define boolIsValid(b) \
  533.     ((bool) ((b) == false || (b) == true))
  534.  
  535. /*
  536.  * PointerIsValid --
  537.  *    True iff pointer is valid.
  538.  */
  539. #define PointerIsValid(pointer)    (bool)((Pointer)(pointer) != NULL)
  540.  
  541. /*
  542.  * PointerIsInBounds --
  543.  *    True iff pointer is within given bounds.
  544.  *
  545.  * Note:
  546.  *    Assumes the bounded interval to be [min,max),
  547.  *    i.e. closed on the left and open on the right.
  548.  */
  549. #define PointerIsInBounds(pointer, min, max) \
  550.     ((min) <= (pointer) && (pointer) < (max))
  551.  
  552. /*
  553.  * PointerIsAligned --
  554.  *    True iff pointer is properly aligned to point to the given type.
  555.  */
  556. #define PointerIsAligned(pointer, type)    \
  557.     (((long)(pointer) % (sizeof (type))) == 0)
  558.  
  559. /*
  560.  * SizeIsValid --
  561.  *    True iff size is valid.
  562.  *
  563.  * Note:
  564.  *    Assumes that Size is an unsigned type.
  565.  *
  566.  *    Assumes valid size to be in the interval (0,infinity).
  567.  */
  568. #define SizeIsValid(size)        ((size) > 0)
  569.  
  570. /*
  571.  * SizeIsInBounds --
  572.  *    True iff size is within given bounds.
  573.  *
  574.  * Note:
  575.  *    Assumes Size is an unsigned type.
  576.  *
  577.  *    Assumes the bounded interval to be (0,max].
  578.  */
  579. #define SizeIsInBounds(size, max)    (0 < (size) && (size) <= (max))
  580.  
  581. /*
  582.  * IndexIsValid --
  583.  *    True iff index is valid.
  584.  *
  585.  * Note:
  586.  *    Assumes Index is an unsigned type.
  587.  *
  588.  *    Assumes valid index to be in the interval [0,infinity).
  589.  */
  590. #define IndexIsValid(index)    true
  591.  
  592. /*
  593.  * IndexIsInBounds --
  594.  *    True iff index is within given bounds.
  595.  *
  596.  * Note:
  597.  *    Assumes Index is an unsigned type.
  598.  *
  599.  *    Assumes the bounded interval to be [0,max).
  600.  */
  601. #define IndexIsInBounds(index, max)    ((index) < (max))
  602.  
  603. /*
  604.  * CountIsValid --
  605.  *    True iff count is valid
  606.  *
  607.  * Note:
  608.  *    Assumes Count is an unsigned type.
  609.  *
  610.  *    Assumes valid counts to be in the interval [0,infinity).
  611.  */
  612. #define CountIsValid(count)        true
  613.  
  614. /*
  615.  * CountIsInBounds --
  616.  *    True iff count is within given bounds.
  617.  *
  618.  * Note:
  619.  *    Assumes Count is an unsigned type.
  620.  *
  621.  *    Assumes the bounded interval to be [0,max).
  622.  */
  623. #define CountIsInBounds(count, max)    IndexIsInBounds(count, max)
  624.  
  625. /*
  626.  * OffsetIsInBounds --
  627.  *    True iff offset is within given bounds.
  628.  *
  629.  * Note:
  630.  *    Assumes the bounded interval to be [0,max).
  631.  */
  632. #define OffsetIsInBounds(offset, min, max) \
  633.     ((min) <= (offset) && (offset) < (max))
  634.  
  635. /*
  636.  * StringIsValid --
  637.  *    True iff string is valid.
  638.  */
  639. #define    StringIsValid(string)    PointerIsValid(string)
  640.  
  641. /*
  642.  * ReturnStatusIsValid --
  643.  *    True iff return status is valid.
  644.  *
  645.  * Note:
  646.  *    Assumes that a library function can only indicate
  647.  *    sucess or failure.
  648.  */
  649. #define ReturnStatusIsValid(status) \
  650.     ((-1) <= (status) && (status) <= 0)
  651.  
  652. /*
  653.  * ReturnStatusIsSucess --
  654.  *    True iff return status indicates a sucessful call.
  655.  */
  656. #define SucessfulReturnStatus(status) \
  657.     ((status) >= 0)
  658.  
  659. /*
  660.  * End COMPILER AND HARDWARE DEPENDENT section
  661.  */
  662.  
  663. /*
  664.  * Begin COMPILER AND MACHINE INDEPENDENT section
  665.  */
  666. /* ----------------------------------------------------------------
  667.  *        Section 7:  offsetof, lengthof, endof
  668.  * ----------------------------------------------------------------
  669.  */
  670. /*
  671.  * offsetof --
  672.  *    Offset of a structure/union field within that structure/union.
  673.  */
  674. /*
  675.  * under Linux definded in /usr/lib/gcc-lib/.../include/stddef.h
  676.  */
  677. #undef offsetof
  678. #define offsetof(type, field)    ((long) &((type *)0)->field)
  679.  
  680. /*
  681.  * lengthof --
  682.  *    Number of elements in an array.
  683.  */
  684. #define lengthof(array)    (sizeof (array) / sizeof ((array)[0]))
  685.  
  686. /*
  687.  * endof --
  688.  *    Address of the element one past the last in an array.
  689.  */
  690. #define endof(array)    (&array[lengthof(array)])
  691.  
  692. /* ----------------------------------------------------------------
  693.  *        Section 8:  exception handling definitions
  694.  *                Assert, Trap, etc macros
  695.  * ----------------------------------------------------------------
  696.  */
  697. /*
  698.  * Exception Handling definitions
  699.  */
  700.  
  701. typedef String    ExcMessage;
  702. typedef struct Exception {
  703.     ExcMessage    message;
  704. } Exception;
  705.  
  706. extern Exception    FailedAssertion;
  707. extern Exception    BadArg;
  708. extern Exception    BadState;
  709.  
  710. extern
  711. void
  712. ExceptionalCondition ARGS((
  713.     const String    conditionName,
  714.     const Exception    *exceptionP,
  715.     const String    details,
  716.     const String    fileName,
  717.     int        lineNumber
  718. ));
  719.  
  720. /*
  721.  * NO_ASSERT_CHECKING, if defined, turns off all the assertions.
  722.  * - plai  9/5/90
  723.  *
  724.  * It should _NOT_ be undef'ed in releases or in benchmark copies
  725.  * 
  726.  * #undef NO_ASSERT_CHECKING
  727.  */
  728.  
  729. /*
  730.  * Trap --
  731.  *    Generates an exception if the given condition is true.
  732.  *
  733.  * Note:
  734.  *    The trailing else is used to allow this macro to be used in
  735.  *    single statement if, while, for, and do expressions, e.g.
  736.  *
  737.  *        if (tapeCount < 3)
  738.  *            Trap(fd == -1, FailedFileOpen);
  739.  *
  740.  *        if (requests > 0)
  741.  *            Assert(!QueueIsEmpty(queue));
  742.  *
  743.  *        if (!beenHere)
  744.  *            AssertArg(PointerIsValid(initP));
  745.  *
  746.  *        if (beenHere)
  747.  *            AssertState(ModuleInitialized)
  748.  */
  749. #define Trap(condition, exception) \
  750.     if (condition) \
  751.         ExceptionalCondition(CppAsString(condition), &(exception), \
  752.             (String)NULL, __FILE__, __LINE__); \
  753.     else
  754.  
  755. /*    
  756.  *  TrapMacro is the same as Trap but it's intended for use in macros:
  757.  *
  758.  *    #define foo(x) (AssertM(x != 0) && bar(x))
  759.  *
  760.  *  Isn't CPP fun?
  761.  */
  762. #define TrapMacro(condition, exception) \
  763.     ((bool) ((! condition) || \
  764.          (ExceptionalCondition(CppAsString(condition), \
  765.                   &(exception), \
  766.                   (String) NULL, __FILE__, __LINE__), \
  767.           false)))
  768.     
  769. #ifdef NO_ASSERT_CHECKING
  770. #define Assert(condition)
  771. #define AssertMacro(condition)    true
  772. #define AssertArg(condition)
  773. #define AssertState(condition)
  774. #else
  775. #define Assert(condition) \
  776.     Trap(!(condition), FailedAssertion)
  777.  
  778. #define AssertMacro(condition) \
  779.     TrapMacro(!(condition), FailedAssertion)
  780.  
  781. #define AssertArg(condition) \
  782.     Trap(!(condition), BadArg)
  783.  
  784. #define AssertState(condition) \
  785.     Trap(!(condition), BadState)
  786.  
  787. #endif   /* NO_ASSERT_CHECKING */
  788.  
  789. /*
  790.  * LogTrap --
  791.  *    Generates an exception with a message if the given condition is true.
  792.  *
  793.  * Note:
  794.  *    The trailing else is used to allow this macro to be used in
  795.  *    single statement if, while, for, and do expressions, e.g.
  796.  *
  797.  *        if (slowPointer)
  798.  *            LogTrap(!PointerIsValid(pointer), NoMoreMemory,
  799.  *                ("size 0x%x", size));
  800.  *
  801.  *        if (requests > 0)
  802.  *            LogAssert(!QueueIsEmpty(queue), ("req. %d", requests));
  803.  *
  804.  *        if (PointerIsSlowPointer(pointer))
  805.  *            LogAssertArg(pointer >= &end, ("ptr 0x%x", pointer));
  806.  *
  807.  *        if (!slowPointer)
  808.  *            LogAssertState(IsStackable(Current),
  809.  *                ("state %d", typeof(current)))
  810.  */
  811. #define LogTrap(condition, exception, printArgs) \
  812.     if (condition) \
  813.         ExceptionalCondition(CppAsString(condition), &(exception), \
  814.             form printArgs, __FILE__, __LINE__); \
  815.     else
  816.  
  817. /*    
  818.  *  LogTrapMacro is the same as LogTrap but it's intended for use in macros:
  819.  *
  820.  *    #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
  821.  */
  822. #define LogTrapMacro(condition, exception, printArgs) \
  823.     ((bool) ((! condition) || \
  824.          (ExceptionalCondition(CppAsString(condition), \
  825.                    &(exception), \
  826.                    form printArgs, __FILE__, __LINE__), \
  827.           false)))
  828.     
  829. #ifdef NO_ASSERT_CHECKING
  830. #define LogAssert(condition, printArgs)
  831. #define LogAssertMacro(condition, printArgs) true
  832. #define LogAssertArg(condition, printArgs)
  833. #define LogAssertState(condition, printArgs)
  834. #else
  835. #define LogAssert(condition, printArgs) \
  836.     LogTrap(!(condition), FailedAssertion, printArgs)
  837.  
  838. #define LogAssertMacro(condition, printArgs) \
  839.     LogTrapMacro(!(condition), FailedAssertion, printArgs)
  840.  
  841. #define LogAssertArg(condition, printArgs) \
  842.     LogTrap(!(condition), BadArg, printArgs)
  843.  
  844. #define LogAssertState(condition, printArgs) \
  845.     LogTrap(!(condition), BadState, printArgs)
  846.  
  847. #endif   /* NO_ASSERT_CHECKING */
  848.  
  849. /* ----------------------------------------------------------------
  850.  *        Section 9:  Min, Max, Abs macros
  851.  * ----------------------------------------------------------------
  852.  */
  853. /*
  854.  * Max --
  855.  *    Return the maximum of two numbers.
  856.  */
  857. #define Max(x, y)    ((x) > (y) ? (x) : (y))
  858. #ifndef MAX
  859. #define MAX(x, y)    ((x) > (y) ? (x) : (y))
  860. #endif /* MAX */
  861.  
  862. /*
  863.  * Min --
  864.  *    Return the minimum of two numbers.
  865.  */
  866. #define Min(x, y)    ((x) < (y) ? (x) : (y))
  867. #ifndef MIN
  868. #define MIN(x, y)    ((x) < (y) ? (x) : (y))
  869. #endif /* MIN */
  870.  
  871. /*
  872.  * Abs --
  873.  *    Return the absolute value of the argument.
  874.  */
  875. #define Abs(x)        ((x) >= 0 ? (x) : -(x))
  876.  
  877. /* ----------------------------------------------------------------
  878.  *        Section 10:  LintCast, RevisionId, RcsId, SccsId macros
  879.  * ----------------------------------------------------------------
  880.  */
  881. /*
  882.  * LintCast --
  883.  *    Coerce value to a given type, without upsetting lint.
  884.  *    Use *ONLY* for *VALID* casts that lint complains about.
  885.  *
  886.  * Note:
  887.  *    The ?: operator is used to avoid "variable unused" warnings.
  888.  */
  889. #ifdef    lint
  890. # define LintCast(type, value)    ((value) ? ((type) 0) : ((type) 0))
  891.  
  892. # define RevisionId(var, value)
  893. #else    /* !defined(lint) */
  894. # define LintCast(type, value)    ((type) (value))
  895.  
  896. # ifdef XSTR
  897. #  define RevisionId(var, value)    static char *var = value
  898. # else    /* !defined(XSTR) */
  899. #  define RevisionId(var, value)    static char var[] = value
  900. # endif    /* !defined(XSTR) */
  901. #endif    /* !defined(lint) */
  902.  
  903. /* ----------------
  904.  *    RcsId and SccsId macros..
  905.  * ----------------
  906.  */
  907. #if (defined(lint) || defined(SABER))
  908. #define SccsId(id)    
  909. #define RcsId(id)    
  910. #else
  911. #define SccsId(id)    RevisionId(_SccsId_, id)
  912. #define RcsId(id)    RevisionId(_RcsId_, id)
  913. #endif
  914.  
  915. /* ----------------------------------------------------------------
  916.  *        Section 11:  SymbolDecl, ExternDecl macros
  917.  * ----------------------------------------------------------------
  918.  */
  919. /*
  920.  * SymbolDecl --
  921.  * ExternDecl --
  922.  *    Dynamic function and data symbol declaration macros.
  923.  *
  924.  * Sample usage:
  925.  *
  926.  *    extern int fooValue;
  927.  *    extern void foo ARGS((void));
  928.  *    // more declarations ...
  929.  *
  930.  *    #define yourfilename_SYMBOLS \
  931.  *        ExternDecl(fooValue), \
  932.  *        SymbolDecl(foo)
  933.  *
  934.  *    // DO *NOT* INCLUDE A TRAILING ^ COMMA
  935.  */
  936. #ifndef SABER
  937. #define constantquote(x)"
  938. #define prefixquote(x)constantquote(x)x
  939. #define prefixquoteunder(x)prefixquote(_)x
  940. #define symstring(x)prefixquoteunder(x)"
  941.  
  942. #define SymbolDecl(_symbol_) \
  943.     { (func_ptr)_symbol_, symstring(_symbol_) }
  944.  
  945. #define ExternDecl(_external_) \
  946.     { (data_ptr)&(_external_), symstring(_external_) }
  947. #else
  948. #define SymbolDecl(_symbol_) 
  949. #define ExternDecl(_external_)
  950. #endif
  951.  
  952. /* ----------------------------------------------------------------
  953.  *        Section 12: externs
  954.  * ----------------------------------------------------------------
  955.  */
  956.  
  957. /* ----------------
  958.  *    form is used by assert and the exception handling stuff
  959.  * ----------------
  960.  */
  961. extern
  962. String
  963. form ARGS(( int, ... ));
  964.  
  965. /* ----------------
  966.  *    end of c.h
  967.  * ----------------
  968.  */
  969. #endif    /* !defined(CIncluded) */
  970.