]>
Commit | Line | Data |
---|---|---|
84832e05 UD |
1 | #include <obstack.h> |
2 | #include <stdint.h> | |
3 | #include <stdio.h> | |
4 | #include <stdlib.h> | |
5 | ||
6 | #define obstack_chunk_alloc verbose_malloc | |
7 | #define obstack_chunk_free verbose_free | |
8 | #define ALIGN_BOUNDARY 64 | |
9 | #define ALIGN_MASK (ALIGN_BOUNDARY - 1) | |
10 | #define OBJECT_SIZE 1000 | |
11 | ||
12 | static void * | |
13 | verbose_malloc (size_t size) | |
14 | { | |
15 | void *buf = malloc (size); | |
59553897 | 16 | printf ("malloc (%zu) => %p\n", size, buf); |
84832e05 UD |
17 | return buf; |
18 | } | |
19 | ||
20 | static void | |
21 | verbose_free (void *buf) | |
22 | { | |
23 | free (buf); | |
24 | printf ("free (%p)\n", buf); | |
25 | } | |
26 | ||
29955b5d AS |
27 | static int |
28 | do_test (void) | |
84832e05 | 29 | { |
84832e05 | 30 | int result = 0; |
a6027576 | 31 | int align = 2; |
84832e05 | 32 | |
a6027576 | 33 | while (align <= 64) |
84832e05 | 34 | { |
a6027576 UD |
35 | struct obstack obs; |
36 | int i; | |
37 | int align_mask = align - 1; | |
38 | ||
39 | printf ("\n Alignment mask: %d\n", align_mask); | |
40 | ||
41 | obstack_init (&obs); | |
42 | obstack_alignment_mask (&obs) = align_mask; | |
43 | /* finish an empty object to take alignment into account */ | |
44 | obstack_finish (&obs); | |
45 | ||
46 | /* let's allocate some objects and print their addresses */ | |
47 | for (i = 15; i > 0; --i) | |
48 | { | |
49 | void *obj = obstack_alloc (&obs, OBJECT_SIZE); | |
50 | ||
51 | printf ("obstack_alloc (%u) => %p \t%s\n", OBJECT_SIZE, obj, | |
52 | ((uintptr_t) obj & align_mask) ? "(not aligned)" : ""); | |
53 | result |= ((uintptr_t) obj & align_mask) != 0; | |
54 | } | |
84832e05 | 55 | |
a6027576 UD |
56 | /* clean up */ |
57 | obstack_free (&obs, 0); | |
84832e05 | 58 | |
a6027576 UD |
59 | align <<= 1; |
60 | } | |
84832e05 UD |
61 | |
62 | return result; | |
63 | } | |
29955b5d AS |
64 | |
65 | #define TEST_FUNCTION do_test () | |
66 | #include "../test-skeleton.c" |