1 /* align.c -- test alignment (important for 16-bit systems)
2 
3    This file is part of the LZO real-time data compression library.
4 
5    Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer
6    All Rights Reserved.
7 
8    The LZO library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of
11    the License, or (at your option) any later version.
12 
13    The LZO library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with the LZO library; see the file COPYING.
20    If not, write to the Free Software Foundation, Inc.,
21    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 
23    Markus F.X.J. Oberhumer
24    <markus@oberhumer.com>
25    http://www.oberhumer.com/opensource/lzo/
26  */
27 
28 
29 #if 0
30 #include "src/lzo_conf.h"
31 #include "src/lzo_ptr.h"
32 #endif
33 #include <lzo/lzoconf.h>
34 
35 /* utility layer */
36 #define WANT_LZO_MALLOC 1
37 #include "examples/portab.h"
38 
39 
40 static int opt_verbose = 0;
41 
42 
43 /*************************************************************************
44 //
45 **************************************************************************/
46 
align_test(lzo_bytep block,lzo_uint len,lzo_uint step)47 static unsigned long align_test(lzo_bytep block, lzo_uint len, lzo_uint step)
48 {
49     lzo_bytep b1 = block;
50     lzo_bytep b2 = block;
51     lzo_bytep k1 = NULL;
52     lzo_bytep k2 = NULL;
53     lzo_bytep k;
54     lzo_bytep x;
55     lzo_uint offset = 0;
56     unsigned long i = 0;
57 
58     assert(step > 0);
59     assert(step <= 65536ul);
60     assert((step & (step - 1)) == 0);
61 
62     for (offset = step; offset < len; offset += step)
63     {
64         k1 = LZO_PTR_ALIGN_UP(b1 + 1, step);
65         k2 = b2 + offset;
66         if (k1 != k2)
67         {
68             printf("error 1: i %lu step %ld offset %ld: "
69                    "%p (%ld) %p (%ld)\n",
70                    i, (long) step, (long) offset,
71                    k1, (long) (k1 - block),
72                    k2, (long) (k2 - block));
73             return 0;
74         }
75         if (k1 - step != b1)
76         {
77             printf("error 2: i %lu step %ld offset %ld: "
78                    "%p (%ld) %p (%ld)\n",
79                    i, (long) step, (long) offset,
80                    b1, (long) (b1 - block),
81                    k1, (long) (k1 - block));
82             return 0;
83         }
84 
85         assert(k1 > b1);
86         assert(k2 > b2);
87         assert((lzo_uint)(k2 - b2) == offset);
88         assert(k1 - offset == b2);
89 #if defined(PTR_ALIGNED_4)
90         if (step == 4)
91         {
92             assert(PTR_ALIGNED_4(k1));
93             assert(PTR_ALIGNED_4(k2));
94             assert(PTR_ALIGNED2_4(k1,k2));
95         }
96 #endif
97 #if defined(PTR_ALIGNED_8)
98         if (step == 8)
99         {
100             assert(PTR_ALIGNED_8(k1));
101             assert(PTR_ALIGNED_8(k2));
102             assert(PTR_ALIGNED2_8(k1,k2));
103         }
104 #endif
105 #if defined(PTR_LINEAR)
106         assert((PTR_LINEAR(k1) & (step-1)) == 0);
107         assert((PTR_LINEAR(k2) & (step-1)) == 0);
108 #endif
109 
110         for (k = b1 + 1; k <= k1; k++)
111         {
112             x = LZO_PTR_ALIGN_UP(k, step);
113             if (x != k1)
114             {
115                 printf("error 3: base: %p %p %p  i %lu step %ld offset %ld: "
116                        "%p (%ld) %p (%ld) %p (%ld)\n",
117                        block, b1, b2,
118                        i, (long) step, (long) offset,
119                        k1, (long) (k1 - block),
120                        k, (long) (k - block),
121                        x, (long) (x - block));
122                 return 0;
123             }
124         }
125 
126         b1 = k1;
127         i++;
128     }
129 
130     return i;
131 }
132 
133 
134 /*************************************************************************
135 //
136 **************************************************************************/
137 
138 #define BLOCK_SIZE  (128*1024ul)
139 
main(int argc,char * argv[])140 int main(int argc, char *argv[])
141 {
142     lzo_bytep buf;
143     lzo_uint step;
144 
145     if (argc >= 2 && strcmp(argv[1],"-v") == 0)
146         opt_verbose = 1;
147 
148     if (lzo_init() != LZO_E_OK)
149     {
150         printf("lzo_init() failed !!!\n");
151         return 3;
152     }
153     buf = (lzo_bytep) lzo_malloc(2*BLOCK_SIZE + 256);
154     if (buf == NULL)
155     {
156         printf("out of memory\n");
157         return 2;
158     }
159 
160 #if defined(lzo_uintptr_t)
161     printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) (lzo_uintptr_t) buf);
162 #elif defined(__LZO_MMODEL_HUGE)
163     printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) buf);
164 #else
165     printf("Align init: %p ( 0x%lx )\n", buf, (unsigned long) (size_t) buf);
166 #endif
167 
168     for (step = 1; step <= 65536ul; step *= 2)
169     {
170         lzo_bytep block = buf;
171         unsigned long n;
172         unsigned gap;
173 
174         gap = __lzo_align_gap(block, step);
175         block = LZO_PTR_ALIGN_UP(block, step);
176         if (opt_verbose >= 1)
177             printf("STEP %5lu: GAP: %5lu  %p %p %5lu\n",
178                    (unsigned long) step, (unsigned long) gap, buf, block,
179                    (unsigned long) (block - buf));
180         n = align_test(block, BLOCK_SIZE, step);
181         if (n == 0)
182             return 1;
183         if ((n + 1) * step != BLOCK_SIZE)
184         {
185             printf("error 4: %ld %lu\n", (long)step, n);
186             return 1;
187         }
188     }
189 
190     lzo_free(buf);
191     printf("Alignment test passed.\n");
192     return 0;
193 }
194 
195 
196 /* vim:set ts=4 sw=4 et: */
197