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