home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cdisk.zip / VDD / BASEMAC.H < prev    next >
C/C++ Source or Header  |  1993-02-03  |  4KB  |  117 lines

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