align.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* align.c -- test alignment (important for 16-bit systems)
  2. This file is part of the LZO real-time data compression library.
  3. Copyright (C) 1996-2015 Markus Franz Xaver Johannes Oberhumer
  4. All Rights Reserved.
  5. The LZO 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 LZO 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 LZO library; see the file COPYING.
  15. If not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. Markus F.X.J. Oberhumer
  18. <markus@oberhumer.com>
  19. http://www.oberhumer.com/opensource/lzo/
  20. */
  21. #if 0
  22. #include "src/lzo_conf.h"
  23. #include "src/lzo_ptr.h"
  24. #endif
  25. #include <lzo/lzoconf.h>
  26. /* utility layer */
  27. #define WANT_LZO_MALLOC 1
  28. #include "examples/portab.h"
  29. static int opt_verbose = 0;
  30. /*************************************************************************
  31. //
  32. **************************************************************************/
  33. static unsigned long align_test(lzo_bytep block, lzo_uint len, lzo_uint step)
  34. {
  35. lzo_bytep b1 = block;
  36. lzo_bytep b2 = block;
  37. lzo_bytep k1 = NULL;
  38. lzo_bytep k2 = NULL;
  39. lzo_bytep k;
  40. lzo_bytep x;
  41. lzo_uint offset = 0;
  42. unsigned long i = 0;
  43. assert(step > 0);
  44. assert(step <= 65536ul);
  45. assert((step & (step - 1)) == 0);
  46. for (offset = step; offset < len; offset += step)
  47. {
  48. k1 = LZO_PTR_ALIGN_UP(b1 + 1, step);
  49. k2 = b2 + offset;
  50. if (k1 != k2)
  51. {
  52. printf("error 1: i %lu step %ld offset %ld: "
  53. "%p (%ld) %p (%ld)\n",
  54. i, (long) step, (long) offset,
  55. k1, (long) (k1 - block),
  56. k2, (long) (k2 - block));
  57. return 0;
  58. }
  59. if (k1 - step != b1)
  60. {
  61. printf("error 2: i %lu step %ld offset %ld: "
  62. "%p (%ld) %p (%ld)\n",
  63. i, (long) step, (long) offset,
  64. b1, (long) (b1 - block),
  65. k1, (long) (k1 - block));
  66. return 0;
  67. }
  68. assert(k1 > b1);
  69. assert(k2 > b2);
  70. assert((lzo_uint)(k2 - b2) == offset);
  71. assert(k1 - offset == b2);
  72. #if defined(PTR_ALIGNED_4)
  73. if (step == 4)
  74. {
  75. assert(PTR_ALIGNED_4(k1));
  76. assert(PTR_ALIGNED_4(k2));
  77. assert(PTR_ALIGNED2_4(k1,k2));
  78. }
  79. #endif
  80. #if defined(PTR_ALIGNED_8)
  81. if (step == 8)
  82. {
  83. assert(PTR_ALIGNED_8(k1));
  84. assert(PTR_ALIGNED_8(k2));
  85. assert(PTR_ALIGNED2_8(k1,k2));
  86. }
  87. #endif
  88. #if defined(PTR_LINEAR)
  89. assert((PTR_LINEAR(k1) & (step-1)) == 0);
  90. assert((PTR_LINEAR(k2) & (step-1)) == 0);
  91. #endif
  92. for (k = b1 + 1; k <= k1; k++)
  93. {
  94. x = LZO_PTR_ALIGN_UP(k, step);
  95. if (x != k1)
  96. {
  97. printf("error 3: base: %p %p %p i %lu step %ld offset %ld: "
  98. "%p (%ld) %p (%ld) %p (%ld)\n",
  99. block, b1, b2,
  100. i, (long) step, (long) offset,
  101. k1, (long) (k1 - block),
  102. k, (long) (k - block),
  103. x, (long) (x - block));
  104. return 0;
  105. }
  106. }
  107. b1 = k1;
  108. i++;
  109. }
  110. return i;
  111. }
  112. /*************************************************************************
  113. //
  114. **************************************************************************/
  115. #define BLOCK_SIZE (128*1024ul)
  116. int main(int argc, char *argv[])
  117. {
  118. lzo_bytep buf;
  119. lzo_uint step;
  120. if (argc >= 2 && strcmp(argv[1],"-v") == 0)
  121. opt_verbose = 1;
  122. if (lzo_init() != LZO_E_OK)
  123. {
  124. printf("lzo_init() failed !!!\n");
  125. return 3;
  126. }
  127. buf = (lzo_bytep) lzo_malloc(2*BLOCK_SIZE + 256);
  128. if (buf == NULL)
  129. {
  130. printf("out of memory\n");
  131. return 2;
  132. }
  133. #if defined(lzo_uintptr_t)
  134. printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) (lzo_uintptr_t) buf);
  135. #elif defined(__LZO_MMODEL_HUGE)
  136. printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) buf);
  137. #else
  138. printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) (size_t) buf);
  139. #endif
  140. for (step = 1; step <= 65536ul; step *= 2)
  141. {
  142. lzo_bytep block = buf;
  143. unsigned long n;
  144. unsigned gap;
  145. gap = __lzo_align_gap(block, step);
  146. block = LZO_PTR_ALIGN_UP(block, step);
  147. if (opt_verbose >= 1)
  148. printf("STEP %5lu: GAP: %5lu %p %p %5lu\n",
  149. (unsigned long) step, (unsigned long) gap, buf, block,
  150. (unsigned long) (block - buf));
  151. n = align_test(block, BLOCK_SIZE, step);
  152. if (n == 0)
  153. return 1;
  154. if ((n + 1) * step != BLOCK_SIZE)
  155. {
  156. printf("error 4: %ld %lu\n", (long)step, n);
  157. return 1;
  158. }
  159. }
  160. lzo_free(buf);
  161. printf("Alignment test passed.\n");
  162. return 0;
  163. }
  164. /* vim:set ts=4 sw=4 et: */