home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / header45.zip / basemac.h < prev    next >
C/C++ Source or Header  |  1999-03-15  |  5KB  |  131 lines

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