ucl_ptr.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* ucl_ptr.h -- low-level pointer constructs
  2. This file is part of the UCL data compression library.
  3. Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
  4. All Rights Reserved.
  5. The UCL library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. The UCL library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with the UCL library; see the file COPYING.
  15. If not, write to the Free Software Foundation, Inc.,
  16. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. Markus F.X.J. Oberhumer
  18. <markus@oberhumer.com>
  19. */
  20. /* WARNING: this file should *not* be used by applications. It is
  21. part of the implementation of the library and is subject
  22. to change.
  23. */
  24. #ifndef __UCL_PTR_H
  25. #define __UCL_PTR_H
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /* This is the lowest part of the UCL library.
  30. * It deals with pointer representations at bit level.
  31. */
  32. /***********************************************************************
  33. // Includes
  34. ************************************************************************/
  35. #if defined(__UCL_DOS16) || defined(__UCL_WIN16)
  36. # include <dos.h>
  37. # if 1 && defined(__WATCOMC__)
  38. # include <i86.h>
  39. __UCL_EXTERN_C unsigned char _HShift;
  40. # define __UCL_HShift _HShift
  41. # elif 1 && defined(_MSC_VER)
  42. __UCL_EXTERN_C unsigned short __near _AHSHIFT;
  43. # define __UCL_HShift ((unsigned) &_AHSHIFT)
  44. # elif defined(__UCL_WIN16)
  45. # define __UCL_HShift 3
  46. # else
  47. # define __UCL_HShift 12
  48. # endif
  49. # if !defined(_FP_SEG) && defined(FP_SEG)
  50. # define _FP_SEG FP_SEG
  51. # endif
  52. # if !defined(_FP_OFF) && defined(FP_OFF)
  53. # define _FP_OFF FP_OFF
  54. # endif
  55. #endif
  56. /***********************************************************************
  57. // Integral types
  58. ************************************************************************/
  59. /* ptrdiff_t */
  60. #if !defined(ucl_ptrdiff_t)
  61. #if (UINT_MAX >= UCL_0xffffffffL)
  62. typedef ptrdiff_t ucl_ptrdiff_t;
  63. #else
  64. typedef long ucl_ptrdiff_t;
  65. #endif
  66. #endif
  67. /* Unsigned type that has *exactly* the same number of bits as a ucl_voidp */
  68. #if !defined(__UCL_HAVE_PTR_T)
  69. # if defined(ucl_ptr_t)
  70. # define __UCL_HAVE_PTR_T
  71. # endif
  72. #endif
  73. #if !defined(__UCL_HAVE_PTR_T)
  74. # if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED_LONG)
  75. # if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED_LONG)
  76. typedef unsigned long ucl_ptr_t;
  77. typedef long ucl_sptr_t;
  78. # define __UCL_HAVE_PTR_T
  79. # endif
  80. # endif
  81. #endif
  82. #if !defined(__UCL_HAVE_PTR_T)
  83. # if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED)
  84. # if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED)
  85. typedef unsigned int ucl_ptr_t;
  86. typedef int ucl_sptr_t;
  87. # define __UCL_HAVE_PTR_T
  88. # endif
  89. # endif
  90. #endif
  91. #if !defined(__UCL_HAVE_PTR_T)
  92. # if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED_SHORT)
  93. # if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED_SHORT)
  94. typedef unsigned short ucl_ptr_t;
  95. typedef short ucl_sptr_t;
  96. # define __UCL_HAVE_PTR_T
  97. # endif
  98. # endif
  99. #endif
  100. #if !defined(__UCL_HAVE_PTR_T)
  101. # if defined(UCL_HAVE_CONFIG_H) || defined(SIZEOF_CHAR_P)
  102. # error "no suitable type for ucl_ptr_t"
  103. # else
  104. typedef unsigned long ucl_ptr_t;
  105. typedef long ucl_sptr_t;
  106. # define __UCL_HAVE_PTR_T
  107. # endif
  108. #endif
  109. /***********************************************************************
  110. //
  111. ************************************************************************/
  112. /* Always use the safe (=integral) version for pointer-comparisions.
  113. * The compiler should optimize away the additional casts anyway.
  114. *
  115. * Note that this only works if the representation and ordering
  116. * of the pointer and the integral is the same (at bit level).
  117. *
  118. * Most 16-bit compilers have their own view about pointers -
  119. * fortunately they don't care about comparing pointers
  120. * that are pointing to Nirvana.
  121. */
  122. #if defined(__UCL_DOS16) || defined(__UCL_WIN16)
  123. #define PTR(a) ((ucl_bytep) (a))
  124. /* only need the low bits of the pointer -> offset is ok */
  125. #define PTR_ALIGNED_4(a) ((_FP_OFF(a) & 3) == 0)
  126. #define PTR_ALIGNED2_4(a,b) (((_FP_OFF(a) | _FP_OFF(b)) & 3) == 0)
  127. #else
  128. #define PTR(a) ((ucl_ptr_t) (a))
  129. #define PTR_LINEAR(a) PTR(a)
  130. #define PTR_ALIGNED_4(a) ((PTR_LINEAR(a) & 3) == 0)
  131. #define PTR_ALIGNED_8(a) ((PTR_LINEAR(a) & 7) == 0)
  132. #define PTR_ALIGNED2_4(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 3) == 0)
  133. #define PTR_ALIGNED2_8(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 7) == 0)
  134. #endif
  135. #define PTR_LT(a,b) (PTR(a) < PTR(b))
  136. #define PTR_GE(a,b) (PTR(a) >= PTR(b))
  137. #define PTR_DIFF(a,b) ((ucl_ptrdiff_t) (PTR(a) - PTR(b)))
  138. UCL_EXTERN(ucl_ptr_t)
  139. __ucl_ptr_linear(const ucl_voidp ptr);
  140. typedef union
  141. {
  142. char a_char;
  143. unsigned char a_uchar;
  144. short a_short;
  145. unsigned short a_ushort;
  146. int a_int;
  147. unsigned int a_uint;
  148. long a_long;
  149. unsigned long a_ulong;
  150. ucl_int a_ucl_int;
  151. ucl_uint a_ucl_uint;
  152. ucl_int32 a_ucl_int32;
  153. ucl_uint32 a_ucl_uint32;
  154. ptrdiff_t a_ptrdiff_t;
  155. ucl_ptrdiff_t a_ucl_ptrdiff_t;
  156. ucl_ptr_t a_ucl_ptr_t;
  157. ucl_voidp a_ucl_voidp;
  158. void * a_void_p;
  159. ucl_bytep a_ucl_bytep;
  160. ucl_bytepp a_ucl_bytepp;
  161. ucl_uintp a_ucl_uintp;
  162. ucl_uint * a_ucl_uint_p;
  163. ucl_uint32p a_ucl_uint32p;
  164. ucl_uint32 * a_ucl_uint32_p;
  165. unsigned char * a_uchar_p;
  166. char * a_char_p;
  167. }
  168. ucl_align_t;
  169. #ifdef __cplusplus
  170. } /* extern "C" */
  171. #endif
  172. #endif /* already included */
  173. /*
  174. vi:ts=4:et
  175. */