home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ddkx86v1.zip / DDKX86 / H / BASEMAC.H < prev    next >
C/C++ Source or Header  |  1995-04-14  |  5KB  |  141 lines

  1. /*DDK*************************************************************************/
  2. /*                                                                           */
  3. /* COPYRIGHT (C) Microsoft Corporation, 1989                                 */
  4. /* COPYRIGHT    Copyright (C) 1995 IBM Corporation                           */
  5. /*                                                                           */
  6. /*    The following IBM OS/2 WARP source code is provided to you solely for  */
  7. /*    the purpose of assisting you in your development of OS/2 WARP device   */
  8. /*    drivers. You may use this code in accordance with the IBM License      */
  9. /*    Agreement provided in the IBM Device Driver Source Kit for OS/2. This  */
  10. /*    Copyright statement may not be removed.                                */
  11. /*                                                                           */
  12. /*****************************************************************************/
  13. /*static char *SCCSID = "@(#)basemac.h    6.1 90/11/15";*/
  14. /***    BASEMAC.H
  15.  *
  16.  *    SCCSID = @(#)basemac.h    13.20 90/09/06
  17.  *
  18.  *    Macros required for other include files
  19.  *
  20.  *
  21.  *    MODIFICATION HISTORY
  22.  *        10/14/88  JTP    Created.
  23.  *        12/04/88  JTP    Added OS/2-specific macros.
  24.  */
  25.  
  26.  
  27. /*** Generic macros
  28.  */
  29.  
  30. #define NElements(array) ((sizeof array)/(sizeof array[0]))
  31.  
  32. #define SWAP(a,b,tmp)    (tmp=b, b=a, a=tmp)
  33.  
  34.  
  35. // Assorted macros from STDLIB.H...
  36. #define min(a, b)    (((a) < (b))? (a) : (b))
  37. #define max(a, b)    (((a) > (b))? (a) : (b))
  38.  
  39.  
  40. // To extract offset or selector from a FAR32 (16:32) pointer
  41. #define OFFSETOF32(p)    (((PDWORD)&(p))[0])
  42. #define SEGMENTOF32(p)    (((PWORD)&(p))[2])
  43.  
  44. // To extract offset or selector from any FAR (16:16) pointer
  45. #define OFFSETOF16(p)    (((PWORD)&(p))[0])
  46. #define SEGMENTOF16(p)    (((PWORD)&(p))[1])
  47.  
  48. // For now, the default operators assume they're working on 16:16 pointers
  49. #define OFFSETOF    OFFSETOF16
  50. #define SEGMENTOF    SEGMENTOF16
  51.  
  52. // To convert a tiled 16:16 address to a 0:32 address
  53. #define MAKEFLATP(fp)    ((PVOID)((SEGMENTOF(fp)&~7)<<13 | OFFSETOF(fp)))
  54.  
  55. // To extract any byte, word, dword, pfn, etc. from the given item
  56. #define BYTEOF(p,i)    (((PBYTE)&(p))[i])
  57. #define WORDOF(p,i)    (((PWORD)&(p))[i])
  58. #define DWORDOF(p,i)    (((PDWORD)&(p))[i])
  59. #define PFNOF(p,i)    (((PPFN)&(p))[i])
  60.  
  61. // To test/set bits
  62. #define TESTBIT(i,b)    (((i)&(b)) != 0)
  63. #define SETBIT(i,b,f)    (i = ((i)&~(b)) | (b)*(!!(f)))
  64. #define SETBITB(i,b,f)    (i = (BYTE)(((i)&~(b)) | (b)*(!!(f))))
  65.  
  66. // ZEROBITS returns the number of low zero bits in a 32-bit constant
  67. #define _Z2(l)        ((l)&1?0:(l)&2?1:2)
  68. #define _Z4(l)        ((l)&3?_Z2(l):_Z2((l)>>2)+2)
  69. #define _Z8(l)        ((l)&15?_Z4(l):_Z4((l)>>4)+4)
  70. #define _Z16(l)     ((l)&255?_Z8(l):_Z8((l)>>8)+8)
  71. #define _Z32(l)     ((l)&65535?_Z16(l):_Z16((l)>>16)+16)
  72. #define ZEROBITS(l)    _Z32(l)
  73.  
  74. // LOG2 returns the nearest base-2 log of a 32-bit constant, rounded up
  75. #define _L2(l)        ((l)&~1?2:(l)&1)
  76. #define _L4(l)        ((l)&~3?_L2((l)>>2)+2:_L2(l))
  77. #define _L8(l)        ((l)&~15?_L4((l)>>4)+4:_L4(l))
  78. #define _L16(l)     ((l)&~255?_L8((l)>>8)+8:_L8(l))
  79. #define _L32(l)     ((l)&~65535?_L16((l)>>16)+16:_L16(l))
  80. #define LOG2(l)     _L32((l)-1)
  81.  
  82. // EXP2 returns 2 raised to the given power
  83. #define EXP2(l)     (1 << (l))
  84.  
  85. // Unit conversion macros
  86. #define KBFROMBYTES(nb)     (((nb)+KSIZE-1)/KSIZE)
  87. #define BYTESFROMKB(nkb)    ((nkb)*KSIZE)
  88. #define PAGESFROMBYTES(nb)    (((nb)+PAGESIZE-1)/PAGESIZE)
  89.  
  90. // To obtain a pointer to the page containing the given linear address
  91. #define PPAGEFROMP(p)        ((PVOID)((ULONG)(p) & ~(PAGESIZE-1)))
  92. #define PPAGEFROMPGNO(p)    ((PVOID)((ULONG)(p) * PAGESIZE))
  93. #define PGNOFROMP(p)        ((ULONG)(p) / PAGESIZE)
  94.  
  95.  
  96. // To create a usable FAR pointer from an unusable FAR16 pointer
  97. #define FPFROMF16P(fp,f16p)    OFFSETOF32(fp) = OFFSETOF16(f16p),\
  98.                 SEGMENTOF32(fp) = SEGMENTOF16(f16p)
  99.  
  100. // To create pointers from V86-mode segments+offsets
  101. // Note the validity of these pointers depends on the context they're used in
  102. #define PFROMVP(vp)        ((PVOID)((WORDOF(vp,1)<<4)+WORDOF(vp,0)))
  103. #define PFROMVADDR(seg,off)    ((PVOID)(((WORD)(seg)<<4)+(WORD)(off)))
  104. #define VPFROMVADDR(seg,off)    ((VPVOID)(((WORD)(seg)<<16)|(WORD)(off)))
  105.  
  106. // To create V86 pointers from normal (flat) pointers
  107. #define HISEG(p)        ((USHORT)((ULONG)(p)>>4))
  108. #define LOOFF(p)        ((USHORT)((ULONG)(p)&0xf))
  109. #define VPFROMP(p)        ((VPVOID)((((ULONG)(p)&~0xf)<<12)|((ULONG)(p)&0xf)))
  110.  
  111. #define LOSEG(p)        (((ULONG)(p)-HIOFF(p))>>4)
  112. #define HIOFF(p)        (min(0xfff0,(ULONG)(p)&0xffff0)|((ULONG)(p)&0xf))
  113. #define LOSEGVPFROMP(p)     ((VPVOID)((LOSEG(p)<<16)|HIOFF(p)))
  114.  
  115.  
  116. // To calculate the byte offset of a field in a structure of type "type"
  117. #define FIELDOFFSET(type,field)    ((ULONG)&(((type *)0)->field))
  118.  
  119.  
  120. /*** Older stuff (discouraged -JTP)
  121.  */
  122.  
  123. // To extract high and low order parts of a 32-bit quantity
  124. #define LOUSHORT(l)        (((PUSHORT)&(l))[0])
  125. #define HIUSHORT(l)        (((PUSHORT)&(l))[1])
  126.  
  127. // To create pointer from V86-mode segment+offset
  128. #define SEGOFF2P(seg,off)    ((PVOID)((seg<<4)+(USHORT)off))
  129.  
  130.  
  131. /*** OS/2-specific macros
  132.  */
  133.  
  134. #ifdef    INCL_SSTODS
  135.  
  136. #define SSToDS(p)    ((void *) (TKSSBase + (unsigned) (p)))
  137.  
  138. extern char *TKSSBase;
  139.  
  140. #endif
  141.