1 /* align.c -- test alignment (important for 16-bit systems)
3 This file is part of the LZO real-time data compression library.
5 Copyright (C) 2011 Markus Franz Xaver Johannes Oberhumer
6 Copyright (C) 2010 Markus Franz Xaver Johannes Oberhumer
7 Copyright (C) 2009 Markus Franz Xaver Johannes Oberhumer
8 Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
9 Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
10 Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
11 Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
12 Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
13 Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
14 Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
15 Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
16 Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
17 Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
18 Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
19 Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
20 Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
23 The LZO library is free software; you can redistribute it and/or
24 modify it under the terms of the GNU General Public License as
25 published by the Free Software Foundation; either version 2 of
26 the License, or (at your option) any later version.
28 The LZO library is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
33 You should have received a copy of the GNU General Public License
34 along with the LZO library; see the file COPYING.
35 If not, write to the Free Software Foundation, Inc.,
36 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
38 Markus F.X.J. Oberhumer
39 <markus@oberhumer.com>
40 http://www.oberhumer.com/opensource/lzo/
45 #include "src/lzo_conf.h"
46 #include "src/lzo_ptr.h"
48 #include "lzo/lzoconf.h"
51 #define WANT_LZO_MALLOC 1
52 #include "examples/portab.h"
58 /*************************************************************************
60 **************************************************************************/
62 unsigned long align_test(lzo_bytep block
, lzo_uint len
, lzo_uint step
)
74 assert(step
<= 65536L);
75 assert((step
& (step
- 1)) == 0);
77 for (offset
= step
; offset
< len
; offset
+= step
)
79 k1
= LZO_PTR_ALIGN_UP(b1
+1,step
);
83 printf("error 1: i %lu step %ld offset %ld: "
84 "%p (%ld) %p (%ld)\n",
85 i
, (long) step
, (long) offset
,
86 k1
, (long) (k1
- block
),
87 k2
, (long) (k2
- block
));
92 printf("error 2: i %lu step %ld offset %ld: "
93 "%p (%ld) %p (%ld)\n",
94 i
, (long) step
, (long) offset
,
95 b1
, (long) (b1
- block
),
96 k1
, (long) (k1
- block
));
102 assert((lzo_uint
)(k2
- b2
) == offset
);
103 assert(k1
- offset
== b2
);
104 #if defined(PTR_ALIGNED_4)
107 assert(PTR_ALIGNED_4(k1
));
108 assert(PTR_ALIGNED_4(k2
));
109 assert(PTR_ALIGNED2_4(k1
,k2
));
112 #if defined(PTR_ALIGNED_8)
115 assert(PTR_ALIGNED_8(k1
));
116 assert(PTR_ALIGNED_8(k2
));
117 assert(PTR_ALIGNED2_8(k1
,k2
));
120 #if defined(PTR_LINEAR)
121 assert((PTR_LINEAR(k1
) & (step
-1)) == 0);
122 assert((PTR_LINEAR(k2
) & (step
-1)) == 0);
125 for (k
= b1
+ 1; k
<= k1
; k
++)
127 x
= LZO_PTR_ALIGN_UP(k
,step
);
130 printf("error 3: base: %p %p %p i %lu step %ld offset %ld: "
131 "%p (%ld) %p (%ld) %p (%ld)\n",
133 i
, (long) step
, (long) offset
,
134 k1
, (long) (k1
- block
),
135 k
, (long) (k
- block
),
136 x
, (long) (x
- block
));
149 /*************************************************************************
151 **************************************************************************/
153 #define BLOCK_LEN (128*1024ul)
155 int main(int argc
, char *argv
[])
160 if (argc
>= 2 && strcmp(argv
[1],"-v") == 0)
163 if (lzo_init() != LZO_E_OK
)
165 printf("lzo_init() failed !!!\n");
168 buf
= (lzo_bytep
) lzo_malloc(2*BLOCK_LEN
+ 256);
171 printf("out of memory\n");
175 #if defined(lzo_uintptr_t)
176 printf("Align init: %p ( 0x%lx )\n", buf
, (unsigned long) (lzo_uintptr_t
) buf
);
177 #elif defined(__LZO_MMODEL_HUGE)
178 printf("Align init: %p ( 0x%lx )\n", buf
, (unsigned long) buf
);
180 printf("Align init: %p ( 0x%lx )\n", buf
, (unsigned long) (size_t) buf
);
183 for (step
= 1; step
<= 65536L; step
*= 2)
185 lzo_bytep block
= buf
;
189 gap
= __lzo_align_gap(block
,step
);
190 block
= LZO_PTR_ALIGN_UP(block
,step
);
191 if (opt_verbose
>= 1)
192 printf("STEP %5lu: GAP: %5lu %p %p %5lu\n",
193 (unsigned long) step
, (unsigned long) gap
, buf
, block
,
194 (unsigned long) (block
- buf
));
195 n
= align_test(block
,BLOCK_LEN
,step
);
198 if ((n
+ 1) * step
!= BLOCK_LEN
)
200 printf("error 4: %ld %lu\n", (long)step
, n
);
206 printf("Alignment test passed.\n");