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) 1996-2014 Markus Franz Xaver Johannes Oberhumer
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.
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.
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.
23 Markus F.X.J. Oberhumer
24 <markus@oberhumer.com>
25 http://www.oberhumer.com/opensource/lzo/
30 #include "src/lzo_conf.h"
31 #include "src/lzo_ptr.h"
33 #include "lzo/lzoconf.h"
36 #define WANT_LZO_MALLOC 1
37 #include "examples/portab.h"
40 static int opt_verbose
= 0;
43 /*************************************************************************
45 **************************************************************************/
47 static unsigned long align_test(lzo_bytep block
, lzo_uint len
, lzo_uint step
)
59 assert(step
<= 65536L);
60 assert((step
& (step
- 1)) == 0);
62 for (offset
= step
; offset
< len
; offset
+= step
)
64 k1
= LZO_PTR_ALIGN_UP(b1
+1,step
);
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
));
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
));
87 assert((lzo_uint
)(k2
- b2
) == offset
);
88 assert(k1
- offset
== b2
);
89 #if defined(PTR_ALIGNED_4)
92 assert(PTR_ALIGNED_4(k1
));
93 assert(PTR_ALIGNED_4(k2
));
94 assert(PTR_ALIGNED2_4(k1
,k2
));
97 #if defined(PTR_ALIGNED_8)
100 assert(PTR_ALIGNED_8(k1
));
101 assert(PTR_ALIGNED_8(k2
));
102 assert(PTR_ALIGNED2_8(k1
,k2
));
105 #if defined(PTR_LINEAR)
106 assert((PTR_LINEAR(k1
) & (step
-1)) == 0);
107 assert((PTR_LINEAR(k2
) & (step
-1)) == 0);
110 for (k
= b1
+ 1; k
<= k1
; k
++)
112 x
= LZO_PTR_ALIGN_UP(k
,step
);
115 printf("error 3: base: %p %p %p i %lu step %ld offset %ld: "
116 "%p (%ld) %p (%ld) %p (%ld)\n",
118 i
, (long) step
, (long) offset
,
119 k1
, (long) (k1
- block
),
120 k
, (long) (k
- block
),
121 x
, (long) (x
- block
));
134 /*************************************************************************
136 **************************************************************************/
138 #define BLOCK_SIZE (128*1024ul)
140 int main(int argc
, char *argv
[])
145 if (argc
>= 2 && strcmp(argv
[1],"-v") == 0)
148 if (lzo_init() != LZO_E_OK
)
150 printf("lzo_init() failed !!!\n");
153 buf
= (lzo_bytep
) lzo_malloc(2*BLOCK_SIZE
+ 256);
156 printf("out of memory\n");
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
);
165 printf("Align init: %p ( 0x%lx )\n", buf
, (unsigned long) (size_t) buf
);
168 for (step
= 1; step
<= 65536L; step
*= 2)
170 lzo_bytep block
= buf
;
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
);
183 if ((n
+ 1) * step
!= BLOCK_SIZE
)
185 printf("error 4: %ld %lu\n", (long)step
, n
);
191 printf("Alignment test passed.\n");
196 /* vim:set ts=4 sw=4 et: */